1 ! Copyright (C) 2009, 2010, 2011 Joe Groff, Slava Pestov, John Benediktsson.
2 ! See https://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 { t } [ [ struct-test-foo new ] { <struct> } inlined? ] unit-test
44 { t } [ [ struct-test-foo boa ] { <struct-boa> } inlined? ] unit-test
45 { 16 } [ struct-test-bar heap-size ] unit-test
46 { 123 } [ struct-test-foo <struct> y>> ] unit-test
47 { 123 } [ struct-test-bar <struct> foo>> y>> ] unit-test
50 1 2 3 t struct-test-foo <struct-boa> struct-test-bar <struct-boa>
59 { 7654 } [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
60 { 7654 } [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
63 { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
65 { { "y" int } 0x7F00007F }
68 B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
72 { { { "underlying" f } } } [
73 f struct-test-foo memory>struct
77 { 55 t } [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } ?of ] unit-test
78 { 55 t } [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } ?of ] unit-test
79 { t t } [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } ?of ] unit-test
80 { f t } [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } ?of ] unit-test
81 { { "nonexist" "bool" } f } [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } ?of ] unit-test
82 { "nonexist" f } [ S{ struct-test-foo } make-mirror "nonexist" ?of ] unit-test
83 { f t } [ f struct-test-foo memory>struct make-mirror "underlying" ?of ] unit-test
85 { S{ struct-test-foo { x 3 } { y 2 } { z f } } } [
86 S{ struct-test-foo { x 1 } { y 2 } { z f } }
87 [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
90 { S{ struct-test-foo { x 1 } { y 5 } { z f } } } [
91 S{ struct-test-foo { x 1 } { y 2 } { z f } }
92 [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
95 { S{ struct-test-foo { x 1 } { y 2 } { z t } } } [
96 S{ struct-test-foo { x 1 } { y 2 } { z f } }
97 [ make-mirror [ t { "z" "bool" } ] 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" "underlying" ] dip set-at ] keep
105 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
106 S{ struct-test-foo { x 1 } { y 2 } { z f } }
107 [ make-mirror [ "nonsense" "nonexist" ] dip set-at ] keep
110 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
111 S{ struct-test-foo { x 1 } { y 2 } { z f } }
112 [ make-mirror [ "nonsense" { "nonexist" "int" } ] dip set-at ] keep
115 { S{ struct-test-foo { x 1 } { y 123 } { z f } } } [
116 S{ struct-test-foo { x 1 } { y 2 } { z f } }
117 [ make-mirror { "y" "int" } swap delete-at ] keep
120 { S{ struct-test-foo { x 0 } { y 2 } { z f } } } [
121 S{ struct-test-foo { x 1 } { y 2 } { z f } }
122 [ make-mirror { "x" "char" } 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 { "nonexist" "char" } swap delete-at ] keep
130 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
131 S{ struct-test-foo { x 1 } { y 2 } { z f } }
132 [ make-mirror "underlying" swap delete-at ] keep
135 { S{ struct-test-foo { x 1 } { y 2 } { z f } } } [
136 S{ struct-test-foo { x 1 } { y 2 } { z f } }
137 [ make-mirror "nonsense" swap delete-at ] keep
140 { S{ struct-test-foo { x 0 } { y 123 } { z f } } } [
141 S{ struct-test-foo { x 1 } { y 2 } { z t } }
142 [ make-mirror clear-assoc ] keep
145 { POSTPONE: STRUCT: }
146 [ struct-test-foo struct-definer-word ] unit-test
148 UNION-STRUCT: struct-test-float-and-bits
152 { 1.0 } [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
153 { 4 } [ struct-test-float-and-bits heap-size ] unit-test
155 { 123 } [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
157 { POSTPONE: UNION-STRUCT: }
158 [ struct-test-float-and-bits struct-definer-word ] unit-test
160 STRUCT: struct-test-string-ptr
165 struct-test-string-ptr <struct>
166 "hello world" utf8 malloc-string &free >>x
171 { "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" }
173 H{ { boa-tuples? f } { c-object-pointers? f } } [
174 struct-test-foo <struct> 7654 >>y unparse
178 { "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" }
180 H{ { c-object-pointers? t } } [
181 12 <byte-array> struct-test-foo memory>struct unparse
185 { "S{ struct-test-foo f 0 7654 f }" }
187 H{ { boa-tuples? t } { c-object-pointers? f } } [
188 struct-test-foo <struct> 7654 >>y unparse
192 { "S@ struct-test-foo f" }
194 H{ { c-object-pointers? f } } [
195 f struct-test-foo memory>struct unparse
199 : with-default-string-writer ( quot -- str )
200 64 margin [ with-string-writer ] with-variable ; inline
202 { "USING: alien.c-types classes.struct ;
203 IN: classes.struct.tests
204 STRUCT: struct-test-foo
205 { x char initial: 0 } { y int initial: 123 } { z bool } ;
207 [ [ struct-test-foo see ] with-default-string-writer ] unit-test
209 { "USING: alien.c-types classes.struct ;
210 IN: classes.struct.tests
211 UNION-STRUCT: struct-test-float-and-bits
212 { f float initial: 0.0 } { bits uint initial: 0 } ;
214 [ [ struct-test-float-and-bits see ] with-default-string-writer ] unit-test
228 { class $[ cell 4 = integer fixnum ? ] }
238 } } [ struct-test-foo lookup-c-type fields>> ] unit-test
252 { class $[ cell 4 = integer fixnum ? ] }
255 } } [ struct-test-float-and-bits lookup-c-type fields>> ] unit-test
257 STRUCT: struct-test-equality-1
259 STRUCT: struct-test-equality-2
265 struct-test-equality-1 <struct> 5 >>x
266 struct-test-equality-1 malloc-struct &free 5 >>x =
272 struct-test-equality-1 <struct> 5 >>x
273 struct-test-equality-2 malloc-struct &free 5 >>y =
277 STRUCT: struct-test-array-slots
279 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
282 { 11 } [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
285 struct-test-array-slots <struct>
286 [ y>> [ 8 3 ] dip set-nth ]
287 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
290 STRUCT: struct-test-optimization
291 { x { int 3 } } { y int } ;
293 SPECIALIZED-ARRAY: struct-test-optimization
295 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
297 [ 3 struct-test-optimization <c-direct-array> third y>> ]
298 { <tuple> <tuple-boa> memory>struct y>> } inlined?
301 { t } [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
304 [ struct-test-optimization memory>struct x>> second ]
305 { memory>struct x>> int <c-direct-array> <tuple> <tuple-boa> } inlined?
308 { f } [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
311 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
317 struct-test-optimization specialized-array-vocab forget-vocab
318 ] with-compilation-unit
321 ! Test cloning structs
322 STRUCT: clone-test-struct { x int } { y char[3] } ;
324 { 1 char-array{ 9 1 1 } } [
325 clone-test-struct <struct>
326 1 >>x char-array{ 9 1 1 } >>y
328 [ x>> ] [ y>> char >c-array ] bi
331 { t 1 char-array{ 9 1 1 } } [
333 clone-test-struct malloc-struct &free
334 1 >>x char-array{ 9 1 1 } >>y
336 [ >c-ptr byte-array? ] [ x>> ] [ y>> char >c-array ] tri
340 STRUCT: struct-that's-a-word { x int } ;
342 : struct-that's-a-word ( -- ) "OOPS" throw ;
344 { -77 } [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
346 ! Interactive parsing of struct slot definitions
348 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
349 "struct-class-test-1" parse-stream
350 ] [ error>> error>> unexpected-eof? ] must-fail-with
353 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x uint } ;" eval( -- )
354 ] [ error>> duplicate-slot-names? ] must-fail-with
357 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-duplicate-slots { x uint } { x float } ;" eval( -- )
358 ] [ error>> duplicate-slot-names? ] must-fail-with
360 ! S{ with non-struct type
362 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
364 ] [ error>> no-method? ] must-fail-with
366 ! Subclassing a struct class should not be allowed
368 "USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
370 ] [ error>> bad-superclass? ] must-fail-with
372 ! Changing a superclass into a struct should reset the subclass
373 TUPLE: will-become-struct ;
375 TUPLE: a-subclass < will-become-struct ;
377 { f } [ will-become-struct struct-class? ] unit-test
379 { will-become-struct } [ a-subclass superclass-of ] unit-test
381 { } [ "IN: classes.struct.tests USING: classes.struct alien.c-types ; STRUCT: will-become-struct { x int } ;" eval( -- ) ] unit-test
383 { t } [ will-become-struct struct-class? ] unit-test
385 { tuple } [ a-subclass superclass-of ] unit-test
387 STRUCT: bit-field-test
392 { S{ bit-field-test f 0 0 0 } } [ bit-field-test <struct> ] unit-test
393 { S{ bit-field-test f 1 -2 3 } } [ bit-field-test <struct> 1 >>a 2 >>b 3 >>c ] unit-test
394 { 4095 } [ bit-field-test <struct> 8191 >>a a>> ] unit-test
395 { 1 } [ bit-field-test <struct> 1 >>b b>> ] unit-test
396 { -2 } [ bit-field-test <struct> 2 >>b b>> ] unit-test
397 { 1 } [ bit-field-test <struct> 257 >>c c>> ] unit-test
398 { 3 } [ bit-field-test heap-size ] unit-test
408 referent malloc-struct &free
415 STRUCT: self-referent
421 self-referent <struct>
422 self-referent malloc-struct &free
429 C-TYPE: forward-referent
430 STRUCT: backward-referent
431 { x forward-referent* }
433 STRUCT: forward-referent
434 { x backward-referent* }
439 forward-referent <struct>
440 backward-referent malloc-struct &free
449 backward-referent <struct>
450 forward-referent malloc-struct &free
458 STRUCT: ppc-align-test-1
462 [ 16 ] [ ppc-align-test-1 heap-size ] unit-test
464 STRUCT: ppc-align-test-2
468 [ 16 ] [ ppc-align-test-2 heap-size ] unit-test
469 [ 8 ] [ "x" ppc-align-test-2 offset-of ] unit-test
472 STRUCT: struct-test-delegate
474 STRUCT: struct-test-delegator
475 { del struct-test-delegate }
477 CONSULT: struct-test-delegate struct-test-delegator del>> ;
479 { S{ struct-test-delegator f S{ struct-test-delegate f 7 } 8 } } [
480 struct-test-delegator <struct>
485 SPECIALIZED-ARRAY: void*
487 STRUCT: silly-array-field-test { x int*[3] } ;
489 { t } [ silly-array-field-test <struct> x>> void*-array? ] unit-test
492 PACKED-STRUCT: packed-struct-test
499 { 15 } [ packed-struct-test heap-size ] unit-test
501 { 0 } [ "d" packed-struct-test offset-of ] unit-test
502 { 4 } [ "e" packed-struct-test offset-of ] unit-test
503 { 6 } [ "f" packed-struct-test offset-of ] unit-test
504 { 10 } [ "g" packed-struct-test offset-of ] unit-test
505 { 11 } [ "h" packed-struct-test offset-of ] unit-test
507 { POSTPONE: PACKED-STRUCT: }
508 [ packed-struct-test struct-definer-word ] unit-test
510 STRUCT: struct-1 { a c:int } ;
511 PACKED-STRUCT: struct-1-packed { a c:int } ;
512 UNION-STRUCT: struct-1-union { a c:int } ;
514 { "USING: alien.c-types classes.struct ;
515 IN: classes.struct.tests
516 STRUCT: struct-1 { a int initial: 0 } ;
518 [ \ struct-1 [ see ] with-default-string-writer ] unit-test
519 { "USING: alien.c-types classes.struct ;
520 IN: classes.struct.tests
521 PACKED-STRUCT: struct-1-packed { a int initial: 0 } ;
523 [ \ struct-1-packed [ see ] with-default-string-writer ] unit-test
524 { "USING: alien.c-types classes.struct ;
525 IN: classes.struct.tests
526 STRUCT: struct-1-union { a int initial: 0 } ;
528 [ \ struct-1-union [ see ] with-default-string-writer ] unit-test
531 STRUCT: going-to-redefine { a uint } ;
533 "IN: classes.struct.tests TUPLE: going-to-redefine b ;" eval( -- )
535 { f } [ \ going-to-redefine \ clone ?lookup-method ] unit-test
536 { f } [ \ going-to-redefine \ struct-slot-values ?lookup-method ] unit-test
538 ! Test reset-class on structs, which should forget all the accessors, clone, and struct-slot-values
539 STRUCT: some-accessors { aaa uint } { bbb int } ;
540 { } [ [ \ some-accessors reset-class ] with-compilation-unit ] unit-test
541 { f } [ \ some-accessors \ a>> ?lookup-method ] unit-test
542 { f } [ \ some-accessors \ a<< ?lookup-method ] unit-test
543 { f } [ \ some-accessors \ b>> ?lookup-method ] unit-test
544 { f } [ \ some-accessors \ b<< ?lookup-method ] unit-test
545 { f } [ \ some-accessors \ clone ?lookup-method ] unit-test
546 { f } [ \ some-accessors \ struct-slot-values ?lookup-method ] unit-test
548 << \ some-accessors forget >>
551 { 0 } [ struct-test-equality-1 new hashcode ] unit-test
555 struct-test-equality-1 <struct> 5 >>x
556 struct-test-equality-1 malloc-struct &free 5 >>x
561 ! Same slots, so the hashcode should be the same.
563 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
564 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
570 B{ 98 0 33 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct
571 B{ 98 0 22 0 1 1 1 1 1 1 1 1 } struct-test-foo memory>struct