]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types-tests.factor
10a7b50ebb965eb1cd7715cab447217898c3817e
[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 UNION-STRUCT: foo
16     { a int }
17     { b int } ;
18
19 [ t ] [ pointer: void c-type void* c-type = ] unit-test
20 [ t ] [ pointer: int  c-type void* c-type = ] unit-test
21 [ t ] [ pointer: int* c-type void* c-type = ] unit-test
22 [ f ] [ pointer: foo  c-type void* c-type = ] unit-test
23 [ t ] [ pointer: foo* c-type void* c-type = ] unit-test
24
25 [ t ] [ pointer: char c-type char* c-type = ] unit-test
26
27 [ t ] [ foo heap-size int heap-size = ] unit-test
28
29 TYPEDEF: int MyInt
30
31 [ t ] [ int   c-type          MyInt c-type = ] unit-test
32 [ t ] [ void* c-type pointer: MyInt c-type = ] unit-test
33
34 [ 32 ] [ { int 8 } heap-size ] unit-test
35
36 TYPEDEF: char MyChar
37
38 [ t ] [ pointer: char c-type pointer: MyChar c-type = ] unit-test
39 [ t ] [ char*         c-type pointer: MyChar c-type = ] unit-test
40
41 TYPEDEF: char MyFunkyChar
42 { char* ascii } pointer: MyFunkyChar typedef
43
44 [ f ] [ pointer: char c-type pointer: MyFunkyChar c-type = ] unit-test
45 [ { char* ascii } ] [ pointer: MyFunkyChar c-type ] unit-test
46
47 TYPEDEF: MyFunkyChar MyFunkyTypedef
48
49 [ { char* ascii } ] [ pointer: MyFunkyTypedef c-type ] unit-test
50
51 TYPEDEF: MyFunkyChar* MyFunkyString
52
53 [ { char* ascii } ] [ MyFunkyString c-type ] unit-test
54
55 TYPEDEF: char* MyString
56
57 [ t ] [ char* 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 TYPEDEF: char* MyLPBYTE
65
66 [ t ] [ { char* utf8 } c-type MyLPBYTE c-type = ] unit-test
67
68 [
69     0 B{ 1 2 3 4 } <displaced-alien> <void*>
70 ] must-fail
71
72 os windows? cpu x86.64? and [
73     [ -2147467259 ] [ 2147500037 <long> *long ] unit-test
74 ] when
75
76 [ 0 ] [ -10 uchar c-type-clamp ] unit-test
77 [ 12 ] [ 12 uchar c-type-clamp ] unit-test
78 [ -10 ] [ -10 char c-type-clamp ] unit-test
79 [ 127 ] [ 230 char c-type-clamp ] unit-test
80 [ t ] [ pi dup float c-type-clamp = ] unit-test
81
82 C-TYPE: opaque
83
84 [ t ] [ void* c-type pointer: opaque c-type = ] unit-test
85 [ opaque c-type ] [ no-c-type? ] must-fail-with
86
87 [ """
88     USING: alien.syntax ;
89     IN: alien.c-types.tests
90     FUNCTION: opaque return_opaque ( ) ;
91 """ eval( -- ) ] [ no-c-type? ] must-fail-with
92
93 C-TYPE: forward
94 STRUCT: backward { x forward* } ;
95 STRUCT: forward { x backward* } ;
96
97 [ t ] [ forward c-type struct-c-type? ] unit-test
98 [ t ] [ backward c-type struct-c-type? ] unit-test
99
100 DEFER: struct-redefined
101
102 [ f ]
103 [
104
105     """
106     USING: alien.c-types classes.struct ;
107     IN: alien.c-types.tests
108
109     STRUCT: struct-redefined { x int } ;
110     """ eval( -- )
111
112     """
113     USING: alien.syntax ;
114     IN: alien.c-types.tests
115
116     C-TYPE: struct-redefined
117     """ eval( -- )
118
119     \ struct-redefined class?
120 ] unit-test
121
122 [
123     "IN: alien.c-types.tests
124     USE: alien.syntax
125     USE: alien.c-types
126     TYPEDEF: int type-redefinition-test
127     TYPEDEF: int type-redefinition-test" eval( -- )
128 ]
129 [ error>> error>> redefine-error? ]
130 must-fail-with