Chris@0
|
1 #!/usr/bin/env ruby
|
Chris@0
|
2
|
Chris@0
|
3 # test_binarytree.rb
|
Chris@0
|
4 #
|
Chris@0
|
5 # $Revision: 1.5 $ by $Author: anupamsg $
|
Chris@0
|
6 # $Name: $
|
Chris@0
|
7 #
|
Chris@0
|
8 # Copyright (c) 2006, 2007 Anupam Sengupta
|
Chris@0
|
9 #
|
Chris@0
|
10 # All rights reserved.
|
Chris@0
|
11 #
|
Chris@0
|
12 # Redistribution and use in source and binary forms, with or without modification,
|
Chris@0
|
13 # are permitted provided that the following conditions are met:
|
Chris@0
|
14 #
|
Chris@0
|
15 # - Redistributions of source code must retain the above copyright notice, this
|
Chris@0
|
16 # list of conditions and the following disclaimer.
|
Chris@0
|
17 #
|
Chris@0
|
18 # - Redistributions in binary form must reproduce the above copyright notice, this
|
Chris@0
|
19 # list of conditions and the following disclaimer in the documentation and/or
|
Chris@0
|
20 # other materials provided with the distribution.
|
Chris@0
|
21 #
|
Chris@0
|
22 # - Neither the name of the organization nor the names of its contributors may
|
Chris@0
|
23 # be used to endorse or promote products derived from this software without
|
Chris@0
|
24 # specific prior written permission.
|
Chris@0
|
25 #
|
Chris@0
|
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
Chris@0
|
27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
Chris@0
|
28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
Chris@0
|
29 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
Chris@0
|
30 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
Chris@0
|
31 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
Chris@0
|
32 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
Chris@0
|
33 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
Chris@0
|
35 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
36 #
|
Chris@0
|
37
|
Chris@0
|
38 require 'test/unit'
|
Chris@0
|
39 require 'tree/binarytree'
|
Chris@0
|
40
|
Chris@0
|
41 module TestTree
|
Chris@0
|
42 # Test class for the Tree node.
|
Chris@0
|
43 class TestBinaryTreeNode < Test::Unit::TestCase
|
Chris@0
|
44
|
Chris@0
|
45 def setup
|
Chris@0
|
46 @root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
|
Chris@0
|
47
|
Chris@0
|
48 @left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
|
Chris@0
|
49 @right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
|
Chris@0
|
50
|
Chris@0
|
51 end
|
Chris@0
|
52
|
Chris@0
|
53 def teardown
|
Chris@0
|
54 @root.remove!(@left_child1)
|
Chris@0
|
55 @root.remove!(@right_child1)
|
Chris@0
|
56 @root = nil
|
Chris@0
|
57 end
|
Chris@0
|
58
|
Chris@0
|
59 def test_initialize
|
Chris@0
|
60 assert_not_nil(@root, "Binary tree's Root should have been created")
|
Chris@0
|
61 end
|
Chris@0
|
62
|
Chris@0
|
63 def test_add
|
Chris@0
|
64 @root.add @left_child1
|
Chris@0
|
65 assert_same(@left_child1, @root.leftChild, "The left node should be left_child1")
|
Chris@0
|
66 assert_same(@left_child1, @root.firstChild, "The first node should be left_child1")
|
Chris@0
|
67
|
Chris@0
|
68 @root.add @right_child1
|
Chris@0
|
69 assert_same(@right_child1, @root.rightChild, "The right node should be right_child1")
|
Chris@0
|
70 assert_same(@right_child1, @root.lastChild, "The first node should be right_child1")
|
Chris@0
|
71
|
Chris@0
|
72 assert_raise RuntimeError do
|
Chris@0
|
73 @root.add Tree::BinaryTreeNode.new("The third child!")
|
Chris@0
|
74 end
|
Chris@0
|
75
|
Chris@0
|
76 assert_raise RuntimeError do
|
Chris@0
|
77 @root << Tree::BinaryTreeNode.new("The third child!")
|
Chris@0
|
78 end
|
Chris@0
|
79 end
|
Chris@0
|
80
|
Chris@0
|
81 def test_leftChild
|
Chris@0
|
82 @root << @left_child1
|
Chris@0
|
83 @root << @right_child1
|
Chris@0
|
84 assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
|
Chris@0
|
85 assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child")
|
Chris@0
|
86 end
|
Chris@0
|
87
|
Chris@0
|
88 def test_rightChild
|
Chris@0
|
89 @root << @left_child1
|
Chris@0
|
90 @root << @right_child1
|
Chris@0
|
91 assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
|
Chris@0
|
92 assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child")
|
Chris@0
|
93 end
|
Chris@0
|
94
|
Chris@0
|
95 def test_leftChild_equals
|
Chris@0
|
96 @root << @left_child1
|
Chris@0
|
97 @root << @right_child1
|
Chris@0
|
98 assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
|
Chris@0
|
99
|
Chris@0
|
100 @root.leftChild = Tree::BinaryTreeNode.new("New Left Child")
|
Chris@0
|
101 assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child")
|
Chris@0
|
102 assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
|
Chris@0
|
103
|
Chris@0
|
104 # Now set the left child as nil, and retest
|
Chris@0
|
105 @root.leftChild = nil
|
Chris@0
|
106 assert_nil(@root.leftChild, "The left child should now be nil")
|
Chris@0
|
107 assert_nil(@root.firstChild, "The first child is now nil")
|
Chris@0
|
108 assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child")
|
Chris@0
|
109 end
|
Chris@0
|
110
|
Chris@0
|
111 def test_rightChild_equals
|
Chris@0
|
112 @root << @left_child1
|
Chris@0
|
113 @root << @right_child1
|
Chris@0
|
114 assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1")
|
Chris@0
|
115
|
Chris@0
|
116 @root.rightChild = Tree::BinaryTreeNode.new("New Right Child")
|
Chris@0
|
117 assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child")
|
Chris@0
|
118 assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
|
Chris@0
|
119 assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child")
|
Chris@0
|
120
|
Chris@0
|
121 # Now set the right child as nil, and retest
|
Chris@0
|
122 @root.rightChild = nil
|
Chris@0
|
123 assert_nil(@root.rightChild, "The right child should now be nil")
|
Chris@0
|
124 assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child")
|
Chris@0
|
125 assert_nil(@root.lastChild, "The first child is now nil")
|
Chris@0
|
126 end
|
Chris@0
|
127
|
Chris@0
|
128 def test_isLeftChild_eh
|
Chris@0
|
129 @root << @left_child1
|
Chris@0
|
130 @root << @right_child1
|
Chris@0
|
131
|
Chris@0
|
132 assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
Chris@0
|
133 assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
|
Chris@0
|
134
|
Chris@0
|
135 # Now set the right child as nil, and retest
|
Chris@0
|
136 @root.rightChild = nil
|
Chris@0
|
137 assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
|
Chris@0
|
138
|
Chris@0
|
139 assert(!@root.isLeftChild?, "Root is neither left child nor right")
|
Chris@0
|
140 end
|
Chris@0
|
141
|
Chris@0
|
142 def test_isRightChild_eh
|
Chris@0
|
143 @root << @left_child1
|
Chris@0
|
144 @root << @right_child1
|
Chris@0
|
145
|
Chris@0
|
146 assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
Chris@0
|
147 assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
Chris@0
|
148
|
Chris@0
|
149 # Now set the left child as nil, and retest
|
Chris@0
|
150 @root.leftChild = nil
|
Chris@0
|
151 assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
Chris@0
|
152 assert(!@root.isRightChild?, "Root is neither left child nor right")
|
Chris@0
|
153 end
|
Chris@0
|
154
|
Chris@0
|
155 def test_swap_children
|
Chris@0
|
156 @root << @left_child1
|
Chris@0
|
157 @root << @right_child1
|
Chris@0
|
158
|
Chris@0
|
159 assert(@right_child1.isRightChild?, "right_child1 should be the right child")
|
Chris@0
|
160 assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
|
Chris@0
|
161
|
Chris@0
|
162 @root.swap_children
|
Chris@0
|
163
|
Chris@0
|
164 assert(@right_child1.isLeftChild?, "right_child1 should now be the left child")
|
Chris@0
|
165 assert(@left_child1.isRightChild?, "left_child1 should now be the right child")
|
Chris@0
|
166 assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child")
|
Chris@0
|
167 assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child")
|
Chris@0
|
168 assert_equal(@right_child1, @root[0], "right_child1 should now be the first child")
|
Chris@0
|
169 assert_equal(@left_child1, @root[1], "left_child1 should now be the last child")
|
Chris@0
|
170 end
|
Chris@0
|
171 end
|
Chris@0
|
172 end
|
Chris@0
|
173
|
Chris@0
|
174 # $Log: test_binarytree.rb,v $
|
Chris@0
|
175 # Revision 1.5 2007/12/22 00:28:59 anupamsg
|
Chris@0
|
176 # Added more test cases, and enabled ZenTest compatibility.
|
Chris@0
|
177 #
|
Chris@0
|
178 # Revision 1.4 2007/12/18 23:11:29 anupamsg
|
Chris@0
|
179 # Minor documentation changes in the binarytree class.
|
Chris@0
|
180 #
|
Chris@0
|
181 # Revision 1.3 2007/10/02 03:07:30 anupamsg
|
Chris@0
|
182 # * Rakefile: Added an optional task for rcov code coverage.
|
Chris@0
|
183 #
|
Chris@0
|
184 # * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
|
Chris@0
|
185 #
|
Chris@0
|
186 # * test/test_tree.rb: Removed dependency on the redundant "Person" class.
|
Chris@0
|
187 #
|
Chris@0
|
188 # Revision 1.2 2007/08/30 22:06:13 anupamsg
|
Chris@0
|
189 # Added a new swap_children method for the Binary Tree class.
|
Chris@0
|
190 # Also made minor documentation updates and test additions.
|
Chris@0
|
191 #
|
Chris@0
|
192 # Revision 1.1 2007/07/21 04:52:37 anupamsg
|
Chris@0
|
193 # Renamed the test files.
|
Chris@0
|
194 #
|
Chris@0
|
195 # Revision 1.4 2007/07/19 02:03:57 anupamsg
|
Chris@0
|
196 # Minor syntax correction.
|
Chris@0
|
197 #
|
Chris@0
|
198 # Revision 1.3 2007/07/19 02:02:12 anupamsg
|
Chris@0
|
199 # Removed useless files (including rdoc, which should be generated for each release.
|
Chris@0
|
200 #
|
Chris@0
|
201 # Revision 1.2 2007/07/18 20:15:06 anupamsg
|
Chris@0
|
202 # Added two predicate methods in BinaryTreeNode to determine whether a node
|
Chris@0
|
203 # is a left or a right node.
|
Chris@0
|
204 #
|