]> gitweb.factorcode.org Git - factor.git/blob - basis/specialized-arrays/specialized-arrays-tests.factor
Slices over specialized arrays can now be passed to C functions, written to binary...
[factor.git] / basis / specialized-arrays / specialized-arrays-tests.factor
1 IN: specialized-arrays.tests
2 USING: tools.test alien.syntax specialized-arrays
3 specialized-arrays.private sequences alien 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
8 compiler.test ;
9 FROM: alien.c-types => int float bool char float ulonglong ushort uint
10 heap-size little-endian? ;
11
12 SPECIALIZED-ARRAY: int
13 SPECIALIZED-ARRAYS: bool ushort char uint float ulonglong ;
14
15 [ t ] [ { 1 2 3 } >int-array int-array? ] unit-test
16
17 [ t ] [ int-array{ 1 2 3 } int-array? ] unit-test
18
19 [ 2 ] [ int-array{ 1 2 3 } second ] unit-test
20
21 [ t ] [
22     { t f t } >bool-array underlying>>
23     { 1 0 1 } bool heap-size {
24         { 1 [ >char-array ] }
25         { 4 [ >uint-array ] }
26     } case underlying>> =
27 ] unit-test
28
29 [ ushort-array{ 1234 } ] [
30     little-endian? B{ 210 4 } B{ 4 210 } ? byte-array>ushort-array
31 ] unit-test
32
33 [ B{ 210 4 1 } byte-array>ushort-array ] must-fail
34
35 [ { 3 1 3 3 7 } ] [
36     int-array{ 3 1 3 3 7 } malloc-byte-array 5 <direct-int-array> >array
37 ] unit-test
38
39 [ float-array{ HEX: 1.222,222   HEX: 1.111,112   } ]
40 [ float-array{ HEX: 1.222,222,2 HEX: 1.111,111,1 } ] unit-test
41
42 [ f ] [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] unit-test
43
44 [ f ] [ [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] compile-call ] unit-test
45
46 [ ushort-array{ 0 0 0 } ] [
47     3 ALIEN: 123 100 <direct-ushort-array> new-sequence
48     [ drop 0 ] map!
49 ] unit-test
50
51 STRUCT: test-struct
52     { x int }
53     { y int } ;
54
55 SPECIALIZED-ARRAY: test-struct
56
57 [ 1 ] [
58     1 test-struct-array{ } new-sequence length
59 ] unit-test
60
61 [ V{ test-struct } ] [
62     [ [ test-struct-array <struct> ] test-struct-array{ } output>sequence first ] final-classes
63 ] unit-test
64
65 : make-point ( x y -- struct )
66     test-struct <struct-boa> ;
67
68 [ 5/4 ] [
69     2 <test-struct-array>
70     1 2 make-point over set-first
71     3 4 make-point over set-second
72     0 [ [ x>> ] [ y>> ] bi / + ] reduce
73 ] unit-test
74
75 [ 5/4 ] [
76     [
77         2 malloc-test-struct-array
78         dup &free drop
79         1 2 make-point over set-first
80         3 4 make-point over set-second
81         0 [ [ x>> ] [ y>> ] bi / + ] reduce
82     ] with-destructors
83 ] unit-test
84
85 [ ] [ ALIEN: 123 10 <direct-test-struct-array> drop ] unit-test
86
87 [ ] [
88     [
89         10 malloc-test-struct-array
90         &free drop
91     ] with-destructors
92 ] unit-test
93
94 [ 15 ] [ 15 10 <test-struct-array> resize length ] unit-test
95
96 [ S{ test-struct f 12 20 } ] [
97     test-struct-array{
98         S{ test-struct f  4 20 } 
99         S{ test-struct f 12 20 }
100         S{ test-struct f 20 20 }
101     } second
102 ] unit-test
103
104 ! Regression
105 STRUCT: fixed-string { text char[64] } ;
106
107 SPECIALIZED-ARRAY: fixed-string
108
109 [ { ALIEN: 100 ALIEN: 140 ALIEN: 180 ALIEN: 1c0 } ] [
110     ALIEN: 100 4 <direct-fixed-string-array> [ (underlying)>> ] { } map-as
111 ] unit-test
112
113 ! Ensure that byte-length works with direct arrays
114 [ 400 ] [
115     ALIEN: 123 100 <direct-int-array> byte-length
116 ] unit-test
117
118 ! Test prettyprinting
119 [ "int-array{ 1 2 3 }" ] [ int-array{ 1 2 3 } unparse ] unit-test
120 [ "int-array@ f 100" ] [ f 100 <direct-int-array> unparse ] unit-test
121
122 ! If the C type doesn't exist, don't generate a vocab
123 SYMBOL: __does_not_exist__
124
125 [
126     """
127 IN: specialized-arrays.tests
128 USING: specialized-arrays ;
129
130 SPECIALIZED-ARRAY: __does_not_exist__ """ eval( -- )
131 ] must-fail
132
133 [ ] [
134     """
135 IN: specialized-arrays.tests
136 USING: alien.c-types classes.struct specialized-arrays ;
137
138 STRUCT: __does_not_exist__ { x int } ;
139
140 SPECIALIZED-ARRAY: __does_not_exist__
141 """ eval( -- )
142 ] unit-test
143
144 [ f ] [
145     "__does_not_exist__-array{"
146     __does_not_exist__ specialized-array-vocab lookup
147     deferred?
148 ] unit-test
149
150 [ ] [
151     [
152         \ __does_not_exist__ forget
153         __does_not_exist__ specialized-array-vocab forget-vocab
154     ] with-compilation-unit
155 ] unit-test
156
157 STRUCT: struct-resize-test { x int } ;
158
159 SPECIALIZED-ARRAY: struct-resize-test
160
161 [ 40 ] [ 10 <struct-resize-test-array> byte-length ] unit-test
162
163 : struct-resize-test-usage ( seq -- seq )
164     [ struct-resize-test <struct> swap >>x ] map
165     >struct-resize-test-array
166     [ x>> ] { } map-as ;
167     
168 [ { 10 20 30 } ] [ { 10 20 30 } struct-resize-test-usage ] unit-test
169
170 [ ] [ "IN: specialized-arrays.tests USE: classes.struct USE: alien.c-types STRUCT: struct-resize-test { x int } { y int } ;" eval( -- ) ] unit-test
171
172 [ 80 ] [ 10 <struct-resize-test-array> byte-length ] unit-test
173
174 [ { 10 20 30 } ] [ { 10 20 30 } struct-resize-test-usage ] unit-test