rubytree 0.5.1 → 0.5.2
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.
- data/ChangeLog +14 -0
- data/History.txt +9 -0
- data/Manifest.txt +1 -1
- data/README +1 -1
- data/Rakefile +1 -0
- data/lib/tree.rb +6 -5
- data/test/test_binarytree.rb +107 -102
- data/test/test_tree.rb +532 -471
- metadata +4 -4
data/ChangeLog
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
2007-12-21 Anupam Sengupta <anupamsg@gmail.com>
|
2
|
+
|
3
|
+
* Rakefile: Added the rcov option to exclude rcov itself from
|
4
|
+
coverage reports.
|
5
|
+
|
6
|
+
* lib/tree.rb: Minor comment changes.
|
7
|
+
|
8
|
+
* test/test_tree.rb: Added the TestTree enclosing module, and
|
9
|
+
renamed tests to meet ZenTest requirements. Also added a few
|
10
|
+
missing test cases.
|
11
|
+
|
12
|
+
* test/test_binarytree.rb: Added the TestTree enclosing Module,
|
13
|
+
and renamed the tests to meet ZenTest requirements.
|
14
|
+
|
1
15
|
2007-12-19 Anupam Sengupta <anupamsg@gmail.com>
|
2
16
|
|
3
17
|
* README (Module): Modified the install instructions from source.
|
data/History.txt
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
= 0.5.2 / 2007-12-21
|
2
|
+
|
3
|
+
* Added more test cases and enabled ZenTest compatibility for the test case
|
4
|
+
names.
|
5
|
+
|
6
|
+
= 0.5.1 / 2007-12-20
|
7
|
+
|
8
|
+
* Minor code refactoring.
|
9
|
+
|
1
10
|
= 0.5.0 / 2007-12-18
|
2
11
|
|
3
12
|
* Fixed the marshalling code to correctly handle non-string content.
|
data/Manifest.txt
CHANGED
data/README
CHANGED
@@ -144,4 +144,4 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
144
144
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
145
145
|
|
146
146
|
|
147
|
-
(Document Revision: $Revision: 1.
|
147
|
+
(Document Revision: $Revision: 1.16 $ by $Author: anupamsg $)
|
data/Rakefile
CHANGED
@@ -176,6 +176,7 @@ begin
|
|
176
176
|
require 'rcov/rcovtask'
|
177
177
|
Rcov::RcovTask.new do |t|
|
178
178
|
t.test_files = FileList['test/test*.rb']
|
179
|
+
t.rcov_opts << "--exclude 'rcov.rb'" # rcov itself should not be profiled
|
179
180
|
# t.verbose = true # uncomment to see the executed commands
|
180
181
|
end
|
181
182
|
rescue LoadError
|
data/lib/tree.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# tree.rb
|
2
2
|
#
|
3
|
-
# $Revision: 1.
|
3
|
+
# $Revision: 1.29 $ by $Author: anupamsg $
|
4
4
|
# $Name: $
|
5
5
|
#
|
6
6
|
# = tree.rb - Generic Multi-way Tree implementation
|
@@ -48,7 +48,7 @@
|
|
48
48
|
module Tree
|
49
49
|
|
50
50
|
# Rubytree Package Version
|
51
|
-
VERSION = '0.5.
|
51
|
+
VERSION = '0.5.2'
|
52
52
|
|
53
53
|
# == TreeNode Class Description
|
54
54
|
#
|
@@ -132,13 +132,11 @@ module Tree
|
|
132
132
|
|
133
133
|
# Print the string representation of this node.
|
134
134
|
def to_s
|
135
|
-
|
136
135
|
"Node Name: #{@name}" +
|
137
136
|
" Content: " + (@content || "<Empty>") +
|
138
137
|
" Parent: " + (isRoot?() ? "<None>" : @parent.name) +
|
139
138
|
" Children: #{@children.length}" +
|
140
139
|
" Total Nodes: #{size()}"
|
141
|
-
|
142
140
|
end
|
143
141
|
|
144
142
|
# Returns an array of ancestors in reversed order (the first element is the
|
@@ -452,7 +450,7 @@ module Tree
|
|
452
450
|
# This is the root node, hence initialize self.
|
453
451
|
initialize(name, content)
|
454
452
|
|
455
|
-
nodes[name] = self # Add self to the
|
453
|
+
nodes[name] = self # Add self to the list of nodes
|
456
454
|
end
|
457
455
|
end
|
458
456
|
end
|
@@ -477,6 +475,9 @@ module Tree
|
|
477
475
|
end
|
478
476
|
|
479
477
|
# $Log: tree.rb,v $
|
478
|
+
# Revision 1.29 2007/12/22 00:28:59 anupamsg
|
479
|
+
# Added more test cases, and enabled ZenTest compatibility.
|
480
|
+
#
|
480
481
|
# Revision 1.28 2007/12/20 03:19:33 anupamsg
|
481
482
|
# * README (Module): Modified the install instructions from source.
|
482
483
|
# (Module): Updated the minor version number.
|
data/test/test_binarytree.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# test_binarytree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.5 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -38,138 +38,143 @@
|
|
38
38
|
require 'test/unit'
|
39
39
|
require 'tree/binarytree'
|
40
40
|
|
41
|
-
|
42
|
-
class
|
41
|
+
module TestTree
|
42
|
+
# Test class for the Tree node.
|
43
|
+
class TestBinaryTreeNode < Test::Unit::TestCase
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
def setup
|
46
|
+
@root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
@left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
|
49
|
+
@right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
|
49
50
|
|
50
|
-
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
def teardown
|
54
|
+
@root.remove!(@left_child1)
|
55
|
+
@root.remove!(@right_child1)
|
56
|
+
@root = nil
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
def test_initialize
|
60
|
+
assert_not_nil(@root, "Binary tree's Root should have been created")
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
def test_add
|
64
|
+
@root.add @left_child1
|
65
|
+
assert_same(@left_child1, @root.leftChild, "The left node should be left_child1")
|
66
|
+
assert_same(@left_child1, @root.firstChild, "The first node should be left_child1")
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
@root.add @right_child1
|
69
|
+
assert_same(@right_child1, @root.rightChild, "The right node should be right_child1")
|
70
|
+
assert_same(@right_child1, @root.lastChild, "The first node should be right_child1")
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
assert_raise RuntimeError do
|
73
|
+
@root.add Tree::BinaryTreeNode.new("The third child!")
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
-
|
76
|
+
assert_raise RuntimeError do
|
77
|
+
@root << Tree::BinaryTreeNode.new("The third child!")
|
78
|
+
end
|
77
79
|
end
|
78
|
-
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
81
|
+
def test_leftChild
|
82
|
+
@root << @left_child1
|
83
|
+
@root << @right_child1
|
84
|
+
assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
|
85
|
+
assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child")
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
88
|
+
def test_rightChild
|
89
|
+
@root << @left_child1
|
90
|
+
@root << @right_child1
|
91
|
+
assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
|
92
|
+
assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child")
|
93
|
+
end
|
93
94
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
95
|
+
def test_leftChild_equals
|
96
|
+
@root << @left_child1
|
97
|
+
@root << @right_child1
|
98
|
+
assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
|
98
99
|
|
99
|
-
|
100
|
-
|
101
|
-
|
100
|
+
@root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
|
101
|
+
assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
|
102
|
+
assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
|
102
103
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
104
|
+
# Now set the left child as nil, and retest
|
105
|
+
@root.leftChild = nil
|
106
|
+
assert_nil(@root.leftChild, "The left child should now be nil")
|
107
|
+
assert_nil(@root.firstChild, "The first child is now nil")
|
108
|
+
assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
|
109
|
+
end
|
109
110
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
111
|
+
def test_rightChild_equals
|
112
|
+
@root << @left_child1
|
113
|
+
@root << @right_child1
|
114
|
+
assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
|
115
|
+
|
116
|
+
@root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
|
117
|
+
assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
|
118
|
+
assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
|
119
|
+
assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child")
|
120
|
+
|
121
|
+
# Now set the right child as nil, and retest
|
122
|
+
@root.rightChild = nil
|
123
|
+
assert_nil(@root.rightChild, "The right child should now be nil")
|
124
|
+
assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
|
125
|
+
assert_nil(@root.lastChild, "The first child is now nil")
|
126
|
+
end
|
126
127
|
|
127
|
-
|
128
|
-
|
129
|
-
|
128
|
+
def test_isLeftChild_eh
|
129
|
+
@root << @left_child1
|
130
|
+
@root << @right_child1
|
130
131
|
|
131
|
-
|
132
|
-
|
132
|
+
assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
133
|
+
assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
|
133
134
|
|
134
|
-
|
135
|
-
|
136
|
-
|
135
|
+
# Now set the right child as nil, and retest
|
136
|
+
@root.rightChild = nil
|
137
|
+
assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
137
138
|
|
138
|
-
|
139
|
-
|
139
|
+
assert(!@root.isLeftChild?, "Root is neither left child nor right")
|
140
|
+
end
|
140
141
|
|
141
|
-
|
142
|
-
|
143
|
-
|
142
|
+
def test_isRightChild_eh
|
143
|
+
@root << @left_child1
|
144
|
+
@root << @right_child1
|
144
145
|
|
145
|
-
|
146
|
-
|
146
|
+
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
147
|
+
assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
147
148
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
149
|
+
# Now set the left child as nil, and retest
|
150
|
+
@root.leftChild = nil
|
151
|
+
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
152
|
+
assert(!@root.isRightChild?, "Root is neither left child nor right")
|
153
|
+
end
|
153
154
|
|
154
|
-
|
155
|
-
|
156
|
-
|
155
|
+
def test_swap_children
|
156
|
+
@root << @left_child1
|
157
|
+
@root << @right_child1
|
157
158
|
|
158
|
-
|
159
|
-
|
159
|
+
assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
160
|
+
assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
160
161
|
|
161
|
-
|
162
|
-
assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
|
163
|
-
assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
|
164
|
-
assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
|
165
|
-
assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
|
166
|
-
assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
|
167
|
-
assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
|
162
|
+
@root.swap_children
|
168
163
|
|
164
|
+
assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
|
165
|
+
assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
|
166
|
+
assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
|
167
|
+
assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
|
168
|
+
assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
|
169
|
+
assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
|
170
|
+
end
|
169
171
|
end
|
170
172
|
end
|
171
173
|
|
172
174
|
# $Log: test_binarytree.rb,v $
|
175
|
+
# Revision 1.5 2007/12/22 00:28:59 anupamsg
|
176
|
+
# Added more test cases, and enabled ZenTest compatibility.
|
177
|
+
#
|
173
178
|
# Revision 1.4 2007/12/18 23:11:29 anupamsg
|
174
179
|
# Minor documentation changes in the binarytree class.
|
175
180
|
#
|
data/test/test_tree.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# testtree.rb
|
4
4
|
#
|
5
|
-
# $Revision: 1.
|
5
|
+
# $Revision: 1.6 $ by $Author: anupamsg $
|
6
6
|
# $Name: $
|
7
7
|
#
|
8
8
|
# Copyright (c) 2006, 2007 Anupam Sengupta
|
@@ -38,591 +38,652 @@
|
|
38
38
|
require 'test/unit'
|
39
39
|
require 'tree'
|
40
40
|
|
41
|
-
|
42
|
-
class
|
41
|
+
module TestTree
|
42
|
+
# Test class for the Tree node.
|
43
|
+
class TestTreeNode < Test::Unit::TestCase
|
43
44
|
|
44
|
-
|
45
|
+
Person = Struct::new(:First, :last)
|
45
46
|
|
46
|
-
|
47
|
-
|
47
|
+
def setup
|
48
|
+
@root = Tree::TreeNode.new("ROOT", "Root Node")
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
@child1 = Tree::TreeNode.new("Child1", "Child Node 1")
|
51
|
+
@child2 = Tree::TreeNode.new("Child2", "Child Node 2")
|
52
|
+
@child3 = Tree::TreeNode.new("Child3", "Child Node 3")
|
53
|
+
@child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
|
53
54
|
|
54
|
-
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
57
|
+
# Create this structure for the tests
|
58
|
+
#
|
59
|
+
# +----------+
|
60
|
+
# | ROOT |
|
61
|
+
# +-+--------+
|
62
|
+
# |
|
63
|
+
# | +---------------+
|
64
|
+
# +----+ CHILD1 |
|
65
|
+
# | +---------------+
|
66
|
+
# |
|
67
|
+
# | +---------------+
|
68
|
+
# +----+ CHILD2 |
|
69
|
+
# | +---------------+
|
70
|
+
# |
|
71
|
+
# | +---------------+ +------------------+
|
72
|
+
# +----+ CHILD3 +---+ CHILD4 |
|
73
|
+
# +---------------+ +------------------+
|
74
|
+
#
|
75
|
+
def loadChildren
|
76
|
+
@root << @child1
|
77
|
+
@root << @child2
|
78
|
+
@root << @child3 << @child4
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
81
|
+
def teardown
|
82
|
+
@root = nil
|
83
|
+
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
85
|
+
def test_root_setup
|
86
|
+
assert_not_nil(@root, "Root cannot be nil")
|
87
|
+
assert_nil(@root.parent, "Parent of root node should be nil")
|
88
|
+
assert_not_nil(@root.name, "Name should not be nil")
|
89
|
+
assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
|
90
|
+
assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
|
91
|
+
assert(@root.isRoot?, "Should identify as root")
|
92
|
+
assert(!@root.hasChildren?, "Cannot have any children")
|
93
|
+
assert(@root.hasContent?, "This root should have content")
|
94
|
+
assert_equal(1, @root.size, "Number of nodes should be one")
|
95
|
+
assert_nil(@root.siblings, "Root cannot have any children")
|
96
|
+
|
97
|
+
assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
|
98
|
+
end
|
98
99
|
|
99
|
-
|
100
|
-
|
100
|
+
def test_root
|
101
|
+
loadChildren
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
103
|
+
assert_same(@root, @root.root, "Root's root is self")
|
104
|
+
assert_same(@root, @child1.root, "Root should be ROOT")
|
105
|
+
assert_same(@root, @child4.root, "Root should be ROOT")
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
108
|
+
def test_hasContent_eh
|
109
|
+
aNode = Tree::TreeNode.new("A Node")
|
110
|
+
assert_nil(aNode.content, "The node should not have content")
|
111
|
+
assert(!aNode.hasContent?, "The node should not have content")
|
111
112
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
113
|
+
aNode.content = "Something"
|
114
|
+
assert_not_nil(aNode.content, "The node should now have content")
|
115
|
+
assert(aNode.hasContent?, "The node should now have content")
|
116
|
+
end
|
116
117
|
|
117
|
-
|
118
|
-
|
118
|
+
def test_length
|
119
|
+
loadChildren
|
120
|
+
assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
|
121
|
+
end
|
119
122
|
|
120
|
-
|
121
|
-
|
123
|
+
def test_spaceship # Test the <=> operator.
|
124
|
+
firstNode = Tree::TreeNode.new(1)
|
125
|
+
secondNode = Tree::TreeNode.new(2)
|
122
126
|
|
123
|
-
|
124
|
-
|
125
|
-
secondNode = Tree::TreeNode.new(2)
|
127
|
+
assert_equal(firstNode <=> nil, +1)
|
128
|
+
assert_equal(firstNode <=> secondNode, -1)
|
126
129
|
|
127
|
-
|
128
|
-
|
130
|
+
secondNode = Tree::TreeNode.new(1)
|
131
|
+
assert_equal(firstNode <=> secondNode, 0)
|
129
132
|
|
130
|
-
|
131
|
-
|
133
|
+
firstNode = Tree::TreeNode.new("ABC")
|
134
|
+
secondNode = Tree::TreeNode.new("XYZ")
|
132
135
|
|
133
|
-
|
134
|
-
|
136
|
+
assert_equal(firstNode <=> nil, +1)
|
137
|
+
assert_equal(firstNode <=> secondNode, -1)
|
135
138
|
|
136
|
-
|
137
|
-
|
139
|
+
secondNode = Tree::TreeNode.new("ABC")
|
140
|
+
assert_equal(firstNode <=> secondNode, 0)
|
141
|
+
end
|
138
142
|
|
139
|
-
|
140
|
-
|
141
|
-
end
|
143
|
+
def test_to_s
|
144
|
+
aNode = Tree::TreeNode.new("A Node", "Some Content")
|
142
145
|
|
143
|
-
|
144
|
-
aNode = Tree::TreeNode.new("A Node", "Some Content")
|
146
|
+
expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
|
145
147
|
|
146
|
-
|
148
|
+
assert_equal(expectedString, aNode.to_s, "The string representation should be same")
|
149
|
+
end
|
147
150
|
|
148
|
-
|
149
|
-
|
151
|
+
def test_firstSibling
|
152
|
+
loadChildren
|
150
153
|
|
151
|
-
|
152
|
-
|
154
|
+
assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
|
155
|
+
assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
|
156
|
+
assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
|
157
|
+
assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
|
158
|
+
assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
|
159
|
+
end
|
153
160
|
|
154
|
-
|
155
|
-
|
156
|
-
assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
|
157
|
-
assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
|
158
|
-
assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
|
159
|
-
end
|
161
|
+
def test_isFirstSibling_eh
|
162
|
+
loadChildren
|
160
163
|
|
161
|
-
|
162
|
-
|
164
|
+
assert(@root.isFirstSibling?, "Root's first sibling is itself")
|
165
|
+
assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
|
166
|
+
assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
|
167
|
+
assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
|
168
|
+
assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
|
169
|
+
end
|
163
170
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
171
|
+
def test_isLastSibling_eh
|
172
|
+
loadChildren
|
173
|
+
|
174
|
+
assert(@root.isLastSibling?, "Root's last sibling is itself")
|
175
|
+
assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
|
176
|
+
assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
|
177
|
+
assert( @child3.isLastSibling?, "Child3's last sibling is itself")
|
178
|
+
assert( @child4.isLastSibling?, "Child4's last sibling is itself")
|
179
|
+
end
|
170
180
|
|
171
|
-
|
172
|
-
|
181
|
+
def test_lastSibling
|
182
|
+
loadChildren
|
173
183
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
184
|
+
assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
|
185
|
+
assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
|
186
|
+
assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
|
187
|
+
assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
|
188
|
+
assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
|
189
|
+
end
|
180
190
|
|
181
|
-
|
182
|
-
|
191
|
+
def test_siblings
|
192
|
+
loadChildren
|
183
193
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
end
|
194
|
+
siblings = []
|
195
|
+
@child1.siblings { |sibling| siblings << sibling}
|
196
|
+
assert_equal(2, siblings.length, "Should have two siblings")
|
197
|
+
assert(siblings.include?(@child2), "Should have 2nd child as sibling")
|
198
|
+
assert(siblings.include?(@child3), "Should have 3rd child as sibling")
|
190
199
|
|
191
|
-
|
192
|
-
|
200
|
+
siblings.clear
|
201
|
+
siblings = @child1.siblings
|
202
|
+
assert_equal(2, siblings.length, "Should have two siblings")
|
193
203
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
assert(siblings.include?(@child2), "Should have 2nd child as sibling")
|
198
|
-
assert(siblings.include?(@child3), "Should have 3rd child as sibling")
|
204
|
+
siblings.clear
|
205
|
+
@child4.siblings {|sibling| siblings << sibling}
|
206
|
+
assert(siblings.empty?, "Should not have any children")
|
199
207
|
|
200
|
-
|
201
|
-
siblings = @child1.siblings
|
202
|
-
assert_equal(2, siblings.length, "Should have two siblings")
|
208
|
+
end
|
203
209
|
|
204
|
-
|
205
|
-
|
206
|
-
assert(siblings.empty?, "Should not have any children")
|
210
|
+
def test_isOnlyChild_eh
|
211
|
+
loadChildren
|
207
212
|
|
208
|
-
|
213
|
+
assert(!@child1.isOnlyChild?, "Child1 is not the only child")
|
214
|
+
assert(!@child2.isOnlyChild?, "Child2 is not the only child")
|
215
|
+
assert(!@child3.isOnlyChild?, "Child3 is not the only child")
|
216
|
+
assert( @child4.isOnlyChild?, "Child4 is not the only child")
|
217
|
+
end
|
209
218
|
|
210
|
-
|
211
|
-
|
219
|
+
def test_nextSibling
|
220
|
+
loadChildren
|
212
221
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
222
|
+
assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
|
223
|
+
assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
|
224
|
+
assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
|
225
|
+
assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
|
226
|
+
end
|
218
227
|
|
219
|
-
|
220
|
-
|
228
|
+
def test_previousSibling
|
229
|
+
loadChildren
|
221
230
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
231
|
+
assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
|
232
|
+
assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
|
233
|
+
assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
|
234
|
+
assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
|
235
|
+
end
|
227
236
|
|
228
|
-
|
229
|
-
|
237
|
+
def test_add
|
238
|
+
assert(!@root.hasChildren?, "Should not have any children")
|
230
239
|
|
231
|
-
|
232
|
-
assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
|
233
|
-
assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
|
234
|
-
assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
|
235
|
-
end
|
240
|
+
@root.add(@child1)
|
236
241
|
|
237
|
-
|
238
|
-
assert(!@root.hasChildren?, "Should not have any children")
|
242
|
+
@root << @child2
|
239
243
|
|
240
|
-
|
244
|
+
assert(@root.hasChildren?, "Should have children")
|
245
|
+
assert_equal(3, @root.size, "Should have three nodes")
|
241
246
|
|
242
|
-
|
247
|
+
@root << @child3 << @child4
|
243
248
|
|
244
|
-
|
245
|
-
|
249
|
+
assert_equal(5, @root.size, "Should have five nodes")
|
250
|
+
assert_equal(2, @child3.size, "Should have two nodes")
|
246
251
|
|
247
|
-
|
252
|
+
assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
|
248
253
|
|
249
|
-
|
250
|
-
assert_equal(2, @child3.size, "Should have two nodes")
|
254
|
+
end
|
251
255
|
|
252
|
-
|
256
|
+
def test_remove_bang
|
257
|
+
@root << @child1
|
258
|
+
@root << @child2
|
253
259
|
|
254
|
-
|
260
|
+
assert(@root.hasChildren?, "Should have children")
|
261
|
+
assert_equal(3, @root.size, "Should have three nodes")
|
255
262
|
|
256
|
-
|
257
|
-
|
258
|
-
|
263
|
+
@root.remove!(@child1)
|
264
|
+
assert_equal(2, @root.size, "Should have two nodes")
|
265
|
+
@root.remove!(@child2)
|
259
266
|
|
260
|
-
|
261
|
-
|
267
|
+
assert(!@root.hasChildren?, "Should have no children")
|
268
|
+
assert_equal(1, @root.size, "Should have one node")
|
262
269
|
|
263
|
-
|
264
|
-
|
265
|
-
@root.remove!(@child2)
|
270
|
+
@root << @child1
|
271
|
+
@root << @child2
|
266
272
|
|
267
|
-
|
268
|
-
|
273
|
+
assert(@root.hasChildren?, "Should have children")
|
274
|
+
assert_equal(3, @root.size, "Should have three nodes")
|
269
275
|
|
270
|
-
|
271
|
-
@root << @child2
|
276
|
+
@root.removeAll!
|
272
277
|
|
273
|
-
|
274
|
-
|
278
|
+
assert(!@root.hasChildren?, "Should have no children")
|
279
|
+
assert_equal(1, @root.size, "Should have one node")
|
275
280
|
|
276
|
-
|
281
|
+
end
|
277
282
|
|
278
|
-
|
279
|
-
|
283
|
+
def test_removeAll_bang
|
284
|
+
loadChildren
|
285
|
+
assert(@root.hasChildren?, "Should have children")
|
286
|
+
@root.removeAll!
|
280
287
|
|
281
|
-
|
288
|
+
assert(!@root.hasChildren?, "Should have no children")
|
289
|
+
assert_equal(1, @root.size, "Should have one node")
|
290
|
+
end
|
282
291
|
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
292
|
+
def test_removeFromParent_bang
|
293
|
+
loadChildren
|
294
|
+
assert(@root.hasChildren?, "Should have children")
|
295
|
+
assert(!@root.isLeaf?, "Root is not a leaf here")
|
296
|
+
|
297
|
+
child1 = @root[0]
|
298
|
+
assert_not_nil(child1, "Child 1 should exist")
|
299
|
+
assert_same(@root, child1.root, "Child 1's root should be ROOT")
|
300
|
+
assert(@root.include?(child1), "root should have child1")
|
301
|
+
child1.removeFromParent!
|
302
|
+
assert_same(child1, child1.root, "Child 1's root should be self")
|
303
|
+
assert(!@root.include?(child1), "root should not have child1")
|
304
|
+
|
305
|
+
child1.removeFromParent!
|
306
|
+
assert_same(child1, child1.root, "Child 1's root should still be self")
|
307
|
+
end
|
287
308
|
|
288
|
-
|
289
|
-
|
290
|
-
end
|
309
|
+
def test_children
|
310
|
+
loadChildren
|
291
311
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
child1.removeFromParent!
|
302
|
-
assert_same(child1, child1.root, "Child 1's root should be self")
|
303
|
-
assert(!@root.include?(child1), "root should not have child1")
|
304
|
-
|
305
|
-
child1.removeFromParent!
|
306
|
-
assert_same(child1, child1.root, "Child 1's root should still be self")
|
307
|
-
end
|
312
|
+
assert(@root.hasChildren?, "Should have children")
|
313
|
+
assert_equal(5, @root.size, "Should have four nodes")
|
314
|
+
assert(@child3.hasChildren?, "Should have children")
|
315
|
+
assert(!@child3.isLeaf?, "Should not be a leaf")
|
316
|
+
|
317
|
+
children = []
|
318
|
+
for child in @root.children
|
319
|
+
children << child
|
320
|
+
end
|
308
321
|
|
309
|
-
|
310
|
-
|
322
|
+
assert_equal(3, children.length, "Should have three direct children")
|
323
|
+
assert(!children.include?(@root), "Should not have root")
|
324
|
+
assert(children.include?(@child1), "Should have child 1")
|
325
|
+
assert(children.include?(@child2), "Should have child 2")
|
326
|
+
assert(children.include?(@child3), "Should have child 3")
|
327
|
+
assert(!children.include?(@child4), "Should not have child 4")
|
311
328
|
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
assert(!@child3.isLeaf?, "Should not be a leaf")
|
329
|
+
children.clear
|
330
|
+
children = @root.children
|
331
|
+
assert_equal(3, children.length, "Should have three children")
|
316
332
|
|
317
|
-
children = []
|
318
|
-
for child in @root.children
|
319
|
-
children << child
|
320
333
|
end
|
321
334
|
|
322
|
-
|
323
|
-
|
324
|
-
assert(children.include?(@child1), "Should have child 1")
|
325
|
-
assert(children.include?(@child2), "Should have child 2")
|
326
|
-
assert(children.include?(@child3), "Should have child 3")
|
327
|
-
assert(!children.include?(@child4), "Should not have child 4")
|
335
|
+
def test_firstChild
|
336
|
+
loadChildren
|
328
337
|
|
329
|
-
|
330
|
-
|
331
|
-
|
338
|
+
assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
|
339
|
+
assert_nil(@child1.firstChild, "Child1 does not have any children")
|
340
|
+
assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
|
332
341
|
|
333
|
-
|
342
|
+
end
|
334
343
|
|
335
|
-
|
336
|
-
|
344
|
+
def test_lastChild
|
345
|
+
loadChildren
|
337
346
|
|
338
|
-
|
339
|
-
|
340
|
-
|
347
|
+
assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
|
348
|
+
assert_nil(@child1.lastChild, "Child1 does not have any children")
|
349
|
+
assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
|
341
350
|
|
342
|
-
|
351
|
+
end
|
343
352
|
|
344
|
-
|
345
|
-
|
353
|
+
def test_find
|
354
|
+
loadChildren
|
355
|
+
foundNode = @root.find { |node| node == @child2}
|
356
|
+
assert_same(@child2, foundNode, "The node should be Child 2")
|
346
357
|
|
347
|
-
|
348
|
-
|
349
|
-
assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
|
358
|
+
foundNode = @root.find { |node| node == @child4}
|
359
|
+
assert_same(@child4, foundNode, "The node should be Child 4")
|
350
360
|
|
351
|
-
|
361
|
+
foundNode = @root.find { |node| node.name == "Child31" }
|
362
|
+
assert_same(@child4, foundNode, "The node should be Child 4")
|
363
|
+
foundNode = @root.find { |node| node.name == "NOT PRESENT" }
|
364
|
+
assert_nil(foundNode, "The node should not be found")
|
365
|
+
end
|
352
366
|
|
353
|
-
|
354
|
-
|
355
|
-
foundNode = @root.find { |node| node == @child2}
|
356
|
-
assert_same(@child2, foundNode, "The node should be Child 2")
|
367
|
+
def test_parentage
|
368
|
+
loadChildren
|
357
369
|
|
358
|
-
|
359
|
-
|
370
|
+
assert_nil(@root.parentage, "Root does not have any parentage")
|
371
|
+
assert_equal([@root], @child1.parentage, "Child1 has Root as its parent")
|
372
|
+
assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors")
|
373
|
+
end
|
360
374
|
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
375
|
+
def test_each
|
376
|
+
loadChildren
|
377
|
+
assert(@root.hasChildren?, "Should have children")
|
378
|
+
assert_equal(5, @root.size, "Should have five nodes")
|
379
|
+
assert(@child3.hasChildren?, "Should have children")
|
380
|
+
|
381
|
+
nodes = []
|
382
|
+
@root.each { |node| nodes << node }
|
383
|
+
|
384
|
+
assert_equal(5, nodes.length, "Should have FIVE NODES")
|
385
|
+
assert(nodes.include?(@root), "Should have root")
|
386
|
+
assert(nodes.include?(@child1), "Should have child 1")
|
387
|
+
assert(nodes.include?(@child2), "Should have child 2")
|
388
|
+
assert(nodes.include?(@child3), "Should have child 3")
|
389
|
+
assert(nodes.include?(@child4), "Should have child 4")
|
390
|
+
end
|
366
391
|
|
367
|
-
|
368
|
-
|
392
|
+
def test_each_leaf
|
393
|
+
loadChildren
|
369
394
|
|
370
|
-
|
371
|
-
|
372
|
-
assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors")
|
373
|
-
end
|
395
|
+
nodes = []
|
396
|
+
@root.each_leaf { |node| nodes << node }
|
374
397
|
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
@root.each { |node| nodes << node }
|
383
|
-
|
384
|
-
assert_equal(5, nodes.length, "Should have FIVE NODES")
|
385
|
-
assert(nodes.include?(@root), "Should have root")
|
386
|
-
assert(nodes.include?(@child1), "Should have child 1")
|
387
|
-
assert(nodes.include?(@child2), "Should have child 2")
|
388
|
-
assert(nodes.include?(@child3), "Should have child 3")
|
389
|
-
assert(nodes.include?(@child4), "Should have child 4")
|
390
|
-
end
|
398
|
+
assert_equal(3, nodes.length, "Should have THREE LEAF NODES")
|
399
|
+
assert(!nodes.include?(@root), "Should not have root")
|
400
|
+
assert(nodes.include?(@child1), "Should have child 1")
|
401
|
+
assert(nodes.include?(@child2), "Should have child 2")
|
402
|
+
assert(!nodes.include?(@child3), "Should not have child 3")
|
403
|
+
assert(nodes.include?(@child4), "Should have child 4")
|
404
|
+
end
|
391
405
|
|
392
|
-
|
393
|
-
|
406
|
+
def test_parent
|
407
|
+
loadChildren
|
408
|
+
assert_nil(@root.parent, "Root's parent should be nil")
|
409
|
+
assert_equal(@root, @child1.parent, "Parent should be root")
|
410
|
+
assert_equal(@root, @child3.parent, "Parent should be root")
|
411
|
+
assert_equal(@child3, @child4.parent, "Parent should be child3")
|
412
|
+
assert_equal(@root, @child4.parent.parent, "Parent should be root")
|
413
|
+
end
|
394
414
|
|
395
|
-
|
396
|
-
|
415
|
+
def test_indexed_access
|
416
|
+
loadChildren
|
417
|
+
assert_equal(@child1, @root[0], "Should be the first child")
|
418
|
+
assert_equal(@child4, @root[2][0], "Should be the grandchild")
|
419
|
+
assert_nil(@root["TEST"], "Should be nil")
|
420
|
+
assert_raise(RuntimeError) { @root[nil] }
|
421
|
+
end
|
397
422
|
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
assert(nodes.include?(@child4), "Should have child 4")
|
404
|
-
end
|
423
|
+
def test_printTree
|
424
|
+
loadChildren
|
425
|
+
#puts
|
426
|
+
#@root.printTree
|
427
|
+
end
|
405
428
|
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
429
|
+
# Tests the binary dumping mechanism with an Object content node
|
430
|
+
def test_marshal_dump
|
431
|
+
# Setup Test Data
|
432
|
+
test_root = Tree::TreeNode.new("ROOT", "Root Node")
|
433
|
+
test_content = {"KEY1" => "Value1", "KEY2" => "Value2" }
|
434
|
+
test_child = Tree::TreeNode.new("Child", test_content)
|
435
|
+
test_content2 = ["AValue1", "AValue2", "AValue3"]
|
436
|
+
test_grand_child = Tree::TreeNode.new("Grand Child 1", test_content2)
|
437
|
+
test_root << test_child << test_grand_child
|
438
|
+
|
439
|
+
# Perform the test operation
|
440
|
+
data = Marshal.dump(test_root) # Marshal
|
441
|
+
new_root = Marshal.load(data) # And unmarshal
|
442
|
+
|
443
|
+
# Test the root node
|
444
|
+
assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
|
445
|
+
assert_equal(test_root.content, new_root.content, "Must have root's content")
|
446
|
+
assert(new_root.isRoot?, "Must be the ROOT node")
|
447
|
+
assert(new_root.hasChildren?, "Must have a child node")
|
448
|
+
|
449
|
+
# Test the child node
|
450
|
+
new_child = new_root[test_child.name]
|
451
|
+
assert_equal(test_child.name, new_child.name, "Must have child 1")
|
452
|
+
assert(new_child.hasContent?, "Child must have content")
|
453
|
+
assert(new_child.isOnlyChild?, "Child must be the only child")
|
454
|
+
|
455
|
+
new_child_content = new_child.content
|
456
|
+
assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
|
457
|
+
assert_equal(test_child.content.size, new_child_content.size, "The content should have same size")
|
458
|
+
|
459
|
+
# Test the grand-child node
|
460
|
+
new_grand_child = new_child[test_grand_child.name]
|
461
|
+
assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
|
462
|
+
assert(new_grand_child.hasContent?, "Grand-child must have content")
|
463
|
+
assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
|
464
|
+
|
465
|
+
new_grand_child_content = new_grand_child.content
|
466
|
+
assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
|
467
|
+
assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size")
|
468
|
+
end
|
414
469
|
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
470
|
+
# marshal_load and marshal_dump are symmetric methods
|
471
|
+
# This alias is for satisfying ZenTest
|
472
|
+
alias test_marshal_load test_marshal_dump
|
473
|
+
|
474
|
+
# Test the collect method from the mixed-in Enumerable functionality.
|
475
|
+
def test_collect
|
476
|
+
loadChildren
|
477
|
+
collectArray = @root.collect do |node|
|
478
|
+
node.content = "abc"
|
479
|
+
node
|
480
|
+
end
|
481
|
+
collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
|
482
|
+
end
|
422
483
|
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
484
|
+
# Test freezing the tree
|
485
|
+
def test_freezeTree_bang
|
486
|
+
loadChildren
|
487
|
+
@root.content = "ABC"
|
488
|
+
assert_equal("ABC", @root.content, "Content should be 'ABC'")
|
489
|
+
@root.freezeTree!
|
490
|
+
assert_raise(TypeError) {@root.content = "123"}
|
491
|
+
assert_raise(TypeError) {@root[0].content = "123"}
|
492
|
+
end
|
428
493
|
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
# Perform the test operation
|
440
|
-
data = Marshal.dump(test_root) # Marshal
|
441
|
-
new_root = Marshal.load(data) # And unmarshal
|
442
|
-
|
443
|
-
# Test the root node
|
444
|
-
assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
|
445
|
-
assert_equal(test_root.content, new_root.content, "Must have root's content")
|
446
|
-
assert(new_root.isRoot?, "Must be the ROOT node")
|
447
|
-
assert(new_root.hasChildren?, "Must have a child node")
|
448
|
-
|
449
|
-
# Test the child node
|
450
|
-
new_child = new_root[test_child.name]
|
451
|
-
assert_equal(test_child.name, new_child.name, "Must have child 1")
|
452
|
-
assert(new_child.hasContent?, "Child must have content")
|
453
|
-
assert(new_child.isOnlyChild?, "Child must be the only child")
|
454
|
-
|
455
|
-
new_child_content = new_child.content
|
456
|
-
assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
|
457
|
-
assert_equal(test_child.content.size, new_child_content.size, "The content should have same size")
|
458
|
-
|
459
|
-
# Test the grand-child node
|
460
|
-
new_grand_child = new_child[test_grand_child.name]
|
461
|
-
assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
|
462
|
-
assert(new_grand_child.hasContent?, "Grand-child must have content")
|
463
|
-
assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
|
464
|
-
|
465
|
-
new_grand_child_content = new_grand_child.content
|
466
|
-
assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
|
467
|
-
assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size")
|
468
|
-
end
|
494
|
+
# Test whether the content is accesible
|
495
|
+
def test_content
|
496
|
+
pers = Person::new("John", "Doe")
|
497
|
+
@root.content = pers
|
498
|
+
assert_same(pers, @root.content, "Content should be the same")
|
499
|
+
end
|
500
|
+
|
501
|
+
# Test the depth computation algorithm
|
502
|
+
def test_depth
|
503
|
+
assert_equal(1, @root.depth, "A single node's depth is 1")
|
469
504
|
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
505
|
+
@root << @child1
|
506
|
+
assert_equal(2, @root.depth, "This should be of depth 2")
|
507
|
+
|
508
|
+
@root << @child2
|
509
|
+
assert_equal(2, @root.depth, "This should be of depth 2")
|
510
|
+
|
511
|
+
@child2 << @child3
|
512
|
+
assert_equal(3, @root.depth, "This should be of depth 3")
|
513
|
+
assert_equal(2, @child2.depth, "This should be of depth 2")
|
514
|
+
|
515
|
+
@child3 << @child4
|
516
|
+
assert_equal(4, @root.depth, "This should be of depth 4")
|
476
517
|
end
|
477
|
-
collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
|
478
|
-
end
|
479
518
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
@root.content = "ABC"
|
484
|
-
assert_equal("ABC", @root.content, "Content should be 'ABC'")
|
485
|
-
@root.freezeTree!
|
486
|
-
assert_raise(TypeError) {@root.content = "123"}
|
487
|
-
assert_raise(TypeError) {@root[0].content = "123"}
|
488
|
-
end
|
519
|
+
# Test the breadth computation algorithm
|
520
|
+
def test_breadth
|
521
|
+
assert_equal(1, @root.breadth, "A single node's breadth is 1")
|
489
522
|
|
490
|
-
|
491
|
-
|
492
|
-
pers = Person::new("John", "Doe")
|
493
|
-
@root.content = pers
|
494
|
-
assert_same(pers, @root.content, "Content should be the same")
|
495
|
-
end
|
523
|
+
@root << @child1
|
524
|
+
assert_equal(1, @root.breadth, "This should be of breadth 1")
|
496
525
|
|
497
|
-
|
498
|
-
|
499
|
-
|
526
|
+
@root << @child2
|
527
|
+
assert_equal(2, @child1.breadth, "This should be of breadth 2")
|
528
|
+
assert_equal(2, @child2.breadth, "This should be of breadth 2")
|
500
529
|
|
501
|
-
|
502
|
-
|
530
|
+
@root << @child3
|
531
|
+
assert_equal(3, @child1.breadth, "This should be of breadth 3")
|
532
|
+
assert_equal(3, @child2.breadth, "This should be of breadth 3")
|
503
533
|
|
504
|
-
|
505
|
-
|
534
|
+
@child3 << @child4
|
535
|
+
assert_equal(1, @child4.breadth, "This should be of breadth 1")
|
536
|
+
end
|
506
537
|
|
507
|
-
|
508
|
-
|
509
|
-
|
538
|
+
# Test the breadth for each
|
539
|
+
def test_breadth_each
|
540
|
+
j = Tree::TreeNode.new("j")
|
541
|
+
f = Tree::TreeNode.new("f")
|
542
|
+
k = Tree::TreeNode.new("k")
|
543
|
+
a = Tree::TreeNode.new("a")
|
544
|
+
d = Tree::TreeNode.new("d")
|
545
|
+
h = Tree::TreeNode.new("h")
|
546
|
+
z = Tree::TreeNode.new("z")
|
547
|
+
|
548
|
+
# The expected order of response
|
549
|
+
expected_array = [j,
|
550
|
+
f, k,
|
551
|
+
a, h, z,
|
552
|
+
d]
|
553
|
+
|
554
|
+
# Create the following Tree
|
555
|
+
# j <-- level 0 (Root)
|
556
|
+
# / \
|
557
|
+
# f k <-- level 1
|
558
|
+
# / \ \
|
559
|
+
# a h z <-- level 2
|
560
|
+
# \
|
561
|
+
# d <-- level 3
|
562
|
+
j << f << a << d
|
563
|
+
f << h
|
564
|
+
j << k << z
|
565
|
+
|
566
|
+
# Create the response
|
567
|
+
result_array = Array.new
|
568
|
+
j.breadth_each { |node| result_array << node.detached_copy }
|
569
|
+
|
570
|
+
expected_array.each_index do |i|
|
571
|
+
assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
|
572
|
+
end
|
573
|
+
end
|
510
574
|
|
511
|
-
@child3 << @child4
|
512
|
-
assert_equal(4, @root.depth, "This should be of depth 4")
|
513
|
-
end
|
514
575
|
|
515
|
-
|
516
|
-
|
517
|
-
|
576
|
+
def test_preordered_each
|
577
|
+
j = Tree::TreeNode.new("j")
|
578
|
+
f = Tree::TreeNode.new("f")
|
579
|
+
k = Tree::TreeNode.new("k")
|
580
|
+
a = Tree::TreeNode.new("a")
|
581
|
+
d = Tree::TreeNode.new("d")
|
582
|
+
h = Tree::TreeNode.new("h")
|
583
|
+
z = Tree::TreeNode.new("z")
|
584
|
+
|
585
|
+
# The expected order of response
|
586
|
+
expected_array = [j, f, a, d, h, k, z]
|
587
|
+
|
588
|
+
# Create the following Tree
|
589
|
+
# j <-- level 0 (Root)
|
590
|
+
# / \
|
591
|
+
# f k <-- level 1
|
592
|
+
# / \ \
|
593
|
+
# a h z <-- level 2
|
594
|
+
# \
|
595
|
+
# d <-- level 3
|
596
|
+
j << f << a << d
|
597
|
+
f << h
|
598
|
+
j << k << z
|
599
|
+
|
600
|
+
result_array = []
|
601
|
+
j.preordered_each { |node| result_array << node.detached_copy}
|
602
|
+
|
603
|
+
expected_array.each_index do |i|
|
604
|
+
# Match only the names.
|
605
|
+
assert_equal(expected_array[i].name, result_array[i].name)
|
606
|
+
end
|
607
|
+
end
|
518
608
|
|
519
|
-
|
520
|
-
|
609
|
+
def test_detached_copy
|
610
|
+
loadChildren
|
521
611
|
|
522
|
-
|
523
|
-
|
524
|
-
|
612
|
+
assert(@root.hasChildren?, "The root should have children")
|
613
|
+
copy_of_root = @root.detached_copy
|
614
|
+
assert(!copy_of_root.hasChildren?, "The copy should not have children")
|
615
|
+
assert_equal(@root.name, copy_of_root.name, "The names should be equal")
|
525
616
|
|
526
|
-
|
527
|
-
|
528
|
-
|
617
|
+
# Try the same test with a child node
|
618
|
+
assert(!@child3.isRoot?, "Child 3 is not a root")
|
619
|
+
assert(@child3.hasChildren?, "Child 3 has children")
|
620
|
+
copy_of_child3 = @child3.detached_copy
|
621
|
+
assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
|
622
|
+
assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
|
623
|
+
end
|
529
624
|
|
530
|
-
|
531
|
-
|
532
|
-
|
625
|
+
def test_hasChildren_eh
|
626
|
+
loadChildren
|
627
|
+
assert(@root.hasChildren?, "The Root node MUST have children")
|
628
|
+
end
|
533
629
|
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
k = Tree::TreeNode.new("k")
|
539
|
-
a = Tree::TreeNode.new("a")
|
540
|
-
d = Tree::TreeNode.new("d")
|
541
|
-
h = Tree::TreeNode.new("h")
|
542
|
-
z = Tree::TreeNode.new("z")
|
543
|
-
|
544
|
-
# The expected order of response
|
545
|
-
expected_array = [j,
|
546
|
-
f, k,
|
547
|
-
a, h, z,
|
548
|
-
d]
|
549
|
-
|
550
|
-
# Create the following Tree
|
551
|
-
# j <-- level 0 (Root)
|
552
|
-
# / \
|
553
|
-
# f k <-- level 1
|
554
|
-
# / \ \
|
555
|
-
# a h z <-- level 2
|
556
|
-
# \
|
557
|
-
# d <-- level 3
|
558
|
-
j << f << a << d
|
559
|
-
f << h
|
560
|
-
j << k << z
|
561
|
-
|
562
|
-
# Create the response
|
563
|
-
result_array = Array.new
|
564
|
-
j.breadth_each { |node| result_array << node.detached_copy }
|
565
|
-
|
566
|
-
expected_array.each_index do |i|
|
567
|
-
assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
|
630
|
+
def test_isLeaf_eh
|
631
|
+
loadChildren
|
632
|
+
assert(!@child3.isLeaf?, "Child 3 is not a leaf node")
|
633
|
+
assert(@child4.isLeaf?, "Child 4 is a leaf node")
|
568
634
|
end
|
569
|
-
end
|
570
635
|
|
636
|
+
def test_isRoot_eh
|
637
|
+
loadChildren
|
638
|
+
assert(@root.isRoot?, "The ROOT node must respond as the root node")
|
639
|
+
end
|
571
640
|
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
d = Tree::TreeNode.new("d")
|
578
|
-
h = Tree::TreeNode.new("h")
|
579
|
-
z = Tree::TreeNode.new("z")
|
580
|
-
|
581
|
-
# The expected order of response
|
582
|
-
expected_array = [j, f, a, d, h, k, z]
|
583
|
-
|
584
|
-
# Create the following Tree
|
585
|
-
# j <-- level 0 (Root)
|
586
|
-
# / \
|
587
|
-
# f k <-- level 1
|
588
|
-
# / \ \
|
589
|
-
# a h z <-- level 2
|
590
|
-
# \
|
591
|
-
# d <-- level 3
|
592
|
-
j << f << a << d
|
593
|
-
f << h
|
594
|
-
j << k << z
|
595
|
-
|
596
|
-
result_array = []
|
597
|
-
j.preordered_each { |node| result_array << node.detached_copy}
|
598
|
-
|
599
|
-
expected_array.each_index do |i|
|
600
|
-
# Match only the names.
|
601
|
-
assert_equal(expected_array[i].name, result_array[i].name)
|
641
|
+
def test_content_equals
|
642
|
+
@root.content = nil
|
643
|
+
assert_nil(@root.content, "Root's content should be nil")
|
644
|
+
@root.content = "ABCD"
|
645
|
+
assert_equal("ABCD", @root.content, "Root's content should now be 'ABCD'")
|
602
646
|
end
|
603
|
-
end
|
604
647
|
|
605
|
-
|
606
|
-
|
648
|
+
def test_size
|
649
|
+
assert_equal(1, @root.size, "Root's size should be 1")
|
650
|
+
loadChildren
|
651
|
+
assert_equal(5, @root.size, "Root's size should be 5")
|
652
|
+
assert_equal(2, @child3.size, "Child 3's size should be 2")
|
653
|
+
end
|
607
654
|
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
655
|
+
def test_lt2 # Test the << method
|
656
|
+
@root << @child1
|
657
|
+
@root << @child2
|
658
|
+
@root << @child3 << @child4
|
659
|
+
assert_not_nil(@root['Child1'], "Child 1 should have been added to Root")
|
660
|
+
assert_not_nil(@root['Child2'], "Child 2 should have been added to Root")
|
661
|
+
assert_not_nil(@root['Child3'], "Child 3 should have been added to Root")
|
662
|
+
assert_not_nil(@child3['Child31'], "Child 31 should have been added to Child3")
|
663
|
+
end
|
612
664
|
|
613
|
-
#
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
665
|
+
def test_index # Test the [] method
|
666
|
+
assert_raise(RuntimeError) {@root[nil]}
|
667
|
+
|
668
|
+
@root << @child1
|
669
|
+
@root << @child2
|
670
|
+
assert_equal(@child1.name, @root['Child1'].name, "Child 1 should be returned")
|
671
|
+
assert_equal(@child1.name, @root[0].name, "Child 1 should be returned")
|
672
|
+
assert_equal(@child2.name, @root['Child2'].name, "Child 2 should be returned")
|
673
|
+
assert_equal(@child2.name, @root[1].name, "Child 2 should be returned")
|
620
674
|
|
675
|
+
assert_nil(@root['Some Random Name'], "Should return nil")
|
676
|
+
assert_nil(@root[99], "Should return nil")
|
677
|
+
end
|
678
|
+
end
|
621
679
|
end
|
622
680
|
|
623
681
|
__END__
|
624
682
|
|
625
683
|
# $Log: test_tree.rb,v $
|
684
|
+
# Revision 1.6 2007/12/22 00:28:59 anupamsg
|
685
|
+
# Added more test cases, and enabled ZenTest compatibility.
|
686
|
+
#
|
626
687
|
# Revision 1.5 2007/12/19 02:24:18 anupamsg
|
627
688
|
# Updated the marshalling logic to handle non-string contents on the nodes.
|
628
689
|
#
|