]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser-tests.factor
fb1c76182f0c5926d463a2f6c0c6fd701b1fb257
[factor.git] / basis / alien / parser / parser-tests.factor
1 ! (c)2009 Joe Groff bsd license
2 USING: accessors alien.c-types alien.parser alien.parser.private
3 alien.syntax compiler.units continuations debugger eval fry kernel
4 lexer namespaces parser sequences sets summary tools.test
5 vocabs.parser words ;
6 IN: alien.parser.tests
7
8 <<
9
10 : with-parsing ( lines quot -- )
11     [ <lexer> ] [ '[ _ with-compilation-unit ] ] bi* with-lexer ; inline
12
13 ! (CREATE-C-TYPE)
14 { "hello" } [
15     { "hello" } [ CREATE-C-TYPE name>> ] with-parsing
16 ] unit-test
17
18 ! Check that it deletes from old-definitions
19 { 0 } [
20     { } [
21         "hello" current-vocab create-word
22         old-definitions get first adjoin
23         "hello" (CREATE-C-TYPE) drop
24         old-definitions get first cardinality
25     ] with-parsing
26 ] unit-test
27
28 ! make-callback-type
29 { "what-type" } [
30     { } [
31         void "what-type" f { } { } make-callback-type 2drop name>>
32     ] with-parsing
33 ] unit-test
34
35 { 1 } [
36     { } [
37         "hello" current-vocab create-word
38         old-definitions get first adjoin
39         void "hello" f { } { } make-callback-type 3drop
40         old-definitions get first cardinality
41     ] with-parsing
42 ] unit-test
43
44 ! parse-enum-name
45 { t } [
46     { "ayae" } [ parse-enum-name new-definitions get first in? ] with-parsing
47 ] unit-test
48
49 ! validate-c-type-name
50 { "Cannot define a C type “hello*” that ends with an asterisk (*)" } [
51     [ "hello*" validate-c-type-name ] [ ] recover summary
52 ] unit-test
53
54 >>
55
56 TYPEDEF: char char2
57
58 SYMBOL: not-c-type
59
60 CONSTANT: eleven 11
61
62 [
63     "alien.parser.tests" use-vocab
64     "alien.c-types" use-vocab
65
66     [ int ] [ "int" parse-c-type ] unit-test
67     [ { int 5 } ] [ "int[5]" parse-c-type ] unit-test
68     [ { int 5 10 11 } ] [ "int[5][10][11]" parse-c-type ] unit-test
69     [ { int 5 10 eleven } ] [ "int[5][10][eleven]" parse-c-type ] unit-test
70     [ pointer: void ] [ "void*" parse-c-type ] unit-test
71     [ pointer: int ] [ "int*" parse-c-type ] unit-test
72     [ pointer: int* ] [ "int**" parse-c-type ] unit-test
73     [ pointer: int** ] [ "int***" parse-c-type ] unit-test
74     [ pointer: int*** ] [ "int****" parse-c-type ] unit-test
75     [ { pointer: int 3 } ] [ "int*[3]" parse-c-type ] unit-test
76     [ { pointer: void 3 } ] [ "void*[3]" parse-c-type ] unit-test
77     [ pointer: { int 3 } ] [ "int[3]*" parse-c-type ] unit-test
78     [ c-string ] [ "c-string" parse-c-type ] unit-test
79     [ char2 ] [ "char2" parse-c-type ] unit-test
80     [ pointer: char2 ] [ "char2*" parse-c-type ] unit-test
81
82     [ "void[3]" parse-c-type ] must-fail
83     [ "int[3" parse-c-type ] must-fail
84     [ "int[3][4" parse-c-type ] must-fail
85     [ "not-word" parse-c-type ] [ error>> no-word-error? ] must-fail-with
86 ] with-file-vocabs
87
88 FUNCTION: void* alien-parser-function-effect-test ( int *arg1, float arg2 )
89
90 { ( arg1 arg2 -- void* ) } [
91     \ alien-parser-function-effect-test "declared-effect" word-prop
92 ] unit-test
93
94 { t } [ \ alien-parser-function-effect-test inline? ] unit-test
95
96 FUNCTION-ALIAS: (alien-parser-function-effect-test) void* alien-parser-function-effect-test ( int *arg1, float arg2 )
97
98 { ( arg1 arg2 -- void* ) } [
99     \ (alien-parser-function-effect-test) "declared-effect" word-prop
100 ] unit-test
101
102 { t } [ \ (alien-parser-function-effect-test) inline? ] unit-test
103
104 CALLBACK: void* alien-parser-callback-effect-test ( int *arg1 float arg2 )
105
106 { ( arg1 arg2 -- void* ) } [
107     \ alien-parser-callback-effect-test "callback-effect" word-prop
108 ] unit-test
109
110 { t } [ \ alien-parser-callback-effect-test inline? ] unit-test
111
112 ! Reported by mnestic
113 TYPEDEF: int alien-parser-test-int ! reasonably unique name...
114
115 { "OK!" } [
116     [
117         "USE: specialized-arrays SPECIALIZED-ARRAY: alien-parser-test-int" eval( -- )
118         ! after restart, we end up here
119         "OK!"
120     ] [ :1 ] recover
121 ] unit-test
122
123 ! Redefinitions
124 { } [
125     [ C-TYPE: hi TYPEDEF: void* hi ] with-compilation-unit
126 ] unit-test