1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
_debug_info = 1; () = evalfile ("inc.sl");
print ("Testing Associative Arrays ...");
static define key_to_value (k)
{
return "<<<" + k + ">>>";
}
static define value_to_key (v)
{
strcompress (v, "<>");
}
static define add_to_x (x, k)
{
x[k] = key_to_value(k);
}
static variable Default_Value = "****Default-Value****";
define setup (type)
{
variable x = Assoc_Type [type, Default_Value];
add_to_x (x, "foo");
add_to_x (x, "bar");
add_to_x (x, "silly");
add_to_x (x, "cow");
add_to_x (x, "dog");
add_to_x (x, "chicken");
return x;
}
static variable X;
% Test create/destuction of arrays
loop (20) X = setup (Any_Type);
loop (20) X = setup (String_Type);
static variable k, v;
foreach (X)
{
(k, v) = ();
if ((k != value_to_key(v)) or (v != key_to_value (k))
or (X[k] != v))
failed ("foreach");
}
foreach (X) using ("keys")
{
k = ();
if (X[k] != key_to_value (k))
failed ("foreach using keys");
}
foreach (X) using ("keys", "values")
{
(k, v) = ();
if ((k != value_to_key(v)) or (v != key_to_value (k))
or (X[k] != v))
failed ("foreach using keys, values");
}
k = assoc_get_keys (X);
v = assoc_get_values (X);
static variable i;
_for (0, length(k)-1, 1)
{
i = ();
if (v[i] != X[k[i]])
failed ("assoc_get_keys/values");
assoc_delete_key (X, k[i]);
}
if (length (X) != 0)
error ("assoc_delete_key failed");
if (X["*******************"] != Default_Value)
failed ("default value");
static define eqs (a, b)
{
variable len;
len = length (a);
if (len != length (b))
return 0;
len == length (where (a == b));
}
static define neqs (a, b)
{
not (eqs (a, b));
}
static define store_and_test (a, indx, value)
{
a[indx] = value;
if (typeof (value) != typeof(a[indx]))
failed ("typeof (value)");
if (neqs (a[indx], value))
failed ("a[indx] != value");
}
X = Assoc_Type[];
store_and_test (X, "string", "string");
store_and_test (X, "array", ["a", "b", "c"]);
store_and_test (X, "int", 3);
#ifexists Complex_Type
store_and_test (X, "z", 3+2i);
#endif
static variable V = assoc_get_values (X);
static variable K = assoc_get_keys (X);
static variable i;
_for (0, length(X)-1, 1)
{
i=();
if (neqs(X[K[i]], @V[i]))
failed ("assoc_get_values");
}
print ("Ok\n");
exit (0);
|