Mercurial > hg > soundsoftware-site
comparison vendor/gems/rubytree-0.5.2/test/.svn/text-base/test_binarytree.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 #!/usr/bin/env ruby | |
2 | |
3 # test_binarytree.rb | |
4 # | |
5 # $Revision: 1.5 $ by $Author: anupamsg $ | |
6 # $Name: $ | |
7 # | |
8 # Copyright (c) 2006, 2007 Anupam Sengupta | |
9 # | |
10 # All rights reserved. | |
11 # | |
12 # Redistribution and use in source and binary forms, with or without modification, | |
13 # are permitted provided that the following conditions are met: | |
14 # | |
15 # - Redistributions of source code must retain the above copyright notice, this | |
16 # list of conditions and the following disclaimer. | |
17 # | |
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. | |
21 # | |
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. | |
25 # | |
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. | |
36 # | |
37 | |
38 require 'test/unit' | |
39 require 'tree/binarytree' | |
40 | |
41 module TestTree | |
42 # Test class for the Tree node. | |
43 class TestBinaryTreeNode < Test::Unit::TestCase | |
44 | |
45 def setup | |
46 @root = Tree::BinaryTreeNode.new("ROOT", "Root Node") | |
47 | |
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") | |
50 | |
51 end | |
52 | |
53 def teardown | |
54 @root.remove!(@left_child1) | |
55 @root.remove!(@right_child1) | |
56 @root = nil | |
57 end | |
58 | |
59 def test_initialize | |
60 assert_not_nil(@root, "Binary tree's Root should have been created") | |
61 end | |
62 | |
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") | |
67 | |
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") | |
71 | |
72 assert_raise RuntimeError do | |
73 @root.add Tree::BinaryTreeNode.new("The third child!") | |
74 end | |
75 | |
76 assert_raise RuntimeError do | |
77 @root << Tree::BinaryTreeNode.new("The third child!") | |
78 end | |
79 end | |
80 | |
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 | |
87 | |
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 | |
94 | |
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") | |
99 | |
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") | |
103 | |
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 | |
110 | |
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 | |
127 | |
128 def test_isLeftChild_eh | |
129 @root << @left_child1 | |
130 @root << @right_child1 | |
131 | |
132 assert(@left_child1.isLeftChild?, "left_child1 should be the left child") | |
133 assert(!@right_child1.isLeftChild?, "left_child1 should be the left child") | |
134 | |
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") | |
138 | |
139 assert(!@root.isLeftChild?, "Root is neither left child nor right") | |
140 end | |
141 | |
142 def test_isRightChild_eh | |
143 @root << @left_child1 | |
144 @root << @right_child1 | |
145 | |
146 assert(@right_child1.isRightChild?, "right_child1 should be the right child") | |
147 assert(!@left_child1.isRightChild?, "right_child1 should be the right child") | |
148 | |
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 | |
154 | |
155 def test_swap_children | |
156 @root << @left_child1 | |
157 @root << @right_child1 | |
158 | |
159 assert(@right_child1.isRightChild?, "right_child1 should be the right child") | |
160 assert(!@left_child1.isRightChild?, "right_child1 should be the right child") | |
161 | |
162 @root.swap_children | |
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 | |
171 end | |
172 end | |
173 | |
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 # | |
178 # Revision 1.4 2007/12/18 23:11:29 anupamsg | |
179 # Minor documentation changes in the binarytree class. | |
180 # | |
181 # Revision 1.3 2007/10/02 03:07:30 anupamsg | |
182 # * Rakefile: Added an optional task for rcov code coverage. | |
183 # | |
184 # * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class. | |
185 # | |
186 # * test/test_tree.rb: Removed dependency on the redundant "Person" class. | |
187 # | |
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. | |
191 # | |
192 # Revision 1.1 2007/07/21 04:52:37 anupamsg | |
193 # Renamed the test files. | |
194 # | |
195 # Revision 1.4 2007/07/19 02:03:57 anupamsg | |
196 # Minor syntax correction. | |
197 # | |
198 # Revision 1.3 2007/07/19 02:02:12 anupamsg | |
199 # Removed useless files (including rdoc, which should be generated for each release. | |
200 # | |
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. | |
204 # |