Chris@0: #!/usr/bin/env ruby Chris@0: Chris@0: # test_binarytree.rb Chris@0: # Chris@0: # $Revision: 1.5 $ by $Author: anupamsg $ Chris@0: # $Name: $ Chris@0: # Chris@0: # Copyright (c) 2006, 2007 Anupam Sengupta Chris@0: # Chris@0: # All rights reserved. Chris@0: # Chris@0: # Redistribution and use in source and binary forms, with or without modification, Chris@0: # are permitted provided that the following conditions are met: Chris@0: # Chris@0: # - Redistributions of source code must retain the above copyright notice, this Chris@0: # list of conditions and the following disclaimer. Chris@0: # Chris@0: # - Redistributions in binary form must reproduce the above copyright notice, this Chris@0: # list of conditions and the following disclaimer in the documentation and/or Chris@0: # other materials provided with the distribution. Chris@0: # Chris@0: # - Neither the name of the organization nor the names of its contributors may Chris@0: # be used to endorse or promote products derived from this software without Chris@0: # specific prior written permission. Chris@0: # Chris@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@0: # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@0: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE Chris@0: # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR Chris@0: # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES Chris@0: # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; Chris@0: # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON Chris@0: # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT Chris@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@0: # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: # Chris@0: Chris@0: require 'test/unit' Chris@0: require 'tree/binarytree' Chris@0: Chris@0: module TestTree Chris@0: # Test class for the Tree node. Chris@0: class TestBinaryTreeNode < Test::Unit::TestCase Chris@0: Chris@0: def setup Chris@0: @root = Tree::BinaryTreeNode.new("ROOT", "Root Node") Chris@0: Chris@0: @left_child1 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left") Chris@0: @right_child1 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right") Chris@0: Chris@0: end Chris@0: Chris@0: def teardown Chris@0: @root.remove!(@left_child1) Chris@0: @root.remove!(@right_child1) Chris@0: @root = nil Chris@0: end Chris@0: Chris@0: def test_initialize Chris@0: assert_not_nil(@root, "Binary tree's Root should have been created") Chris@0: end Chris@0: Chris@0: def test_add Chris@0: @root.add @left_child1 Chris@0: assert_same(@left_child1, @root.leftChild, "The left node should be left_child1") Chris@0: assert_same(@left_child1, @root.firstChild, "The first node should be left_child1") Chris@0: Chris@0: @root.add @right_child1 Chris@0: assert_same(@right_child1, @root.rightChild, "The right node should be right_child1") Chris@0: assert_same(@right_child1, @root.lastChild, "The first node should be right_child1") Chris@0: Chris@0: assert_raise RuntimeError do Chris@0: @root.add Tree::BinaryTreeNode.new("The third child!") Chris@0: end Chris@0: Chris@0: assert_raise RuntimeError do Chris@0: @root << Tree::BinaryTreeNode.new("The third child!") Chris@0: end Chris@0: end Chris@0: Chris@0: def test_leftChild Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1") Chris@0: assert_not_same(@right_child1, @root.leftChild, "The right_child1 is not the left child") Chris@0: end Chris@0: Chris@0: def test_rightChild Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1") Chris@0: assert_not_same(@left_child1, @root.rightChild, "The left_child1 is not the left child") Chris@0: end Chris@0: Chris@0: def test_leftChild_equals Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: assert_same(@left_child1, @root.leftChild, "The left child should be 'left_child1") Chris@0: Chris@0: @root.leftChild = Tree::BinaryTreeNode.new("New Left Child") Chris@0: assert_equal("New Left Child", @root.leftChild.name, "The left child should now be the new child") Chris@0: assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child") Chris@0: Chris@0: # Now set the left child as nil, and retest Chris@0: @root.leftChild = nil Chris@0: assert_nil(@root.leftChild, "The left child should now be nil") Chris@0: assert_nil(@root.firstChild, "The first child is now nil") Chris@0: assert_equal("B Child at Right", @root.lastChild.name, "The last child should now be the right child") Chris@0: end Chris@0: Chris@0: def test_rightChild_equals Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: assert_same(@right_child1, @root.rightChild, "The right child should be 'right_child1") Chris@0: Chris@0: @root.rightChild = Tree::BinaryTreeNode.new("New Right Child") Chris@0: assert_equal("New Right Child", @root.rightChild.name, "The right child should now be the new child") Chris@0: assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child") Chris@0: assert_equal("New Right Child", @root.lastChild.name, "The last child should now be the right child") Chris@0: Chris@0: # Now set the right child as nil, and retest Chris@0: @root.rightChild = nil Chris@0: assert_nil(@root.rightChild, "The right child should now be nil") Chris@0: assert_equal("A Child at Left", @root.firstChild.name, "The first child should now be the left child") Chris@0: assert_nil(@root.lastChild, "The first child is now nil") Chris@0: end Chris@0: Chris@0: def test_isLeftChild_eh Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: Chris@0: assert(@left_child1.isLeftChild?, "left_child1 should be the left child") Chris@0: assert(!@right_child1.isLeftChild?, "left_child1 should be the left child") Chris@0: Chris@0: # Now set the right child as nil, and retest Chris@0: @root.rightChild = nil Chris@0: assert(@left_child1.isLeftChild?, "left_child1 should be the left child") Chris@0: Chris@0: assert(!@root.isLeftChild?, "Root is neither left child nor right") Chris@0: end Chris@0: Chris@0: def test_isRightChild_eh Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: Chris@0: assert(@right_child1.isRightChild?, "right_child1 should be the right child") Chris@0: assert(!@left_child1.isRightChild?, "right_child1 should be the right child") Chris@0: Chris@0: # Now set the left child as nil, and retest Chris@0: @root.leftChild = nil Chris@0: assert(@right_child1.isRightChild?, "right_child1 should be the right child") Chris@0: assert(!@root.isRightChild?, "Root is neither left child nor right") Chris@0: end Chris@0: Chris@0: def test_swap_children Chris@0: @root << @left_child1 Chris@0: @root << @right_child1 Chris@0: Chris@0: assert(@right_child1.isRightChild?, "right_child1 should be the right child") Chris@0: assert(!@left_child1.isRightChild?, "right_child1 should be the right child") Chris@0: Chris@0: @root.swap_children Chris@0: Chris@0: assert(@right_child1.isLeftChild?, "right_child1 should now be the left child") Chris@0: assert(@left_child1.isRightChild?, "left_child1 should now be the right child") Chris@0: assert_equal(@right_child1, @root.firstChild, "right_child1 should now be the first child") Chris@0: assert_equal(@left_child1, @root.lastChild, "left_child1 should now be the last child") Chris@0: assert_equal(@right_child1, @root[0], "right_child1 should now be the first child") Chris@0: assert_equal(@left_child1, @root[1], "left_child1 should now be the last child") Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # $Log: test_binarytree.rb,v $ Chris@0: # Revision 1.5 2007/12/22 00:28:59 anupamsg Chris@0: # Added more test cases, and enabled ZenTest compatibility. Chris@0: # Chris@0: # Revision 1.4 2007/12/18 23:11:29 anupamsg Chris@0: # Minor documentation changes in the binarytree class. Chris@0: # Chris@0: # Revision 1.3 2007/10/02 03:07:30 anupamsg Chris@0: # * Rakefile: Added an optional task for rcov code coverage. Chris@0: # Chris@0: # * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class. Chris@0: # Chris@0: # * test/test_tree.rb: Removed dependency on the redundant "Person" class. Chris@0: # Chris@0: # Revision 1.2 2007/08/30 22:06:13 anupamsg Chris@0: # Added a new swap_children method for the Binary Tree class. Chris@0: # Also made minor documentation updates and test additions. Chris@0: # Chris@0: # Revision 1.1 2007/07/21 04:52:37 anupamsg Chris@0: # Renamed the test files. Chris@0: # Chris@0: # Revision 1.4 2007/07/19 02:03:57 anupamsg Chris@0: # Minor syntax correction. Chris@0: # Chris@0: # Revision 1.3 2007/07/19 02:02:12 anupamsg Chris@0: # Removed useless files (including rdoc, which should be generated for each release. Chris@0: # Chris@0: # Revision 1.2 2007/07/18 20:15:06 anupamsg Chris@0: # Added two predicate methods in BinaryTreeNode to determine whether a node Chris@0: # is a left or a right node. Chris@0: #