]> gitweb.factorcode.org Git - factor.git/blob - basis/tuple-arrays/tuple-arrays-tests.factor
34cd559595fc22419b3b305d79374f256caa0a76
[factor.git] / basis / tuple-arrays / tuple-arrays-tests.factor
1 USING: tuple-arrays sequences tools.test namespaces kernel
2 math accessors classes.tuple eval classes.struct ;
3 IN: tuple-arrays.tests
4
5 SYMBOL: mat
6 TUPLE: foo bar ; final
7 C: <foo> foo
8 TUPLE-ARRAY: foo
9
10 [ 2 ] [ 2 <foo-array> dup mat set length ] unit-test
11 [ T{ foo } ] [ mat get first ] unit-test
12 [ T{ foo f 2 } ] [ T{ foo f 2 } 0 mat get [ set-nth ] keep first ] unit-test
13 [ t ] [ { T{ foo f 1 } T{ foo f 2 } } >foo-array dup mat set foo-array? ] unit-test
14 [ T{ foo f 3 } t ]
15 [ mat get [ bar>> 2 + <foo> ] map [ first ] keep foo-array? ] unit-test
16
17 [ 2 ] [ 2 <foo-array> dup mat set length ] unit-test
18 [ T{ foo } ] [ mat get first ] unit-test
19 [ T{ foo f 1 } ] [ T{ foo f 1 } 0 mat get [ set-nth ] keep first ] unit-test
20
21 TUPLE: baz { bing integer } bong ; final
22 TUPLE-ARRAY: baz
23
24 [ 0 ] [ 1 <baz-array> first bing>> ] unit-test
25 [ f ] [ 1 <baz-array> first bong>> ] unit-test
26
27 TUPLE: broken x ; final
28 : broken ( -- ) ;
29
30 TUPLE-ARRAY: broken
31
32 [ 100 ] [ 100 <broken-array> length ] unit-test
33
34 ! Can't define a tuple array for a non-tuple class
35 [ "IN: tuple-arrays.tests USING: tuple-arrays words ; TUPLE-ARRAY: word" eval( -- ) ]
36 [ error>> not-a-tuple? ]
37 must-fail-with
38
39 ! Can't define a tuple array for a non-final class
40 TUPLE: non-final x ;
41
42 [ "IN: tuple-arrays.tests USE: tuple-arrays TUPLE-ARRAY: non-final" eval( -- ) ]
43 [ error>> not-final? ]
44 must-fail-with
45
46 ! Empty tuple
47 TUPLE: empty-tuple ; final
48
49 TUPLE-ARRAY: empty-tuple
50
51 [ 100 ] [ 100 <empty-tuple-array> length ] unit-test
52 [ T{ empty-tuple } ] [ 100 <empty-tuple-array> first ] unit-test
53 [ ] [ T{ empty-tuple } 100 <empty-tuple-array> set-first ] unit-test
54
55 ! Changing a tuple into a struct shouldn't break the tuple array to the point
56 ! of crashing Factor
57 TUPLE: tuple-to-struct x ; final
58
59 TUPLE-ARRAY: tuple-to-struct
60
61 [ f ] [ tuple-to-struct struct-class? ] unit-test
62
63 ! This shouldn't crash
64 [ ] [
65     "IN: tuple-arrays.tests
66     USING: alien.c-types classes.struct ;
67     STRUCT: tuple-to-struct { x int } ;"
68     eval( -- )
69 ] unit-test
70
71 [ t ] [ tuple-to-struct struct-class? ] unit-test