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 ;
8 FROM: alien.c-types => float ;
10 SPECIALIZED-ARRAY: int
11 SPECIALIZED-ARRAY: bool
12 SPECIALIZED-ARRAY: ushort
13 SPECIALIZED-ARRAY: char
14 SPECIALIZED-ARRAY: uint
15 SPECIALIZED-ARRAY: float
16 SPECIALIZED-ARRAY: ulonglong
18 [ ulonglong ] [ ulonglong-array{ } element-type ] unit-test
20 [ t ] [ { 1 2 3 } >int-array int-array? ] unit-test
22 [ t ] [ int-array{ 1 2 3 } int-array? ] unit-test
24 [ 2 ] [ int-array{ 1 2 3 } second ] unit-test
27 { t f t } >bool-array underlying>>
28 { 1 0 1 } "bool" heap-size {
34 [ ushort-array{ 1234 } ] [
35 little-endian? B{ 210 4 } B{ 4 210 } ? byte-array>ushort-array
38 [ B{ 210 4 1 } byte-array>ushort-array ] must-fail
41 int-array{ 3 1 3 3 7 } malloc-byte-array 5 <direct-int-array> >array
44 [ f ] [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] unit-test
46 [ f ] [ [ float-array{ 4 3 2 1 } dup clone [ underlying>> ] bi@ eq? ] compile-call ] unit-test
48 [ ushort-array{ 0 0 0 } ] [
49 3 ALIEN: 123 100 <direct-ushort-array> new-sequence
50 dup [ drop 0 ] change-each
57 SPECIALIZED-ARRAY: test-struct
60 1 test-struct-array{ } new-sequence length
63 [ V{ test-struct } ] [
64 [ [ test-struct-array <struct> ] test-struct-array{ } output>sequence first ] final-classes
67 : make-point ( x y -- struct )
68 test-struct <struct-boa> ;
72 1 2 make-point over set-first
73 3 4 make-point over set-second
74 0 [ [ x>> ] [ y>> ] bi / + ] reduce
79 2 malloc-test-struct-array
81 1 2 make-point over set-first
82 3 4 make-point over set-second
83 0 [ [ x>> ] [ y>> ] bi / + ] reduce
87 [ ] [ ALIEN: 123 10 <direct-test-struct-array> drop ] unit-test
91 10 malloc-test-struct-array
96 [ 15 ] [ 15 10 <test-struct-array> resize length ] unit-test
98 [ S{ test-struct f 12 20 } ] [
100 S{ test-struct f 4 20 }
101 S{ test-struct f 12 20 }
102 S{ test-struct f 20 20 }
107 STRUCT: fixed-string { text char[64] } ;
109 SPECIALIZED-ARRAY: fixed-string
111 [ { ALIEN: 100 ALIEN: 140 ALIEN: 180 ALIEN: 1c0 } ] [
112 ALIEN: 100 4 <direct-fixed-string-array> [ (underlying)>> ] { } map-as
115 ! Ensure that byte-length works with direct arrays
117 ALIEN: 123 100 <direct-int-array> byte-length
120 ! Test prettyprinting
121 [ "int-array{ 1 2 3 }" ] [ int-array{ 1 2 3 } unparse ] unit-test
122 [ "int-array@ f 100" ] [ f 100 <direct-int-array> unparse ] unit-test
124 ! If the C type doesn't exist, don't generate a vocab
126 [ "__does_not_exist__" specialized-array-vocab forget-vocab ] with-compilation-unit
127 "__does_not_exist__" c-types get delete-at
132 IN: specialized-arrays.tests
133 USING: specialized-arrays ;
135 SPECIALIZED-ARRAY: __does_not_exist__ """ eval( -- )
140 IN: specialized-arrays.tests
141 USING: alien.c-types classes.struct specialized-arrays ;
143 STRUCT: __does_not_exist__ { x int } ;
145 SPECIALIZED-ARRAY: __does_not_exist__
150 "__does_not_exist__-array{"
151 "__does_not_exist__" specialized-array-vocab lookup