1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.libraries
3 alien.structs.fields alien.syntax ascii assocs byte-arrays
4 classes.struct classes.tuple.private combinators
5 compiler.tree.debugger compiler.units destructors
6 io.encodings.utf8 io.pathnames io.streams.string kernel libc
7 literals math mirrors multiline namespaces prettyprint
8 prettyprint.config see sequences specialized-arrays.char int
9 specialized-arrays.ushort struct-arrays system tools.test ;
10 IN: classes.struct.tests
13 : libfactor-ffi-tests-path ( -- string )
14 "resource:" (normalize-path)
16 { [ os winnt? ] [ "libfactor-ffi-test.dll" ] }
17 { [ os macosx? ] [ "libfactor-ffi-test.dylib" ] }
18 { [ os unix? ] [ "libfactor-ffi-test.so" ] }
21 "f-cdecl" libfactor-ffi-tests-path "cdecl" add-library
23 "f-stdcall" libfactor-ffi-tests-path "stdcall" add-library
26 SYMBOL: struct-test-empty
28 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
29 [ struct-must-have-slots? ] must-fail-with
31 STRUCT: struct-test-foo
33 { y int initial: 123 }
36 STRUCT: struct-test-bar
37 { w ushort initial: HEX: ffff }
38 { foo struct-test-foo } ;
40 [ 12 ] [ struct-test-foo heap-size ] unit-test
41 [ 12 ] [ struct-test-foo <struct> byte-length ] unit-test
42 [ 16 ] [ struct-test-bar heap-size ] unit-test
43 [ 123 ] [ struct-test-foo <struct> y>> ] unit-test
44 [ 123 ] [ struct-test-bar <struct> foo>> y>> ] unit-test
47 1 2 3 t struct-test-foo <struct-boa> struct-test-bar <struct-boa>
56 [ 7654 ] [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
57 [ 7654 ] [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
60 { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } }
62 { { "y" "int" } HEX: 7F00007F }
65 B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct
69 [ { { "underlying" f } } ] [
70 f struct-test-foo memory>struct
74 [ 55 t ] [ S{ struct-test-foo { x 55 } } make-mirror { "x" "char" } swap at* ] unit-test
75 [ 55 t ] [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } swap at* ] unit-test
76 [ t t ] [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } swap at* ] unit-test
77 [ f t ] [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } swap at* ] unit-test
78 [ f f ] [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } swap at* ] unit-test
79 [ f f ] [ S{ struct-test-foo } make-mirror "nonexist" swap at* ] unit-test
80 [ f t ] [ f struct-test-foo memory>struct make-mirror "underlying" swap at* ] unit-test
82 [ S{ struct-test-foo { x 3 } { y 2 } { z f } } ] [
83 S{ struct-test-foo { x 1 } { y 2 } { z f } }
84 [ make-mirror [ 3 { "x" "char" } ] dip set-at ] keep
87 [ S{ struct-test-foo { x 1 } { y 5 } { z f } } ] [
88 S{ struct-test-foo { x 1 } { y 2 } { z f } }
89 [ make-mirror [ 5 { "y" "int" } ] dip set-at ] keep
92 [ S{ struct-test-foo { x 1 } { y 2 } { z t } } ] [
93 S{ struct-test-foo { x 1 } { y 2 } { z f } }
94 [ make-mirror [ t { "z" "bool" } ] dip set-at ] keep
97 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
98 S{ struct-test-foo { x 1 } { y 2 } { z f } }
99 [ make-mirror [ "nonsense" "underlying" ] dip set-at ] keep
102 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
103 S{ struct-test-foo { x 1 } { y 2 } { z f } }
104 [ make-mirror [ "nonsense" "nonexist" ] dip set-at ] keep
107 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
108 S{ struct-test-foo { x 1 } { y 2 } { z f } }
109 [ make-mirror [ "nonsense" { "nonexist" "int" } ] dip set-at ] keep
112 [ S{ struct-test-foo { x 1 } { y 123 } { z f } } ] [
113 S{ struct-test-foo { x 1 } { y 2 } { z f } }
114 [ make-mirror { "y" "int" } swap delete-at ] keep
117 [ S{ struct-test-foo { x 0 } { y 2 } { z f } } ] [
118 S{ struct-test-foo { x 1 } { y 2 } { z f } }
119 [ make-mirror { "x" "char" } swap delete-at ] keep
122 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
123 S{ struct-test-foo { x 1 } { y 2 } { z f } }
124 [ make-mirror { "nonexist" "char" } swap delete-at ] keep
127 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
128 S{ struct-test-foo { x 1 } { y 2 } { z f } }
129 [ make-mirror "underlying" swap delete-at ] keep
132 [ S{ struct-test-foo { x 1 } { y 2 } { z f } } ] [
133 S{ struct-test-foo { x 1 } { y 2 } { z f } }
134 [ make-mirror "nonsense" swap delete-at ] keep
137 [ S{ struct-test-foo { x 0 } { y 123 } { z f } } ] [
138 S{ struct-test-foo { x 1 } { y 2 } { z t } }
139 [ make-mirror clear-assoc ] keep
142 UNION-STRUCT: struct-test-float-and-bits
146 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
147 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
149 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] 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 } }" ]
166 c-object-pointers? off
167 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
171 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
174 c-object-pointers? on
175 12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
179 [ "S{ struct-test-foo f 0 7654 f }" ]
183 c-object-pointers? off
184 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
188 [ "S@ struct-test-foo f" ]
191 c-object-pointers? off
192 f struct-test-foo memory>struct [ pprint ] with-string-writer
196 [ <" USING: classes.struct ;
197 IN: classes.struct.tests
198 STRUCT: struct-test-foo
199 { x char initial: 0 } { y int initial: 123 } { z bool } ;
201 [ [ struct-test-foo see ] with-string-writer ] unit-test
203 [ <" USING: classes.struct ;
204 IN: classes.struct.tests
205 UNION-STRUCT: struct-test-float-and-bits
206 { f float initial: 0.0 } { bits uint initial: 0 } ;
208 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
232 } ] [ "struct-test-foo" c-type fields>> ] unit-test
249 } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
251 STRUCT: struct-test-equality-1
253 STRUCT: struct-test-equality-2
258 struct-test-equality-1 <struct> 5 >>x
259 struct-test-equality-1 malloc-struct &free 5 >>x =
265 struct-test-equality-1 <struct> 5 >>x
266 struct-test-equality-2 malloc-struct &free 5 >>y =
272 struct-test-equality-1 <struct> 5 >>x
273 struct-test-equality-1 malloc-struct &free 5 >>x
278 STRUCT: struct-test-ffi-foo
283 FUNCTION: int ffi_test_11 ( int a, struct-test-ffi-foo b, int c ) ;
285 [ 14 ] [ 1 2 3 struct-test-ffi-foo <struct-boa> 4 ffi_test_11 ] unit-test
287 STRUCT: struct-test-array-slots
289 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
292 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
295 struct-test-array-slots <struct>
296 [ y>> [ 8 3 ] dip set-nth ]
297 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
300 STRUCT: struct-test-optimization
301 { x { "int" 3 } } { y int } ;
303 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
305 [ 3 struct-test-optimization <direct-struct-array> third y>> ]
306 { <tuple> <tuple-boa> memory>struct y>> } inlined?
309 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
312 [ struct-test-optimization memory>struct x>> second ]
313 { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
316 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
318 ! Test cloning structs
319 STRUCT: clone-test-struct { x int } { y char[3] } ;
321 [ 1 char-array{ 9 1 1 } ] [
322 clone-test-struct <struct>
323 1 >>x char-array{ 9 1 1 } >>y
325 [ x>> ] [ y>> >char-array ] bi
328 [ t 1 char-array{ 9 1 1 } ] [
330 clone-test-struct malloc-struct &free
331 1 >>x char-array{ 9 1 1 } >>y
333 [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
337 STRUCT: struct-that's-a-word { x int } ;
339 : struct-that's-a-word ( -- ) "OOPS" throw ;
341 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test