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
136
137
138
139
140
141
142
143
144
|
_debug_info = 1; () = evalfile ("inc.sl");
print ("Testing structures ...");
variable S = struct
{
a, b, c
};
S.a = "a";
S.b = "b";
S.c = "c";
variable U = @Struct_Type ("a", "b", "c");
variable abc = get_struct_field_names (U);
if ((abc[0] != "a")
or (abc[1] != "b")
or (abc[2] != "c"))
failed ("@Struct_Type");
abc = ["a", "b", "c"];
U = @Struct_Type (abc);
if (length (where (abc != get_struct_field_names (U))))
failed ("@Struct_Type([abc])");
variable T = @S;
if (S.a != T.a) failed ("Unable to copy via @S");
if (S.b != T.b) failed ("Unable to copy via @S");
if (S.c != T.c) failed ("Unable to copy via @S");
T.a = "XXX";
if (T.a == S.a) failed ("Unable to copy via @S");
set_struct_fields (T, 1, 2, "three");
if ((T.c != "three") or (T.a != 1) or (T.b != 2))
failed ("set_struct_fields");
T.a++;
T.a += 3;
T.a -= 20;
if (T.a != -15)
failed ("structure arithmetic");
T.c = S;
S.a = T;
if (T != T.c.a)
failed ("Unable to create a circular list");
typedef struct
{
TT_x, TT_y
}
TT;
T = @TT;
if (typeof (T) != TT)
failed ("typeof(T)");
if (0 == is_struct_type (T))
failed ("is_struct_type");
S = typecast (T, Struct_Type);
if (typeof (S) != Struct_Type)
failed ("typecast");
% C structures
S = get_c_struct ();
if ((typeof (S.h) != Short_Type)
or (typeof (S.l) != Long_Type)
or (typeof (S.b) != Char_Type))
failed ("get_c_struct field types");
static define print_struct(s)
{
foreach (get_struct_field_names (s))
{
variable f = ();
vmessage ("S.%s = %S", f, get_struct_field (s, f));
}
}
#ifexists Complex_Type
S.z = 1+2i;
#endif
S.a = [1:10];
#ifexists Double_Type
S.d = PI;
#endif
S.s = "foobar";
S.ro_str = "FOO";
loop (10)
set_c_struct (S);
loop (10)
T = get_c_struct ();
%print_struct (T);
if ((not __eqs(S.a, T.a))
#ifexists Complex_Type
or (S.z != T.z)
#endif
#ifexists Double_Type
or (S.d != T.d)
#endif
or (T.ro_str != "read-only"))
failed ("C Struct");
loop (10)
get_c_struct_via_ref (&T);
%print_struct (T);
if ((not __eqs(S.a, T.a))
#ifexists Complex_Type
or (S.z != T.z)
#endif
#ifexists Double_Type
or (S.d != T.d)
#endif
or (T.ro_str != "read-only"))
failed ("C Struct");
static define count_args ()
{
if (_NARGS != 0)
failed ("foreach using with NULL");
}
static define test_foreach_using_with_null (s)
{
foreach (s) using ("next")
{
s = ();
}
count_args ();
}
test_foreach_using_with_null (NULL);
print ("Ok\n");
exit (0);
|