5 # $Revision: 1.5 $ by $Author: anupamsg $
8 # Copyright (c) 2006, 2007 Anupam Sengupta
10 # All rights reserved.
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
15 # - Redistributions of source code must retain the above copyright notice, this
16 # list of conditions and the following disclaimer.
18 # - Redistributions in binary form must reproduce the above copyright notice, this
19 # list of conditions and the following disclaimer in the documentation and/or
20 # other materials provided with the distribution.
22 # - Neither the name of the organization nor the names of its contributors may
23 # be used to endorse or promote products derived from this software without
24 # specific prior written permission.
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 require 'tree/binarytree'
42 # Test class for the Tree node.
43 class TestBinaryTreeNode < Test::Unit::TestCase
46 @root = Tree::BinaryTreeNode.new("ROOT", "Root Node")
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")
54 @root.remove!(@left_child1)
55 @root.remove!(@right_child1)
60 assert_not_nil(@root, "Binary tree's Root should have been created")
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")
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")
72 assert_raise RuntimeError do
73 @root.add Tree::BinaryTreeNode.new("The third child!")
76 assert_raise RuntimeError do
77 @root << Tree::BinaryTreeNode.new("The third child!")
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")
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")
95 def test_leftChild_equals
97 @root << @right_child1
98 assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1")
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")
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")
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")
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")
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")
128 def test_isLeftChild_eh
129 @root << @left_child1
130 @root << @right_child1
132 assert(@left_child1.isLeftChild?, "left_child1 should be the left child")
133 assert(!@right_child1.isLeftChild?, "left_child1 should be the left child")
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")
139 assert(!@root.isLeftChild?, "Root is neither left child nor right")
142 def test_isRightChild_eh
143 @root << @left_child1
144 @root << @right_child1
146 assert(@right_child1.isRightChild?, "right_child1 should be the right child")
147 assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
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")
155 def test_swap_children
156 @root << @left_child1
157 @root << @right_child1
159 assert(@right_child1.isRightChild?, "right_child1 should be the right child")
160 assert(!@left_child1.isRightChild?, "right_child1 should be the right child")
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")
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.
178 # Revision 1.4 2007/12/18 23:11:29 anupamsg
179 # Minor documentation changes in the binarytree class.
181 # Revision 1.3 2007/10/02 03:07:30 anupamsg
182 # * Rakefile: Added an optional task for rcov code coverage.
184 # * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
186 # * test/test_tree.rb: Removed dependency on the redundant "Person" class.
188 # Revision 1.2 2007/08/30 22:06:13 anupamsg
189 # Added a new swap_children method for the Binary Tree class.
190 # Also made minor documentation updates and test additions.
192 # Revision 1.1 2007/07/21 04:52:37 anupamsg
193 # Renamed the test files.
195 # Revision 1.4 2007/07/19 02:03:57 anupamsg
196 # Minor syntax correction.
198 # Revision 1.3 2007/07/19 02:02:12 anupamsg
199 # Removed useless files (including rdoc, which should be generated for each release.
201 # Revision 1.2 2007/07/18 20:15:06 anupamsg
202 # Added two predicate methods in BinaryTreeNode to determine whether a node
203 # is a left or a right node.