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
10 SPECIALIZED-ARRAY: bool
11 SPECIALIZED-ARRAY: ushort
12 SPECIALIZED-ARRAY: char
13 SPECIALIZED-ARRAY: uint
14 SPECIALIZED-ARRAY: float
16 [ t ] [ { 1 2 3 } >int-array int-array? ] unit-test
18 [ t ] [ int-array{ 1 2 3 } int-array? ] unit-test
20 [ 2 ] [ int-array{ 1 2 3 } second ] unit-test
23 { t f t } >bool-array underlying>>
24 { 1 0 1 } "bool" heap-size {
30 [ ushort-array{ 1234 } ] [
31 little-endian? B{ 210 4 } B{ 4 210 } ? byte-array>ushort-array
34 [ B{ 210 4 1 } byte-array>ushort-array ] must-fail
37 int-array{ 3 1 3 3 7 } malloc-byte-array 5 <direct-int-array> >array
40 [ f ] [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] unit-test
42 [ f ] [ [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] compile-call ] unit-test
44 [ ushort-array{ 0 0 0 } ] [
45 3 ALIEN: 123 100 <direct-ushort-array> new-sequence
46 dup [ drop 0 ] change-each
53 SPECIALIZED-ARRAY: test-struct
56 1 test-struct-array{ } new-sequence length
59 [ V{ test-struct } ] [
60 [ [ test-struct-array <struct> ] test-struct-array{ } output>sequence first ] final-classes
63 : make-point ( x y -- struct )
64 test-struct <struct-boa> ;
68 1 2 make-point over set-first
69 3 4 make-point over set-second
70 0 [ [ x>> ] [ y>> ] bi / + ] reduce
75 2 malloc-test-struct-array
77 1 2 make-point over set-first
78 3 4 make-point over set-second
79 0 [ [ x>> ] [ y>> ] bi / + ] reduce
83 [ ] [ ALIEN: 123 10 <direct-test-struct-array> drop ] unit-test
87 10 malloc-test-struct-array
92 [ 15 ] [ 15 10 <test-struct-array> resize length ] unit-test
94 [ S{ test-struct f 12 20 } ] [
96 S{ test-struct f 4 20 }
97 S{ test-struct f 12 20 }
98 S{ test-struct f 20 20 }
103 STRUCT: fixed-string { text char[64] } ;
105 SPECIALIZED-ARRAY: fixed-string
107 [ { ALIEN: 100 ALIEN: 140 ALIEN: 180 ALIEN: 1c0 } ] [
108 ALIEN: 100 4 <direct-fixed-string-array> [ (underlying)>> ] { } map-as
111 ! Ensure that byte-length works with direct arrays
113 ALIEN: 123 100 <direct-int-array> byte-length
116 ! Test prettyprinting
117 [ "int-array{ 1 2 3 }" ] [ int-array{ 1 2 3 } unparse ] unit-test
118 [ "int-array@ f 100" ] [ f 100 <direct-int-array> unparse ] unit-test
120 ! If the C type doesn't exist, don't generate a vocab
122 [ "__does_not_exist__" specialized-array-vocab forget-vocab ] with-compilation-unit
123 "__does_not_exist__" c-types get delete-at
128 IN: specialized-arrays.tests
129 USING: specialized-arrays ;
131 SPECIALIZED-ARRAY: __does_not_exist__ "> eval( -- )
136 IN: specialized-arrays.tests
137 USING: classes.struct specialized-arrays ;
139 STRUCT: __does_not_exist__ { x int } ;
141 SPECIALIZED-ARRAY: __does_not_exist__
146 "__does_not_exist__-array{"
147 "__does_not_exist__" specialized-array-vocab lookup