1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types ascii
3 assocs byte-arrays classes.struct classes.tuple.private
4 combinators compiler.tree.debugger compiler.units destructors
5 io.encodings.utf8 io.pathnames io.streams.string kernel libc
6 literals math mirrors multiline namespaces prettyprint
7 prettyprint.config see sequences specialized-arrays system
8 tools.test parser lexer eval layouts ;
9 SPECIALIZED-ARRAY: char
10 SPECIALIZED-ARRAY: int
11 SPECIALIZED-ARRAY: ushort
12 IN: classes.struct.tests
14 SYMBOL: struct-test-empty
16 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
17 [ struct-must-have-slots? ] must-fail-with
19 STRUCT: struct-test-foo
21 { y int initial: 123 }
24 STRUCT: struct-test-bar
25 { w ushort initial: HEX: ffff }
26 { foo struct-test-foo } ;
28 [ 12 ] [ struct-test-foo heap-size ] unit-test
29 [ 12 ] [ struct-test-foo <struct> byte-length ] unit-test
30 [ 16 ] [ struct-test-bar heap-size ] unit-test
31 [ 123 ] [ struct-test-foo <struct> y>> ] unit-test
32 [ 123 ] [ struct-test-bar <struct> foo>> y>> ] unit-test
35 1 2 3 t struct-test-foo <struct-boa> struct-test-bar <struct-boa>
44 [ 7654 ] [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
45 [ 7654 ] [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
48 { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
50 { { "y" "int" } HEX: 7F00007F }
53 B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
57 [ { { "underlying" f } } ] [
58 f struct-test-foo memory>struct
62 [ 55 t ] [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } swap at* ] unit-test
63 [ 55 t ] [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } swap at* ] unit-test
64 [ t t ] [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } swap at* ] unit-test
65 [ f t ] [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } swap at* ] unit-test
66 [ f f ] [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } swap at* ] unit-test
67 [ f f ] [ S{ struct-test-foo } make-mirror "nonexist" swap at* ] unit-test
68 [ f t ] [ f struct-test-foo memory>struct make-mirror "underlying" swap at* ] unit-test
70 [ S{ struct-test-foo { x 3 } { y 2 } { z f } } ] [
71 S{ struct-test-foo { x 1 } { y 2 } { z f } }
72 [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
75 [ S{ struct-test-foo { x 1 } { y 5 } { z f } } ] [
76 S{ struct-test-foo { x 1 } { y 2 } { z f } }
77 [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
80 [ S{ struct-test-foo { x 1 } { y 2 } { z t } } ] [
81 S{ struct-test-foo { x 1 } { y 2 } { z f } }
82 [ make-mirror [ t { "z" "bool" } ] dip set-at ] keep
85 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
86 S{ struct-test-foo { x 1 } { y 2 } { z f } }
87 [ make-mirror [ "nonsense" "underlying" ] 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" "nonexist" ] 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" "int" } ] dip set-at ] keep
100 [ S{ struct-test-foo { x 1 } { y 123 } { z f } } ] [
101 S{ struct-test-foo { x 1 } { y 2 } { z f } }
102 [ make-mirror { "y" "int" } swap delete-at ] keep
105 [ S{ struct-test-foo { x 0 } { y 2 } { z f } } ] [
106 S{ struct-test-foo { x 1 } { y 2 } { z f } }
107 [ make-mirror { "x" "char" } swap delete-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 { "nonexist" "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 "underlying" 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 "nonsense" swap delete-at ] keep
125 [ S{ struct-test-foo { x 0 } { y 123 } { z f } } ] [
126 S{ struct-test-foo { x 1 } { y 2 } { z t } }
127 [ make-mirror clear-assoc ] keep
130 UNION-STRUCT: struct-test-float-and-bits
134 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
135 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
137 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
139 STRUCT: struct-test-string-ptr
144 struct-test-string-ptr <struct>
145 "hello world" utf8 malloc-string &free >>x
150 [ "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" ]
154 c-object-pointers? off
155 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
159 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
162 c-object-pointers? on
163 12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
167 [ "S{ struct-test-foo f 0 7654 f }" ]
171 c-object-pointers? off
172 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
176 [ "S@ struct-test-foo f" ]
179 c-object-pointers? off
180 f struct-test-foo memory>struct [ pprint ] with-string-writer
184 [ <" USING: classes.struct ;
185 IN: classes.struct.tests
186 STRUCT: struct-test-foo
187 { x char initial: 0 } { y int initial: 123 } { z bool } ;
189 [ [ struct-test-foo see ] with-string-writer ] unit-test
191 [ <" USING: classes.struct ;
192 IN: classes.struct.tests
193 UNION-STRUCT: struct-test-float-and-bits
194 { f float initial: 0.0 } { bits uint initial: 0 } ;
196 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
220 } ] [ "struct-test-foo" c-type fields>> ] unit-test
237 } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
239 STRUCT: struct-test-equality-1
241 STRUCT: struct-test-equality-2
246 struct-test-equality-1 <struct> 5 >>x
247 struct-test-equality-1 malloc-struct &free 5 >>x =
253 struct-test-equality-1 <struct> 5 >>x
254 struct-test-equality-2 malloc-struct &free 5 >>y =
260 struct-test-equality-1 <struct> 5 >>x
261 struct-test-equality-1 malloc-struct &free 5 >>x
266 STRUCT: struct-test-array-slots
268 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
271 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
274 struct-test-array-slots <struct>
275 [ y>> [ 8 3 ] dip set-nth ]
276 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
279 STRUCT: struct-test-optimization
280 { x { "int" 3 } } { y int } ;
282 SPECIALIZED-ARRAY: struct-test-optimization
284 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
286 [ 3 <direct-struct-test-optimization-array> third y>> ]
287 { <tuple> <tuple-boa> memory>struct y>> } inlined?
290 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
293 [ struct-test-optimization memory>struct x>> second ]
294 { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
297 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
300 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
304 ! Test cloning structs
305 STRUCT: clone-test-struct { x int } { y char[3] } ;
307 [ 1 char-array{ 9 1 1 } ] [
308 clone-test-struct <struct>
309 1 >>x char-array{ 9 1 1 } >>y
311 [ x>> ] [ y>> >char-array ] bi
314 [ t 1 char-array{ 9 1 1 } ] [
316 clone-test-struct malloc-struct &free
317 1 >>x char-array{ 9 1 1 } >>y
319 [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
323 STRUCT: struct-that's-a-word { x int } ;
325 : struct-that's-a-word ( -- ) "OOPS" throw ;
327 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test
329 ! Interactive parsing of struct slot definitions
331 "USE: classes.struct IN: classes.struct.tests STRUCT: unexpected-eof-test" <string-reader>
332 "struct-class-test-1" parse-stream
333 ] [ error>> error>> unexpected-eof? ] must-fail-with
335 ! S{ with non-struct type
337 "USE: classes.struct IN: classes.struct.tests TUPLE: not-a-struct ; S{ not-a-struct }"
341 ! Subclassing a struct class should not be allowed
343 "USE: classes.struct IN: classes.struct.tests STRUCT: a-struct { x int } ; TUPLE: not-a-struct < a-struct ;"
347 ! Remove c-type when struct class is forgotten
349 "USE: classes.struct IN: classes.struct.tests TUPLE: a-struct ;" eval( -- )
352 [ f ] [ "a-struct" c-types get key? ] unit-test