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
10 SPECIALIZED-ARRAY: char
11 SPECIALIZED-ARRAY: int
12 SPECIALIZED-ARRAY: ushort
13 IN: classes.struct.tests
16 : libfactor-ffi-tests-path ( -- string )
17 "resource:" (normalize-path)
19 { [ os winnt? ] [ "libfactor-ffi-test.dll" ] }
20 { [ os macosx? ] [ "libfactor-ffi-test.dylib" ] }
21 { [ os unix? ] [ "libfactor-ffi-test.so" ] }
24 "f-cdecl" libfactor-ffi-tests-path "cdecl" add-library
26 "f-stdcall" libfactor-ffi-tests-path "stdcall" add-library
29 SYMBOL: struct-test-empty
31 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
32 [ struct-must-have-slots? ] must-fail-with
34 STRUCT: struct-test-foo
36 { y int initial: 123 }
39 STRUCT: struct-test-bar
40 { w ushort initial: HEX: ffff }
41 { foo struct-test-foo } ;
43 [ 12 ] [ struct-test-foo heap-size ] unit-test
44 [ 12 ] [ struct-test-foo <struct> byte-length ] 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" } HEX: 7F00007F }
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" } swap at* ] unit-test
78 [ 55 t ] [ S{ struct-test-foo { y 55 } } make-mirror { "y" "int" } swap at* ] unit-test
79 [ t t ] [ S{ struct-test-foo { z t } } make-mirror { "z" "bool" } swap at* ] unit-test
80 [ f t ] [ S{ struct-test-foo { z f } } make-mirror { "z" "bool" } swap at* ] unit-test
81 [ f f ] [ S{ struct-test-foo } make-mirror { "nonexist" "bool" } swap at* ] unit-test
82 [ f f ] [ S{ struct-test-foo } make-mirror "nonexist" swap at* ] unit-test
83 [ f t ] [ f struct-test-foo memory>struct make-mirror "underlying" swap at* ] 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 UNION-STRUCT: struct-test-float-and-bits
149 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
150 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
152 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
154 STRUCT: struct-test-string-ptr
159 struct-test-string-ptr <struct>
160 "hello world" utf8 malloc-string &free >>x
165 [ "S{ struct-test-foo { x 0 } { y 7654 } { z f } }" ]
169 c-object-pointers? off
170 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
174 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
177 c-object-pointers? on
178 12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
182 [ "S{ struct-test-foo f 0 7654 f }" ]
186 c-object-pointers? off
187 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
191 [ "S@ struct-test-foo f" ]
194 c-object-pointers? off
195 f struct-test-foo memory>struct [ pprint ] with-string-writer
199 [ <" USING: classes.struct ;
200 IN: classes.struct.tests
201 STRUCT: struct-test-foo
202 { x char initial: 0 } { y int initial: 123 } { z bool } ;
204 [ [ struct-test-foo see ] with-string-writer ] unit-test
206 [ <" USING: classes.struct ;
207 IN: classes.struct.tests
208 UNION-STRUCT: struct-test-float-and-bits
209 { f float initial: 0.0 } { bits uint initial: 0 } ;
211 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
235 } ] [ "struct-test-foo" c-type fields>> ] unit-test
252 } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
254 STRUCT: struct-test-equality-1
256 STRUCT: struct-test-equality-2
261 struct-test-equality-1 <struct> 5 >>x
262 struct-test-equality-1 malloc-struct &free 5 >>x =
268 struct-test-equality-1 <struct> 5 >>x
269 struct-test-equality-2 malloc-struct &free 5 >>y =
275 struct-test-equality-1 <struct> 5 >>x
276 struct-test-equality-1 malloc-struct &free 5 >>x
281 STRUCT: struct-test-ffi-foo
286 FUNCTION: int ffi_test_11 ( int a, struct-test-ffi-foo b, int c ) ;
288 [ 14 ] [ 1 2 3 struct-test-ffi-foo <struct-boa> 4 ffi_test_11 ] unit-test
290 STRUCT: struct-test-array-slots
292 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
295 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
298 struct-test-array-slots <struct>
299 [ y>> [ 8 3 ] dip set-nth ]
300 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
303 STRUCT: struct-test-optimization
304 { x { "int" 3 } } { y int } ;
306 SPECIALIZED-ARRAY: struct-test-optimization
308 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
310 [ 3 <direct-struct-test-optimization-array> third y>> ]
311 { <tuple> <tuple-boa> memory>struct y>> } inlined?
314 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
317 [ struct-test-optimization memory>struct x>> second ]
318 { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
321 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
324 [ struct-test-optimization <struct> struct-test-optimization <struct> [ x>> ] bi@ ]
328 ! Test cloning structs
329 STRUCT: clone-test-struct { x int } { y char[3] } ;
331 [ 1 char-array{ 9 1 1 } ] [
332 clone-test-struct <struct>
333 1 >>x char-array{ 9 1 1 } >>y
335 [ x>> ] [ y>> >char-array ] bi
338 [ t 1 char-array{ 9 1 1 } ] [
340 clone-test-struct malloc-struct &free
341 1 >>x char-array{ 9 1 1 } >>y
343 [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
347 STRUCT: struct-that's-a-word { x int } ;
349 : struct-that's-a-word ( -- ) "OOPS" throw ;
351 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test