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-readonly
26 { x uint read-only } ;
28 { S{ struct-test-readonly f 10 } } [
29 10 struct-test-readonly <struct-boa>
32 STRUCT: struct-test-foo
34 { y int initial: 123 }
37 STRUCT: struct-test-bar
38 { w ushort initial: 0xffff }
39 { foo struct-test-foo } ;
41 { 12 } [ struct-test-foo heap-size ] unit-test
42 { 12 } [ struct-test-foo <struct> byte-length ] unit-test
43 { 16 } [ struct-test-bar heap-size ] unit-test
44 { 123 } [ struct-test-foo <struct> y>> ] unit-test
45 { 123 } [ struct-test-bar <struct> foo>> y>> ] unit-test
48 1 2 3 t struct-test-foo <struct-boa> struct-test-bar <struct-boa>
57 { 7654 } [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
58 { 7654 } [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
61 { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
63 { { "y" int } 0x7F00007F }
66 B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
70 { { { "underlying" f } } } [
71 f struct-test-foo memory>struct
75 { 55 t } [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } ?of ] unit-test
76 { 55 t } [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } ?of ] unit-test
77 { t t } [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } ?of ] unit-test
78 { f t } [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } ?of ] unit-test
79 { { "nonexist" "bool" } f } [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } ?of ] unit-test
80 { "nonexist" f } [ S{ struct-test-foo } make-mirror "nonexist" ?of ] unit-test
81 { f t } [ f struct-test-foo memory>struct make-mirror "underlying" ?of ] unit-test
83 { S{ struct-test-foo { x 3 } { y 2 } { z f } } } [
84 S{ struct-test-foo { x 1 } { y 2 } { z f } }
85 [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
88 { S{ struct-test-foo { x 1 } { y 5 } { z f } } } [
89 S{ struct-test-foo { x 1 } { y 2 } { z f } }
90 [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
93 { S{ struct-test-foo { x 1 } { y 2 } { z t } } } [
94 S{ struct-test-foo { x 1 } { y 2 } { z f } }
95 [ make-mirror [ t { "z" "bool" } ] dip set-at ] keep
98 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
99 S{ struct-test-foo { x 1 } { y 2 } { z f } }
100 [ make-mirror [ "nonsense" "underlying" ] dip set-at ] keep
103 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
104 S{ struct-test-foo { x 1 } { y 2 } { z f } }
105 [ make-mirror [ "nonsense" "nonexist" ] dip set-at ] keep
108 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
109 S{ struct-test-foo { x 1 } { y 2 } { z f } }
110 [ make-mirror [ "nonsense" { "nonexist" "int" } ] dip set-at ] keep
113 { S{ struct-test-foo { x 1 } { y 123 } { z f } } } [
114 S{ struct-test-foo { x 1 } { y 2 } { z f } }
115 [ make-mirror { "y" "int" } swap delete-at ] keep
118 { S{ struct-test-foo { x 0 } { y 2 } { z f } } } [
119 S{ struct-test-foo { x 1 } { y 2 } { z f } }
120 [ make-mirror { "x" "char" } swap delete-at ] keep
123 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
124 S{ struct-test-foo { x 1 } { y 2 } { z f } }
125 [ make-mirror { "nonexist" "char" } swap delete-at ] keep
128 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
129 S{ struct-test-foo { x 1 } { y 2 } { z f } }
130 [ make-mirror "underlying" swap delete-at ] keep
133 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
134 S{ struct-test-foo { x 1 } { y 2 } { z f } }
135 [ make-mirror "nonsense" swap delete-at ] keep
138 { S{ struct-test-foo { x 0 } { y 123 } { z f } } } [
139 S{ struct-test-foo { x 1 } { y 2 } { z t } }
140 [ make-mirror clear-assoc ] keep
143 { POSTPONE: STRUCT: }
144 [ struct-test-foo struct-definer-word ] unit-test
146 UNION-STRUCT: struct-test-float-and-bits
150 { 1.0 } [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
151 { 4 } [ struct-test-float-and-bits heap-size ] unit-test
153 { 123 } [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
155 { POSTPONE: UNION-STRUCT: }
156 [ struct-test-float-and-bits struct-definer-word ] unit-test
158 STRUCT: struct-test-string-ptr
163 struct-test-string-ptr <struct>
164 "hello world" utf8 malloc-string &free >>x
169 { "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" }
171 H{ { boa-tuples? f } { c-object-pointers? f } } [
172 struct-test-foo <struct> 7654 >>y unparse
176 { "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" }
178 H{ { c-object-pointers? t } } [
179 12 <byte-array> struct-test-foo memory>struct unparse
183 { "S{ struct-test-foo f 0 7654 f }" }
185 H{ { boa-tuples? t } { c-object-pointers? f } } [
186 struct-test-foo <struct> 7654 >>y unparse
190 { "S@ struct-test-foo f" }
192 H{ { c-object-pointers? f } } [
193 f struct-test-foo memory>struct unparse
197 : with-default-string-writer ( quot -- str )
198 64 margin [ with-string-writer ] with-variable ; inline
200 { "USING: alien.c-types classes.struct ;
201 IN: classes.struct.tests
202 STRUCT: struct-test-foo
203 { x char initial: 0 } { y int initial: 123 } { z bool } ;
205 [ [ struct-test-foo see ] with-default-string-writer ] unit-test
207 { "USING: alien.c-types classes.struct ;
208 IN: classes.struct.tests
209 UNION-STRUCT: struct-test-float-and-bits
210 { f float initial: 0.0 } { bits uint initial: 0 } ;
212 [ [ struct-test-float-and-bits see ] with-default-string-writer ] unit-test
226 { class $[ cell 4 = integer fixnum ? ] }
236 } } [ struct-test-foo lookup-c-type fields>> ] unit-test
250 { class $[ cell 4 = integer fixnum ? ] }
253 } } [ struct-test-float-and-bits lookup-c-type fields>> ] unit-test
255 STRUCT: struct-test-equality-1
257 STRUCT: struct-test-equality-2
263 struct-test-equality-1 <struct> 5 >>x
264 struct-test-equality-1 malloc-struct &free 5 >>x =
270 struct-test-equality-1 <struct> 5 >>x
271 struct-test-equality-2 malloc-struct &free 5 >>y =
275 STRUCT: struct-test-array-slots
277 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
280 { 11 } [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
283 struct-test-array-slots <struct>
284 [ y>> [ 8 3 ] dip set-nth ]
285 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
288 STRUCT: struct-test-optimization
289 { x { int 3 } } { y int } ;
291 SPECIALIZED-ARRAY: struct-test-optimization
293 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
295 [ 3 struct-test-optimization <c-direct-array> third y>> ]
296 { <tuple> <tuple-boa> memory>struct y>> } inlined?
299 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
302 [ struct-test-optimization memory>struct x>> second ]
303 { memory>struct x>> int <c-direct-array> <tuple> <tuple-boa> } inlined?
306 { f } [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
309 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
315 struct-test-optimization specialized-array-vocab forget-vocab
316 ] with-compilation-unit
319 ! Test cloning structs
320 STRUCT: clone-test-struct { x int } { y char[3] } ;
322 { 1 char-array{ 9 1 1 } } [
323 clone-test-struct <struct>
324 1 >>x char-array{ 9 1 1 } >>y
326 [ x>> ] [ y>> char >c-array ] bi
329 { t 1 char-array{ 9 1 1 } } [
331 clone-test-struct malloc-struct &free
332 1 >>x char-array{ 9 1 1 } >>y
334 [ >c-ptr byte-array? ] [ x>> ] [ y>> char >c-array ] tri
338 STRUCT: struct-that's-a-word { x int } ;
340 : struct-that's-a-word ( -- ) "OOPS" throw ;
342 { -77 } [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
344 ! Interactive parsing of struct slot definitions
346 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
347 "struct-class-test-1" parse-stream
348 ] [ error>> error>> unexpected-eof? ] must-fail-with
351 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x uint } ;" eval( -- )
352 ] [ error>> duplicate-slot-names? ] must-fail-with
355 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x float } ;" eval( -- )
356 ] [ error>> duplicate-slot-names? ] must-fail-with
358 ! S{ with non-struct type
360 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
362 ] [ error>> no-method? ] must-fail-with
364 ! Subclassing a struct class should not be allowed
366 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
368 ] [ error>> bad-superclass? ] must-fail-with
370 ! Changing a superclass into a struct should reset the subclass
371 TUPLE: will-become-struct ;
373 TUPLE: a-subclass < will-become-struct ;
375 { f } [ will-become-struct struct-class? ] unit-test
377 { will-become-struct } [ a-subclass superclass-of ] unit-test
379 { } [ "IN: classes.struct.tests USING: classes.struct alien.c-types ; STRUCT: will-become-struct { x int } ;" eval( -- ) ] unit-test
381 { t } [ will-become-struct struct-class? ] unit-test
383 { tuple } [ a-subclass superclass-of ] unit-test
385 STRUCT: bit-field-test
390 { S{ bit-field-test f 0 0 0 } } [ bit-field-test <struct> ] unit-test
391 { S{ bit-field-test f 1 -2 3 } } [ bit-field-test <struct> 1 >>a 2 >>b 3 >>c ] unit-test
392 { 4095 } [ bit-field-test <struct> 8191 >>a a>> ] unit-test
393 { 1 } [ bit-field-test <struct> 1 >>b b>> ] unit-test
394 { -2 } [ bit-field-test <struct> 2 >>b b>> ] unit-test
395 { 1 } [ bit-field-test <struct> 257 >>c c>> ] unit-test
396 { 3 } [ bit-field-test heap-size ] unit-test
406 referent malloc-struct &free
413 STRUCT: self-referent
419 self-referent <struct>
420 self-referent malloc-struct &free
427 C-TYPE: forward-referent
428 STRUCT: backward-referent
429 { x forward-referent* }
431 STRUCT: forward-referent
432 { x backward-referent* }
437 forward-referent <struct>
438 backward-referent malloc-struct &free
447 backward-referent <struct>
448 forward-referent malloc-struct &free
456 STRUCT: ppc-align-test-1
460 [ 16 ] [ ppc-align-test-1 heap-size ] unit-test
462 STRUCT: ppc-align-test-2
466 [ 16 ] [ ppc-align-test-2 heap-size ] unit-test
467 [ 8 ] [ "x" ppc-align-test-2 offset-of ] unit-test
470 STRUCT: struct-test-delegate
472 STRUCT: struct-test-delegator
473 { del struct-test-delegate }
475 CONSULT: struct-test-delegate struct-test-delegator del>> ;
477 { S{ struct-test-delegator f S{ struct-test-delegate f 7 } 8 } } [
478 struct-test-delegator <struct>
483 SPECIALIZED-ARRAY: void*
485 STRUCT: silly-array-field-test { x int*[3] } ;
487 { t } [ silly-array-field-test <struct> x>> void*-array? ] unit-test
490 PACKED-STRUCT: packed-struct-test
497 { 15 } [ packed-struct-test heap-size ] unit-test
499 { 0 } [ "d" packed-struct-test offset-of ] unit-test
500 { 4 } [ "e" packed-struct-test offset-of ] unit-test
501 { 6 } [ "f" packed-struct-test offset-of ] unit-test
502 { 10 } [ "g" packed-struct-test offset-of ] unit-test
503 { 11 } [ "h" packed-struct-test offset-of ] unit-test
505 { POSTPONE: PACKED-STRUCT: }
506 [ packed-struct-test struct-definer-word ] unit-test
508 STRUCT: struct-1 { a c:int } ;
509 PACKED-STRUCT: struct-1-packed { a c:int } ;
510 UNION-STRUCT: struct-1-union { a c:int } ;
512 { "USING: alien.c-types classes.struct ;
513 IN: classes.struct.tests
514 STRUCT: struct-1 { a int initial: 0 } ;
516 [ \ struct-1 [ see ] with-default-string-writer ] unit-test
517 { "USING: alien.c-types classes.struct ;
518 IN: classes.struct.tests
519 PACKED-STRUCT: struct-1-packed { a int initial: 0 } ;
521 [ \ struct-1-packed [ see ] with-default-string-writer ] unit-test
522 { "USING: alien.c-types classes.struct ;
523 IN: classes.struct.tests
524 STRUCT: struct-1-union { a int initial: 0 } ;
526 [ \ struct-1-union [ see ] with-default-string-writer ] unit-test
529 STRUCT: going-to-redefine { a uint } ;
531 "IN: classes.struct.tests TUPLE: going-to-redefine b ;" eval( -- )
533 { f } [ \ going-to-redefine \ clone ?lookup-method ] unit-test
534 { f } [ \ going-to-redefine \ struct-slot-values ?lookup-method ] unit-test
536 ! Test reset-class on structs, which should forget all the accessors, clone, and struct-slot-values
537 STRUCT: some-accessors { aaa uint } { bbb int } ;
538 { } [ [ \ some-accessors reset-class ] with-compilation-unit ] unit-test
539 { f } [ \ some-accessors \ a>> ?lookup-method ] unit-test
540 { f } [ \ some-accessors \ a<< ?lookup-method ] unit-test
541 { f } [ \ some-accessors \ b>> ?lookup-method ] unit-test
542 { f } [ \ some-accessors \ b<< ?lookup-method ] unit-test
543 { f } [ \ some-accessors \ clone ?lookup-method ] unit-test
544 { f } [ \ some-accessors \ struct-slot-values ?lookup-method ] unit-test
546 << \ some-accessors forget >>
549 { 0 } [ struct-test-equality-1 new hashcode ] unit-test
553 struct-test-equality-1 <struct> 5 >>x
554 struct-test-equality-1 malloc-struct &free 5 >>x
559 ! Same slots, so the hashcode should be the same.
561 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
562 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
568 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
569 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct