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