rubytree 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/API-CHANGES.md +153 -0
- data/Gemfile +2 -6
- data/Gemfile.lock +40 -55
- data/History.md +410 -0
- data/LICENSE.md +1 -1
- data/README.md +21 -24
- data/Rakefile +41 -20
- data/TODO.org +19 -15
- data/examples/example_basic.rb +14 -7
- data/lib/rubytree.rb +2 -3
- data/lib/tree/binarytree.rb +13 -12
- data/lib/tree/tree_deps.rb +2 -5
- data/lib/tree/utils/hash_converter.rb +129 -121
- data/lib/tree/utils/json_converter.rb +80 -77
- data/lib/tree/utils/metrics_methods.rb +14 -47
- data/lib/tree/utils/path_methods.rb +11 -11
- data/lib/tree/utils/tree_merge_handler.rb +80 -80
- data/lib/tree/utils/utils.rb +9 -7
- data/lib/tree/version.rb +3 -4
- data/lib/tree.rb +80 -82
- data/rubytree.gemspec +61 -38
- data/spec/spec_helper.rb +4 -2
- data/spec/tree_spec.rb +129 -28
- data/test/run_test.rb +4 -3
- data/test/test_binarytree.rb +25 -45
- data/test/test_rubytree_require.rb +2 -1
- data/test/test_subclassed_node.rb +2 -20
- data/test/test_thread_and_fiber.rb +4 -4
- data/test/test_tree.rb +126 -199
- metadata +103 -69
- data/API-CHANGES.rdoc +0 -99
- data/History.rdoc +0 -303
- data/lib/tree/utils/camel_case_method_handler.rb +0 -77
- data/setup.rb +0 -1565
data/test/test_tree.rb
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
# test_tree.rb - This file is part of the RubyTree package.
|
4
4
|
#
|
5
|
-
# Copyright (c) 2006
|
6
|
-
#
|
7
|
-
# All rights reserved.
|
5
|
+
# Copyright (c) 2006-2022 Anupam Sengupta. All rights reserved.
|
8
6
|
#
|
9
7
|
# Redistribution and use in source and binary forms, with or without modification,
|
10
8
|
# are permitted provided that the following conditions are met:
|
@@ -31,9 +29,9 @@
|
|
31
29
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
32
30
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
31
|
#
|
32
|
+
# frozen_string_literal: true
|
34
33
|
|
35
34
|
require 'test/unit'
|
36
|
-
require 'structured_warnings'
|
37
35
|
require 'json'
|
38
36
|
require_relative '../lib/tree/tree_deps'
|
39
37
|
|
@@ -96,9 +94,9 @@ module TestTree
|
|
96
94
|
assert_not_nil(@root.name, 'Name should not be nil')
|
97
95
|
assert_equal('ROOT', @root.name, "Name should be 'ROOT'")
|
98
96
|
assert_equal('Root Node', @root.content, "Content should be 'Root Node'")
|
99
|
-
assert(@root.
|
100
|
-
assert(!@root.
|
101
|
-
assert(@root.
|
97
|
+
assert(@root.root?, 'Should identify as root')
|
98
|
+
assert(!@root.children?, 'Cannot have any children')
|
99
|
+
assert(@root.content?, 'This root should have content')
|
102
100
|
assert_equal(1, @root.size, 'Number of nodes should be one')
|
103
101
|
assert_equal(0, @root.siblings.length, 'This root does not have any children')
|
104
102
|
assert_equal(0, @root.in_degree, 'Root should have an in-degree of 0')
|
@@ -110,7 +108,7 @@ module TestTree
|
|
110
108
|
def test_root
|
111
109
|
setup_test_tree
|
112
110
|
|
113
|
-
#
|
111
|
+
# @todo: Should probably change this logic. Root's root should
|
114
112
|
# return nil so that the possibility of a recursive error does not exist
|
115
113
|
# at all.
|
116
114
|
assert_same(@root, @root.root, "Root's root is self")
|
@@ -146,21 +144,21 @@ module TestTree
|
|
146
144
|
|
147
145
|
assert_same(Tree::TreeNode, tree.class)
|
148
146
|
assert_same(tree.name, :A)
|
149
|
-
assert_equal(true, tree.
|
150
|
-
assert_equal(false, tree.
|
147
|
+
assert_equal(true, tree.root?)
|
148
|
+
assert_equal(false, tree.leaf?)
|
151
149
|
assert_equal(9, tree.size)
|
152
150
|
assert_equal('Root content', tree.content)
|
153
151
|
assert_equal(3, tree.children.count) # B, C, D
|
154
152
|
|
155
153
|
leaf_with_content = tree[:B][:F][:I]
|
156
154
|
assert_equal('Leaf content', leaf_with_content.content)
|
157
|
-
assert_equal(true, leaf_with_content.
|
155
|
+
assert_equal(true, leaf_with_content.leaf?)
|
158
156
|
|
159
157
|
leaf_without_content = tree[:C]
|
160
|
-
assert_equal(true, leaf_without_content.
|
158
|
+
assert_equal(true, leaf_without_content.leaf?)
|
161
159
|
|
162
160
|
interior_node = tree[:B][:F]
|
163
|
-
assert_equal(false, interior_node.
|
161
|
+
assert_equal(false, interior_node.leaf?)
|
164
162
|
assert_equal(2, interior_node.children.count)
|
165
163
|
|
166
164
|
# Can't make a node without a name
|
@@ -196,21 +194,21 @@ module TestTree
|
|
196
194
|
|
197
195
|
assert_same(Tree::TreeNode, tree.class)
|
198
196
|
assert_same(:A, tree.name)
|
199
|
-
assert_equal(true, tree.
|
200
|
-
assert_equal(false, tree.
|
197
|
+
assert_equal(true, tree.root?)
|
198
|
+
assert_equal(false, tree.leaf?)
|
201
199
|
assert_equal(9, tree.size)
|
202
200
|
assert_equal('Root content', tree.content)
|
203
201
|
assert_equal(3, tree.children.count) # B, C, D
|
204
202
|
|
205
203
|
leaf_with_content = tree[:B][:F][:I]
|
206
204
|
assert_equal('Leaf content', leaf_with_content.content)
|
207
|
-
assert_equal(true, leaf_with_content.
|
205
|
+
assert_equal(true, leaf_with_content.leaf?)
|
208
206
|
|
209
207
|
leaf_without_content = tree[:C]
|
210
|
-
assert_equal(true, leaf_without_content.
|
208
|
+
assert_equal(true, leaf_without_content.leaf?)
|
211
209
|
|
212
210
|
interior_node = tree[:B][:F]
|
213
|
-
assert_equal(false, interior_node.
|
211
|
+
assert_equal(false, interior_node.leaf?)
|
214
212
|
assert_equal(2, interior_node.children.count)
|
215
213
|
end
|
216
214
|
|
@@ -236,7 +234,7 @@ module TestTree
|
|
236
234
|
assert_equal(2, added_children.count)
|
237
235
|
assert_equal(7, tree.size)
|
238
236
|
assert_equal('G content', tree[:G].content)
|
239
|
-
assert_equal(true, tree[:G].
|
237
|
+
assert_equal(true, tree[:G].leaf?)
|
240
238
|
assert_equal(5, tree[:B].size)
|
241
239
|
assert_equal(3, tree[:B].children.count)
|
242
240
|
|
@@ -291,24 +289,15 @@ module TestTree
|
|
291
289
|
def test_has_content_eh
|
292
290
|
a_node = Tree::TreeNode.new('A Node')
|
293
291
|
assert_nil(a_node.content, 'The node should not have content')
|
294
|
-
assert(!a_node.
|
292
|
+
assert(!a_node.content?, 'The node should not have content')
|
295
293
|
|
296
294
|
a_node.content = 'Something'
|
297
295
|
assert_not_nil(a_node.content, 'The node should now have content')
|
298
|
-
assert(a_node.
|
299
|
-
end
|
300
|
-
|
301
|
-
# Test the equivalence of size and length methods.
|
302
|
-
def test_length_is_size
|
303
|
-
setup_test_tree
|
304
|
-
assert_equal(@root.size, @root.length, 'Length and size methods should return the same result')
|
296
|
+
assert(a_node.content?, 'The node should now have content')
|
305
297
|
end
|
306
298
|
|
307
299
|
# Test the <=> operator.
|
308
300
|
def test_spaceship
|
309
|
-
require 'structured_warnings'
|
310
|
-
StructuredWarnings::StandardWarning.disable # Disable the warnings for using integers as node names
|
311
|
-
|
312
301
|
first_node = Tree::TreeNode.new(1)
|
313
302
|
second_node = Tree::TreeNode.new(2)
|
314
303
|
|
@@ -326,8 +315,6 @@ module TestTree
|
|
326
315
|
|
327
316
|
second_node = Tree::TreeNode.new('ABC')
|
328
317
|
assert_equal(0, first_node <=> second_node)
|
329
|
-
|
330
|
-
StructuredWarnings::StandardWarning.enable
|
331
318
|
end
|
332
319
|
|
333
320
|
# Test the inclusion of Comparable
|
@@ -342,7 +329,7 @@ module TestTree
|
|
342
329
|
assert(node_b > node_a, "Node B is lexically 'greater than' node A")
|
343
330
|
assert(node_b >= node_a, "Node B is lexically 'greater than' node A")
|
344
331
|
|
345
|
-
assert(
|
332
|
+
assert(node_a != node_b, 'Node A and Node B are not equal')
|
346
333
|
assert(node_b.between?(node_a, node_c), 'Node B is lexically between node A and node C')
|
347
334
|
end
|
348
335
|
|
@@ -382,7 +369,7 @@ module TestTree
|
|
382
369
|
def test_first_sibling
|
383
370
|
setup_test_tree
|
384
371
|
|
385
|
-
#
|
372
|
+
# @todo: Need to fix the first_sibling method to return nil for nodes with no siblings.
|
386
373
|
assert_same(@root, @root.first_sibling, "Root's first sibling is itself")
|
387
374
|
assert_same(@child1, @child1.first_sibling, "Child1's first sibling is itself")
|
388
375
|
assert_same(@child1, @child2.first_sibling, "Child2's first sibling should be child1")
|
@@ -391,26 +378,26 @@ module TestTree
|
|
391
378
|
assert_not_same(@child1, @child4.first_sibling, "Child4's first sibling is itself")
|
392
379
|
end
|
393
380
|
|
394
|
-
# Test the
|
381
|
+
# Test the first_sibling? method.
|
395
382
|
def test_is_first_sibling_eh
|
396
383
|
setup_test_tree
|
397
384
|
|
398
|
-
assert(@root.
|
399
|
-
assert(@child1.
|
400
|
-
assert(!@child2.
|
401
|
-
assert(!@child3.
|
402
|
-
assert(@child4.
|
385
|
+
assert(@root.first_sibling?, "Root's first sibling is itself")
|
386
|
+
assert(@child1.first_sibling?, "Child1's first sibling is itself")
|
387
|
+
assert(!@child2.first_sibling?, 'Child2 is not the first sibling')
|
388
|
+
assert(!@child3.first_sibling?, 'Child3 is not the first sibling')
|
389
|
+
assert(@child4.first_sibling?, "Child4's first sibling is itself")
|
403
390
|
end
|
404
391
|
|
405
|
-
# Test the
|
392
|
+
# Test the last_sibling? method.
|
406
393
|
def test_is_last_sibling_eh
|
407
394
|
setup_test_tree
|
408
395
|
|
409
|
-
assert(@root.
|
410
|
-
assert(!@child1.
|
411
|
-
assert(!@child2.
|
412
|
-
assert(@child3.
|
413
|
-
assert(@child4.
|
396
|
+
assert(@root.last_sibling?, "Root's last sibling is itself")
|
397
|
+
assert(!@child1.last_sibling?, 'Child1 is not the last sibling')
|
398
|
+
assert(!@child2.last_sibling?, 'Child2 is not the last sibling')
|
399
|
+
assert(@child3.last_sibling?, "Child3's last sibling is itself")
|
400
|
+
assert(@child4.last_sibling?, "Child4's last sibling is itself")
|
414
401
|
end
|
415
402
|
|
416
403
|
# Test the last_sibling method.
|
@@ -452,15 +439,15 @@ module TestTree
|
|
452
439
|
assert_equal(0, siblings.length, 'Root should not have any siblings')
|
453
440
|
end
|
454
441
|
|
455
|
-
# Test the
|
442
|
+
# Test the only_child? method.
|
456
443
|
def test_is_only_child_eh
|
457
444
|
setup_test_tree
|
458
445
|
|
459
|
-
assert(@root.
|
460
|
-
assert(!@child1.
|
461
|
-
assert(!@child2.
|
462
|
-
assert(!@child3.
|
463
|
-
assert(@child4.
|
446
|
+
assert(@root.only_child?, 'Root is an only child')
|
447
|
+
assert(!@child1.only_child?, 'Child1 is not the only child')
|
448
|
+
assert(!@child2.only_child?, 'Child2 is not the only child')
|
449
|
+
assert(!@child3.only_child?, 'Child3 is not the only child')
|
450
|
+
assert(@child4.only_child?, 'Child4 is an only child')
|
464
451
|
end
|
465
452
|
|
466
453
|
# Test the next_sibling method.
|
@@ -487,14 +474,14 @@ module TestTree
|
|
487
474
|
|
488
475
|
# Test the add method.
|
489
476
|
def test_add
|
490
|
-
assert(!@root.
|
477
|
+
assert(!@root.children?, 'Should not have any children')
|
491
478
|
|
492
479
|
assert_equal(1, @root.size, 'Should have 1 node (the root)')
|
493
480
|
@root.add(@child1)
|
494
481
|
|
495
482
|
@root << @child2
|
496
483
|
|
497
|
-
assert(@root.
|
484
|
+
assert(@root.children?, 'Should have children')
|
498
485
|
assert_equal(3, @root.size, 'Should have three nodes')
|
499
486
|
|
500
487
|
@root << @child3 << @child4
|
@@ -533,7 +520,8 @@ module TestTree
|
|
533
520
|
begin
|
534
521
|
root << two << deep
|
535
522
|
rescue RuntimeError => e
|
536
|
-
raise("Error! The RuntimeError #{e} should not have been thrown.
|
523
|
+
raise("Error! The RuntimeError #{e} should not have been thrown. " \
|
524
|
+
'The same node can be added to different branches.')
|
537
525
|
end
|
538
526
|
|
539
527
|
assert_raise(ArgumentError) { root << three << three }
|
@@ -549,7 +537,7 @@ module TestTree
|
|
549
537
|
|
550
538
|
# Test Addition at a specific position
|
551
539
|
def test_add_at_specific_position
|
552
|
-
assert(!@root.
|
540
|
+
assert(!@root.children?, 'Should not have any children')
|
553
541
|
|
554
542
|
assert_equal(1, @root.size, 'Should have 1 node (the root)')
|
555
543
|
@root.add(@child1) # First Child added at position 0
|
@@ -579,7 +567,9 @@ module TestTree
|
|
579
567
|
|
580
568
|
# Now, a negative test. We are preventing addition to a position that does not exist.
|
581
569
|
assert_raise(RuntimeError) do
|
582
|
-
|
570
|
+
# Fifth child inserted beyond the last position that is valid (at 5th
|
571
|
+
# pos).
|
572
|
+
@root.add(@child5, @root.children.size + 1)
|
583
573
|
end
|
584
574
|
# Validate that we still have children = [@child1, @child3, @child2, @child4]
|
585
575
|
assert_equal(@child1, @root[0])
|
@@ -589,9 +579,12 @@ module TestTree
|
|
589
579
|
assert_nil(@root[4])
|
590
580
|
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
591
581
|
|
592
|
-
# Another negative test.
|
582
|
+
# Another negative test. Lets attempt to add from the end at a position
|
583
|
+
# that is not available
|
593
584
|
assert_raise(RuntimeError) do
|
594
|
-
|
585
|
+
# Fifth child inserted beyond the first position that is valid; i.e. at
|
586
|
+
# -6
|
587
|
+
@root.add(@child5, -(@root.children.size + 2))
|
595
588
|
end
|
596
589
|
assert_nil(@root[-5])
|
597
590
|
assert_equal(@child1, @root[-4])
|
@@ -600,8 +593,11 @@ module TestTree
|
|
600
593
|
assert_equal(@child4, @root[-1])
|
601
594
|
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
602
595
|
|
603
|
-
# Lets correctly add the fifth child from the end to effectively prepend
|
604
|
-
|
596
|
+
# Lets correctly add the fifth child from the end to effectively prepend
|
597
|
+
# the node.
|
598
|
+
|
599
|
+
# Fifth child inserted beyond the first position; i.e. at -5
|
600
|
+
@root.add(@child5, -(@root.children.size + 1))
|
605
601
|
assert_nil(@root[-6])
|
606
602
|
assert_equal(@child5, @root[-5])
|
607
603
|
assert_equal(@child1, @root[-4])
|
@@ -658,40 +654,40 @@ module TestTree
|
|
658
654
|
@root << @child1
|
659
655
|
@root << @child2
|
660
656
|
|
661
|
-
assert(@root.
|
657
|
+
assert(@root.children?, 'Should have children')
|
662
658
|
assert_equal(3, @root.size, 'Should have three nodes')
|
663
659
|
|
664
660
|
@root.remove!(@child1)
|
665
661
|
assert_equal(2, @root.size, 'Should have two nodes')
|
666
662
|
@root.remove!(@child2)
|
667
663
|
|
668
|
-
assert(!@root.
|
664
|
+
assert(!@root.children?, 'Should have no children')
|
669
665
|
assert_equal(1, @root.size, 'Should have one node')
|
670
666
|
|
671
667
|
@root << @child1
|
672
668
|
@root << @child2
|
673
669
|
|
674
|
-
assert(@root.
|
670
|
+
assert(@root.children?, 'Should have children')
|
675
671
|
assert_equal(3, @root.size, 'Should have three nodes')
|
676
672
|
|
677
673
|
@root.remove_all!
|
678
674
|
|
679
|
-
assert(!@root.
|
675
|
+
assert(!@root.children?, 'Should have no children')
|
680
676
|
assert_equal(1, @root.size, 'Should have one node')
|
681
677
|
|
682
678
|
# Some negative testing
|
683
679
|
@root.remove!(nil)
|
684
|
-
assert(!@root.
|
680
|
+
assert(!@root.children?, 'Should have no children')
|
685
681
|
assert_equal(1, @root.size, 'Should have one node')
|
686
682
|
end
|
687
683
|
|
688
684
|
# Test the remove_all! method.
|
689
685
|
def test_remove_all_bang
|
690
686
|
setup_test_tree
|
691
|
-
assert(@root.
|
687
|
+
assert(@root.children?, 'Should have children')
|
692
688
|
@root.remove_all!
|
693
689
|
|
694
|
-
assert(!@root.
|
690
|
+
assert(!@root.children?, 'Should have no children')
|
695
691
|
assert_equal(1, @root.size, 'Should have one node')
|
696
692
|
end
|
697
693
|
|
@@ -699,8 +695,8 @@ module TestTree
|
|
699
695
|
def test_remove_from_parent_bang
|
700
696
|
setup_test_tree
|
701
697
|
|
702
|
-
assert(@root.
|
703
|
-
assert(!@root.
|
698
|
+
assert(@root.children?, 'Should have children')
|
699
|
+
assert(!@root.leaf?, 'Root is not a leaf here')
|
704
700
|
|
705
701
|
child1 = @root[0]
|
706
702
|
assert_not_nil(child1, 'Child 1 should exist')
|
@@ -718,10 +714,10 @@ module TestTree
|
|
718
714
|
def test_children
|
719
715
|
setup_test_tree
|
720
716
|
|
721
|
-
assert(@root.
|
717
|
+
assert(@root.children?, 'Should have children')
|
722
718
|
assert_equal(5, @root.size, 'Should have five nodes')
|
723
|
-
assert(@child3.
|
724
|
-
assert(!@child3.
|
719
|
+
assert(@child3.children?, 'Should have children')
|
720
|
+
assert(!@child3.leaf?, 'Should not be a leaf')
|
725
721
|
|
726
722
|
assert_equal(1, @child3.node_height, 'The subtree at Child 3 should have a height of 1')
|
727
723
|
[@child1, @child2, @child4].each do |child|
|
@@ -795,9 +791,9 @@ module TestTree
|
|
795
791
|
def test_each
|
796
792
|
setup_test_tree
|
797
793
|
|
798
|
-
assert(@root.
|
794
|
+
assert(@root.children?, 'Should have children')
|
799
795
|
assert_equal(5, @root.size, 'Should have five nodes')
|
800
|
-
assert(@child3.
|
796
|
+
assert(@child3.children?, 'Should have children')
|
801
797
|
|
802
798
|
nodes = []
|
803
799
|
@root.each { |node| nodes << node }
|
@@ -882,14 +878,14 @@ module TestTree
|
|
882
878
|
# Test the root node
|
883
879
|
assert_equal(test_root.name, new_root.name, 'Must identify as ROOT')
|
884
880
|
assert_equal(test_root.content, new_root.content, "Must have root's content")
|
885
|
-
assert(new_root.
|
886
|
-
assert(new_root.
|
881
|
+
assert(new_root.root?, 'Must be the ROOT node')
|
882
|
+
assert(new_root.children?, 'Must have a child node')
|
887
883
|
|
888
884
|
# Test the child node
|
889
885
|
new_child = new_root[test_child.name]
|
890
886
|
assert_equal(test_child.name, new_child.name, 'Must have child 1')
|
891
|
-
assert(new_child.
|
892
|
-
assert(new_child.
|
887
|
+
assert(new_child.content?, 'Child must have content')
|
888
|
+
assert(new_child.only_child?, 'Child must be the only child')
|
893
889
|
|
894
890
|
new_child_content = new_child.content
|
895
891
|
assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
|
@@ -898,8 +894,8 @@ module TestTree
|
|
898
894
|
# Test the grand-child node
|
899
895
|
new_grand_child = new_child[test_grand_child.name]
|
900
896
|
assert_equal(test_grand_child.name, new_grand_child.name, 'Must have grand child')
|
901
|
-
assert(new_grand_child.
|
902
|
-
assert(new_grand_child.
|
897
|
+
assert(new_grand_child.content?, 'Grand-child must have content')
|
898
|
+
assert(new_grand_child.only_child?, 'Grand-child must be the only child')
|
903
899
|
|
904
900
|
new_grand_child_content = new_grand_child.content
|
905
901
|
assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
|
@@ -948,34 +944,6 @@ module TestTree
|
|
948
944
|
assert_same(person, @root.content, 'Content should be the same')
|
949
945
|
end
|
950
946
|
|
951
|
-
# Test the depth computation algorithm. Note that this is an incorrect computation and actually returns height+1
|
952
|
-
# instead of depth. This method has been deprecated in this release and may be removed in the future.
|
953
|
-
def test_depth
|
954
|
-
require 'structured_warnings'
|
955
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { do_deprecated_depth }
|
956
|
-
rescue LoadError
|
957
|
-
# Since the structured_warnings package is not present, we revert to good old Kernel#warn behavior.
|
958
|
-
do_deprecated_depth
|
959
|
-
end
|
960
|
-
|
961
|
-
# Run the assertions for the deprecated depth method.
|
962
|
-
def do_deprecated_depth
|
963
|
-
assert_equal(1, @root.depth, "A single node's depth is 1")
|
964
|
-
|
965
|
-
@root << @child1
|
966
|
-
assert_equal(2, @root.depth, 'This should be of depth 2')
|
967
|
-
|
968
|
-
@root << @child2
|
969
|
-
assert_equal(2, @root.depth, 'This should be of depth 2')
|
970
|
-
|
971
|
-
@child2 << @child3
|
972
|
-
assert_equal(3, @root.depth, 'This should be of depth 3')
|
973
|
-
assert_equal(2, @child2.depth, 'This should be of depth 2')
|
974
|
-
|
975
|
-
@child3 << @child4
|
976
|
-
assert_equal(4, @root.depth, 'This should be of depth 4')
|
977
|
-
end
|
978
|
-
|
979
947
|
# Test the height computation algorithm
|
980
948
|
def test_node_height
|
981
949
|
assert_equal(0, @root.node_height, "A single node's height is 0")
|
@@ -1000,8 +968,7 @@ module TestTree
|
|
1000
968
|
assert_equal(0, @child4.node_height, 'This should be of height 0')
|
1001
969
|
end
|
1002
970
|
|
1003
|
-
# Test the depth computation algorithm.
|
1004
|
-
# Tree::TreeNode#depth was incorrectly computing the height of the node - instead of its depth.
|
971
|
+
# Test the depth computation algorithm.
|
1005
972
|
def test_node_depth
|
1006
973
|
assert_equal(0, @root.node_depth, "A root node's depth is 0")
|
1007
974
|
|
@@ -1090,9 +1057,8 @@ module TestTree
|
|
1090
1057
|
end
|
1091
1058
|
|
1092
1059
|
assert_equal(Enumerator, j.breadth_each.class) if defined?(Enumerator.class) # Without a block
|
1093
|
-
|
1094
|
-
|
1095
|
-
end # Without a block
|
1060
|
+
# Without a block
|
1061
|
+
assert_equal(Enumerable::Enumerator, j.breadth_each.class) if defined?(Enumerable::Enumerator.class)
|
1096
1062
|
|
1097
1063
|
# Now test without a block
|
1098
1064
|
result_array = j.breadth_each.collect { |node| node }
|
@@ -1136,10 +1102,11 @@ module TestTree
|
|
1136
1102
|
assert_equal(expected_array[i].name, result_array[i].name)
|
1137
1103
|
end
|
1138
1104
|
|
1139
|
-
assert_equal(Enumerator, j.preordered_each.class)
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1105
|
+
assert_equal(Enumerator, j.preordered_each.class) \
|
1106
|
+
if defined?(Enumerator.class) # Without a block
|
1107
|
+
|
1108
|
+
assert_equal(Enumerable::Enumerator, j.preordered_each.class) \
|
1109
|
+
if defined?(Enumerable::Enumerator.class)
|
1143
1110
|
end
|
1144
1111
|
|
1145
1112
|
# Test the postordered_each method.
|
@@ -1179,9 +1146,9 @@ module TestTree
|
|
1179
1146
|
end
|
1180
1147
|
|
1181
1148
|
assert_equal(Enumerator, j.postordered_each.class) if defined?(Enumerator.class) # Without a block
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1149
|
+
|
1150
|
+
assert_equal(Enumerable::Enumerator, j.postordered_each.class) \
|
1151
|
+
if defined?(Enumerable::Enumerator.class)
|
1185
1152
|
|
1186
1153
|
# Now test without a block
|
1187
1154
|
result_array = j.postordered_each.collect { |node| node }
|
@@ -1196,75 +1163,75 @@ module TestTree
|
|
1196
1163
|
def test_detached_copy
|
1197
1164
|
setup_test_tree
|
1198
1165
|
|
1199
|
-
assert(@root.
|
1166
|
+
assert(@root.children?, 'The root should have children')
|
1200
1167
|
copy_of_root = @root.detached_copy
|
1201
|
-
assert(!copy_of_root.
|
1168
|
+
assert(!copy_of_root.children?, 'The copy should not have children')
|
1202
1169
|
assert_equal(@root.name, copy_of_root.name, 'The names should be equal')
|
1203
1170
|
|
1204
1171
|
# Try the same test with a child node
|
1205
|
-
assert(!@child3.
|
1206
|
-
assert(@child3.
|
1172
|
+
assert(!@child3.root?, 'Child 3 is not a root')
|
1173
|
+
assert(@child3.children?, 'Child 3 has children')
|
1207
1174
|
copy_of_child3 = @child3.detached_copy
|
1208
|
-
assert(copy_of_child3.
|
1209
|
-
assert(!copy_of_child3.
|
1175
|
+
assert(copy_of_child3.root?, "Child 3's copy is a root")
|
1176
|
+
assert(!copy_of_child3.children?, "Child 3's copy does not have children")
|
1210
1177
|
end
|
1211
1178
|
|
1212
1179
|
# Test the detached_subtree_copy method.
|
1213
1180
|
def test_detached_subtree_copy
|
1214
1181
|
setup_test_tree
|
1215
1182
|
|
1216
|
-
assert(@root.
|
1183
|
+
assert(@root.children?, 'The root should have children.')
|
1217
1184
|
tree_copy = @root.detached_subtree_copy
|
1218
1185
|
|
1219
1186
|
assert_equal(@root.name, tree_copy.name, 'The names should be equal.')
|
1220
1187
|
assert_not_equal(@root.object_id, tree_copy.object_id, 'Object_ids should differ.')
|
1221
|
-
assert(tree_copy.
|
1222
|
-
assert(tree_copy.
|
1188
|
+
assert(tree_copy.root?, 'Copied root should be a root node.')
|
1189
|
+
assert(tree_copy.children?, 'Copied tree should have children.')
|
1223
1190
|
assert_equal(tree_copy.children.count, @root.children.count,
|
1224
1191
|
'Copied tree and the original tree should have same number of children.')
|
1225
1192
|
|
1226
1193
|
assert_equal(tree_copy[0].name, @child1.name, 'The names of Child1 (original and copy) should be same.')
|
1227
1194
|
assert_not_equal(tree_copy[0].object_id, @child1.object_id,
|
1228
1195
|
'Child1 Object_ids (original and copy) should differ.')
|
1229
|
-
assert(!tree_copy[0].
|
1230
|
-
assert(!tree_copy[0].
|
1196
|
+
assert(!tree_copy[0].root?, 'Child1 copied should not be root.')
|
1197
|
+
assert(!tree_copy[0].children?, 'Child1 copied should not have children.')
|
1231
1198
|
|
1232
1199
|
assert_equal(tree_copy[1].name, @child2.name, 'The names of Child2 (original and copy) should be same.')
|
1233
1200
|
assert_not_equal(tree_copy[1].object_id, @child2.object_id,
|
1234
1201
|
'Child2 Object_ids (original and copy) should differ.')
|
1235
|
-
assert(!tree_copy[1].
|
1236
|
-
assert(!tree_copy[1].
|
1202
|
+
assert(!tree_copy[1].root?, 'Child2 copied should not be root.')
|
1203
|
+
assert(!tree_copy[1].children?, 'Child2 copied should not have children.')
|
1237
1204
|
|
1238
1205
|
assert_equal(tree_copy[2].name, @child3.name, 'The names of Child3 (original and copy) should be same.')
|
1239
1206
|
assert_not_equal(tree_copy[2].object_id, @child3.object_id,
|
1240
1207
|
'Child3 Object_ids (original and copy) should differ.')
|
1241
|
-
assert(!tree_copy[2].
|
1242
|
-
assert(tree_copy[2].
|
1208
|
+
assert(!tree_copy[2].root?, 'Child3 copied should not be root.')
|
1209
|
+
assert(tree_copy[2].children?, 'Child3 copied should have children.')
|
1243
1210
|
|
1244
1211
|
assert_equal(tree_copy[2][0].name, @child4.name, 'The names of Child4 (original and copy) should be same.')
|
1245
1212
|
assert_not_equal(tree_copy[2][0].object_id, @child4.object_id,
|
1246
1213
|
'Child4 Object_ids (original and copy) should differ.')
|
1247
|
-
assert(!tree_copy[2][0].
|
1248
|
-
assert(!tree_copy[2][0].
|
1214
|
+
assert(!tree_copy[2][0].root?, 'Child4 copied should not be root.')
|
1215
|
+
assert(!tree_copy[2][0].children?, 'Child4 copied should not have children.')
|
1249
1216
|
end
|
1250
1217
|
|
1251
|
-
# Test the
|
1218
|
+
# Test the children? method.
|
1252
1219
|
def test_has_children_eh
|
1253
1220
|
setup_test_tree
|
1254
|
-
assert(@root.
|
1221
|
+
assert(@root.children?, 'The Root node MUST have children')
|
1255
1222
|
end
|
1256
1223
|
|
1257
|
-
# test the
|
1224
|
+
# test the leaf? method.
|
1258
1225
|
def test_is_leaf_eh
|
1259
1226
|
setup_test_tree
|
1260
|
-
assert(!@child3.
|
1261
|
-
assert(@child4.
|
1227
|
+
assert(!@child3.leaf?, 'Child 3 is not a leaf node')
|
1228
|
+
assert(@child4.leaf?, 'Child 4 is a leaf node')
|
1262
1229
|
end
|
1263
1230
|
|
1264
|
-
# Test the
|
1231
|
+
# Test the root? method.
|
1265
1232
|
def test_is_root_eh
|
1266
1233
|
setup_test_tree
|
1267
|
-
assert(@root.
|
1234
|
+
assert(@root.root?, 'The ROOT node must respond as the root node')
|
1268
1235
|
end
|
1269
1236
|
|
1270
1237
|
# Test the content= method.
|
@@ -1285,7 +1252,7 @@ module TestTree
|
|
1285
1252
|
end
|
1286
1253
|
|
1287
1254
|
# Test the << method.
|
1288
|
-
def test_lt2
|
1255
|
+
def test_lt2
|
1289
1256
|
@root << @child1
|
1290
1257
|
@root << @child2
|
1291
1258
|
@root << @child3 << @child4
|
@@ -1296,7 +1263,7 @@ module TestTree
|
|
1296
1263
|
end
|
1297
1264
|
|
1298
1265
|
# Test the [] method.
|
1299
|
-
def test_index
|
1266
|
+
def test_index
|
1300
1267
|
assert_raise(ArgumentError) { @root[nil] }
|
1301
1268
|
|
1302
1269
|
@root << @child1
|
@@ -1304,7 +1271,8 @@ module TestTree
|
|
1304
1271
|
assert_equal(@child1.name, @root['Child1'].name, 'Child 1 should be returned')
|
1305
1272
|
assert_equal(@child1.name, @root[0].name, 'Child 1 should be returned')
|
1306
1273
|
assert_equal(@child1.name, @root[-2].name, 'Child 1 should be returned') # Negative access also works
|
1307
|
-
assert_equal(@child1.name,
|
1274
|
+
assert_equal(@child1.name,
|
1275
|
+
@root[-@root.children.size].name, 'Child 1 should be returned') # Negative access also works
|
1308
1276
|
|
1309
1277
|
assert_equal(@child2.name, @root['Child2'].name, 'Child 2 should be returned')
|
1310
1278
|
assert_equal(@child2.name, @root[1].name, 'Child 2 should be returned')
|
@@ -1407,46 +1375,12 @@ module TestTree
|
|
1407
1375
|
assert_equal(k[1].name, root_node[1].name, 'Child 2 should be returned')
|
1408
1376
|
end
|
1409
1377
|
|
1410
|
-
# Test the old CamelCase method names
|
1411
|
-
def test_old_camel_case_names
|
1412
|
-
setup_test_tree
|
1413
|
-
|
1414
|
-
meth_names_to_test = %w[isRoot? isLeaf? hasContent?
|
1415
|
-
hasChildren? firstChild lastChild
|
1416
|
-
firstSibling isFirstSibling? lastSibling isLastSibling?
|
1417
|
-
isOnlyChild? nextSibling previousSibling nodeHeight nodeDepth
|
1418
|
-
removeFromParent! removeAll! freezeTree! ]
|
1419
|
-
|
1420
|
-
require 'structured_warnings'
|
1421
|
-
|
1422
|
-
StructuredWarnings::DeprecatedMethodWarning.disable do
|
1423
|
-
# noinspection RubyResolve
|
1424
|
-
assert(@root.isRoot?) # Test if the original method is really called
|
1425
|
-
end
|
1426
|
-
|
1427
|
-
meth_names_to_test.each do |meth_name|
|
1428
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.send(meth_name) }
|
1429
|
-
end
|
1430
|
-
|
1431
|
-
# Special Case for printTree to avoid putting stuff on the STDOUT during the unit test.
|
1432
|
-
begin
|
1433
|
-
require 'stringio'
|
1434
|
-
$stdout = StringIO.new
|
1435
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.send('printTree') }
|
1436
|
-
ensure
|
1437
|
-
$stdout = STDOUT
|
1438
|
-
end
|
1439
|
-
end
|
1440
|
-
|
1441
1378
|
# Test usage of integers as node names
|
1442
1379
|
def test_integer_node_names
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
@n_child2 = Tree::TreeNode.new(2, 'Child Node 2')
|
1448
|
-
@n_child3 = Tree::TreeNode.new('three', 'Child Node 3')
|
1449
|
-
end
|
1380
|
+
@n_root = Tree::TreeNode.new(0, 'Root Node')
|
1381
|
+
@n_child1 = Tree::TreeNode.new(1, 'Child Node 1')
|
1382
|
+
@n_child2 = Tree::TreeNode.new(2, 'Child Node 2')
|
1383
|
+
@n_child3 = Tree::TreeNode.new('three', 'Child Node 3')
|
1450
1384
|
|
1451
1385
|
@n_root << @n_child1
|
1452
1386
|
@n_root << @n_child2
|
@@ -1454,18 +1388,11 @@ module TestTree
|
|
1454
1388
|
|
1455
1389
|
# Node[n] is really accessing the nth child with a zero-base
|
1456
1390
|
assert_not_equal(@n_root[1].name, 1) # This is really the second child
|
1457
|
-
assert_equal(@n_root[0].name, 1) # This will work, as it is the first child
|
1458
|
-
assert_equal(@n_root[1
|
1391
|
+
assert_equal(@n_root[0].name, '1') # This will work, as it is the first child
|
1392
|
+
assert_equal(@n_root[1].name, '2') # This will work, as the flag is now enabled
|
1459
1393
|
|
1460
1394
|
# Sanity check for the "normal" string name cases. Both cases should work.
|
1461
|
-
assert_equal(@n_root['three'
|
1462
|
-
|
1463
|
-
StructuredWarnings::StandardWarning.disable
|
1464
|
-
assert_equal(@n_root['three', true].name, 'three')
|
1465
|
-
|
1466
|
-
# Also ensure that the warning is actually being thrown
|
1467
|
-
StructuredWarnings::StandardWarning.enable
|
1468
|
-
assert_warn(StructuredWarnings::StandardWarning) { assert_equal(@n_root['three', true].name, 'three') }
|
1395
|
+
assert_equal(@n_root['three'].name, 'three')
|
1469
1396
|
end
|
1470
1397
|
|
1471
1398
|
# Test the addition of a node to itself as a child
|
@@ -1485,10 +1412,10 @@ module TestTree
|
|
1485
1412
|
setup_test_tree
|
1486
1413
|
|
1487
1414
|
leafs = @root.each_leaf
|
1488
|
-
parents = leafs.collect
|
1489
|
-
leafs.each
|
1415
|
+
parents = leafs.collect(&:parent)
|
1416
|
+
leafs.each(&:remove_from_parent!)
|
1490
1417
|
parents.each do |parent|
|
1491
|
-
assert(parent.
|
1418
|
+
assert(parent.leaf?) unless parent.children?
|
1492
1419
|
end
|
1493
1420
|
end
|
1494
1421
|
|