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