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