cast 0.1.0 → 0.2.0

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, 2, 0]
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/test_helper'
2
+
3
+ Dir["#{ROOT}/test/**/*_test.rb"].each do |path|
4
+ require path
5
+ end
@@ -1,18 +1,16 @@
1
- ###
2
- ### ##################################################################
3
- ###
4
- ### Tests for miscellaneous methods specific to individual Node
5
- ### classes.
6
- ###
7
- ### ##################################################################
8
- ###
1
+ ######################################################################
2
+ #
3
+ # Tests for miscellaneous methods specific to individual Node classes.
4
+ #
5
+ ######################################################################
6
+
7
+ require 'test/test_helper'
8
+
9
9
  class MiscTests < Test::Unit::TestCase
10
- ###
11
- ### ----------------------------------------------------------------
12
- ### Declarator#
13
- ### declaration type
14
- ### ----------------------------------------------------------------
15
- ###
10
+
11
+ # ------------------------------------------------------------------
12
+ # Declarator#declaration type
13
+ # ------------------------------------------------------------------
16
14
 
17
15
  def test_declarator_declaration
18
16
  tor = C::Declarator.new(nil, 'x')
@@ -29,7 +27,7 @@ class MiscTests < Test::Unit::TestCase
29
27
  end
30
28
 
31
29
  def test_declarator_type
32
- ## int i, *j, k[], l(), *m[10];
30
+ # int i, *j, k[], l(), *m[10];
33
31
  decl = C::Declaration.new(C::Int.new)
34
32
  decl.declarators << C::Declarator.new(nil, 'i')
35
33
  decl.declarators << C::Declarator.new(C::Pointer.new, 'j')
@@ -62,14 +60,29 @@ Array
62
60
  EOS
63
61
  end
64
62
 
65
- ###
66
- ### ----------------------------------------------------------------
67
- ### DirectType#
68
- ### direct_type indirect_type
69
- ### IndirectType#
70
- ### direct_type indirect_type direct_type=
71
- ### ----------------------------------------------------------------
72
- ###
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
+ # ------------------------------------------------------------------
73
86
 
74
87
  def test_type_direct_type
75
88
  d = C::Int.new
@@ -153,4 +166,59 @@ EOS
153
166
  x = C::Void.new
154
167
  assert_raise(NoMethodError){t.direct_type = x}
155
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
156
224
  end
@@ -0,0 +1,323 @@
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/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
+ end