casty 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module C
2
+ VERSION = [0, 3, 1]
3
+
4
+ class << VERSION
5
+ include Comparable
6
+
7
+ def to_s
8
+ join('.')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ Dir["#{ROOT}/test/**/*_test.rb"].each do |path|
4
+ require path
5
+ end
@@ -0,0 +1,224 @@
1
+ ######################################################################
2
+ #
3
+ # Tests for miscellaneous methods specific to individual Node classes.
4
+ #
5
+ ######################################################################
6
+
7
+ require 'test_helper'
8
+
9
+ class MiscTests < Test::Unit::TestCase
10
+
11
+ # ------------------------------------------------------------------
12
+ # Declarator#declaration type
13
+ # ------------------------------------------------------------------
14
+
15
+ def test_declarator_declaration
16
+ tor = C::Declarator.new(nil, 'x')
17
+ assert_nil(tor.declaration)
18
+
19
+ list = C::NodeArray[tor]
20
+ assert_nil(tor.declaration)
21
+
22
+ tion = C::Declaration.new(C::Int.new, list)
23
+ assert_same(tion, tor.declaration)
24
+
25
+ list.detach
26
+ assert_nil(tor.declaration)
27
+ end
28
+
29
+ def test_declarator_type
30
+ # int i, *j, k[], l(), *m[10];
31
+ decl = C::Declaration.new(C::Int.new)
32
+ decl.declarators << C::Declarator.new(nil, 'i')
33
+ decl.declarators << C::Declarator.new(C::Pointer.new, 'j')
34
+ decl.declarators << C::Declarator.new(C::Array.new, 'k')
35
+ decl.declarators << C::Declarator.new(C::Function.new, 'l')
36
+ arr = C::Array.new(C::Pointer.new, C::IntLiteral.new(10))
37
+ decl.declarators << C::Declarator.new(arr, 'm')
38
+
39
+ assert_equal_inspect_strs(decl.declarators[0].type.inspect, <<EOS)
40
+ Int
41
+ EOS
42
+ assert_equal_inspect_strs(decl.declarators[1].type.inspect, <<EOS)
43
+ Pointer
44
+ type: Int
45
+ EOS
46
+ assert_equal_inspect_strs(decl.declarators[2].type.inspect, <<EOS)
47
+ Array
48
+ type: Int
49
+ EOS
50
+ assert_equal_inspect_strs(decl.declarators[3].type.inspect, <<EOS)
51
+ Function
52
+ type: Int
53
+ EOS
54
+ assert_equal_inspect_strs(decl.declarators[4].type.inspect, <<EOS)
55
+ Array
56
+ type: Pointer
57
+ type: Int
58
+ length: IntLiteral
59
+ val: 10
60
+ EOS
61
+ end
62
+
63
+ # ------------------------------------------------------------------
64
+ # FunctionDef
65
+ # ------------------------------------------------------------------
66
+
67
+ def test_function_def_initialize
68
+ fd = C::FunctionDef.new
69
+ assert fd.def.Block?
70
+ assert fd.def.stmts.empty?
71
+ end
72
+
73
+ # ------------------------------------------------------------------
74
+ # BlockExpression
75
+ # ------------------------------------------------------------------
76
+
77
+ def test_function_def_initialize
78
+ expr = C::BlockExpression.new
79
+ assert expr.block.Block?
80
+ assert expr.block.stmts.empty?
81
+ end
82
+
83
+ # ------------------------------------------------------------------
84
+ # DirectType, IndirectType
85
+ # ------------------------------------------------------------------
86
+
87
+ def test_type_direct_type
88
+ d = C::Int.new
89
+ t = C::Pointer.new(d)
90
+ assert_same(d, t.direct_type)
91
+
92
+ d = C::Float.new
93
+ t = C::Pointer.new(C::Pointer.new(d))
94
+ assert_same(d, t.direct_type)
95
+
96
+ d = C::Struct.new('S')
97
+ t = C::Array.new(d)
98
+ assert_same(d, t.direct_type)
99
+
100
+ d = C::CustomType.new('T')
101
+ t = C::Function.new(d)
102
+ assert_same(d, t.direct_type)
103
+
104
+ t = C::Pointer.new(nil)
105
+ assert_nil(t.direct_type)
106
+
107
+ t = C::Int.new
108
+ assert_same(t, t.direct_type)
109
+ end
110
+
111
+ def test_type_indirect_type
112
+ d = C::Int.new
113
+ t = C::Pointer.new(d)
114
+ assert_equal(C::Pointer.new, t.indirect_type)
115
+
116
+ d = C::Float.new
117
+ t = C::Pointer.new(C::Pointer.new(d))
118
+ assert_equal(C::Pointer.new(C::Pointer.new), t.indirect_type)
119
+
120
+ d = C::Struct.new('S')
121
+ t = C::Array.new(d, C::IntLiteral.new(10))
122
+ assert_equal(C::Array.new(nil, C::IntLiteral.new(10)), t.indirect_type)
123
+
124
+ d = C::CustomType.new('T')
125
+ t = C::Function.new(d)
126
+ assert_equal(C::Function.new, t.indirect_type)
127
+
128
+ t = C::Pointer.new(nil)
129
+ assert_copy(t, t.indirect_type)
130
+
131
+ t = C::Int.new
132
+ assert_nil(t.indirect_type)
133
+ end
134
+
135
+ def test_type_set_direct_type
136
+ d = C::Int.new
137
+ t = C::Pointer.new(d)
138
+ x = C::Int.new
139
+ t.direct_type = x
140
+ assert_same(x, t.type)
141
+
142
+ d = C::Float.new
143
+ t = C::Pointer.new(C::Pointer.new(d))
144
+ x = C::Float.new
145
+ t.direct_type = x
146
+ assert_same(x, t.type.type)
147
+
148
+ d = C::Struct.new('S')
149
+ t = C::Array.new(d)
150
+ x = C::Struct.new('T')
151
+ t.direct_type = x
152
+ assert_same(x, t.type)
153
+
154
+ d = C::CustomType.new('T')
155
+ t = C::Function.new(d)
156
+ x = C::Void.new
157
+ t.direct_type = x
158
+ assert_same(x, t.type)
159
+
160
+ t = C::Pointer.new(nil)
161
+ x = C::Imaginary.new
162
+ t.direct_type = x
163
+ assert_same(x, t.type)
164
+
165
+ t = C::Int.new
166
+ x = C::Void.new
167
+ assert_raise(NoMethodError){t.direct_type = x}
168
+ end
169
+
170
+ # ------------------------------------------------------------------
171
+ # CharLiteral StringLiteral
172
+ # ------------------------------------------------------------------
173
+
174
+ def test_char_literal_wide
175
+ c = C::CharLiteral.new('abc', 'L')
176
+ assert(c.wide?)
177
+ assert_equal('L', c.prefix)
178
+
179
+ c.prefix = nil
180
+ assert(!c.wide?)
181
+ assert_nil(c.prefix)
182
+
183
+ c.prefix = 'x'
184
+ assert(!c.wide?)
185
+ assert_equal('x', c.prefix)
186
+
187
+ c.wide = false
188
+ assert(!c.wide?)
189
+ assert_equal('x', c.prefix)
190
+
191
+ c.wide = true
192
+ assert(c.wide?)
193
+ assert_equal('L', c.prefix)
194
+
195
+ c.wide = false
196
+ assert(!c.wide?)
197
+ assert_equal(nil, c.prefix)
198
+ end
199
+ def test_string_literal_wide
200
+ s = C::StringLiteral.new('abc', 'L')
201
+ assert(s.wide?)
202
+ assert_equal('L', s.prefix)
203
+
204
+ s.prefix = nil
205
+ assert(!s.wide?)
206
+ assert_nil(s.prefix)
207
+
208
+ s.prefix = 'x'
209
+ assert(!s.wide?)
210
+ assert_equal('x', s.prefix)
211
+
212
+ s.wide = false
213
+ assert(!s.wide?)
214
+ assert_equal('x', s.prefix)
215
+
216
+ s.wide = true
217
+ assert(s.wide?)
218
+ assert_equal('L', s.prefix)
219
+
220
+ s.wide = false
221
+ assert(!s.wide?)
222
+ assert_equal(nil, s.prefix)
223
+ end
224
+ end
@@ -0,0 +1,355 @@
1
+ ######################################################################
2
+ #
3
+ # Lexer routine tests. We only test nontrivial tokens, as things like
4
+ # operators are exercised sufficiently in the parser tests
5
+ # (test_parser.rb).
6
+ #
7
+ ######################################################################
8
+
9
+ require 'test_helper'
10
+
11
+ class LexerTest < Test::Unit::TestCase
12
+ def check(s)
13
+ check_ast(s){|inp| C::Parser.new.parse(inp)}
14
+ end
15
+
16
+ def test_id_and_typename
17
+ check <<EOS
18
+ typedef int Mytype;
19
+ void f() {
20
+ mytype * a;
21
+ Mytype * a;
22
+ _1234, _a1234, _1234L;
23
+ }
24
+ ----
25
+ TranslationUnit
26
+ entities:
27
+ - Declaration
28
+ storage: typedef
29
+ type: Int
30
+ declarators:
31
+ - Declarator
32
+ name: "Mytype"
33
+ - FunctionDef
34
+ type: Function
35
+ type: Void
36
+ name: "f"
37
+ def: Block
38
+ stmts:
39
+ - ExpressionStatement
40
+ expr: Multiply
41
+ expr1: Variable
42
+ name: "mytype"
43
+ expr2: Variable
44
+ name: "a"
45
+ - Declaration
46
+ type: CustomType
47
+ name: "Mytype"
48
+ declarators:
49
+ - Declarator
50
+ indirect_type: Pointer
51
+ name: "a"
52
+ - ExpressionStatement
53
+ expr: Comma
54
+ exprs:
55
+ - Variable
56
+ name: "_1234"
57
+ - Variable
58
+ name: "_a1234"
59
+ - Variable
60
+ name: "_1234L"
61
+ EOS
62
+ end
63
+ def test_int_literal
64
+ check <<EOS
65
+ void f() {
66
+ 0, 00, 000, 0x0, 0x00;
67
+ 1234, 1234l, 1234ll;
68
+ 01234u, 01234ul, 01234ull;
69
+ 0x1234U, 0x1234L, 0x1234ULL;
70
+ }
71
+ ----
72
+ TranslationUnit
73
+ entities:
74
+ - FunctionDef
75
+ type: Function
76
+ type: Void
77
+ name: "f"
78
+ def: Block
79
+ stmts:
80
+ - ExpressionStatement
81
+ expr: Comma
82
+ exprs:
83
+ - IntLiteral
84
+ val: 0
85
+ - IntLiteral
86
+ format: oct
87
+ val: 0
88
+ - IntLiteral
89
+ format: oct
90
+ val: 0
91
+ - IntLiteral
92
+ format: hex
93
+ val: 0
94
+ - IntLiteral
95
+ format: hex
96
+ val: 0
97
+ - ExpressionStatement
98
+ expr: Comma
99
+ exprs:
100
+ - IntLiteral
101
+ val: 1234
102
+ - IntLiteral
103
+ val: 1234
104
+ suffix: "l"
105
+ - IntLiteral
106
+ val: 1234
107
+ suffix: "ll"
108
+ - ExpressionStatement
109
+ expr: Comma
110
+ exprs:
111
+ - IntLiteral
112
+ format: oct
113
+ val: 668
114
+ suffix: "u"
115
+ - IntLiteral
116
+ format: oct
117
+ val: 668
118
+ suffix: "ul"
119
+ - IntLiteral
120
+ format: oct
121
+ val: 668
122
+ suffix: "ull"
123
+ - ExpressionStatement
124
+ expr: Comma
125
+ exprs:
126
+ - IntLiteral
127
+ format: hex
128
+ val: 4660
129
+ suffix: "U"
130
+ - IntLiteral
131
+ format: hex
132
+ val: 4660
133
+ suffix: "L"
134
+ - IntLiteral
135
+ format: hex
136
+ val: 4660
137
+ suffix: "ULL"
138
+ EOS
139
+ assert_raise(C::ParseError){C::Parser.new.parse('void f() {12lll;}')}
140
+ assert_raise(C::ParseError){C::Parser.new.parse('void f() {12ulL;}')}
141
+ assert_raise(C::ParseError){C::Parser.new.parse('void f() {12lul;}')}
142
+ assert_raise(C::ParseError){C::Parser.new.parse('void f() {123_4;}')}
143
+ end
144
+ def test_float_literal
145
+ check <<EOS
146
+ void f() {
147
+ 123e4, 123E-4;
148
+ 123.4e10, .123E-4;
149
+ 123.4e5, 123.E-10;
150
+
151
+ 0xabp2, 0xabcP-10;
152
+ 0xabc.dp3, 0xabcP-11;
153
+ 0xabc.dp4, 0xabc.P-12;
154
+ }
155
+ ----
156
+ TranslationUnit
157
+ entities:
158
+ - FunctionDef
159
+ type: Function
160
+ type: Void
161
+ name: "f"
162
+ def: Block
163
+ stmts:
164
+ - ExpressionStatement
165
+ expr: Comma
166
+ exprs:
167
+ - FloatLiteral
168
+ val: 1230000.0
169
+ - FloatLiteral
170
+ val: 0.0123
171
+ - ExpressionStatement
172
+ expr: Comma
173
+ exprs:
174
+ - FloatLiteral
175
+ val: 1234000000000.0
176
+ - FloatLiteral
177
+ val: 1.23e-05
178
+ - ExpressionStatement
179
+ expr: Comma
180
+ exprs:
181
+ - FloatLiteral
182
+ val: 12340000.0
183
+ - FloatLiteral
184
+ val: 1.23e-08
185
+ - ExpressionStatement
186
+ expr: Comma
187
+ exprs:
188
+ - FloatLiteral
189
+ format: hex
190
+ val: 684.0
191
+ - FloatLiteral
192
+ format: hex
193
+ val: 2.68359375
194
+ - ExpressionStatement
195
+ expr: Comma
196
+ exprs:
197
+ - FloatLiteral
198
+ format: hex
199
+ val: 21990.5
200
+ - FloatLiteral
201
+ format: hex
202
+ val: 1.341796875
203
+ - ExpressionStatement
204
+ expr: Comma
205
+ exprs:
206
+ - FloatLiteral
207
+ format: hex
208
+ val: 43981.0
209
+ - FloatLiteral
210
+ format: hex
211
+ val: 0.6708984375
212
+ EOS
213
+ assert_raise(C::ParseError){C::Parser.new.parse('void f() {0x123.4pa;}')}
214
+ end
215
+ def test_string_literal
216
+ check <<EOS
217
+ void f() {
218
+ "ab", L"ab", x"ab";
219
+ "a\\0b", L"a\\x0fb", "a\\vb";
220
+ "a b", L"a b";
221
+ "a
222
+ b", L"a
223
+ b";
224
+ }
225
+ ----
226
+ TranslationUnit
227
+ entities:
228
+ - FunctionDef
229
+ type: Function
230
+ type: Void
231
+ name: "f"
232
+ def: Block
233
+ stmts:
234
+ - ExpressionStatement
235
+ expr: Comma
236
+ exprs:
237
+ - StringLiteral
238
+ val: "ab"
239
+ - StringLiteral
240
+ prefix: "L"
241
+ val: "ab"
242
+ - StringLiteral
243
+ prefix: "x"
244
+ val: "ab"
245
+ - ExpressionStatement
246
+ expr: Comma
247
+ exprs:
248
+ - StringLiteral
249
+ val: "a\\\\0b"
250
+ - StringLiteral
251
+ prefix: "L"
252
+ val: "a\\\\x0fb"
253
+ - StringLiteral
254
+ val: "a\\\\vb"
255
+ - ExpressionStatement
256
+ expr: Comma
257
+ exprs:
258
+ - StringLiteral
259
+ val: "a\\tb"
260
+ - StringLiteral
261
+ prefix: "L"
262
+ val: "a\\tb"
263
+ - ExpressionStatement
264
+ expr: Comma
265
+ exprs:
266
+ - StringLiteral
267
+ val: "a\\nb"
268
+ - StringLiteral
269
+ prefix: "L"
270
+ val: "a\\nb"
271
+ EOS
272
+ assert_raise(C::ParseError){C::Parser.new.parse('void f() {xy"ab";}')}
273
+ end
274
+ def test_char_literal
275
+ check <<EOS
276
+ void f() {
277
+ 'a', L'a', x'a';
278
+ '\\0', L'\\xf', '\\v';
279
+ ' ', L'
280
+ ';
281
+ }
282
+ ----
283
+ TranslationUnit
284
+ entities:
285
+ - FunctionDef
286
+ type: Function
287
+ type: Void
288
+ name: "f"
289
+ def: Block
290
+ stmts:
291
+ - ExpressionStatement
292
+ expr: Comma
293
+ exprs:
294
+ - CharLiteral
295
+ val: "a"
296
+ - CharLiteral
297
+ prefix: "L"
298
+ val: "a"
299
+ - CharLiteral
300
+ prefix: "x"
301
+ val: "a"
302
+ - ExpressionStatement
303
+ expr: Comma
304
+ exprs:
305
+ - CharLiteral
306
+ val: "\\\\0"
307
+ - CharLiteral
308
+ prefix: "L"
309
+ val: "\\\\xf"
310
+ - CharLiteral
311
+ val: "\\\\v"
312
+ - ExpressionStatement
313
+ expr: Comma
314
+ exprs:
315
+ - CharLiteral
316
+ val: "\\t"
317
+ - CharLiteral
318
+ prefix: "L"
319
+ val: "\\n"
320
+ EOS
321
+ assert_raise(C::ParseError){C::Parser.new.parse("void f() {xy'ab';}")}
322
+ end
323
+
324
+ def test_directive_at_file_start
325
+ check <<EOS
326
+ # 12 gcc stuff
327
+ int a;
328
+ ----
329
+ TranslationUnit
330
+ entities:
331
+ - Declaration
332
+ type: Int
333
+ declarators:
334
+ - Declarator
335
+ name: "a"
336
+ EOS
337
+ end
338
+
339
+ def test_directive
340
+ check <<EOS
341
+ int a;
342
+ # 12 gcc stuff
343
+ ----
344
+ TranslationUnit
345
+ entities:
346
+ - Declaration
347
+ type: Int
348
+ declarators:
349
+ - Declarator
350
+ name: "a"
351
+ EOS
352
+ assert_raise(C::ParseError){C::Parser.new.parse("#define a b\nint a;")}
353
+ end
354
+
355
+ end