]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types-tests.factor
alien.c-types: not necessary to import `short` differently anymore
[factor.git] / basis / alien / c-types / c-types-tests.factor
1 USING: accessors alien.c-types alien.syntax classes
2 classes.struct compiler.units eval io.encodings.ascii kernel
3 math.constants tools.test ;
4 IN: alien.c-types.tests
5
6 CONSTANT: xyz 123
7
8 { 492 } [ { int xyz } heap-size ] unit-test
9
10 UNION-STRUCT: foo
11     { a int }
12     { b int } ;
13
14 { t } [ pointer: void lookup-c-type void* lookup-c-type = ] unit-test
15 { t } [ pointer: int  lookup-c-type void* lookup-c-type = ] unit-test
16 { t } [ pointer: int* lookup-c-type void* lookup-c-type = ] unit-test
17 { f } [ pointer: foo  lookup-c-type void* lookup-c-type = ] unit-test
18 { t } [ pointer: foo* lookup-c-type void* lookup-c-type = ] unit-test
19
20 { t } [ c-string lookup-c-type c-string lookup-c-type = ] unit-test
21
22 { t } [ foo heap-size int heap-size = ] unit-test
23
24 TYPEDEF: int MyInt
25
26 { t } [ int   lookup-c-type          MyInt lookup-c-type = ] unit-test
27 { t } [ void* lookup-c-type pointer: MyInt lookup-c-type = ] unit-test
28
29 { 32 } [ { int 8 } heap-size ] unit-test
30
31 { } [ pointer: { int 8 } heap-size pointer: void heap-size assert= ] unit-test
32
33 TYPEDEF: char MyChar
34
35 { t } [ pointer: void lookup-c-type pointer: MyChar lookup-c-type = ] unit-test
36
37 TYPEDEF: { c-string ascii } MyFunkyString
38
39 { { c-string ascii } } [ MyFunkyString lookup-c-type ] unit-test
40
41 TYPEDEF: c-string MyString
42
43 { t } [ c-string lookup-c-type MyString          lookup-c-type = ] unit-test
44 { t } [ void* lookup-c-type pointer: MyString lookup-c-type = ] unit-test
45
46 TYPEDEF: int* MyIntArray
47
48 { t } [ void* lookup-c-type MyIntArray lookup-c-type = ] unit-test
49
50 { 0 } [ -10 uchar c-type-clamp ] unit-test
51 { 12 } [ 12 uchar c-type-clamp ] unit-test
52 { -10 } [ -10 char c-type-clamp ] unit-test
53 { 127 } [ 230 char c-type-clamp ] unit-test
54 { t } [ pi dup float c-type-clamp = ] unit-test
55
56 C-TYPE: opaque
57
58 { t } [ void* lookup-c-type pointer: opaque lookup-c-type = ] unit-test
59 [ opaque lookup-c-type ] [ no-c-type? ] must-fail-with
60
61 ! c-type-string
62 {
63     "c-string[ascii]"
64     "foo*"
65     "int[5]"
66     "int**"
67     "MyFunkyString*"
68     "opaque*"
69 } [
70     { c-string ascii } c-type-string
71     pointer: foo c-type-string
72     { int 5 } c-type-string
73     pointer: pointer: int c-type-string
74     pointer: MyFunkyString c-type-string
75     pointer: opaque c-type-string
76 ] unit-test
77
78 [ "
79     USING: alien.syntax ;
80     IN: alien.c-types.tests
81     FUNCTION: opaque return_opaque ( ) ;
82 " eval( -- ) ] [ no-c-type? ] must-fail-with
83
84 C-TYPE: forward
85 STRUCT: backward { x forward* } ;
86 STRUCT: forward { x backward* } ;
87
88 { t } [ forward lookup-c-type struct-c-type? ] unit-test
89 { t } [ backward lookup-c-type struct-c-type? ] unit-test
90
91 DEFER: struct-redefined
92
93 { f }
94 [
95
96     "
97     USING: alien.c-types classes.struct ;
98     IN: alien.c-types.tests
99
100     STRUCT: struct-redefined { x int } ;
101     " eval( -- )
102
103     "
104     USING: alien.syntax ;
105     IN: alien.c-types.tests
106
107     C-TYPE: struct-redefined
108     " eval( -- )
109
110     \ struct-redefined class?
111 ] unit-test
112
113 [
114     "IN: alien.c-types.tests
115     USE: alien.syntax
116     USE: alien.c-types
117     TYPEDEF: int type-redefinition-test
118     TYPEDEF: int type-redefinition-test" eval( -- )
119 ]
120 [ error>> error>> redefine-error? ]
121 must-fail-with
122
123 [
124     "IN: alien.c-types.tests
125     USE: alien.syntax
126     USE: alien.c-types
127     CALLBACK: void cb987 ( )
128     CALLBACK: void cb987 ( )" eval( -- )
129 ]
130 [ error>> error>> redefine-error? ]
131 must-fail-with
132
133 [
134     "IN: alien.c-types.tests
135     USE: alien.syntax
136     USE: alien.c-types
137     FUNCTION: void func987 ( )
138     FUNCTION: void func987 ( )" eval( -- )
139 ]
140 [ error>> error>> redefine-error? ]
141 must-fail-with
142
143 ! generic -> callback
144 "IN: alien.c-types.tests
145 USE: alien.syntax
146 USE: alien.c-types
147 GENERIC: foo-func ( x -- )
148 " eval( -- )
149
150 "IN: alien.c-types.tests
151 USE: alien.syntax
152 USE: alien.c-types
153 CALLBACK: void foo-func ( )
154 " eval( -- )
155
156 ! generic -> typedef
157 "IN: alien.c-types.tests
158 USE: alien.syntax
159 USE: alien.c-types
160 GENERIC: foo-func ( x -- )
161 " eval( -- )
162
163 "IN: alien.c-types.tests
164 USE: alien.syntax
165 USE: alien.c-types
166 TYPEDEF: void* foo-func
167 " eval( -- )