1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.libraries
3 alien.structs.fields alien.syntax ascii byte-arrays classes.struct
4 combinators destructors io.encodings.utf8 io.pathnames io.streams.string
5 kernel libc literals math multiline namespaces prettyprint
6 prettyprint.config see sequences specialized-arrays.ushort
7 system tools.test compiler.tree.debugger struct-arrays
8 classes.tuple.private specialized-arrays.direct.int
9 compiler.units specialized-arrays.char ;
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
59 UNION-STRUCT: struct-test-float-and-bits
63 [ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
64 [ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
66 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
68 STRUCT: struct-test-string-ptr
73 struct-test-string-ptr <struct>
74 "hello world" utf8 malloc-string &free >>x
79 [ "S{ struct-test-foo { y 7654 } }" ]
83 c-object-pointers? off
84 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
88 [ "S@ struct-test-foo B{ 0 0 0 0 0 0 0 0 0 0 0 0 }" ]
92 12 <byte-array> struct-test-foo memory>struct [ pprint ] with-string-writer
96 [ "S{ struct-test-foo f 0 7654 f }" ]
100 c-object-pointers? off
101 struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer
105 [ "S@ struct-test-foo f" ]
108 c-object-pointers? off
109 f struct-test-foo memory>struct [ pprint ] with-string-writer
113 [ <" USING: classes.struct ;
114 IN: classes.struct.tests
115 STRUCT: struct-test-foo
116 { x char initial: 0 } { y int initial: 123 } { z bool } ;
118 [ [ struct-test-foo see ] with-string-writer ] unit-test
120 [ <" USING: classes.struct ;
121 IN: classes.struct.tests
122 UNION-STRUCT: struct-test-float-and-bits
123 { f float initial: 0.0 } { bits uint initial: 0 } ;
125 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
149 } ] [ "struct-test-foo" c-type fields>> ] unit-test
166 } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
168 STRUCT: struct-test-equality-1
170 STRUCT: struct-test-equality-2
175 struct-test-equality-1 <struct> 5 >>x
176 struct-test-equality-1 malloc-struct &free 5 >>x =
182 struct-test-equality-1 <struct> 5 >>x
183 struct-test-equality-2 malloc-struct &free 5 >>y =
189 struct-test-equality-1 <struct> 5 >>x
190 struct-test-equality-1 malloc-struct &free 5 >>x
195 STRUCT: struct-test-ffi-foo
200 FUNCTION: int ffi_test_11 ( int a, struct-test-ffi-foo b, int c ) ;
202 [ 14 ] [ 1 2 3 struct-test-ffi-foo <struct-boa> 4 ffi_test_11 ] unit-test
204 STRUCT: struct-test-array-slots
206 { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
209 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
212 struct-test-array-slots <struct>
213 [ y>> [ 8 3 ] dip set-nth ]
214 [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
217 STRUCT: struct-test-optimization
218 { x { "int" 3 } } { y int } ;
220 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
222 [ 3 struct-test-optimization <direct-struct-array> third y>> ]
223 { <tuple> <tuple-boa> memory>struct y>> } inlined?
226 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
229 [ struct-test-optimization memory>struct x>> second ]
230 { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
233 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
235 ! Test cloning structs
236 STRUCT: clone-test-struct { x int } { y char[3] } ;
238 [ 1 char-array{ 9 1 1 } ] [
239 clone-test-struct <struct>
240 1 >>x char-array{ 9 1 1 } >>y
242 [ x>> ] [ y>> >char-array ] bi
245 [ t 1 char-array{ 9 1 1 } ] [
247 clone-test-struct malloc-struct &free
248 1 >>x char-array{ 9 1 1 } >>y
250 [ >c-ptr byte-array? ] [ x>> ] [ y>> >char-array ] tri
254 STRUCT: struct-that's-a-word { x int } ;
256 : struct-that's-a-word ( -- ) "OOPS" throw ;
258 [ -77 ] [ S{ struct-that's-a-word { x -77 } } clone x>> ] unit-test