1 ! Copyright (C) 2009, 2010, 2011 Joe Groff, Slava Pestov, John Benediktsson.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.syntax
4 assocs byte-arrays classes classes.private classes.struct
5 classes.struct.prettyprint.private classes.tuple
6 classes.tuple.parser classes.tuple.private combinators
7 compiler.tree.debugger compiler.units definitions delegate
8 destructors eval generic generic.single io.encodings.utf8
9 io.streams.string kernel layouts lexer libc literals math
10 mirrors namespaces parser prettyprint prettyprint.config see
11 sequences specialized-arrays specialized-arrays.private
12 system tools.test vocabs ;
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: 0xffff }
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 } 0x7F00007F }
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" } ?of ] unit-test
69 { 55 t } [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } ?of ] unit-test
70 { t t } [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } ?of ] unit-test
71 { f t } [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } ?of ] unit-test
72 { { "nonexist" "bool" } f } [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } ?of ] unit-test
73 { "nonexist" f } [ S{ struct-test-foo } make-mirror "nonexist" ?of ] unit-test
74 { f t } [ f struct-test-foo memory>struct make-mirror "underlying" ?of ] 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 } }" }
164 H{ { boa-tuples? f } { c-object-pointers? f } } [
165 struct-test-foo <struct> 7654 >>y unparse
169 { "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" }
171 H{ { c-object-pointers? t } } [
172 12 <byte-array> struct-test-foo memory>struct unparse
176 { "S{ struct-test-foo f 0 7654 f }" }
178 H{ { boa-tuples? t } { c-object-pointers? f } } [
179 struct-test-foo <struct> 7654 >>y unparse
183 { "S@ struct-test-foo f" }
185 H{ { c-object-pointers? f } } [
186 f struct-test-foo memory>struct unparse
190 { "USING: alien.c-types classes.struct ;
191 IN: classes.struct.tests
192 STRUCT: struct-test-foo
193 { x char initial: 0 } { y int initial: 123 } { z bool } ;
195 [ [ struct-test-foo see ] with-string-writer ] unit-test
197 { "USING: alien.c-types classes.struct ;
198 IN: classes.struct.tests
199 UNION-STRUCT: struct-test-float-and-bits
200 { f float initial: 0.0 } { bits uint initial: 0 } ;
202 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
216 { class $[ cell 4 = integer fixnum ? ] }
226 } } [ struct-test-foo lookup-c-type fields>> ] unit-test
240 { class $[ cell 4 = integer fixnum ? ] }
243 } } [ struct-test-float-and-bits lookup-c-type fields>> ] unit-test
245 STRUCT: struct-test-equality-1
247 STRUCT: struct-test-equality-2
253 struct-test-equality-1 <struct> 5 >>x
254 struct-test-equality-1 malloc-struct &free 5 >>x =
260 struct-test-equality-1 <struct> 5 >>x
261 struct-test-equality-2 malloc-struct &free 5 >>y =
265 STRUCT: struct-test-array-slots
267 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
270 { 11 } [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
273 struct-test-array-slots <struct>
274 [ y>> [ 8 3 ] dip set-nth ]
275 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
278 STRUCT: struct-test-optimization
279 { x { int 3 } } { y int } ;
281 SPECIALIZED-ARRAY: struct-test-optimization
283 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
285 [ 3 struct-test-optimization <c-direct-array> third y>> ]
286 { <tuple> <tuple-boa> memory>struct y>> } inlined?
289 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
292 [ struct-test-optimization memory>struct x>> second ]
293 { memory>struct x>> int <c-direct-array> <tuple> <tuple-boa> } inlined?
296 { f } [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
299 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
305 struct-test-optimization specialized-array-vocab forget-vocab
306 ] with-compilation-unit
309 ! Test cloning structs
310 STRUCT: clone-test-struct { x int } { y char[3] } ;
312 { 1 char-array{ 9 1 1 } } [
313 clone-test-struct <struct>
314 1 >>x char-array{ 9 1 1 } >>y
316 [ x>> ] [ y>> char >c-array ] bi
319 { t 1 char-array{ 9 1 1 } } [
321 clone-test-struct malloc-struct &free
322 1 >>x char-array{ 9 1 1 } >>y
324 [ >c-ptr byte-array? ] [ x>> ] [ y>> char >c-array ] tri
328 STRUCT: struct-that's-a-word { x int } ;
330 : struct-that's-a-word ( -- ) "OOPS" throw ;
332 { -77 } [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
334 ! Interactive parsing of struct slot definitions
336 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
337 "struct-class-test-1" parse-stream
338 ] [ error>> error>> unexpected-eof? ] must-fail-with
341 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x uint } ;" eval( -- )
342 ] [ error>> duplicate-slot-names? ] must-fail-with
345 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x float } ;" eval( -- )
346 ] [ error>> duplicate-slot-names? ] must-fail-with
348 ! S{ with non-struct type
350 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
352 ] [ error>> no-method? ] must-fail-with
354 ! Subclassing a struct class should not be allowed
356 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
358 ] [ error>> bad-superclass? ] must-fail-with
360 ! Changing a superclass into a struct should reset the subclass
361 TUPLE: will-become-struct ;
363 TUPLE: a-subclass < will-become-struct ;
365 { f } [ will-become-struct struct-class? ] unit-test
367 { will-become-struct } [ a-subclass superclass-of ] unit-test
369 { } [ "IN: classes.struct.tests USING: classes.struct alien.c-types ; STRUCT: will-become-struct { x int } ;" eval( -- ) ] unit-test
371 { t } [ will-become-struct struct-class? ] unit-test
373 { tuple } [ a-subclass superclass-of ] unit-test
375 STRUCT: bit-field-test
380 { S{ bit-field-test f 0 0 0 } } [ bit-field-test <struct> ] unit-test
381 { S{ bit-field-test f 1 -2 3 } } [ bit-field-test <struct> 1 >>a 2 >>b 3 >>c ] unit-test
382 { 4095 } [ bit-field-test <struct> 8191 >>a a>> ] unit-test
383 { 1 } [ bit-field-test <struct> 1 >>b b>> ] unit-test
384 { -2 } [ bit-field-test <struct> 2 >>b b>> ] unit-test
385 { 1 } [ bit-field-test <struct> 257 >>c c>> ] unit-test
386 { 3 } [ bit-field-test heap-size ] unit-test
396 referent malloc-struct &free
403 STRUCT: self-referent
409 self-referent <struct>
410 self-referent malloc-struct &free
417 C-TYPE: forward-referent
418 STRUCT: backward-referent
419 { x forward-referent* }
421 STRUCT: forward-referent
422 { x backward-referent* }
427 forward-referent <struct>
428 backward-referent malloc-struct &free
437 backward-referent <struct>
438 forward-referent malloc-struct &free
446 STRUCT: ppc-align-test-1
450 [ 16 ] [ ppc-align-test-1 heap-size ] unit-test
452 STRUCT: ppc-align-test-2
456 [ 16 ] [ ppc-align-test-2 heap-size ] unit-test
457 [ 8 ] [ "x" ppc-align-test-2 offset-of ] unit-test
460 STRUCT: struct-test-delegate
462 STRUCT: struct-test-delegator
463 { del struct-test-delegate }
465 CONSULT: struct-test-delegate struct-test-delegator del>> ;
467 { S{ struct-test-delegator f S{ struct-test-delegate f 7 } 8 } } [
468 struct-test-delegator <struct>
473 SPECIALIZED-ARRAY: void*
475 STRUCT: silly-array-field-test { x int*[3] } ;
477 { t } [ silly-array-field-test <struct> x>> void*-array? ] unit-test
480 PACKED-STRUCT: packed-struct-test
487 { 15 } [ packed-struct-test heap-size ] unit-test
489 { 0 } [ "d" packed-struct-test offset-of ] unit-test
490 { 4 } [ "e" packed-struct-test offset-of ] unit-test
491 { 6 } [ "f" packed-struct-test offset-of ] unit-test
492 { 10 } [ "g" packed-struct-test offset-of ] unit-test
493 { 11 } [ "h" packed-struct-test offset-of ] unit-test
495 { POSTPONE: PACKED-STRUCT: }
496 [ packed-struct-test struct-definer-word ] unit-test
498 STRUCT: struct-1 { a c:int } ;
499 PACKED-STRUCT: struct-1-packed { a c:int } ;
500 UNION-STRUCT: struct-1-union { a c:int } ;
502 { "USING: alien.c-types classes.struct ;
503 IN: classes.struct.tests
504 STRUCT: struct-1 { a int initial: 0 } ;
506 [ \ struct-1 [ see ] with-string-writer ] unit-test
507 { "USING: alien.c-types classes.struct ;
508 IN: classes.struct.tests
509 PACKED-STRUCT: struct-1-packed { a int initial: 0 } ;
511 [ \ struct-1-packed [ see ] with-string-writer ] unit-test
512 { "USING: alien.c-types classes.struct ;
513 IN: classes.struct.tests
514 STRUCT: struct-1-union { a int initial: 0 } ;
516 [ \ struct-1-union [ see ] with-string-writer ] unit-test
519 STRUCT: going-to-redefine { a uint } ;
521 "IN: classes.struct.tests TUPLE: going-to-redefine b ;" eval( -- )
523 { f } [ \ going-to-redefine \ clone ?lookup-method ] unit-test
524 { f } [ \ going-to-redefine \ struct-slot-values ?lookup-method ] unit-test
526 ! Test reset-class on structs, which should forget all the accessors, clone, and struct-slot-values
527 STRUCT: some-accessors { aaa uint } { bbb int } ;
528 { } [ [ \ some-accessors reset-class ] with-compilation-unit ] unit-test
529 { f } [ \ some-accessors \ a>> ?lookup-method ] unit-test
530 { f } [ \ some-accessors \ a<< ?lookup-method ] unit-test
531 { f } [ \ some-accessors \ b>> ?lookup-method ] unit-test
532 { f } [ \ some-accessors \ b<< ?lookup-method ] unit-test
533 { f } [ \ some-accessors \ clone ?lookup-method ] unit-test
534 { f } [ \ some-accessors \ struct-slot-values ?lookup-method ] unit-test
536 << \ some-accessors forget >>
539 { 0 } [ struct-test-equality-1 new hashcode ] unit-test
543 struct-test-equality-1 <struct> 5 >>x
544 struct-test-equality-1 malloc-struct &free 5 >>x
549 ! Same slots, so the hashcode should be the same.
551 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
552 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
558 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
559 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct