]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types-tests.factor
96976b7b6c5d7c8ca5ec4b18a411677769f6213d
[factor.git] / basis / alien / c-types / c-types-tests.factor
1 USING: alien alien.syntax alien.c-types alien.parser
2 eval kernel tools.test sequences system libc alien.strings
3 io.encodings.ascii io.encodings.utf8 math.constants classes.struct classes
4 accessors compiler.units ;
5 IN: alien.c-types.tests
6
7 CONSTANT: xyz 123
8
9 [ 492 ] [ { int xyz } heap-size ] unit-test
10
11 [ -1 ] [ -1 <char> *char ] unit-test
12 [ -1 ] [ -1 <short> *short ] unit-test
13 [ -1 ] [ -1 <int> *int ] unit-test
14
15 ! I don't care if this throws an error or works, but at least
16 ! it should be consistent between platforms
17 [ -1 ] [ -1.0 <int> *int ] unit-test
18 [ -1 ] [ -1.0 <long> *long ] unit-test
19 [ -1 ] [ -1.0 <longlong> *longlong ] unit-test
20 [ 1 ] [ 1.0 <uint> *uint ] unit-test
21 [ 1 ] [ 1.0 <ulong> *ulong ] unit-test
22 [ 1 ] [ 1.0 <ulonglong> *ulonglong ] unit-test
23
24 UNION-STRUCT: foo
25     { a int }
26     { b int } ;
27
28 [ t ] [ pointer: void c-type void* c-type = ] unit-test
29 [ t ] [ pointer: int  c-type void* c-type = ] unit-test
30 [ t ] [ pointer: int* c-type void* c-type = ] unit-test
31 [ f ] [ pointer: foo  c-type void* c-type = ] unit-test
32 [ t ] [ pointer: foo* c-type void* c-type = ] unit-test
33
34 [ t ] [ c-string c-type c-string c-type = ] unit-test
35
36 [ t ] [ foo heap-size int heap-size = ] unit-test
37
38 TYPEDEF: int MyInt
39
40 [ t ] [ int   c-type          MyInt c-type = ] unit-test
41 [ t ] [ void* c-type pointer: MyInt c-type = ] unit-test
42
43 [ 32 ] [ { int 8 } heap-size ] unit-test
44
45 [ ] [ pointer: { int 8 } heap-size pointer: void heap-size assert= ] unit-test
46
47 TYPEDEF: char MyChar
48
49 [ t ] [ pointer: void c-type pointer: MyChar c-type = ] unit-test
50
51 TYPEDEF: { c-string ascii } MyFunkyString
52
53 [ { c-string ascii } ] [ MyFunkyString c-type ] unit-test
54
55 TYPEDEF: c-string MyString
56
57 [ t ] [ c-string c-type MyString          c-type = ] unit-test
58 [ t ] [ void* c-type pointer: MyString c-type = ] unit-test
59
60 TYPEDEF: int* MyIntArray
61
62 [ t ] [ void* c-type MyIntArray c-type = ] unit-test
63
64 [
65     0 B{ 1 2 3 4 } <displaced-alien> <void*>
66 ] must-fail
67
68 os windows? cpu x86.64? and [
69     [ -2147467259 ] [ 2147500037 <long> *long ] unit-test
70 ] when
71
72 [ 0 ] [ -10 uchar c-type-clamp ] unit-test
73 [ 12 ] [ 12 uchar c-type-clamp ] unit-test
74 [ -10 ] [ -10 char c-type-clamp ] unit-test
75 [ 127 ] [ 230 char c-type-clamp ] unit-test
76 [ t ] [ pi dup float c-type-clamp = ] unit-test
77
78 C-TYPE: opaque
79
80 [ t ] [ void* c-type pointer: opaque c-type = ] unit-test
81 [ opaque c-type ] [ no-c-type? ] must-fail-with
82
83 [ """
84     USING: alien.syntax ;
85     IN: alien.c-types.tests
86     FUNCTION: opaque return_opaque ( ) ;
87 """ eval( -- ) ] [ no-c-type? ] must-fail-with
88
89 C-TYPE: forward
90 STRUCT: backward { x forward* } ;
91 STRUCT: forward { x backward* } ;
92
93 [ t ] [ forward c-type struct-c-type? ] unit-test
94 [ t ] [ backward c-type struct-c-type? ] unit-test
95
96 DEFER: struct-redefined
97
98 [ f ]
99 [
100
101     """
102     USING: alien.c-types classes.struct ;
103     IN: alien.c-types.tests
104
105     STRUCT: struct-redefined { x int } ;
106     """ eval( -- )
107
108     """
109     USING: alien.syntax ;
110     IN: alien.c-types.tests
111
112     C-TYPE: struct-redefined
113     """ eval( -- )
114
115     \ struct-redefined class?
116 ] unit-test
117
118 [
119     "IN: alien.c-types.tests
120     USE: alien.syntax
121     USE: alien.c-types
122     TYPEDEF: int type-redefinition-test
123     TYPEDEF: int type-redefinition-test" eval( -- )
124 ]
125 [ error>> error>> redefine-error? ]
126 must-fail-with