cast 0.0.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/CHANGELOG +63 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.markdown +1924 -0
- data/Rakefile +29 -0
- data/cast.gemspec +18 -0
- data/ext/cast.c +6 -0
- data/ext/cast.h +141 -0
- data/ext/extconf.rb +2 -0
- data/ext/parser.c +287 -0
- data/ext/yylex.re +340 -0
- data/lib/cast.rb +11 -9
- data/lib/cast/c.y +904 -0
- data/lib/{c_nodes.rb → cast/c_nodes.rb} +188 -392
- data/lib/{to_debug.rb → cast/inspect.rb} +18 -20
- data/lib/cast/node.rb +741 -0
- data/lib/{node_list.rb → cast/node_list.rb} +175 -176
- data/lib/{parse.rb → cast/parse.rb} +86 -65
- data/lib/cast/preprocessor.rb +59 -0
- data/lib/cast/tempfile.rb +33 -0
- data/lib/{to_s.rb → cast/to_s.rb} +133 -80
- data/lib/cast/version.rb +11 -0
- data/test/c_nodes_test.rb +224 -0
- data/test/lexer_test.rb +335 -0
- data/test/{test_node_list.rb → node_list_test.rb} +401 -413
- data/test/{test_node.rb → node_test.rb} +186 -214
- data/test/parse_test.rb +2068 -0
- data/test/{test_parser.rb → parser_test.rb} +145 -58
- data/test/preprocessor_test.rb +85 -0
- data/test/render_test.rb +2116 -0
- data/test/test_helper.rb +198 -0
- metadata +83 -52
- data/README +0 -6
- data/doc/index.html +0 -2513
- data/install.rb +0 -14
- data/lib/c.tab.rb +0 -3433
- data/lib/c.y +0 -935
- data/lib/node.rb +0 -744
- data/test/common.rb +0 -174
- data/test/run.rb +0 -5
- data/test/test_c_nodes.rb +0 -160
- data/test/test_parse.rb +0 -2014
@@ -1,26 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
require 'common'
|
10
|
-
require 'stringio'
|
11
|
-
|
12
|
-
require 'pp'
|
13
|
-
class C::Node
|
14
|
-
def pretty_print q
|
15
|
-
q.text self.to_debug
|
16
|
-
end
|
17
|
-
end
|
1
|
+
######################################################################
|
2
|
+
#
|
3
|
+
# Tests for Node core functionality.
|
4
|
+
#
|
5
|
+
######################################################################
|
6
|
+
|
7
|
+
require 'test_helper'
|
18
8
|
|
19
9
|
Chain = C::NodeChain
|
20
10
|
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
#
|
12
|
+
# Some Node classes.
|
13
|
+
#
|
24
14
|
class X < C::Node
|
25
15
|
child :a
|
26
16
|
initializer :a
|
@@ -48,12 +38,11 @@ class V < C::Node
|
|
48
38
|
initializer :a
|
49
39
|
end
|
50
40
|
|
51
|
-
class NodeInitializeTest < Test
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
###
|
41
|
+
class NodeInitializeTest < Minitest::Test
|
42
|
+
|
43
|
+
# ------------------------------------------------------------------
|
44
|
+
# initialize
|
45
|
+
# ------------------------------------------------------------------
|
57
46
|
|
58
47
|
def test_initialize_w
|
59
48
|
w = W.new
|
@@ -92,11 +81,10 @@ class NodeInitializeTest < Test::Unit::TestCase
|
|
92
81
|
assert_same(x2, z.b)
|
93
82
|
end
|
94
83
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
###
|
84
|
+
# ------------------------------------------------------------------
|
85
|
+
# Node.new_at
|
86
|
+
# ------------------------------------------------------------------
|
87
|
+
|
100
88
|
def test_new_at
|
101
89
|
pos = C::Node::Pos.new('somefile', 5, 10)
|
102
90
|
xa = X.new
|
@@ -106,7 +94,7 @@ class NodeInitializeTest < Test::Unit::TestCase
|
|
106
94
|
end
|
107
95
|
end
|
108
96
|
|
109
|
-
class NodeEqualTest < Test
|
97
|
+
class NodeEqualTest < Minitest::Test
|
110
98
|
def str
|
111
99
|
"(struct s){.a = 1, [2] = {3, 4}, .b [5] = 6, 7}"
|
112
100
|
end
|
@@ -120,7 +108,7 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
120
108
|
five = C::IntLiteral.new(5)
|
121
109
|
six = C::IntLiteral.new(6)
|
122
110
|
seven = C::IntLiteral.new(7)
|
123
|
-
|
111
|
+
|
124
112
|
mi0 = C::MemberInit.new(Chain[ma], one)
|
125
113
|
mi10 = C::MemberInit.new(nil, three)
|
126
114
|
mi11 = C::MemberInit.new(nil, four)
|
@@ -134,59 +122,58 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
134
122
|
return c
|
135
123
|
end
|
136
124
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
###
|
125
|
+
# ------------------------------------------------------------------
|
126
|
+
# ==, eql?
|
127
|
+
# ------------------------------------------------------------------
|
128
|
+
|
142
129
|
def test_eq
|
143
|
-
|
130
|
+
# copy should be equal
|
144
131
|
assert_equal(node, node)
|
145
132
|
assert(node.eql?(node))
|
146
133
|
|
147
|
-
|
134
|
+
# change any one field and it should be not_equal
|
148
135
|
n = node
|
149
136
|
n.type = nil
|
150
|
-
|
137
|
+
refute_equal(node, n)
|
151
138
|
assert(!node.eql?(n))
|
152
139
|
|
153
140
|
n = node
|
154
141
|
n.member_inits[0].member[0] = C::Member.new('c')
|
155
|
-
|
142
|
+
refute_equal(node, n)
|
156
143
|
assert(!node.eql?(n))
|
157
144
|
copy = node.dup
|
158
145
|
|
159
146
|
n = node
|
160
147
|
n.member_inits[2].member[1] = C::IntLiteral.new(8)
|
161
|
-
|
148
|
+
refute_equal(node, n)
|
162
149
|
assert(!node.eql?(n))
|
163
150
|
|
164
|
-
|
151
|
+
# add a member's init and it should be not_equal
|
165
152
|
n = node
|
166
153
|
n.member_inits[3].init = C::IntLiteral.new(9)
|
167
|
-
|
154
|
+
refute_equal(node, n)
|
168
155
|
assert(!node.eql?(n))
|
169
156
|
|
170
|
-
|
157
|
+
# change a member's init and it should be not_equal
|
171
158
|
n = node
|
172
159
|
n.member_inits[0].init = C::IntLiteral.new(10)
|
173
|
-
|
160
|
+
refute_equal(node, n)
|
174
161
|
assert(!node.eql?(n))
|
175
162
|
|
176
|
-
|
163
|
+
# add a member specifier and it should be not_equal
|
177
164
|
n = node
|
178
165
|
n.member_inits[3].member = Chain[C::Member.new('d')]
|
179
|
-
|
166
|
+
refute_equal(node, n)
|
180
167
|
assert(!node.eql?(n))
|
181
168
|
|
182
|
-
|
169
|
+
# pop a member and it should be not_equal
|
183
170
|
n = node
|
184
171
|
n.member_inits.pop
|
185
|
-
|
172
|
+
refute_equal(node, n)
|
186
173
|
assert(!node.eql?(n))
|
187
174
|
|
188
|
-
|
189
|
-
|
175
|
+
# assign a field a copy of what's there and it should still be
|
176
|
+
# equal
|
190
177
|
n = node
|
191
178
|
n.member_inits[0].member[0] = C::Member.new('a')
|
192
179
|
assert_equal(node, n)
|
@@ -198,17 +185,16 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
198
185
|
assert(node.eql?(n))
|
199
186
|
end
|
200
187
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
###
|
188
|
+
# ------------------------------------------------------------------
|
189
|
+
# hash
|
190
|
+
# ------------------------------------------------------------------
|
191
|
+
|
206
192
|
def test_hash
|
207
|
-
|
193
|
+
# copy should be equal
|
208
194
|
assert_equal(node.hash, node.hash)
|
209
195
|
|
210
|
-
|
211
|
-
|
196
|
+
# should be equal after assigning to a field a copy of what's
|
197
|
+
# there
|
212
198
|
n = node
|
213
199
|
n.member_inits[0].member[0] = C::Member.new('a')
|
214
200
|
assert_equal(node.hash, n.hash)
|
@@ -219,11 +205,9 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
219
205
|
end
|
220
206
|
end
|
221
207
|
|
222
|
-
class NodeCopyTest < Test
|
208
|
+
class NodeCopyTest < Minitest::Test
|
223
209
|
def setup
|
224
|
-
|
225
|
-
|
226
|
-
|
210
|
+
# (struct s){.a = 1, [2] = {3, 4}, .b [5] = 6, 7}
|
227
211
|
|
228
212
|
@c_t_n = 's'
|
229
213
|
@c_t = C::Struct.new(@c_t_n)
|
@@ -273,11 +257,10 @@ class NodeCopyTest < Test::Unit::TestCase
|
|
273
257
|
end
|
274
258
|
attr_accessor :c, :d, :e
|
275
259
|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
###
|
260
|
+
# ------------------------------------------------------------------
|
261
|
+
# dup, clone
|
262
|
+
# ------------------------------------------------------------------
|
263
|
+
|
281
264
|
def check_node value
|
282
265
|
cres = yield(c)
|
283
266
|
dres = yield(d)
|
@@ -290,20 +273,20 @@ class NodeCopyTest < Test::Unit::TestCase
|
|
290
273
|
assert_same(value, dres)
|
291
274
|
assert_same(value, eres)
|
292
275
|
end
|
293
|
-
|
276
|
+
assert_raises(NoMethodError){dres.new_method}
|
294
277
|
case value.object_id
|
295
278
|
when @c.object_id, @c_mis1_i_mis0.object_id
|
296
279
|
assert_same(100, eres.new_method)
|
297
280
|
else
|
298
|
-
|
281
|
+
assert_raises(NoMethodError){eres.new_method}
|
299
282
|
end
|
300
283
|
end
|
301
284
|
|
302
285
|
def test_copy
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
286
|
+
# each element should be equal, but not the same, except for
|
287
|
+
# immediate values
|
288
|
+
#
|
289
|
+
# (struct s){.a = 1, [2] = {3, 4}, .b [5] = 6, 7}
|
307
290
|
assert_tree(c)
|
308
291
|
assert_tree(d)
|
309
292
|
assert_tree(e)
|
@@ -348,14 +331,14 @@ class NodeCopyTest < Test::Unit::TestCase
|
|
348
331
|
end
|
349
332
|
end
|
350
333
|
|
351
|
-
class NodeWalkTest < Test
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
def yields
|
334
|
+
class NodeWalkTest < Minitest::Test
|
335
|
+
#
|
336
|
+
# Collect and return the args yielded to `node.send(method)' as an
|
337
|
+
# Array, each element of which is an array of args yielded.
|
338
|
+
#
|
339
|
+
# Also, assert that the return value of the method is `exp'.
|
340
|
+
#
|
341
|
+
def yields(method, node, exp)
|
359
342
|
ret = []
|
360
343
|
out = node.send(method) do |*args|
|
361
344
|
ret << args
|
@@ -365,18 +348,18 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
365
348
|
return ret
|
366
349
|
end
|
367
350
|
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
def assert_equal_yields
|
351
|
+
#
|
352
|
+
# Assert exp and out are equal, where elements are compared with
|
353
|
+
# Array#same_list?. That is, exp[i].same_list?(out[i]) for all i.
|
354
|
+
#
|
355
|
+
def assert_equal_yields(exp, out)
|
373
356
|
if exp.zip(out).all?{|a,b| a.same_list?(b)}
|
374
357
|
assert(true)
|
375
358
|
else
|
376
359
|
flunk("walk not equal: #{walk_str(out)} (expected #{walk_str(exp)})")
|
377
360
|
end
|
378
361
|
end
|
379
|
-
def walk_str
|
362
|
+
def walk_str(walk)
|
380
363
|
walk.is_a? ::Array or
|
381
364
|
raise "walk_str: expected ::Array"
|
382
365
|
if walk.empty?
|
@@ -404,17 +387,16 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
404
387
|
end
|
405
388
|
end
|
406
389
|
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
## depth_first
|
390
|
+
# ------------------------------------------------------------------
|
391
|
+
# depth_first, reverse_depth_first
|
392
|
+
# ------------------------------------------------------------------
|
393
|
+
|
394
|
+
def check_depth_firsts(node, exp)
|
395
|
+
# depth_first
|
414
396
|
out = yields(:depth_first, node, node)
|
415
397
|
assert_equal_yields exp, out
|
416
398
|
|
417
|
-
|
399
|
+
# reverse_depth_first
|
418
400
|
exp = exp.reverse.map! do |ev, node|
|
419
401
|
if ev == :ascending
|
420
402
|
[:descending, node]
|
@@ -427,13 +409,13 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
427
409
|
end
|
428
410
|
|
429
411
|
def test_depth_first
|
430
|
-
|
412
|
+
# empty node
|
431
413
|
d = C::Int.new
|
432
414
|
d.longness = 1
|
433
415
|
check_depth_firsts(d,
|
434
416
|
[[:descending, d], [:ascending, d]])
|
435
417
|
|
436
|
-
|
418
|
+
# one-storey -- populate both the list child and nonlist child
|
437
419
|
d = C::Declaration.new(C::Int.new)
|
438
420
|
d.declarators << C::Declarator.new
|
439
421
|
d.declarators[0].name = 'one'
|
@@ -450,7 +432,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
450
432
|
[:ascending, d]
|
451
433
|
])
|
452
434
|
|
453
|
-
|
435
|
+
# multi-layer
|
454
436
|
d.declarators[0].indirect_type = C::Function.new
|
455
437
|
d.declarators[0].indirect_type.params = Chain[]
|
456
438
|
d.declarators[0].indirect_type.params << C::Parameter.new(C::Int.new, 'i')
|
@@ -478,8 +460,8 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
478
460
|
])
|
479
461
|
end
|
480
462
|
|
481
|
-
def check_depth_first_prunes
|
482
|
-
|
463
|
+
def check_depth_first_prunes(pruned_nodes, node, exp)
|
464
|
+
# depth_first
|
483
465
|
out = yields(:depth_first, node, node) do |ev, node|
|
484
466
|
if ev.equal? :descending
|
485
467
|
if pruned_nodes.any?{|n| n.equal? node}
|
@@ -488,7 +470,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
488
470
|
end
|
489
471
|
end
|
490
472
|
assert_equal_yields exp, out
|
491
|
-
|
473
|
+
#
|
492
474
|
ret = catch :prune do
|
493
475
|
node.depth_first do |ev, node|
|
494
476
|
throw :prune, :x if ev.equal? :ascending
|
@@ -497,7 +479,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
497
479
|
end
|
498
480
|
assert_same(:x, ret)
|
499
481
|
|
500
|
-
|
482
|
+
# reverse_depth_first
|
501
483
|
exp = exp.reverse.map! do |ev, node|
|
502
484
|
if ev.equal? :ascending
|
503
485
|
[:descending, node]
|
@@ -513,7 +495,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
513
495
|
end
|
514
496
|
end
|
515
497
|
assert_equal_yields exp, out
|
516
|
-
|
498
|
+
#
|
517
499
|
ret = catch :prune do
|
518
500
|
node.reverse_depth_first do |ev, node|
|
519
501
|
throw :prune, :x if ev.equal? :ascending
|
@@ -524,13 +506,13 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
524
506
|
end
|
525
507
|
|
526
508
|
def test_depth_first_prune
|
527
|
-
|
509
|
+
# empty node
|
528
510
|
d = C::Int.new
|
529
511
|
d.longness = 1
|
530
512
|
check_depth_first_prunes([d], d,
|
531
513
|
[[:descending, d], [:ascending, d]])
|
532
514
|
|
533
|
-
|
515
|
+
# one-storey -- populate both the list child and nonlist child
|
534
516
|
d = C::Declaration.new(C::Int.new)
|
535
517
|
d.declarators << C::Declarator.new
|
536
518
|
d.declarators[0].name = 'one'
|
@@ -544,7 +526,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
544
526
|
[:ascending, d]
|
545
527
|
])
|
546
528
|
|
547
|
-
|
529
|
+
# multi-layer
|
548
530
|
d.type = C::Struct.new('S')
|
549
531
|
d.type.members = Chain[]
|
550
532
|
d.type.members << C::Declaration.new(C::Int.new)
|
@@ -567,13 +549,11 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
567
549
|
])
|
568
550
|
end
|
569
551
|
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
### ----------------------------------------------------------------
|
574
|
-
###
|
552
|
+
# ------------------------------------------------------------------
|
553
|
+
# each, reverse_each
|
554
|
+
# ------------------------------------------------------------------
|
575
555
|
|
576
|
-
def iter_str
|
556
|
+
def iter_str(iter)
|
577
557
|
iter.is_a? ::Array or
|
578
558
|
raise "iter_str: expected ::Array"
|
579
559
|
if iter.empty?
|
@@ -592,7 +572,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
592
572
|
return s.string
|
593
573
|
end
|
594
574
|
end
|
595
|
-
def check_each
|
575
|
+
def check_each(node, exp)
|
596
576
|
exp.map!{|n| [n]}
|
597
577
|
|
598
578
|
out = yields(:each, node, node)
|
@@ -603,34 +583,31 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
603
583
|
assert_equal_yields exp, out
|
604
584
|
end
|
605
585
|
def test_each
|
606
|
-
|
586
|
+
# empty
|
607
587
|
parent = X.new
|
608
588
|
check_each(parent, [])
|
609
589
|
|
610
|
-
|
590
|
+
# one child
|
611
591
|
x1 = X.new
|
612
592
|
parent = X.new(x1)
|
613
593
|
check_each(parent, [x1])
|
614
594
|
|
615
|
-
|
595
|
+
# two children
|
616
596
|
x1, x2 = 2.of{X.new}
|
617
597
|
parent = Y.new(x1, x2)
|
618
598
|
check_each(parent, [x1, x2])
|
619
599
|
|
620
|
-
|
600
|
+
# three with some nil and some fields
|
621
601
|
x1, x2, x3, x4, x5 = 5.of{X.new}
|
622
602
|
parent = Z.new(x1, x2, nil, x4, x5)
|
623
603
|
check_each(parent, [x1, x5])
|
624
604
|
end
|
625
605
|
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
### postorder, reverse_postorder
|
630
|
-
### ----------------------------------------------------------------
|
631
|
-
###
|
606
|
+
# ------------------------------------------------------------------
|
607
|
+
# preorder, reverse_preorder, postorder, reverse_postorder
|
608
|
+
# ------------------------------------------------------------------
|
632
609
|
|
633
|
-
def check_preorder
|
610
|
+
def check_preorder(node, exp)
|
634
611
|
exp.map!{|n| [n]}
|
635
612
|
|
636
613
|
out = yields(:preorder, node, node)
|
@@ -640,7 +617,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
640
617
|
exp.reverse!
|
641
618
|
assert_equal_yields exp, out
|
642
619
|
end
|
643
|
-
def check_postorder
|
620
|
+
def check_postorder(node, exp)
|
644
621
|
exp.map!{|n| [n]}
|
645
622
|
|
646
623
|
out = yields(:postorder, node, node)
|
@@ -651,12 +628,12 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
651
628
|
assert_equal_yields exp, out
|
652
629
|
end
|
653
630
|
def test_preorder
|
654
|
-
|
631
|
+
# empty node
|
655
632
|
d = C::Int.new
|
656
633
|
d.longness = 1
|
657
634
|
check_preorder(d, [d])
|
658
635
|
|
659
|
-
|
636
|
+
# one-storey -- populate both the list child and nonlist child
|
660
637
|
d = C::Declaration.new(C::Int.new)
|
661
638
|
d.declarators << C::Declarator.new
|
662
639
|
d.declarators[0].name = 'one'
|
@@ -671,7 +648,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
671
648
|
d.declarators[1]
|
672
649
|
])
|
673
650
|
|
674
|
-
|
651
|
+
# multi-layer
|
675
652
|
d.declarators[0].indirect_type = C::Function.new
|
676
653
|
d.declarators[0].indirect_type.params = Chain[]
|
677
654
|
d.declarators[0].indirect_type.params << C::Parameter.new(C::Int.new, 'i')
|
@@ -692,12 +669,12 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
692
669
|
])
|
693
670
|
end
|
694
671
|
def test_postorder
|
695
|
-
|
672
|
+
# empty node
|
696
673
|
d = C::Int.new
|
697
674
|
d.longness = 1
|
698
675
|
check_preorder(d, [d])
|
699
676
|
|
700
|
-
|
677
|
+
# one-storey -- populate both the list child and nonlist child
|
701
678
|
d = C::Declaration.new(C::Int.new)
|
702
679
|
d.declarators << C::Declarator.new
|
703
680
|
d.declarators[0].name = 'one'
|
@@ -712,7 +689,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
712
689
|
d
|
713
690
|
])
|
714
691
|
|
715
|
-
|
692
|
+
# multi-layer
|
716
693
|
d.declarators[0].indirect_type = C::Function.new
|
717
694
|
d.declarators[0].indirect_type.params = Chain[]
|
718
695
|
d.declarators[0].indirect_type.params << C::Parameter.new(C::Int.new, 'i')
|
@@ -732,7 +709,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
732
709
|
d
|
733
710
|
])
|
734
711
|
end
|
735
|
-
def check_preorder_prune
|
712
|
+
def check_preorder_prune(method, pruned_nodes, root, exp)
|
736
713
|
exp.map!{|n| [n]}
|
737
714
|
|
738
715
|
out = yields(method, root, root) do |node|
|
@@ -744,13 +721,13 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
744
721
|
end
|
745
722
|
|
746
723
|
def test_preorder_prune
|
747
|
-
|
724
|
+
# empty node
|
748
725
|
d = C::Int.new
|
749
726
|
d.longness = 1
|
750
727
|
check_preorder_prune(:preorder, [d], d, [d])
|
751
728
|
check_preorder_prune(:reverse_preorder, [d], d, [d])
|
752
729
|
|
753
|
-
|
730
|
+
# one-storey -- populate both the list child and nonlist child
|
754
731
|
d = C::Declaration.new(C::Int.new)
|
755
732
|
d.declarators << C::Declarator.new
|
756
733
|
d.declarators[0].name = 'one'
|
@@ -769,7 +746,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
769
746
|
d.type,
|
770
747
|
])
|
771
748
|
|
772
|
-
|
749
|
+
# multi-layer
|
773
750
|
d.type = C::Struct.new('S')
|
774
751
|
d.type.members = Chain[]
|
775
752
|
d.type.members << C::Declaration.new(C::Int.new)
|
@@ -798,14 +775,12 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
798
775
|
])
|
799
776
|
end
|
800
777
|
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
### ----------------------------------------------------------------
|
805
|
-
###
|
778
|
+
# ------------------------------------------------------------------
|
779
|
+
# next, prev, list_next, list_prev
|
780
|
+
# ------------------------------------------------------------------
|
806
781
|
|
807
782
|
def test_next_prev
|
808
|
-
|
783
|
+
# list parent
|
809
784
|
i1 = C::Int.new
|
810
785
|
i2 = C::Int.new
|
811
786
|
list = Chain[i1, i2]
|
@@ -814,7 +789,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
814
789
|
assert_same(i1, i2.prev)
|
815
790
|
assert_nil(i1.prev)
|
816
791
|
|
817
|
-
|
792
|
+
# node parent
|
818
793
|
i1 = C::IntLiteral.new(1)
|
819
794
|
i2 = C::IntLiteral.new(2)
|
820
795
|
a = C::Add.new(i1, i2)
|
@@ -823,14 +798,14 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
823
798
|
assert_same(i1, i2.prev)
|
824
799
|
assert_nil(i1.prev)
|
825
800
|
|
826
|
-
|
801
|
+
# no parent
|
827
802
|
i = C::Int.new
|
828
|
-
|
829
|
-
|
803
|
+
assert_raises(C::Node::NoParent){i.next}
|
804
|
+
assert_raises(C::Node::NoParent){i.prev}
|
830
805
|
end
|
831
806
|
|
832
807
|
def test_list_next_prev
|
833
|
-
|
808
|
+
# list parent
|
834
809
|
i1 = C::Int.new
|
835
810
|
i2 = C::Int.new
|
836
811
|
list = Chain[i1, i2]
|
@@ -839,28 +814,28 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
839
814
|
assert_same(i1, i2.list_prev)
|
840
815
|
assert_nil(i1.list_prev)
|
841
816
|
|
842
|
-
|
817
|
+
# node parent
|
843
818
|
i1 = C::IntLiteral.new(1)
|
844
819
|
i2 = C::IntLiteral.new(2)
|
845
820
|
a = C::Add.new(i1, i2)
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
821
|
+
assert_raises(C::Node::BadParent){i1.list_next}
|
822
|
+
assert_raises(C::Node::BadParent){i2.list_next}
|
823
|
+
assert_raises(C::Node::BadParent){i1.list_prev}
|
824
|
+
assert_raises(C::Node::BadParent){i2.list_prev}
|
850
825
|
|
851
|
-
|
826
|
+
# no parent
|
852
827
|
i = C::Int.new
|
853
|
-
|
854
|
-
|
828
|
+
assert_raises(C::Node::NoParent){i.list_next}
|
829
|
+
assert_raises(C::Node::NoParent){i.list_prev}
|
855
830
|
end
|
856
831
|
end
|
857
832
|
|
858
|
-
class NodeTreeTest < Test
|
833
|
+
class NodeTreeTest < Minitest::Test
|
859
834
|
def setup
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
835
|
+
# @c = "(int){[1] = 10,
|
836
|
+
# .x = 20,
|
837
|
+
# [2] .y = 30
|
838
|
+
# }
|
864
839
|
@c = C::CompoundLiteral.new
|
865
840
|
c.type = C::Int.new
|
866
841
|
c.member_inits << C::MemberInit.new
|
@@ -978,26 +953,26 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
978
953
|
assert_tree(mi)
|
979
954
|
assert_same_list([mi1, mi2, mis[1], mis[2]], c.member_inits)
|
980
955
|
|
981
|
-
|
956
|
+
assert_raises(C::Node::NoParent){mi.replace_with(nil)}
|
982
957
|
i1 = C::Int.new
|
983
958
|
i2 = C::Int.new
|
984
|
-
|
959
|
+
assert_raises(ArgumentError){c.type.replace_with(i1, i2)}
|
985
960
|
end
|
986
961
|
|
987
962
|
def test_node_swap_with
|
988
|
-
|
963
|
+
# swap with itself -- attached
|
989
964
|
x = X.new
|
990
965
|
parent = X.new(x)
|
991
966
|
assert_same(x, x.swap_with(x))
|
992
967
|
assert_same(parent, x.parent)
|
993
968
|
assert_same(x, parent.a)
|
994
969
|
|
995
|
-
|
970
|
+
# swap with itself -- detached
|
996
971
|
x = X.new
|
997
972
|
assert_same(x, x.swap_with(x))
|
998
973
|
assert_nil(x.parent)
|
999
974
|
|
1000
|
-
|
975
|
+
# both attached
|
1001
976
|
x = X.new
|
1002
977
|
y = X.new
|
1003
978
|
xp = X.new(x)
|
@@ -1008,7 +983,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1008
983
|
assert_same(yp, x.parent)
|
1009
984
|
assert_same(y, xp.a)
|
1010
985
|
|
1011
|
-
|
986
|
+
# only receiver attached
|
1012
987
|
x = X.new
|
1013
988
|
y = X.new
|
1014
989
|
xp = X.new(x)
|
@@ -1017,7 +992,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1017
992
|
assert_same(xp, y.parent)
|
1018
993
|
assert_same(y, xp.a)
|
1019
994
|
|
1020
|
-
|
995
|
+
# only arg attached
|
1021
996
|
x = X.new
|
1022
997
|
y = X.new
|
1023
998
|
yp = X.new(y)
|
@@ -1026,7 +1001,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1026
1001
|
assert_same(x, yp.a)
|
1027
1002
|
assert_nil(y.parent)
|
1028
1003
|
|
1029
|
-
|
1004
|
+
# neither attached
|
1030
1005
|
x = X.new
|
1031
1006
|
y = X.new
|
1032
1007
|
assert_same(x, x.swap_with(y))
|
@@ -1034,16 +1009,15 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1034
1009
|
assert_nil(y.parent)
|
1035
1010
|
end
|
1036
1011
|
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
###
|
1012
|
+
# ------------------------------------------------------------------
|
1013
|
+
# insert_next, insert_prev
|
1014
|
+
# ------------------------------------------------------------------
|
1015
|
+
|
1042
1016
|
def test_node_insert_next_detached
|
1043
1017
|
x1, x2 = 2.of{X.new}
|
1044
|
-
|
1018
|
+
assert_raises(C::Node::NoParent){x1.insert_next}
|
1045
1019
|
assert_nil(x1.parent)
|
1046
|
-
|
1020
|
+
assert_raises(C::Node::NoParent){x1.insert_next(x2)}
|
1047
1021
|
assert_nil(x1.parent)
|
1048
1022
|
assert_nil(x2.parent)
|
1049
1023
|
end
|
@@ -1051,9 +1025,9 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1051
1025
|
parent = X.new
|
1052
1026
|
x1, x2 = 2.of{X.new}
|
1053
1027
|
parent.a = x1
|
1054
|
-
|
1028
|
+
assert_raises(C::Node::BadParent){x1.insert_next}
|
1055
1029
|
assert_same(parent, x1.parent)
|
1056
|
-
|
1030
|
+
assert_raises(C::Node::BadParent){x1.insert_next(x2)}
|
1057
1031
|
assert_same(parent, x1.parent)
|
1058
1032
|
assert_nil(x2.parent)
|
1059
1033
|
end
|
@@ -1072,9 +1046,9 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1072
1046
|
|
1073
1047
|
def test_node_insert_prev_detached
|
1074
1048
|
a1, a2 = 2.of{X.new}
|
1075
|
-
|
1049
|
+
assert_raises(C::Node::NoParent){a1.insert_prev}
|
1076
1050
|
assert_nil(a1.parent)
|
1077
|
-
|
1051
|
+
assert_raises(C::Node::NoParent){a1.insert_prev(a2)}
|
1078
1052
|
assert_nil(a1.parent)
|
1079
1053
|
assert_nil(a2.parent)
|
1080
1054
|
end
|
@@ -1082,9 +1056,9 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1082
1056
|
parent = X.new
|
1083
1057
|
x1, x2 = 2.of{X.new}
|
1084
1058
|
parent.a = x1
|
1085
|
-
|
1059
|
+
assert_raises(C::Node::BadParent){x1.insert_prev}
|
1086
1060
|
assert_same(parent, x1.parent)
|
1087
|
-
|
1061
|
+
assert_raises(C::Node::BadParent){x1.insert_prev(x2)}
|
1088
1062
|
assert_same(parent, x1.parent)
|
1089
1063
|
assert_nil(x2.parent)
|
1090
1064
|
end
|
@@ -1101,33 +1075,31 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1101
1075
|
assert_same_list([x2, x3, x4, x1], parent)
|
1102
1076
|
end
|
1103
1077
|
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
### ----------------------------------------------------------------
|
1108
|
-
###
|
1078
|
+
# ------------------------------------------------------------------
|
1079
|
+
# node_after, node_before
|
1080
|
+
# ------------------------------------------------------------------
|
1109
1081
|
|
1110
1082
|
def test_node_after_before
|
1111
|
-
|
1083
|
+
# node not a child
|
1112
1084
|
x1, x2 = 2.of{X.new}
|
1113
1085
|
parent = X.new(x1)
|
1114
|
-
|
1115
|
-
|
1086
|
+
assert_raises(ArgumentError){parent.node_after(x2)}
|
1087
|
+
assert_raises(ArgumentError){parent.node_before(x2)}
|
1116
1088
|
|
1117
1089
|
x1, x2 = 2.of{X.new}
|
1118
1090
|
parent = Z.new(nil, x1, nil, x2, nil)
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1091
|
+
assert_raises(ArgumentError){parent.node_after(x1)}
|
1092
|
+
assert_raises(ArgumentError){parent.node_after(x2)}
|
1093
|
+
assert_raises(ArgumentError){parent.node_before(x1)}
|
1094
|
+
assert_raises(ArgumentError){parent.node_before(x2)}
|
1123
1095
|
|
1124
|
-
|
1096
|
+
# one child
|
1125
1097
|
x = X.new
|
1126
1098
|
parent = X.new(x)
|
1127
1099
|
assert_nil(parent.node_after(x))
|
1128
1100
|
assert_nil(parent.node_before(x))
|
1129
1101
|
|
1130
|
-
|
1102
|
+
# two children
|
1131
1103
|
x1 = X.new
|
1132
1104
|
x2 = X.new
|
1133
1105
|
parent = Y.new(x1, x2)
|
@@ -1136,7 +1108,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1136
1108
|
assert_same(x1, parent.node_before(x2))
|
1137
1109
|
assert_nil(parent.node_before(x1))
|
1138
1110
|
|
1139
|
-
|
1111
|
+
# skip over stuff in the middle
|
1140
1112
|
x1, x2, x3, x4, x5 = 5.of{X.new}
|
1141
1113
|
parent = Z.new(x1, x2, nil, x4, x5)
|
1142
1114
|
assert_same(x5, parent.node_after(x1))
|
@@ -1144,7 +1116,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1144
1116
|
assert_same(x1, parent.node_before(x5))
|
1145
1117
|
assert_nil(parent.node_before(x1))
|
1146
1118
|
|
1147
|
-
|
1119
|
+
# skip over stuff at the end
|
1148
1120
|
x1, x2, x3, x4, x5 = 5.of{X.new}
|
1149
1121
|
parent = Z.new(nil, x2, x3, x4, nil)
|
1150
1122
|
assert_nil(parent.node_after(x3))
|
@@ -1152,20 +1124,20 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1152
1124
|
end
|
1153
1125
|
|
1154
1126
|
def test_remove_node
|
1155
|
-
|
1127
|
+
# node not a child
|
1156
1128
|
x1, x2, x3 = 3.of{X.new}
|
1157
1129
|
parent = Z.new(x1, x2)
|
1158
|
-
|
1159
|
-
|
1130
|
+
assert_raises(ArgumentError){parent.remove_node(x2)}
|
1131
|
+
assert_raises(ArgumentError){parent.remove_node(x3)}
|
1160
1132
|
|
1161
|
-
|
1133
|
+
# one child
|
1162
1134
|
x = X.new
|
1163
1135
|
parent = X.new(x)
|
1164
1136
|
assert_same(parent, parent.remove_node(x))
|
1165
1137
|
assert_tree(parent)
|
1166
1138
|
assert_tree(x)
|
1167
1139
|
|
1168
|
-
|
1140
|
+
# two children
|
1169
1141
|
x1, x2 = 2.of{X.new}
|
1170
1142
|
parent = Y.new(x1, x2)
|
1171
1143
|
assert_same(parent, parent.remove_node(x2))
|
@@ -1176,13 +1148,13 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1176
1148
|
end
|
1177
1149
|
|
1178
1150
|
def test_replace_node
|
1179
|
-
|
1151
|
+
# node not a child
|
1180
1152
|
x1, x2, x3, x4 = 3.of{X.new}
|
1181
1153
|
parent = Z.new(x1, x2)
|
1182
|
-
|
1183
|
-
|
1154
|
+
assert_raises(ArgumentError){parent.replace_node(x2, x4)}
|
1155
|
+
assert_raises(ArgumentError){parent.replace_node(x3, x4)}
|
1184
1156
|
|
1185
|
-
|
1157
|
+
# no newnode
|
1186
1158
|
x = X.new
|
1187
1159
|
parent = X.new(x)
|
1188
1160
|
assert_same(parent, parent.replace_node(x))
|
@@ -1190,32 +1162,32 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1190
1162
|
assert_tree(x)
|
1191
1163
|
assert_nil(parent.a)
|
1192
1164
|
|
1193
|
-
|
1165
|
+
# >1 newnode
|
1194
1166
|
x1, x2, x3 = 3.of{X.new}
|
1195
1167
|
parent = X.new(x1)
|
1196
|
-
|
1168
|
+
assert_raises(ArgumentError){parent.replace_node(x1, x2, x3)}
|
1197
1169
|
|
1198
|
-
|
1170
|
+
# one child
|
1199
1171
|
x1, x2 = 2.of{X.new}
|
1200
1172
|
parent = X.new(x1)
|
1201
1173
|
assert_same(parent, parent.replace_node(x1, x2))
|
1202
1174
|
assert_tree(parent)
|
1203
1175
|
assert_tree(x1)
|
1204
1176
|
assert_same(x2, parent.a)
|
1205
|
-
|
1177
|
+
#
|
1206
1178
|
assert_same(parent, parent.replace_node(x2, nil))
|
1207
1179
|
assert_tree(parent)
|
1208
1180
|
assert_tree(x2)
|
1209
1181
|
assert_nil(parent.a)
|
1210
1182
|
|
1211
|
-
|
1183
|
+
# two children
|
1212
1184
|
x1, x2, x3 = 3.of{X.new}
|
1213
1185
|
parent = Y.new(x1, x2)
|
1214
1186
|
assert_same(parent, parent.replace_node(x2, x3))
|
1215
1187
|
assert_tree(parent)
|
1216
1188
|
assert_tree(x2)
|
1217
1189
|
assert_same(x3, parent.b)
|
1218
|
-
|
1190
|
+
#
|
1219
1191
|
assert_same(parent, parent.replace_node(x3, nil))
|
1220
1192
|
assert_tree(parent)
|
1221
1193
|
assert_tree(x3)
|