]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types-tests.factor
rename current string-mangling "char*" to "c-string". char* is now just a boring...
[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 ] [ c-string c-type c-string 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: void c-type pointer: MyChar c-type = ] unit-test
39
40 TYPEDEF: { c-string ascii } MyFunkyString
41
42 [ { c-string ascii } ] [ MyFunkyString c-type ] unit-test
43
44 TYPEDEF: c-string MyString
45
46 [ t ] [ c-string c-type MyString          c-type = ] unit-test
47 [ t ] [ void* c-type pointer: MyString c-type = ] unit-test
48
49 TYPEDEF: int* MyIntArray
50
51 [ t ] [ void* c-type MyIntArray c-type = ] unit-test
52
53 [
54     0 B{ 1 2 3 4 } <displaced-alien> <void*>
55 ] must-fail
56
57 os windows? cpu x86.64? and [
58     [ -2147467259 ] [ 2147500037 <long> *long ] unit-test
59 ] when
60
61 [ 0 ] [ -10 uchar c-type-clamp ] unit-test
62 [ 12 ] [ 12 uchar c-type-clamp ] unit-test
63 [ -10 ] [ -10 char c-type-clamp ] unit-test
64 [ 127 ] [ 230 char c-type-clamp ] unit-test
65 [ t ] [ pi dup float c-type-clamp = ] unit-test
66
67 C-TYPE: opaque
68
69 [ t ] [ void* c-type pointer: opaque c-type = ] unit-test
70 [ opaque c-type ] [ no-c-type? ] must-fail-with
71
72 [ """
73     USING: alien.syntax ;
74     IN: alien.c-types.tests
75     FUNCTION: opaque return_opaque ( ) ;
76 """ eval( -- ) ] [ no-c-type? ] must-fail-with
77
78 C-TYPE: forward
79 STRUCT: backward { x forward* } ;
80 STRUCT: forward { x backward* } ;
81
82 [ t ] [ forward c-type struct-c-type? ] unit-test
83 [ t ] [ backward c-type struct-c-type? ] unit-test
84
85 DEFER: struct-redefined
86
87 [ f ]
88 [
89
90     """
91     USING: alien.c-types classes.struct ;
92     IN: alien.c-types.tests
93
94     STRUCT: struct-redefined { x int } ;
95     """ eval( -- )
96
97     """
98     USING: alien.syntax ;
99     IN: alien.c-types.tests
100
101     C-TYPE: struct-redefined
102     """ eval( -- )
103
104     \ struct-redefined class?
105 ] unit-test
106
107 [
108     "IN: alien.c-types.tests
109     USE: alien.syntax
110     USE: alien.c-types
111     TYPEDEF: int type-redefinition-test
112     TYPEDEF: int type-redefinition-test" eval( -- )
113 ]
114 [ error>> error>> redefine-error? ]
115 must-fail-with