1 IN: specialized-arrays.tests
2 USING: tools.test alien.syntax specialized-arrays
3 specialized-arrays.private sequences alien.c-types accessors
4 kernel arrays combinators compiler compiler.units classes.struct
5 combinators.smart compiler.tree.debugger math libc destructors
6 sequences.private multiline eval words vocabs namespaces
7 assocs prettyprint alien.data math.vectors definitions
9 FROM: alien.c-types => float ;
11 SPECIALIZED-ARRAY: int
12 SPECIALIZED-ARRAYS: bool ushort char uint float ulonglong ;
14 [ t ] [ { 1 2 3 } >int-array int-array? ] unit-test
16 [ t ] [ int-array{ 1 2 3 } int-array? ] unit-test
18 [ 2 ] [ int-array{ 1 2 3 } second ] unit-test
21 { t f t } >bool-array underlying>>
22 { 1 0 1 } bool heap-size {
28 [ ushort-array{ 1234 } ] [
29 little-endian? B{ 210 4 } B{ 4 210 } ? byte-array>ushort-array
32 [ B{ 210 4 1 } byte-array>ushort-array ] must-fail
35 int-array{ 3 1 3 3 7 } malloc-byte-array 5 <direct-int-array> >array
38 [ float-array{ HEX: 1.222,222 HEX: 1.111,112 } ]
39 [ float-array{ HEX: 1.222,222,2 HEX: 1.111,111,1 } ] unit-test
41 [ f ] [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] unit-test
43 [ f ] [ [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] compile-call ] unit-test
45 [ ushort-array{ 0 0 0 } ] [
46 3 ALIEN: 123 100 <direct-ushort-array> new-sequence
54 SPECIALIZED-ARRAY: test-struct
57 1 test-struct-array{ } new-sequence length
60 [ V{ test-struct } ] [
61 [ [ test-struct-array <struct> ] test-struct-array{ } output>sequence first ] final-classes
64 : make-point ( x y -- struct )
65 test-struct <struct-boa> ;
69 1 2 make-point over set-first
70 3 4 make-point over set-second
71 0 [ [ x>> ] [ y>> ] bi / + ] reduce
76 2 malloc-test-struct-array
78 1 2 make-point over set-first
79 3 4 make-point over set-second
80 0 [ [ x>> ] [ y>> ] bi / + ] reduce
84 [ ] [ ALIEN: 123 10 <direct-test-struct-array> drop ] unit-test
88 10 malloc-test-struct-array
93 [ 15 ] [ 15 10 <test-struct-array> resize length ] unit-test
95 [ S{ test-struct f 12 20 } ] [
97 S{ test-struct f 4 20 }
98 S{ test-struct f 12 20 }
99 S{ test-struct f 20 20 }
104 STRUCT: fixed-string { text char[64] } ;
106 SPECIALIZED-ARRAY: fixed-string
108 [ { ALIEN: 100 ALIEN: 140 ALIEN: 180 ALIEN: 1c0 } ] [
109 ALIEN: 100 4 <direct-fixed-string-array> [ (underlying)>> ] { } map-as
112 ! Ensure that byte-length works with direct arrays
114 ALIEN: 123 100 <direct-int-array> byte-length
117 ! Test prettyprinting
118 [ "int-array{ 1 2 3 }" ] [ int-array{ 1 2 3 } unparse ] unit-test
119 [ "int-array@ f 100" ] [ f 100 <direct-int-array> unparse ] unit-test
121 ! If the C type doesn't exist, don't generate a vocab
122 SYMBOL: __does_not_exist__
126 IN: specialized-arrays.tests
127 USING: specialized-arrays ;
129 SPECIALIZED-ARRAY: __does_not_exist__ """ eval( -- )
134 IN: specialized-arrays.tests
135 USING: alien.c-types classes.struct specialized-arrays ;
137 STRUCT: __does_not_exist__ { x int } ;
139 SPECIALIZED-ARRAY: __does_not_exist__
144 "__does_not_exist__-array{"
145 __does_not_exist__ specialized-array-vocab lookup
151 \ __does_not_exist__ forget
152 __does_not_exist__ specialized-array-vocab forget-vocab
153 ] with-compilation-unit
156 STRUCT: struct-resize-test { x int } ;
158 SPECIALIZED-ARRAY: struct-resize-test
160 [ 40 ] [ 10 <struct-resize-test-array> byte-length ] unit-test
162 : struct-resize-test-usage ( seq -- seq )
163 [ struct-resize-test <struct> swap >>x ] map
164 >struct-resize-test-array
167 [ { 10 20 30 } ] [ { 10 20 30 } struct-resize-test-usage ] unit-test
169 [ ] [ "IN: specialized-arrays.tests USE: classes.struct USE: alien.c-types STRUCT: struct-resize-test { x int } { y int } ;" eval( -- ) ] unit-test
171 [ 80 ] [ 10 <struct-resize-test-array> byte-length ] unit-test
173 [ { 10 20 30 } ] [ { 10 20 30 } struct-resize-test-usage ] unit-test