]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / classes / struct / struct-tests.factor
1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.libraries
3 alien.structs.fields alien.syntax ascii classes.struct combinators
4 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 ;
10 IN: classes.struct.tests
11
12 <<
13 : libfactor-ffi-tests-path ( -- string )
14     "resource:" (normalize-path)
15     {
16         { [ os winnt? ]  [ "libfactor-ffi-test.dll" ] }
17         { [ os macosx? ] [ "libfactor-ffi-test.dylib" ] }
18         { [ os unix?  ]  [ "libfactor-ffi-test.so" ] }
19     } cond append-path ;
20
21 "f-cdecl" libfactor-ffi-tests-path "cdecl" add-library
22
23 "f-stdcall" libfactor-ffi-tests-path "stdcall" add-library
24 >>
25
26 SYMBOL: struct-test-empty
27
28 [ [ struct-test-empty { } define-struct-class ] with-compilation-unit ]
29 [ struct-must-have-slots? ] must-fail-with
30
31 STRUCT: struct-test-foo
32     { x char }
33     { y int initial: 123 }
34     { z bool } ;
35
36 STRUCT: struct-test-bar
37     { w ushort initial: HEX: ffff }
38     { foo struct-test-foo } ;
39
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
45
46 [ 1 2 3 t ] [
47     1   2 3 t struct-test-foo <struct-boa>   struct-test-bar <struct-boa>
48     {
49         [ w>> ] 
50         [ foo>> x>> ]
51         [ foo>> y>> ]
52         [ foo>> z>> ]
53     } cleave
54 ] unit-test
55
56 [ 7654 ] [ S{ struct-test-foo f 98 7654 f } y>> ] unit-test
57 [ 7654 ] [ S{ struct-test-foo { y 7654 } } y>> ] unit-test
58
59 UNION-STRUCT: struct-test-float-and-bits
60     { f float }
61     { bits uint } ;
62
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
65
66 [ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
67
68 STRUCT: struct-test-string-ptr
69     { x char* } ;
70
71 [ "hello world" ] [
72     [
73         struct-test-string-ptr <struct>
74         "hello world" utf8 malloc-string &free >>x
75         x>>
76     ] with-destructors
77 ] unit-test
78
79 [ "S{ struct-test-foo { y 7654 } }" ]
80 [
81     f boa-tuples?
82     [ struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer ]
83     with-variable
84 ] unit-test
85
86 [ "S{ struct-test-foo f 0 7654 f }" ]
87 [
88     t boa-tuples?
89     [ struct-test-foo <struct> 7654 >>y [ pprint ] with-string-writer ]
90     with-variable
91 ] unit-test
92
93 [ <" USING: classes.struct ;
94 IN: classes.struct.tests
95 STRUCT: struct-test-foo
96     { x char initial: 0 } { y int initial: 123 } { z bool } ;
97 "> ]
98 [ [ struct-test-foo see ] with-string-writer ] unit-test
99
100 [ <" USING: classes.struct ;
101 IN: classes.struct.tests
102 UNION-STRUCT: struct-test-float-and-bits
103     { f float initial: 0.0 } { bits uint initial: 0 } ;
104 "> ]
105 [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test
106
107 [ {
108     T{ field-spec
109         { name "x" }
110         { offset 0 }
111         { type "char" }
112         { reader x>> }
113         { writer (>>x) }
114     }
115     T{ field-spec
116         { name "y" }
117         { offset 4 }
118         { type "int" }
119         { reader y>> }
120         { writer (>>y) }
121     }
122     T{ field-spec
123         { name "z" }
124         { offset 8 }
125         { type "bool" }
126         { reader z>> }
127         { writer (>>z) }
128     }
129 } ] [ "struct-test-foo" c-type fields>> ] unit-test
130
131 [ {
132     T{ field-spec
133         { name "f" }
134         { offset 0 }
135         { type "float" }
136         { reader f>> }
137         { writer (>>f) }
138     }
139     T{ field-spec
140         { name "bits" }
141         { offset 0 }
142         { type "uint" }
143         { reader bits>> }
144         { writer (>>bits) }
145     }
146 } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
147
148 STRUCT: struct-test-equality-1
149     { x int } ;
150 STRUCT: struct-test-equality-2
151     { y int } ;
152
153 [ t ] [
154     [
155         struct-test-equality-1 <struct> 5 >>x
156         struct-test-equality-1 malloc-struct &free 5 >>x =
157     ] with-destructors
158 ] unit-test
159
160 [ f ] [
161     [
162         struct-test-equality-1 <struct> 5 >>x
163         struct-test-equality-2 malloc-struct &free 5 >>y =
164     ] with-destructors
165 ] unit-test
166
167 STRUCT: struct-test-ffi-foo
168     { x int }
169     { y int } ;
170
171 LIBRARY: f-cdecl
172 FUNCTION: int ffi_test_11 ( int a, struct-test-ffi-foo b, int c ) ;
173
174 [ 14 ] [ 1 2 3 struct-test-ffi-foo <struct-boa> 4 ffi_test_11 ] unit-test
175
176 STRUCT: struct-test-array-slots
177     { x int }
178     { y ushort[6] initial: ushort-array{ 2 3 5 7 11 13 } }
179     { z int } ;
180
181 [ 11 ] [ struct-test-array-slots <struct> y>> 4 swap nth ] unit-test
182
183 [ t ] [
184     struct-test-array-slots <struct>
185     [ y>> [ 8 3 ] dip set-nth ]
186     [ y>> ushort-array{ 2 3 5 8 11 13 } sequence= ] bi
187 ] unit-test
188
189 STRUCT: struct-test-optimization
190     { x { "int" 3 } } { y int } ;
191
192 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
193 [ t ] [
194     [ 3 struct-test-optimization <direct-struct-array> third y>> ]
195     { <tuple> <tuple-boa> memory>struct y>> } inlined?
196 ] unit-test
197
198 [ t ] [ [ struct-test-optimization memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
199
200 [ t ] [
201     [ struct-test-optimization memory>struct x>> second ]
202     { memory>struct x>> <direct-int-array> <tuple> <tuple-boa> } inlined?
203 ] unit-test
204
205 [ f ] [ [ memory>struct y>> ] { memory>struct y>> } inlined? ] unit-test
206
207 [ f ] [ struct-test-foo <struct> dup clone [ >c-ptr ] bi@ eq? ] unit-test