1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.syntax
3 assocs byte-arrays classes classes.private classes.struct
4 classes.struct.prettyprint.private classes.tuple
5 classes.tuple.parser classes.tuple.private combinators
6 compiler.tree.debugger compiler.units definitions delegate
7 destructors eval generic generic.single io.encodings.utf8
8 io.streams.string kernel layouts lexer libc literals math
9 mirrors namespaces parser prettyprint prettyprint.config see
10 sequences specialized-arrays specialized-arrays.private
11 system tools.test vocabs ;
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: 0xffff }
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 } 0x7F00007F }
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" } ?of ] unit-test
68 { 55 t } [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } ?of ] unit-test
69 { t t } [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } ?of ] unit-test
70 { f t } [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } ?of ] unit-test
71 { { "nonexist" "bool" } f } [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } ?of ] unit-test
72 { "nonexist" f } [ S{ struct-test-foo } make-mirror "nonexist" ?of ] unit-test
73 { f t } [ f struct-test-foo memory>struct make-mirror "underlying" ?of ] 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 { POSTPONE: STRUCT: }
136 [ struct-test-foo struct-definer-word ] unit-test
138 UNION-STRUCT: struct-test-float-and-bits
142 { 1.0 } [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
143 { 4 } [ struct-test-float-and-bits heap-size ] unit-test
145 { 123 } [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
147 { POSTPONE: UNION-STRUCT: }
148 [ struct-test-float-and-bits struct-definer-word ] unit-test
150 STRUCT: struct-test-string-ptr
155 struct-test-string-ptr <struct>
156 "hello world" utf8 malloc-string &free >>x
161 { "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" }
163 H{ { boa-tuples? f } { c-object-pointers? f } } [
164 struct-test-foo <struct> 7654 >>y unparse
168 { "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" }
170 H{ { c-object-pointers? t } } [
171 12 <byte-array> struct-test-foo memory>struct unparse
175 { "S{ struct-test-foo f 0 7654 f }" }
177 H{ { boa-tuples? t } { c-object-pointers? f } } [
178 struct-test-foo <struct> 7654 >>y unparse
182 { "S@ struct-test-foo f" }
184 H{ { c-object-pointers? f } } [
185 f struct-test-foo memory>struct unparse
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 lookup-c-type fields>> ] unit-test
239 { class $[ cell 4 = integer fixnum ? ] }
242 } } [ struct-test-float-and-bits lookup-c-type fields>> ] unit-test
244 STRUCT: struct-test-equality-1
246 STRUCT: struct-test-equality-2
252 struct-test-equality-1 <struct> 5 >>x
253 struct-test-equality-1 malloc-struct &free 5 >>x =
259 struct-test-equality-1 <struct> 5 >>x
260 struct-test-equality-2 malloc-struct &free 5 >>y =
264 STRUCT: struct-test-array-slots
266 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
269 { 11 } [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
272 struct-test-array-slots <struct>
273 [ y>> [ 8 3 ] dip set-nth ]
274 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
277 STRUCT: struct-test-optimization
278 { x { int 3 } } { y int } ;
280 SPECIALIZED-ARRAY: struct-test-optimization
282 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
284 [ 3 struct-test-optimization <c-direct-array> third y>> ]
285 { <tuple> <tuple-boa> memory>struct y>> } inlined?
288 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
291 [ struct-test-optimization memory>struct x>> second ]
292 { memory>struct x>> int <c-direct-array> <tuple> <tuple-boa> } inlined?
295 { f } [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
298 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
304 struct-test-optimization specialized-array-vocab forget-vocab
305 ] with-compilation-unit
308 ! Test cloning structs
309 STRUCT: clone-test-struct { x int } { y char[3] } ;
311 { 1 char-array{ 9 1 1 } } [
312 clone-test-struct <struct>
313 1 >>x char-array{ 9 1 1 } >>y
315 [ x>> ] [ y>> char >c-array ] bi
318 { t 1 char-array{ 9 1 1 } } [
320 clone-test-struct malloc-struct &free
321 1 >>x char-array{ 9 1 1 } >>y
323 [ >c-ptr byte-array? ] [ x>> ] [ y>> char >c-array ] tri
327 STRUCT: struct-that's-a-word { x int } ;
329 : struct-that's-a-word ( -- ) "OOPS" throw ;
331 { -77 } [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
333 ! Interactive parsing of struct slot definitions
335 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
336 "struct-class-test-1" parse-stream
337 ] [ error>> error>> unexpected-eof? ] must-fail-with
340 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x uint } ;" eval( -- )
341 ] [ error>> duplicate-slot-names? ] must-fail-with
344 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x float } ;" eval( -- )
345 ] [ error>> duplicate-slot-names? ] must-fail-with
347 ! S{ with non-struct type
349 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
351 ] [ error>> no-method? ] must-fail-with
353 ! Subclassing a struct class should not be allowed
355 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
357 ] [ error>> bad-superclass? ] must-fail-with
359 ! Changing a superclass into a struct should reset the subclass
360 TUPLE: will-become-struct ;
362 TUPLE: a-subclass < will-become-struct ;
364 { f } [ will-become-struct struct-class? ] unit-test
366 { will-become-struct } [ a-subclass superclass-of ] unit-test
368 { } [ "IN: classes.struct.tests USING: classes.struct alien.c-types ; STRUCT: will-become-struct { x int } ;" eval( -- ) ] unit-test
370 { t } [ will-become-struct struct-class? ] unit-test
372 { tuple } [ a-subclass superclass-of ] unit-test
374 STRUCT: bit-field-test
379 { S{ bit-field-test f 0 0 0 } } [ bit-field-test <struct> ] unit-test
380 { S{ bit-field-test f 1 -2 3 } } [ bit-field-test <struct> 1 >>a 2 >>b 3 >>c ] unit-test
381 { 4095 } [ bit-field-test <struct> 8191 >>a a>> ] unit-test
382 { 1 } [ bit-field-test <struct> 1 >>b b>> ] unit-test
383 { -2 } [ bit-field-test <struct> 2 >>b b>> ] unit-test
384 { 1 } [ bit-field-test <struct> 257 >>c c>> ] unit-test
385 { 3 } [ bit-field-test heap-size ] unit-test
395 referent malloc-struct &free
402 STRUCT: self-referent
408 self-referent <struct>
409 self-referent malloc-struct &free
416 C-TYPE: forward-referent
417 STRUCT: backward-referent
418 { x forward-referent* }
420 STRUCT: forward-referent
421 { x backward-referent* }
426 forward-referent <struct>
427 backward-referent malloc-struct &free
436 backward-referent <struct>
437 forward-referent malloc-struct &free
445 STRUCT: ppc-align-test-1
449 [ 16 ] [ ppc-align-test-1 heap-size ] unit-test
451 STRUCT: ppc-align-test-2
455 [ 16 ] [ ppc-align-test-2 heap-size ] unit-test
456 [ 8 ] [ "x" ppc-align-test-2 offset-of ] unit-test
459 STRUCT: struct-test-delegate
461 STRUCT: struct-test-delegator
462 { del struct-test-delegate }
464 CONSULT: struct-test-delegate struct-test-delegator del>> ;
466 { S{ struct-test-delegator f S{ struct-test-delegate f 7 } 8 } } [
467 struct-test-delegator <struct>
472 SPECIALIZED-ARRAY: void*
474 STRUCT: silly-array-field-test { x int*[3] } ;
476 { t } [ silly-array-field-test <struct> x>> void*-array? ] unit-test
479 PACKED-STRUCT: packed-struct-test
486 { 15 } [ packed-struct-test heap-size ] unit-test
488 { 0 } [ "d" packed-struct-test offset-of ] unit-test
489 { 4 } [ "e" packed-struct-test offset-of ] unit-test
490 { 6 } [ "f" packed-struct-test offset-of ] unit-test
491 { 10 } [ "g" packed-struct-test offset-of ] unit-test
492 { 11 } [ "h" packed-struct-test offset-of ] unit-test
494 { POSTPONE: PACKED-STRUCT: }
495 [ packed-struct-test struct-definer-word ] unit-test
497 STRUCT: struct-1 { a c:int } ;
498 PACKED-STRUCT: struct-1-packed { a c:int } ;
499 UNION-STRUCT: struct-1-union { a c:int } ;
501 { "USING: alien.c-types classes.struct ;
502 IN: classes.struct.tests
503 STRUCT: struct-1 { a int initial: 0 } ;
505 [ \ struct-1 [ see ] with-string-writer ] unit-test
506 { "USING: alien.c-types classes.struct ;
507 IN: classes.struct.tests
508 PACKED-STRUCT: struct-1-packed { a int initial: 0 } ;
510 [ \ struct-1-packed [ see ] with-string-writer ] unit-test
511 { "USING: alien.c-types classes.struct ;
512 IN: classes.struct.tests
513 STRUCT: struct-1-union { a int initial: 0 } ;
515 [ \ struct-1-union [ see ] with-string-writer ] unit-test
518 STRUCT: going-to-redefine { a uint } ;
520 "IN: classes.struct.tests TUPLE: going-to-redefine b ;" eval( -- )
522 { f } [ \ going-to-redefine \ clone ?lookup-method ] unit-test
523 { f } [ \ going-to-redefine \ struct-slot-values ?lookup-method ] unit-test
525 ! Test reset-class on structs, which should forget all the accessors, clone, and struct-slot-values
526 STRUCT: some-accessors { aaa uint } { bbb int } ;
527 { } [ [ \ some-accessors reset-class ] with-compilation-unit ] unit-test
528 { f } [ \ some-accessors \ a>> ?lookup-method ] unit-test
529 { f } [ \ some-accessors \ a<< ?lookup-method ] unit-test
530 { f } [ \ some-accessors \ b>> ?lookup-method ] unit-test
531 { f } [ \ some-accessors \ b<< ?lookup-method ] unit-test
532 { f } [ \ some-accessors \ clone ?lookup-method ] unit-test
533 { f } [ \ some-accessors \ struct-slot-values ?lookup-method ] unit-test
535 << \ some-accessors forget >>
538 { 0 } [ struct-test-equality-1 new hashcode ] unit-test
542 struct-test-equality-1 <struct> 5 >>x
543 struct-test-equality-1 malloc-struct &free 5 >>x
548 ! Same slots, so the hashcode should be the same.
550 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
551 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
557 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
558 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct