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