]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct-tests.factor
Merge branch 'master' of git://github.com/killy971/factor
[factor.git] / basis / classes / struct / struct-tests.factor
1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.structs.fields ascii
3 assocs byte-arrays classes.struct classes.tuple.private
4 combinators compiler.tree.debugger compiler.units destructors
5 io.encodings.utf8 io.pathnames io.streams.string kernel libc
6 literals math mirrors multiline namespaces prettyprint
7 prettyprint.config see sequences specialized-arrays system
8 tools.test parser lexer eval ;
9 SPECIALIZED-ARRAY: char
10 SPECIALIZED-ARRAY: int
11 SPECIALIZED-ARRAY: ushort
12 IN: classes.struct.tests
13
14 SYMBOL: struct-test-empty
15
16 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
17 [ struct-must-have-slots? ] must-fail-with
18
19 STRUCT: struct-test-foo
20     { x char }
21     { y int initial: 123 }
22     { z bool } ;
23
24 STRUCT: struct-test-bar
25     { w ushort initial: HEX: ffff }
26     { foo struct-test-foo } ;
27
28 [ 12 ] [ struct-test-foo heap-size ] unit-test
29 [ 12 ] [ struct-test-foo <struct> byte-length ] unit-test
30 [ 16 ] [ struct-test-bar heap-size ] unit-test
31 [ 123 ] [ struct-test-foo <struct> y>> ] unit-test
32 [ 123 ] [ struct-test-bar <struct> foo>> y>> ] unit-test
33
34 [ 1 2 3 t ] [
35     1   2 3 t struct-test-foo <struct-boa>   struct-test-bar <struct-boa>
36     {
37         [ w>> ] 
38         [ foo>> x>> ]
39         [ foo>> y>> ]
40         [ foo>> z>> ]
41     } cleave
42 ] unit-test
43
44 [ 7654 ] [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
45 [ 7654 ] [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
46
47 [ {
48     { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
49     { { "x" "char" } 98            }
50     { { "y" "int"  } HEX: 7F00007F }
51     { { "z" "bool" } f             }
52 } ] [
53     B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
54     make-mirror >alist
55 ] unit-test
56
57 [ { { "underlying" f } } ] [
58     f struct-test-foo memory>struct
59     make-mirror >alist
60 ] unit-test
61
62 [ 55 t ] [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } swap at* ] unit-test
63 [ 55 t ] [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int"  } swap at* ] unit-test
64 [ t  t ] [ S{ struct-test-foo { z t  } } make-mirror { "z" "bool" } swap at* ] unit-test
65 [ f  t ] [ S{ struct-test-foo { z f  } } make-mirror { "z" "bool" } swap at* ] unit-test
66 [ f  f ] [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } swap at* ] unit-test
67 [ f  f ] [ S{ struct-test-foo } make-mirror "nonexist" swap at* ] unit-test
68 [ f  t ] [ f struct-test-foo memory>struct make-mirror "underlying" swap at* ] unit-test
69
70 [ S{ struct-test-foo { x 3 } { y 2 } { z f } } ] [
71     S{ struct-test-foo { x 1 } { y 2 } { z f } }
72     [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
73 ] unit-test
74
75 [ S{ struct-test-foo { x 1 } { y 5 } { z f } } ] [
76     S{ struct-test-foo { x 1 } { y 2 } { z f } }
77     [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
78 ] unit-test
79
80 [ S{ struct-test-foo { x 1 } { y 2 } { z t } } ] [
81     S{ struct-test-foo { x 1 } { y 2 } { z f } }
82     [ make-mirror [ t { "z" "bool" } ] dip set-at ] keep
83 ] unit-test
84
85 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
86     S{ struct-test-foo { x 1 } { y 2 } { z f } }
87     [ make-mirror [ "nonsense" "underlying" ] dip set-at ] keep
88 ] unit-test
89
90 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
91     S{ struct-test-foo { x 1 } { y 2 } { z f } }
92     [ make-mirror [ "nonsense" "nonexist" ] dip set-at ] keep
93 ] unit-test
94
95 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
96     S{ struct-test-foo { x 1 } { y 2 } { z f } }
97     [ make-mirror [ "nonsense" { "nonexist" "int" } ] dip set-at ] keep
98 ] unit-test
99
100 [ S{ struct-test-foo { x 1 } { y 123 } { z f } } ] [
101     S{ struct-test-foo { x 1 } { y 2 } { z f } }
102     [ make-mirror { "y" "int" } swap delete-at ] keep
103 ] unit-test
104
105 [ S{ struct-test-foo { x 0 } { y 2 } { z f } } ] [
106     S{ struct-test-foo { x 1 } { y 2 } { z f } }
107     [ make-mirror { "x" "char" } swap delete-at ] keep
108 ] unit-test
109
110 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
111     S{ struct-test-foo { x 1 } { y 2 } { z f } }
112     [ make-mirror { "nonexist" "char" } swap delete-at ] keep
113 ] unit-test
114
115 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
116     S{ struct-test-foo { x 1 } { y 2 } { z f } }
117     [ make-mirror "underlying" swap delete-at ] keep
118 ] unit-test
119
120 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
121     S{ struct-test-foo { x 1 } { y 2 } { z f } }
122     [ make-mirror "nonsense" swap delete-at ] keep
123 ] unit-test
124
125 [ S{ struct-test-foo { x 0 } { y 123 } { z f } } ] [
126     S{ struct-test-foo { x 1 } { y 2 } { z t } }
127     [ make-mirror clear-assoc ] keep
128 ] unit-test
129
130 UNION-STRUCT: struct-test-float-and-bits
131     { f float }
132     { bits uint } ;
133
134 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
135 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
136
137 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
138
139 STRUCT: struct-test-string-ptr
140     { x char* } ;
141
142 [ "hello world" ] [
143     [
144         struct-test-string-ptr <struct>
145         "hello world" utf8 malloc-string &free >>x
146         x>>
147     ] with-destructors
148 ] unit-test
149
150 [ "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" ]
151 [
152     [
153         boa-tuples? off
154         c-object-pointers? off
155         struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
156     ] with-scope
157 ] unit-test
158
159 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
160 [
161     [
162         c-object-pointers? on
163         12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
164     ] with-scope
165 ] unit-test
166
167 [ "S{ struct-test-foo f 0 7654 f }" ]
168 [
169     [
170         boa-tuples? on
171         c-object-pointers? off
172         struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
173     ] with-scope
174 ] unit-test
175
176 [ "S@ struct-test-foo f" ]
177 [
178     [
179         c-object-pointers? off
180         f struct-test-foo memory>struct [ pprint ] with-string-writer
181     ] with-scope
182 ] unit-test
183
184 [ <" USING: classes.struct ;
185 IN: classes.struct.tests
186 STRUCT: struct-test-foo
187     { x char initial: 0 } { y int initial: 123 } { z bool } ;
188 "> ]
189 [ [ struct-test-foo see ] with-string-writer ] unit-test
190
191 [ <" USING: classes.struct ;
192 IN: classes.struct.tests
193 UNION-STRUCT: struct-test-float-and-bits
194     { f float initial: 0.0 } { bits uint initial: 0 } ;
195 "> ]
196 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
197
198 [ {
199     T{ field-spec
200         { name "x" }
201         { offset 0 }
202         { type "char" }
203         { reader x>> }
204         { writer (>>x) }
205     }
206     T{ field-spec
207         { name "y" }
208         { offset 4 }
209         { type "int" }
210         { reader y>> }
211         { writer (>>y) }
212     }
213     T{ field-spec
214         { name "z" }
215         { offset 8 }
216         { type "bool" }
217         { reader z>> }
218         { writer (>>z) }
219     }
220 } ] [ "struct-test-foo" c-type fields>> ] unit-test
221
222 [ {
223     T{ field-spec
224         { name "f" }
225         { offset 0 }
226         { type "float" }
227         { reader f>> }
228         { writer (>>f) }
229     }
230     T{ field-spec
231         { name "bits" }
232         { offset 0 }
233         { type "uint" }
234         { reader bits>> }
235         { writer (>>bits) }
236     }
237 } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
238
239 STRUCT: struct-test-equality-1
240     { x int } ;
241 STRUCT: struct-test-equality-2
242     { y int } ;
243
244 [ t ] [
245     [
246         struct-test-equality-1 <struct> 5 >>x
247         struct-test-equality-1 malloc-struct &free 5 >>x =
248     ] with-destructors
249 ] unit-test
250
251 [ f ] [
252     [
253         struct-test-equality-1 <struct> 5 >>x
254         struct-test-equality-2 malloc-struct &free 5 >>y =
255     ] with-destructors
256 ] unit-test
257
258 [ t ] [
259     [
260         struct-test-equality-1 <struct> 5 >>x
261         struct-test-equality-1 malloc-struct &free 5 >>x
262         [ hashcode ] bi@ =
263     ] with-destructors
264 ] unit-test
265
266 STRUCT: struct-test-array-slots
267     { x int }
268     { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
269     { z int } ;
270
271 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
272
273 [ t ] [
274     struct-test-array-slots <struct>
275     [ y>> [ 8 3 ] dip set-nth ]
276     [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
277 ] unit-test
278
279 STRUCT: struct-test-optimization
280     { x { "int" 3 } } { y int } ;
281
282 SPECIALIZED-ARRAY: struct-test-optimization
283
284 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
285 [ t ] [
286     [ 3 <direct-struct-test-optimization-array> third y>> ]
287     { <tuple> <tuple-boa> memory>struct y>> } inlined?
288 ] unit-test
289
290 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
291
292 [ t ] [
293     [ struct-test-optimization memory>struct x>> second ]
294     { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
295 ] unit-test
296
297 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
298
299 [ t ] [
300     [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
301     { x>> } inlined?
302 ] unit-test
303
304 ! Test cloning structs
305 STRUCT: clone-test-struct { x int } { y char[3] } ;
306
307 [ 1 char-array{ 9 1 1 } ] [
308     clone-test-struct <struct>
309     1 >>x char-array{ 9 1 1 } >>y
310     clone
311     [ x>> ] [ y>> >char-array ] bi
312 ] unit-test
313
314 [ t 1 char-array{ 9 1 1 } ] [
315     [
316         clone-test-struct malloc-struct &free
317         1 >>x char-array{ 9 1 1 } >>y
318         clone
319         [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
320     ] with-destructors
321 ] unit-test
322
323 STRUCT: struct-that's-a-word { x int } ;
324
325 : struct-that's-a-word ( -- ) "OOPS" throw ;
326
327 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
328
329 ! Interactive parsing of struct slot definitions
330 [
331     "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
332     "struct-class-test-1" parse-stream
333 ] [ error>> error>> unexpected-eof? ] must-fail-with
334
335 ! S{ with non-struct type
336 [
337     "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
338     eval( -- value )
339 ] must-fail
340
341 ! Subclassing a struct class should not be allowed
342 [
343     "USE: classes.struct IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
344     eval( -- )
345 ] must-fail
346
347 ! Remove c-type when struct class is forgotten
348 [ ] [
349     "USE: classes.struct IN: classes.struct.tests TUPLE: a-struct ;" eval( -- )
350 ] unit-test
351
352 [ f ] [ "a-struct" c-types get key? ] unit-test