1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.syntax ascii
3 assocs byte-arrays classes.struct classes.tuple.parser
4 classes.tuple.private classes.tuple combinators compiler.tree.debugger
5 compiler.units delegate destructors io.encodings.utf8 io.pathnames
6 io.streams.string kernel libc literals math mirrors namespaces
7 prettyprint prettyprint.config see sequences specialized-arrays
8 system tools.test parser lexer eval layouts generic.single classes
11 FROM: specialized-arrays.private => specialized-array-vocab ;
12 FROM: classes.struct.private => binary-zero? ;
13 QUALIFIED-WITH: alien.c-types c
14 SPECIALIZED-ARRAY: char
15 SPECIALIZED-ARRAY: int
16 SPECIALIZED-ARRAY: ushort
17 IN: classes.struct.tests
19 SYMBOL: struct-test-empty
21 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
22 [ struct-must-have-slots? ] must-fail-with
24 STRUCT: struct-test-foo
26 { y int initial: 123 }
29 STRUCT: struct-test-bar
30 { w ushort initial: HEX: ffff }
31 { foo struct-test-foo } ;
33 [ 12 ] [ struct-test-foo heap-size ] unit-test
34 [ 12 ] [ struct-test-foo <struct> byte-length ] unit-test
35 [ 16 ] [ struct-test-bar heap-size ] unit-test
36 [ 123 ] [ struct-test-foo <struct> y>> ] unit-test
37 [ 123 ] [ struct-test-bar <struct> foo>> y>> ] unit-test
40 1 2 3 t struct-test-foo <struct-boa> struct-test-bar <struct-boa>
49 [ 7654 ] [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
50 [ 7654 ] [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
53 { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
55 { { "y" int } HEX: 7F00007F }
58 B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
62 [ { { "underlying" f } } ] [
63 f struct-test-foo memory>struct
67 [ 55 t ] [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } swap at* ] unit-test
68 [ 55 t ] [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } swap at* ] unit-test
69 [ t t ] [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } swap at* ] unit-test
70 [ f t ] [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } swap at* ] unit-test
71 [ f f ] [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } swap at* ] unit-test
72 [ f f ] [ S{ struct-test-foo } make-mirror "nonexist" swap at* ] unit-test
73 [ f t ] [ f struct-test-foo memory>struct make-mirror "underlying" swap at* ] unit-test
75 [ S{ struct-test-foo { x 3 } { y 2 } { z f } } ] [
76 S{ struct-test-foo { x 1 } { y 2 } { z f } }
77 [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
80 [ S{ struct-test-foo { x 1 } { y 5 } { z f } } ] [
81 S{ struct-test-foo { x 1 } { y 2 } { z f } }
82 [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
85 [ S{ struct-test-foo { x 1 } { y 2 } { z t } } ] [
86 S{ struct-test-foo { x 1 } { y 2 } { z f } }
87 [ make-mirror [ t { "z" "bool" } ] dip set-at ] keep
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" "underlying" ] dip set-at ] keep
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" ] dip set-at ] keep
100 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
101 S{ struct-test-foo { x 1 } { y 2 } { z f } }
102 [ make-mirror [ "nonsense" { "nonexist" "int" } ] dip set-at ] keep
105 [ S{ struct-test-foo { x 1 } { y 123 } { z f } } ] [
106 S{ struct-test-foo { x 1 } { y 2 } { z f } }
107 [ make-mirror { "y" "int" } swap delete-at ] keep
110 [ S{ struct-test-foo { x 0 } { y 2 } { z f } } ] [
111 S{ struct-test-foo { x 1 } { y 2 } { z f } }
112 [ make-mirror { "x" "char" } swap delete-at ] keep
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 { "nonexist" "char" } swap delete-at ] keep
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 "underlying" swap delete-at ] keep
125 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
126 S{ struct-test-foo { x 1 } { y 2 } { z f } }
127 [ make-mirror "nonsense" swap delete-at ] keep
130 [ S{ struct-test-foo { x 0 } { y 123 } { z f } } ] [
131 S{ struct-test-foo { x 1 } { y 2 } { z t } }
132 [ make-mirror clear-assoc ] keep
135 UNION-STRUCT: struct-test-float-and-bits
139 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
140 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
142 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
144 STRUCT: struct-test-string-ptr
149 struct-test-string-ptr <struct>
150 "hello world" utf8 malloc-string &free >>x
155 [ "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" ]
159 c-object-pointers? off
160 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
164 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
167 c-object-pointers? on
168 12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
172 [ "S{ struct-test-foo f 0 7654 f }" ]
176 c-object-pointers? off
177 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
181 [ "S@ struct-test-foo f" ]
184 c-object-pointers? off
185 f struct-test-foo memory>struct [ pprint ] with-string-writer
189 [ "USING: alien.c-types classes.struct ;
190 IN: classes.struct.tests
191 STRUCT: struct-test-foo
192 { x char initial: 0 } { y int initial: 123 } { z bool } ;
194 [ [ struct-test-foo see ] with-string-writer ] unit-test
196 [ "USING: alien.c-types classes.struct ;
197 IN: classes.struct.tests
198 UNION-STRUCT: struct-test-float-and-bits
199 { f float initial: 0.0 } { bits uint initial: 0 } ;
201 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
215 { class $[ cell 4 = integer fixnum ? ] }
225 } ] [ struct-test-foo c-type fields>> ] unit-test
239 { class $[ cell 4 = integer fixnum ? ] }
242 } ] [ struct-test-float-and-bits c-type fields>> ] unit-test
244 STRUCT: struct-test-equality-1
246 STRUCT: struct-test-equality-2
251 struct-test-equality-1 <struct> 5 >>x
252 struct-test-equality-1 malloc-struct &free 5 >>x =
258 struct-test-equality-1 <struct> 5 >>x
259 struct-test-equality-2 malloc-struct &free 5 >>y =
265 struct-test-equality-1 <struct> 5 >>x
266 struct-test-equality-1 malloc-struct &free 5 >>x
271 STRUCT: struct-test-array-slots
273 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
276 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
279 struct-test-array-slots <struct>
280 [ y>> [ 8 3 ] dip set-nth ]
281 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
284 STRUCT: struct-test-optimization
285 { x { int 3 } } { y int } ;
287 SPECIALIZED-ARRAY: struct-test-optimization
289 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
291 [ 3 <direct-struct-test-optimization-array> third y>> ]
292 { <tuple> <tuple-boa> memory>struct y>> } inlined?
295 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
298 [ struct-test-optimization memory>struct x>> second ]
299 { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
302 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
305 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
311 struct-test-optimization specialized-array-vocab forget-vocab
312 ] with-compilation-unit
315 ! Test cloning structs
316 STRUCT: clone-test-struct { x int } { y char[3] } ;
318 [ 1 char-array{ 9 1 1 } ] [
319 clone-test-struct <struct>
320 1 >>x char-array{ 9 1 1 } >>y
322 [ x>> ] [ y>> >char-array ] bi
325 [ t 1 char-array{ 9 1 1 } ] [
327 clone-test-struct malloc-struct &free
328 1 >>x char-array{ 9 1 1 } >>y
330 [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
334 STRUCT: struct-that's-a-word { x int } ;
336 : struct-that's-a-word ( -- ) "OOPS" throw ;
338 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
340 ! Interactive parsing of struct slot definitions
342 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
343 "struct-class-test-1" parse-stream
344 ] [ error>> error>> unexpected-eof? ] must-fail-with
347 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x uint } ;" eval( -- )
348 ] [ error>> duplicate-slot-names? ] must-fail-with
351 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x float } ;" eval( -- )
352 ] [ error>> duplicate-slot-names? ] must-fail-with
354 ! S{ with non-struct type
356 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
358 ] [ error>> no-method? ] must-fail-with
360 ! Subclassing a struct class should not be allowed
362 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
364 ] [ error>> bad-superclass? ] must-fail-with
366 ! Changing a superclass into a struct should reset the subclass
367 TUPLE: will-become-struct ;
369 TUPLE: a-subclass < will-become-struct ;
371 [ f ] [ will-become-struct struct-class? ] unit-test
373 [ will-become-struct ] [ a-subclass superclass ] unit-test
375 [ ] [ "IN: classes.struct.tests USING: classes.struct alien.c-types ; STRUCT: will-become-struct { x int } ;" eval( -- ) ] unit-test
377 [ t ] [ will-become-struct struct-class? ] unit-test
379 [ tuple ] [ a-subclass superclass ] unit-test
381 STRUCT: bit-field-test
386 [ S{ bit-field-test f 0 0 0 } ] [ bit-field-test <struct> ] unit-test
387 [ S{ bit-field-test f 1 -2 3 } ] [ bit-field-test <struct> 1 >>a 2 >>b 3 >>c ] unit-test
388 [ 4095 ] [ bit-field-test <struct> 8191 >>a a>> ] unit-test
389 [ 1 ] [ bit-field-test <struct> 1 >>b b>> ] unit-test
390 [ -2 ] [ bit-field-test <struct> 2 >>b b>> ] unit-test
391 [ 1 ] [ bit-field-test <struct> 257 >>c c>> ] unit-test
392 [ 3 ] [ bit-field-test heap-size ] unit-test
402 referent malloc-struct &free
409 STRUCT: self-referent
415 self-referent <struct>
416 self-referent malloc-struct &free
423 C-TYPE: forward-referent
424 STRUCT: backward-referent
425 { x forward-referent* }
427 STRUCT: forward-referent
428 { x backward-referent* }
433 forward-referent <struct>
434 backward-referent malloc-struct &free
443 backward-referent <struct>
444 forward-referent malloc-struct &free
452 STRUCT: ppc-align-test-1
456 [ 16 ] [ ppc-align-test-1 heap-size ] unit-test
458 STRUCT: ppc-align-test-2
462 [ 12 ] [ ppc-align-test-2 heap-size ] unit-test
463 [ 4 ] [ "x" ppc-align-test-2 offset-of ] unit-test
466 STRUCT: struct-test-delegate
468 STRUCT: struct-test-delegator
469 { del struct-test-delegate }
471 CONSULT: struct-test-delegate struct-test-delegator del>> ;
473 [ S{ struct-test-delegator f S{ struct-test-delegate f 7 } 8 } ] [
474 struct-test-delegator <struct>