1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.syntax
3 ascii assocs byte-arrays classes.struct
4 classes.struct.prettyprint classes.struct.prettyprint.private
5 classes.tuple.parser classes.tuple.private classes.tuple
6 combinators compiler.tree.debugger compiler.units delegate
7 destructors io.encodings.utf8 io.pathnames io.streams.string
8 kernel libc literals math mirrors namespaces prettyprint
9 prettyprint.config see sequences specialized-arrays system
10 tools.test parser lexer eval layouts generic.single classes
13 FROM: specialized-arrays.private => specialized-array-vocab ;
14 QUALIFIED-WITH: alien.c-types c
15 SPECIALIZED-ARRAY: char
16 SPECIALIZED-ARRAY: int
17 SPECIALIZED-ARRAY: ushort
18 IN: classes.struct.tests
20 SYMBOL: struct-test-empty
22 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
23 [ struct-must-have-slots? ] must-fail-with
25 STRUCT: struct-test-foo
27 { y int initial: 123 }
30 STRUCT: struct-test-bar
31 { w ushort initial: HEX: ffff }
32 { foo struct-test-foo } ;
34 [ 12 ] [ struct-test-foo heap-size ] unit-test
35 [ 12 ] [ struct-test-foo <struct> byte-length ] unit-test
36 [ 16 ] [ struct-test-bar heap-size ] unit-test
37 [ 123 ] [ struct-test-foo <struct> y>> ] unit-test
38 [ 123 ] [ struct-test-bar <struct> foo>> y>> ] unit-test
41 1 2 3 t struct-test-foo <struct-boa> struct-test-bar <struct-boa>
50 [ 7654 ] [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
51 [ 7654 ] [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
54 { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
56 { { "y" int } HEX: 7F00007F }
59 B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
63 [ { { "underlying" f } } ] [
64 f struct-test-foo memory>struct
68 [ 55 t ] [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } swap at* ] unit-test
69 [ 55 t ] [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } swap at* ] unit-test
70 [ t t ] [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } swap at* ] unit-test
71 [ f t ] [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } swap at* ] unit-test
72 [ f f ] [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } swap at* ] unit-test
73 [ f f ] [ S{ struct-test-foo } make-mirror "nonexist" swap at* ] unit-test
74 [ f t ] [ f struct-test-foo memory>struct make-mirror "underlying" swap at* ] unit-test
76 [ S{ struct-test-foo { x 3 } { y 2 } { z f } } ] [
77 S{ struct-test-foo { x 1 } { y 2 } { z f } }
78 [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
81 [ S{ struct-test-foo { x 1 } { y 5 } { z f } } ] [
82 S{ struct-test-foo { x 1 } { y 2 } { z f } }
83 [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
86 [ S{ struct-test-foo { x 1 } { y 2 } { z t } } ] [
87 S{ struct-test-foo { x 1 } { y 2 } { z f } }
88 [ make-mirror [ t { "z" "bool" } ] dip set-at ] keep
91 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
92 S{ struct-test-foo { x 1 } { y 2 } { z f } }
93 [ make-mirror [ "nonsense" "underlying" ] dip set-at ] keep
96 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
97 S{ struct-test-foo { x 1 } { y 2 } { z f } }
98 [ make-mirror [ "nonsense" "nonexist" ] dip set-at ] keep
101 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
102 S{ struct-test-foo { x 1 } { y 2 } { z f } }
103 [ make-mirror [ "nonsense" { "nonexist" "int" } ] dip set-at ] keep
106 [ S{ struct-test-foo { x 1 } { y 123 } { z f } } ] [
107 S{ struct-test-foo { x 1 } { y 2 } { z f } }
108 [ make-mirror { "y" "int" } swap delete-at ] keep
111 [ S{ struct-test-foo { x 0 } { y 2 } { z f } } ] [
112 S{ struct-test-foo { x 1 } { y 2 } { z f } }
113 [ make-mirror { "x" "char" } swap delete-at ] keep
116 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
117 S{ struct-test-foo { x 1 } { y 2 } { z f } }
118 [ make-mirror { "nonexist" "char" } swap delete-at ] keep
121 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
122 S{ struct-test-foo { x 1 } { y 2 } { z f } }
123 [ make-mirror "underlying" swap delete-at ] keep
126 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
127 S{ struct-test-foo { x 1 } { y 2 } { z f } }
128 [ make-mirror "nonsense" swap delete-at ] keep
131 [ S{ struct-test-foo { x 0 } { y 123 } { z f } } ] [
132 S{ struct-test-foo { x 1 } { y 2 } { z t } }
133 [ make-mirror clear-assoc ] keep
136 [ POSTPONE: STRUCT: ]
137 [ struct-test-foo struct-definer-word ] unit-test
139 UNION-STRUCT: struct-test-float-and-bits
143 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
144 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
146 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
148 [ POSTPONE: UNION-STRUCT: ]
149 [ struct-test-float-and-bits struct-definer-word ] unit-test
151 STRUCT: struct-test-string-ptr
156 struct-test-string-ptr <struct>
157 "hello world" utf8 malloc-string &free >>x
162 [ "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" ]
166 c-object-pointers? off
167 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
171 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
174 c-object-pointers? on
175 12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
179 [ "S{ struct-test-foo f 0 7654 f }" ]
183 c-object-pointers? off
184 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
188 [ "S@ struct-test-foo f" ]
191 c-object-pointers? off
192 f struct-test-foo memory>struct [ pprint ] with-string-writer
196 [ "USING: alien.c-types classes.struct ;
197 IN: classes.struct.tests
198 STRUCT: struct-test-foo
199 { x char initial: 0 } { y int initial: 123 } { z bool } ;
201 [ [ struct-test-foo see ] with-string-writer ] unit-test
203 [ "USING: alien.c-types classes.struct ;
204 IN: classes.struct.tests
205 UNION-STRUCT: struct-test-float-and-bits
206 { f float initial: 0.0 } { bits uint initial: 0 } ;
208 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
222 { class $[ cell 4 = integer fixnum ? ] }
232 } ] [ struct-test-foo c-type fields>> ] unit-test
246 { class $[ cell 4 = integer fixnum ? ] }
249 } ] [ struct-test-float-and-bits c-type fields>> ] unit-test
251 STRUCT: struct-test-equality-1
253 STRUCT: struct-test-equality-2
256 [ 0 ] [ struct-test-equality-1 new hashcode ] unit-test
260 struct-test-equality-1 <struct> 5 >>x
261 struct-test-equality-1 malloc-struct &free 5 >>x =
267 struct-test-equality-1 <struct> 5 >>x
268 struct-test-equality-2 malloc-struct &free 5 >>y =
274 struct-test-equality-1 <struct> 5 >>x
275 struct-test-equality-1 malloc-struct &free 5 >>x
280 STRUCT: struct-test-array-slots
282 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
285 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
288 struct-test-array-slots <struct>
289 [ y>> [ 8 3 ] dip set-nth ]
290 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
293 STRUCT: struct-test-optimization
294 { x { int 3 } } { y int } ;
296 SPECIALIZED-ARRAY: struct-test-optimization
298 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
300 [ 3 <direct-struct-test-optimization-array> third y>> ]
301 { <tuple> <tuple-boa> memory>struct y>> } inlined?
304 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
307 [ struct-test-optimization memory>struct x>> second ]
308 { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
311 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
314 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
320 struct-test-optimization specialized-array-vocab forget-vocab
321 ] with-compilation-unit
324 ! Test cloning structs
325 STRUCT: clone-test-struct { x int } { y char[3] } ;
327 [ 1 char-array{ 9 1 1 } ] [
328 clone-test-struct <struct>
329 1 >>x char-array{ 9 1 1 } >>y
331 [ x>> ] [ y>> >char-array ] bi
334 [ t 1 char-array{ 9 1 1 } ] [
336 clone-test-struct malloc-struct &free
337 1 >>x char-array{ 9 1 1 } >>y
339 [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
343 STRUCT: struct-that's-a-word { x int } ;
345 : struct-that's-a-word ( -- ) "OOPS" throw ;
347 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
349 ! Interactive parsing of struct slot definitions
351 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
352 "struct-class-test-1" parse-stream
353 ] [ error>> error>> unexpected-eof? ] must-fail-with
356 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x uint } ;" eval( -- )
357 ] [ error>> duplicate-slot-names? ] must-fail-with
360 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x float } ;" eval( -- )
361 ] [ error>> duplicate-slot-names? ] must-fail-with
363 ! S{ with non-struct type
365 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
367 ] [ error>> no-method? ] must-fail-with
369 ! Subclassing a struct class should not be allowed
371 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
373 ] [ error>> bad-superclass? ] must-fail-with
375 ! Changing a superclass into a struct should reset the subclass
376 TUPLE: will-become-struct ;
378 TUPLE: a-subclass < will-become-struct ;
380 [ f ] [ will-become-struct struct-class? ] unit-test
382 [ will-become-struct ] [ a-subclass superclass ] unit-test
384 [ ] [ "IN: classes.struct.tests USING: classes.struct alien.c-types ; STRUCT: will-become-struct { x int } ;" eval( -- ) ] unit-test
386 [ t ] [ will-become-struct struct-class? ] unit-test
388 [ tuple ] [ a-subclass superclass ] unit-test
390 STRUCT: bit-field-test
395 [ S{ bit-field-test f 0 0 0 } ] [ bit-field-test <struct> ] unit-test
396 [ S{ bit-field-test f 1 -2 3 } ] [ bit-field-test <struct> 1 >>a 2 >>b 3 >>c ] unit-test
397 [ 4095 ] [ bit-field-test <struct> 8191 >>a a>> ] unit-test
398 [ 1 ] [ bit-field-test <struct> 1 >>b b>> ] unit-test
399 [ -2 ] [ bit-field-test <struct> 2 >>b b>> ] unit-test
400 [ 1 ] [ bit-field-test <struct> 257 >>c c>> ] unit-test
401 [ 3 ] [ bit-field-test heap-size ] unit-test
411 referent malloc-struct &free
418 STRUCT: self-referent
424 self-referent <struct>
425 self-referent malloc-struct &free
432 C-TYPE: forward-referent
433 STRUCT: backward-referent
434 { x forward-referent* }
436 STRUCT: forward-referent
437 { x backward-referent* }
442 forward-referent <struct>
443 backward-referent malloc-struct &free
452 backward-referent <struct>
453 forward-referent malloc-struct &free
461 STRUCT: ppc-align-test-1
465 [ 16 ] [ ppc-align-test-1 heap-size ] unit-test
467 STRUCT: ppc-align-test-2
471 cpu ppc? 4 cell = and os macosx? and [
472 [ 12 ] [ ppc-align-test-2 heap-size ] unit-test
473 [ 4 ] [ "x" ppc-align-test-2 offset-of ] unit-test
475 [ 16 ] [ ppc-align-test-2 heap-size ] unit-test
476 [ 8 ] [ "x" ppc-align-test-2 offset-of ] unit-test
480 STRUCT: struct-test-delegate
482 STRUCT: struct-test-delegator
483 { del struct-test-delegate }
485 CONSULT: struct-test-delegate struct-test-delegator del>> ;
487 [ S{ struct-test-delegator f S{ struct-test-delegate f 7 } 8 } ] [
488 struct-test-delegator <struct>
493 SPECIALIZED-ARRAY: void*
495 STRUCT: silly-array-field-test { x int*[3] } ;
497 [ t ] [ silly-array-field-test <struct> x>> void*-array? ] unit-test
500 PACKED-STRUCT: packed-struct-test
507 [ 15 ] [ packed-struct-test heap-size ] unit-test
509 [ 0 ] [ "d" packed-struct-test offset-of ] unit-test
510 [ 4 ] [ "e" packed-struct-test offset-of ] unit-test
511 [ 6 ] [ "f" packed-struct-test offset-of ] unit-test
512 [ 10 ] [ "g" packed-struct-test offset-of ] unit-test
513 [ 11 ] [ "h" packed-struct-test offset-of ] unit-test
515 [ POSTPONE: PACKED-STRUCT: ]
516 [ packed-struct-test struct-definer-word ] unit-test