annotate .svn/pristine/b8/b80a919fe53496e29af18c43cb6375f80fa86ba6.svn-base @ 1517:dffacf8a6908 redmine-2.5

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