cast 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/CHANGELOG +50 -0
- data/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.markdown +1948 -0
- data/Rakefile +22 -0
- data/cast.gemspec +20 -0
- data/ext/cast.c +6 -0
- data/ext/extconf.rb +1 -1
- data/ext/parser.c +2 -2
- data/ext/yylex.c +2592 -4171
- data/ext/yylex.re +20 -26
- data/lib/cast.rb +11 -16
- data/lib/cast/c.tab.rb +2252 -2231
- data/lib/cast/c.y +185 -161
- data/lib/cast/c_nodes.rb +181 -391
- data/lib/cast/inspect.rb +8 -10
- data/lib/cast/node.rb +362 -365
- data/lib/cast/node_list.rb +156 -165
- data/lib/cast/parse.rb +86 -65
- data/lib/cast/preprocessor.rb +72 -0
- data/lib/cast/tempfile.rb +33 -0
- data/lib/cast/to_s.rb +83 -75
- data/lib/cast/version.rb +11 -0
- data/test/all.rb +5 -0
- data/test/{test_c_nodes.rb → c_nodes_test.rb} +91 -23
- data/test/lexer_test.rb +323 -0
- data/test/{test_node_list.rb → node_list_test.rb} +367 -396
- data/test/{test_node.rb → node_test.rb} +142 -160
- data/test/{test_parse.rb → parse_test.rb} +63 -17
- data/test/{test_parser.rb → parser_test.rb} +62 -22
- data/test/preprocessor_test.rb +87 -0
- data/test/render_test.rb +2086 -0
- data/test/{run.rb → test_helper.rb} +76 -88
- metadata +100 -54
- data/README +0 -6
- data/doc/index.html +0 -2505
- data/ext/cast_ext.c +0 -10
@@ -1,16 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
######################################################################
|
2
|
+
#
|
3
|
+
# Tests for Node core functionality.
|
4
|
+
#
|
5
|
+
######################################################################
|
6
|
+
|
7
|
+
require 'test/test_helper'
|
8
8
|
|
9
9
|
Chain = C::NodeChain
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
#
|
12
|
+
# Some Node classes.
|
13
|
+
#
|
14
14
|
class X < C::Node
|
15
15
|
child :a
|
16
16
|
initializer :a
|
@@ -39,11 +39,10 @@ class V < C::Node
|
|
39
39
|
end
|
40
40
|
|
41
41
|
class NodeInitializeTest < Test::Unit::TestCase
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
###
|
42
|
+
|
43
|
+
# ------------------------------------------------------------------
|
44
|
+
# initialize
|
45
|
+
# ------------------------------------------------------------------
|
47
46
|
|
48
47
|
def test_initialize_w
|
49
48
|
w = W.new
|
@@ -82,11 +81,10 @@ class NodeInitializeTest < Test::Unit::TestCase
|
|
82
81
|
assert_same(x2, z.b)
|
83
82
|
end
|
84
83
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
###
|
84
|
+
# ------------------------------------------------------------------
|
85
|
+
# Node.new_at
|
86
|
+
# ------------------------------------------------------------------
|
87
|
+
|
90
88
|
def test_new_at
|
91
89
|
pos = C::Node::Pos.new('somefile', 5, 10)
|
92
90
|
xa = X.new
|
@@ -124,17 +122,16 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
124
122
|
return c
|
125
123
|
end
|
126
124
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
###
|
125
|
+
# ------------------------------------------------------------------
|
126
|
+
# ==, eql?
|
127
|
+
# ------------------------------------------------------------------
|
128
|
+
|
132
129
|
def test_eq
|
133
|
-
|
130
|
+
# copy should be equal
|
134
131
|
assert_equal(node, node)
|
135
132
|
assert(node.eql?(node))
|
136
133
|
|
137
|
-
|
134
|
+
# change any one field and it should be not_equal
|
138
135
|
n = node
|
139
136
|
n.type = nil
|
140
137
|
assert_not_equal(node, n)
|
@@ -151,32 +148,32 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
151
148
|
assert_not_equal(node, n)
|
152
149
|
assert(!node.eql?(n))
|
153
150
|
|
154
|
-
|
151
|
+
# add a member's init and it should be not_equal
|
155
152
|
n = node
|
156
153
|
n.member_inits[3].init = C::IntLiteral.new(9)
|
157
154
|
assert_not_equal(node, n)
|
158
155
|
assert(!node.eql?(n))
|
159
156
|
|
160
|
-
|
157
|
+
# change a member's init and it should be not_equal
|
161
158
|
n = node
|
162
159
|
n.member_inits[0].init = C::IntLiteral.new(10)
|
163
160
|
assert_not_equal(node, n)
|
164
161
|
assert(!node.eql?(n))
|
165
162
|
|
166
|
-
|
163
|
+
# add a member specifier and it should be not_equal
|
167
164
|
n = node
|
168
165
|
n.member_inits[3].member = Chain[C::Member.new('d')]
|
169
166
|
assert_not_equal(node, n)
|
170
167
|
assert(!node.eql?(n))
|
171
168
|
|
172
|
-
|
169
|
+
# pop a member and it should be not_equal
|
173
170
|
n = node
|
174
171
|
n.member_inits.pop
|
175
172
|
assert_not_equal(node, n)
|
176
173
|
assert(!node.eql?(n))
|
177
174
|
|
178
|
-
|
179
|
-
|
175
|
+
# assign a field a copy of what's there and it should still be
|
176
|
+
# equal
|
180
177
|
n = node
|
181
178
|
n.member_inits[0].member[0] = C::Member.new('a')
|
182
179
|
assert_equal(node, n)
|
@@ -188,17 +185,16 @@ class NodeEqualTest < Test::Unit::TestCase
|
|
188
185
|
assert(node.eql?(n))
|
189
186
|
end
|
190
187
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
###
|
188
|
+
# ------------------------------------------------------------------
|
189
|
+
# hash
|
190
|
+
# ------------------------------------------------------------------
|
191
|
+
|
196
192
|
def test_hash
|
197
|
-
|
193
|
+
# copy should be equal
|
198
194
|
assert_equal(node.hash, node.hash)
|
199
195
|
|
200
|
-
|
201
|
-
|
196
|
+
# should be equal after assigning to a field a copy of what's
|
197
|
+
# there
|
202
198
|
n = node
|
203
199
|
n.member_inits[0].member[0] = C::Member.new('a')
|
204
200
|
assert_equal(node.hash, n.hash)
|
@@ -211,9 +207,7 @@ end
|
|
211
207
|
|
212
208
|
class NodeCopyTest < Test::Unit::TestCase
|
213
209
|
def setup
|
214
|
-
|
215
|
-
|
216
|
-
|
210
|
+
# (struct s){.a = 1, [2] = {3, 4}, .b [5] = 6, 7}
|
217
211
|
|
218
212
|
@c_t_n = 's'
|
219
213
|
@c_t = C::Struct.new(@c_t_n)
|
@@ -263,11 +257,10 @@ class NodeCopyTest < Test::Unit::TestCase
|
|
263
257
|
end
|
264
258
|
attr_accessor :c, :d, :e
|
265
259
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
###
|
260
|
+
# ------------------------------------------------------------------
|
261
|
+
# dup, clone
|
262
|
+
# ------------------------------------------------------------------
|
263
|
+
|
271
264
|
def check_node value
|
272
265
|
cres = yield(c)
|
273
266
|
dres = yield(d)
|
@@ -290,10 +283,10 @@ class NodeCopyTest < Test::Unit::TestCase
|
|
290
283
|
end
|
291
284
|
|
292
285
|
def test_copy
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
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}
|
297
290
|
assert_tree(c)
|
298
291
|
assert_tree(d)
|
299
292
|
assert_tree(e)
|
@@ -339,13 +332,13 @@ class NodeCopyTest < Test::Unit::TestCase
|
|
339
332
|
end
|
340
333
|
|
341
334
|
class NodeWalkTest < Test::Unit::TestCase
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
def yields
|
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)
|
349
342
|
ret = []
|
350
343
|
out = node.send(method) do |*args|
|
351
344
|
ret << args
|
@@ -355,18 +348,18 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
355
348
|
return ret
|
356
349
|
end
|
357
350
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
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)
|
363
356
|
if exp.zip(out).all?{|a,b| a.same_list?(b)}
|
364
357
|
assert(true)
|
365
358
|
else
|
366
359
|
flunk("walk not equal: #{walk_str(out)} (expected #{walk_str(exp)})")
|
367
360
|
end
|
368
361
|
end
|
369
|
-
def walk_str
|
362
|
+
def walk_str(walk)
|
370
363
|
walk.is_a? ::Array or
|
371
364
|
raise "walk_str: expected ::Array"
|
372
365
|
if walk.empty?
|
@@ -394,17 +387,16 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
394
387
|
end
|
395
388
|
end
|
396
389
|
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
## depth_first
|
390
|
+
# ------------------------------------------------------------------
|
391
|
+
# depth_first, reverse_depth_first
|
392
|
+
# ------------------------------------------------------------------
|
393
|
+
|
394
|
+
def check_depth_firsts(node, exp)
|
395
|
+
# depth_first
|
404
396
|
out = yields(:depth_first, node, node)
|
405
397
|
assert_equal_yields exp, out
|
406
398
|
|
407
|
-
|
399
|
+
# reverse_depth_first
|
408
400
|
exp = exp.reverse.map! do |ev, node|
|
409
401
|
if ev == :ascending
|
410
402
|
[:descending, node]
|
@@ -417,13 +409,13 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
417
409
|
end
|
418
410
|
|
419
411
|
def test_depth_first
|
420
|
-
|
412
|
+
# empty node
|
421
413
|
d = C::Int.new
|
422
414
|
d.longness = 1
|
423
415
|
check_depth_firsts(d,
|
424
416
|
[[:descending, d], [:ascending, d]])
|
425
417
|
|
426
|
-
|
418
|
+
# one-storey -- populate both the list child and nonlist child
|
427
419
|
d = C::Declaration.new(C::Int.new)
|
428
420
|
d.declarators << C::Declarator.new
|
429
421
|
d.declarators[0].name = 'one'
|
@@ -440,7 +432,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
440
432
|
[:ascending, d]
|
441
433
|
])
|
442
434
|
|
443
|
-
|
435
|
+
# multi-layer
|
444
436
|
d.declarators[0].indirect_type = C::Function.new
|
445
437
|
d.declarators[0].indirect_type.params = Chain[]
|
446
438
|
d.declarators[0].indirect_type.params << C::Parameter.new(C::Int.new, 'i')
|
@@ -468,8 +460,8 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
468
460
|
])
|
469
461
|
end
|
470
462
|
|
471
|
-
def check_depth_first_prunes
|
472
|
-
|
463
|
+
def check_depth_first_prunes(pruned_nodes, node, exp)
|
464
|
+
# depth_first
|
473
465
|
out = yields(:depth_first, node, node) do |ev, node|
|
474
466
|
if ev.equal? :descending
|
475
467
|
if pruned_nodes.any?{|n| n.equal? node}
|
@@ -478,7 +470,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
478
470
|
end
|
479
471
|
end
|
480
472
|
assert_equal_yields exp, out
|
481
|
-
|
473
|
+
#
|
482
474
|
ret = catch :prune do
|
483
475
|
node.depth_first do |ev, node|
|
484
476
|
throw :prune, :x if ev.equal? :ascending
|
@@ -487,7 +479,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
487
479
|
end
|
488
480
|
assert_same(:x, ret)
|
489
481
|
|
490
|
-
|
482
|
+
# reverse_depth_first
|
491
483
|
exp = exp.reverse.map! do |ev, node|
|
492
484
|
if ev.equal? :ascending
|
493
485
|
[:descending, node]
|
@@ -503,7 +495,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
503
495
|
end
|
504
496
|
end
|
505
497
|
assert_equal_yields exp, out
|
506
|
-
|
498
|
+
#
|
507
499
|
ret = catch :prune do
|
508
500
|
node.reverse_depth_first do |ev, node|
|
509
501
|
throw :prune, :x if ev.equal? :ascending
|
@@ -514,13 +506,13 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
514
506
|
end
|
515
507
|
|
516
508
|
def test_depth_first_prune
|
517
|
-
|
509
|
+
# empty node
|
518
510
|
d = C::Int.new
|
519
511
|
d.longness = 1
|
520
512
|
check_depth_first_prunes([d], d,
|
521
513
|
[[:descending, d], [:ascending, d]])
|
522
514
|
|
523
|
-
|
515
|
+
# one-storey -- populate both the list child and nonlist child
|
524
516
|
d = C::Declaration.new(C::Int.new)
|
525
517
|
d.declarators << C::Declarator.new
|
526
518
|
d.declarators[0].name = 'one'
|
@@ -534,7 +526,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
534
526
|
[:ascending, d]
|
535
527
|
])
|
536
528
|
|
537
|
-
|
529
|
+
# multi-layer
|
538
530
|
d.type = C::Struct.new('S')
|
539
531
|
d.type.members = Chain[]
|
540
532
|
d.type.members << C::Declaration.new(C::Int.new)
|
@@ -557,13 +549,11 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
557
549
|
])
|
558
550
|
end
|
559
551
|
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
### ----------------------------------------------------------------
|
564
|
-
###
|
552
|
+
# ------------------------------------------------------------------
|
553
|
+
# each, reverse_each
|
554
|
+
# ------------------------------------------------------------------
|
565
555
|
|
566
|
-
def iter_str
|
556
|
+
def iter_str(iter)
|
567
557
|
iter.is_a? ::Array or
|
568
558
|
raise "iter_str: expected ::Array"
|
569
559
|
if iter.empty?
|
@@ -582,7 +572,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
582
572
|
return s.string
|
583
573
|
end
|
584
574
|
end
|
585
|
-
def check_each
|
575
|
+
def check_each(node, exp)
|
586
576
|
exp.map!{|n| [n]}
|
587
577
|
|
588
578
|
out = yields(:each, node, node)
|
@@ -593,34 +583,31 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
593
583
|
assert_equal_yields exp, out
|
594
584
|
end
|
595
585
|
def test_each
|
596
|
-
|
586
|
+
# empty
|
597
587
|
parent = X.new
|
598
588
|
check_each(parent, [])
|
599
589
|
|
600
|
-
|
590
|
+
# one child
|
601
591
|
x1 = X.new
|
602
592
|
parent = X.new(x1)
|
603
593
|
check_each(parent, [x1])
|
604
594
|
|
605
|
-
|
595
|
+
# two children
|
606
596
|
x1, x2 = 2.of{X.new}
|
607
597
|
parent = Y.new(x1, x2)
|
608
598
|
check_each(parent, [x1, x2])
|
609
599
|
|
610
|
-
|
600
|
+
# three with some nil and some fields
|
611
601
|
x1, x2, x3, x4, x5 = 5.of{X.new}
|
612
602
|
parent = Z.new(x1, x2, nil, x4, x5)
|
613
603
|
check_each(parent, [x1, x5])
|
614
604
|
end
|
615
605
|
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
### postorder, reverse_postorder
|
620
|
-
### ----------------------------------------------------------------
|
621
|
-
###
|
606
|
+
# ------------------------------------------------------------------
|
607
|
+
# preorder, reverse_preorder, postorder, reverse_postorder
|
608
|
+
# ------------------------------------------------------------------
|
622
609
|
|
623
|
-
def check_preorder
|
610
|
+
def check_preorder(node, exp)
|
624
611
|
exp.map!{|n| [n]}
|
625
612
|
|
626
613
|
out = yields(:preorder, node, node)
|
@@ -630,7 +617,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
630
617
|
exp.reverse!
|
631
618
|
assert_equal_yields exp, out
|
632
619
|
end
|
633
|
-
def check_postorder
|
620
|
+
def check_postorder(node, exp)
|
634
621
|
exp.map!{|n| [n]}
|
635
622
|
|
636
623
|
out = yields(:postorder, node, node)
|
@@ -641,12 +628,12 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
641
628
|
assert_equal_yields exp, out
|
642
629
|
end
|
643
630
|
def test_preorder
|
644
|
-
|
631
|
+
# empty node
|
645
632
|
d = C::Int.new
|
646
633
|
d.longness = 1
|
647
634
|
check_preorder(d, [d])
|
648
635
|
|
649
|
-
|
636
|
+
# one-storey -- populate both the list child and nonlist child
|
650
637
|
d = C::Declaration.new(C::Int.new)
|
651
638
|
d.declarators << C::Declarator.new
|
652
639
|
d.declarators[0].name = 'one'
|
@@ -661,7 +648,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
661
648
|
d.declarators[1]
|
662
649
|
])
|
663
650
|
|
664
|
-
|
651
|
+
# multi-layer
|
665
652
|
d.declarators[0].indirect_type = C::Function.new
|
666
653
|
d.declarators[0].indirect_type.params = Chain[]
|
667
654
|
d.declarators[0].indirect_type.params << C::Parameter.new(C::Int.new, 'i')
|
@@ -682,12 +669,12 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
682
669
|
])
|
683
670
|
end
|
684
671
|
def test_postorder
|
685
|
-
|
672
|
+
# empty node
|
686
673
|
d = C::Int.new
|
687
674
|
d.longness = 1
|
688
675
|
check_preorder(d, [d])
|
689
676
|
|
690
|
-
|
677
|
+
# one-storey -- populate both the list child and nonlist child
|
691
678
|
d = C::Declaration.new(C::Int.new)
|
692
679
|
d.declarators << C::Declarator.new
|
693
680
|
d.declarators[0].name = 'one'
|
@@ -702,7 +689,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
702
689
|
d
|
703
690
|
])
|
704
691
|
|
705
|
-
|
692
|
+
# multi-layer
|
706
693
|
d.declarators[0].indirect_type = C::Function.new
|
707
694
|
d.declarators[0].indirect_type.params = Chain[]
|
708
695
|
d.declarators[0].indirect_type.params << C::Parameter.new(C::Int.new, 'i')
|
@@ -722,7 +709,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
722
709
|
d
|
723
710
|
])
|
724
711
|
end
|
725
|
-
def check_preorder_prune
|
712
|
+
def check_preorder_prune(method, pruned_nodes, root, exp)
|
726
713
|
exp.map!{|n| [n]}
|
727
714
|
|
728
715
|
out = yields(method, root, root) do |node|
|
@@ -734,13 +721,13 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
734
721
|
end
|
735
722
|
|
736
723
|
def test_preorder_prune
|
737
|
-
|
724
|
+
# empty node
|
738
725
|
d = C::Int.new
|
739
726
|
d.longness = 1
|
740
727
|
check_preorder_prune(:preorder, [d], d, [d])
|
741
728
|
check_preorder_prune(:reverse_preorder, [d], d, [d])
|
742
729
|
|
743
|
-
|
730
|
+
# one-storey -- populate both the list child and nonlist child
|
744
731
|
d = C::Declaration.new(C::Int.new)
|
745
732
|
d.declarators << C::Declarator.new
|
746
733
|
d.declarators[0].name = 'one'
|
@@ -759,7 +746,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
759
746
|
d.type,
|
760
747
|
])
|
761
748
|
|
762
|
-
|
749
|
+
# multi-layer
|
763
750
|
d.type = C::Struct.new('S')
|
764
751
|
d.type.members = Chain[]
|
765
752
|
d.type.members << C::Declaration.new(C::Int.new)
|
@@ -788,14 +775,12 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
788
775
|
])
|
789
776
|
end
|
790
777
|
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
### ----------------------------------------------------------------
|
795
|
-
###
|
778
|
+
# ------------------------------------------------------------------
|
779
|
+
# next, prev, list_next, list_prev
|
780
|
+
# ------------------------------------------------------------------
|
796
781
|
|
797
782
|
def test_next_prev
|
798
|
-
|
783
|
+
# list parent
|
799
784
|
i1 = C::Int.new
|
800
785
|
i2 = C::Int.new
|
801
786
|
list = Chain[i1, i2]
|
@@ -804,7 +789,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
804
789
|
assert_same(i1, i2.prev)
|
805
790
|
assert_nil(i1.prev)
|
806
791
|
|
807
|
-
|
792
|
+
# node parent
|
808
793
|
i1 = C::IntLiteral.new(1)
|
809
794
|
i2 = C::IntLiteral.new(2)
|
810
795
|
a = C::Add.new(i1, i2)
|
@@ -813,14 +798,14 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
813
798
|
assert_same(i1, i2.prev)
|
814
799
|
assert_nil(i1.prev)
|
815
800
|
|
816
|
-
|
801
|
+
# no parent
|
817
802
|
i = C::Int.new
|
818
803
|
assert_raise(C::Node::NoParent){i.next}
|
819
804
|
assert_raise(C::Node::NoParent){i.prev}
|
820
805
|
end
|
821
806
|
|
822
807
|
def test_list_next_prev
|
823
|
-
|
808
|
+
# list parent
|
824
809
|
i1 = C::Int.new
|
825
810
|
i2 = C::Int.new
|
826
811
|
list = Chain[i1, i2]
|
@@ -829,7 +814,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
829
814
|
assert_same(i1, i2.list_prev)
|
830
815
|
assert_nil(i1.list_prev)
|
831
816
|
|
832
|
-
|
817
|
+
# node parent
|
833
818
|
i1 = C::IntLiteral.new(1)
|
834
819
|
i2 = C::IntLiteral.new(2)
|
835
820
|
a = C::Add.new(i1, i2)
|
@@ -838,7 +823,7 @@ class NodeWalkTest < Test::Unit::TestCase
|
|
838
823
|
assert_raise(C::Node::BadParent){i1.list_prev}
|
839
824
|
assert_raise(C::Node::BadParent){i2.list_prev}
|
840
825
|
|
841
|
-
|
826
|
+
# no parent
|
842
827
|
i = C::Int.new
|
843
828
|
assert_raise(C::Node::NoParent){i.list_next}
|
844
829
|
assert_raise(C::Node::NoParent){i.list_prev}
|
@@ -847,10 +832,10 @@ end
|
|
847
832
|
|
848
833
|
class NodeTreeTest < Test::Unit::TestCase
|
849
834
|
def setup
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
835
|
+
# @c = "(int){[1] = 10,
|
836
|
+
# .x = 20,
|
837
|
+
# [2] .y = 30
|
838
|
+
# }
|
854
839
|
@c = C::CompoundLiteral.new
|
855
840
|
c.type = C::Int.new
|
856
841
|
c.member_inits << C::MemberInit.new
|
@@ -975,19 +960,19 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
975
960
|
end
|
976
961
|
|
977
962
|
def test_node_swap_with
|
978
|
-
|
963
|
+
# swap with itself -- attached
|
979
964
|
x = X.new
|
980
965
|
parent = X.new(x)
|
981
966
|
assert_same(x, x.swap_with(x))
|
982
967
|
assert_same(parent, x.parent)
|
983
968
|
assert_same(x, parent.a)
|
984
969
|
|
985
|
-
|
970
|
+
# swap with itself -- detached
|
986
971
|
x = X.new
|
987
972
|
assert_same(x, x.swap_with(x))
|
988
973
|
assert_nil(x.parent)
|
989
974
|
|
990
|
-
|
975
|
+
# both attached
|
991
976
|
x = X.new
|
992
977
|
y = X.new
|
993
978
|
xp = X.new(x)
|
@@ -998,7 +983,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
998
983
|
assert_same(yp, x.parent)
|
999
984
|
assert_same(y, xp.a)
|
1000
985
|
|
1001
|
-
|
986
|
+
# only receiver attached
|
1002
987
|
x = X.new
|
1003
988
|
y = X.new
|
1004
989
|
xp = X.new(x)
|
@@ -1007,7 +992,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1007
992
|
assert_same(xp, y.parent)
|
1008
993
|
assert_same(y, xp.a)
|
1009
994
|
|
1010
|
-
|
995
|
+
# only arg attached
|
1011
996
|
x = X.new
|
1012
997
|
y = X.new
|
1013
998
|
yp = X.new(y)
|
@@ -1016,7 +1001,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1016
1001
|
assert_same(x, yp.a)
|
1017
1002
|
assert_nil(y.parent)
|
1018
1003
|
|
1019
|
-
|
1004
|
+
# neither attached
|
1020
1005
|
x = X.new
|
1021
1006
|
y = X.new
|
1022
1007
|
assert_same(x, x.swap_with(y))
|
@@ -1024,11 +1009,10 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1024
1009
|
assert_nil(y.parent)
|
1025
1010
|
end
|
1026
1011
|
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
###
|
1012
|
+
# ------------------------------------------------------------------
|
1013
|
+
# insert_next, insert_prev
|
1014
|
+
# ------------------------------------------------------------------
|
1015
|
+
|
1032
1016
|
def test_node_insert_next_detached
|
1033
1017
|
x1, x2 = 2.of{X.new}
|
1034
1018
|
assert_raise(C::Node::NoParent){x1.insert_next}
|
@@ -1091,14 +1075,12 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1091
1075
|
assert_same_list([x2, x3, x4, x1], parent)
|
1092
1076
|
end
|
1093
1077
|
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
### ----------------------------------------------------------------
|
1098
|
-
###
|
1078
|
+
# ------------------------------------------------------------------
|
1079
|
+
# node_after, node_before
|
1080
|
+
# ------------------------------------------------------------------
|
1099
1081
|
|
1100
1082
|
def test_node_after_before
|
1101
|
-
|
1083
|
+
# node not a child
|
1102
1084
|
x1, x2 = 2.of{X.new}
|
1103
1085
|
parent = X.new(x1)
|
1104
1086
|
assert_raise(ArgumentError){parent.node_after(x2)}
|
@@ -1111,13 +1093,13 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1111
1093
|
assert_raise(ArgumentError){parent.node_before(x1)}
|
1112
1094
|
assert_raise(ArgumentError){parent.node_before(x2)}
|
1113
1095
|
|
1114
|
-
|
1096
|
+
# one child
|
1115
1097
|
x = X.new
|
1116
1098
|
parent = X.new(x)
|
1117
1099
|
assert_nil(parent.node_after(x))
|
1118
1100
|
assert_nil(parent.node_before(x))
|
1119
1101
|
|
1120
|
-
|
1102
|
+
# two children
|
1121
1103
|
x1 = X.new
|
1122
1104
|
x2 = X.new
|
1123
1105
|
parent = Y.new(x1, x2)
|
@@ -1126,7 +1108,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1126
1108
|
assert_same(x1, parent.node_before(x2))
|
1127
1109
|
assert_nil(parent.node_before(x1))
|
1128
1110
|
|
1129
|
-
|
1111
|
+
# skip over stuff in the middle
|
1130
1112
|
x1, x2, x3, x4, x5 = 5.of{X.new}
|
1131
1113
|
parent = Z.new(x1, x2, nil, x4, x5)
|
1132
1114
|
assert_same(x5, parent.node_after(x1))
|
@@ -1134,7 +1116,7 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1134
1116
|
assert_same(x1, parent.node_before(x5))
|
1135
1117
|
assert_nil(parent.node_before(x1))
|
1136
1118
|
|
1137
|
-
|
1119
|
+
# skip over stuff at the end
|
1138
1120
|
x1, x2, x3, x4, x5 = 5.of{X.new}
|
1139
1121
|
parent = Z.new(nil, x2, x3, x4, nil)
|
1140
1122
|
assert_nil(parent.node_after(x3))
|
@@ -1142,20 +1124,20 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1142
1124
|
end
|
1143
1125
|
|
1144
1126
|
def test_remove_node
|
1145
|
-
|
1127
|
+
# node not a child
|
1146
1128
|
x1, x2, x3 = 3.of{X.new}
|
1147
1129
|
parent = Z.new(x1, x2)
|
1148
1130
|
assert_raise(ArgumentError){parent.remove_node(x2)}
|
1149
1131
|
assert_raise(ArgumentError){parent.remove_node(x3)}
|
1150
1132
|
|
1151
|
-
|
1133
|
+
# one child
|
1152
1134
|
x = X.new
|
1153
1135
|
parent = X.new(x)
|
1154
1136
|
assert_same(parent, parent.remove_node(x))
|
1155
1137
|
assert_tree(parent)
|
1156
1138
|
assert_tree(x)
|
1157
1139
|
|
1158
|
-
|
1140
|
+
# two children
|
1159
1141
|
x1, x2 = 2.of{X.new}
|
1160
1142
|
parent = Y.new(x1, x2)
|
1161
1143
|
assert_same(parent, parent.remove_node(x2))
|
@@ -1166,13 +1148,13 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1166
1148
|
end
|
1167
1149
|
|
1168
1150
|
def test_replace_node
|
1169
|
-
|
1151
|
+
# node not a child
|
1170
1152
|
x1, x2, x3, x4 = 3.of{X.new}
|
1171
1153
|
parent = Z.new(x1, x2)
|
1172
1154
|
assert_raise(ArgumentError){parent.replace_node(x2, x4)}
|
1173
1155
|
assert_raise(ArgumentError){parent.replace_node(x3, x4)}
|
1174
1156
|
|
1175
|
-
|
1157
|
+
# no newnode
|
1176
1158
|
x = X.new
|
1177
1159
|
parent = X.new(x)
|
1178
1160
|
assert_same(parent, parent.replace_node(x))
|
@@ -1180,32 +1162,32 @@ class NodeTreeTest < Test::Unit::TestCase
|
|
1180
1162
|
assert_tree(x)
|
1181
1163
|
assert_nil(parent.a)
|
1182
1164
|
|
1183
|
-
|
1165
|
+
# >1 newnode
|
1184
1166
|
x1, x2, x3 = 3.of{X.new}
|
1185
1167
|
parent = X.new(x1)
|
1186
1168
|
assert_raise(ArgumentError){parent.replace_node(x1, x2, x3)}
|
1187
1169
|
|
1188
|
-
|
1170
|
+
# one child
|
1189
1171
|
x1, x2 = 2.of{X.new}
|
1190
1172
|
parent = X.new(x1)
|
1191
1173
|
assert_same(parent, parent.replace_node(x1, x2))
|
1192
1174
|
assert_tree(parent)
|
1193
1175
|
assert_tree(x1)
|
1194
1176
|
assert_same(x2, parent.a)
|
1195
|
-
|
1177
|
+
#
|
1196
1178
|
assert_same(parent, parent.replace_node(x2, nil))
|
1197
1179
|
assert_tree(parent)
|
1198
1180
|
assert_tree(x2)
|
1199
1181
|
assert_nil(parent.a)
|
1200
1182
|
|
1201
|
-
|
1183
|
+
# two children
|
1202
1184
|
x1, x2, x3 = 3.of{X.new}
|
1203
1185
|
parent = Y.new(x1, x2)
|
1204
1186
|
assert_same(parent, parent.replace_node(x2, x3))
|
1205
1187
|
assert_tree(parent)
|
1206
1188
|
assert_tree(x2)
|
1207
1189
|
assert_same(x3, parent.b)
|
1208
|
-
|
1190
|
+
#
|
1209
1191
|
assert_same(parent, parent.replace_node(x3, nil))
|
1210
1192
|
assert_tree(parent)
|
1211
1193
|
assert_tree(x3)
|