Chris@0: #!/usr/bin/env ruby Chris@0: Chris@0: # testtree.rb Chris@0: # Chris@0: # $Revision: 1.6 $ 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' Chris@0: Chris@0: module TestTree Chris@0: # Test class for the Tree node. Chris@0: class TestTreeNode < Test::Unit::TestCase Chris@0: Chris@0: Person = Struct::new(:First, :last) Chris@0: Chris@0: def setup Chris@0: @root = Tree::TreeNode.new("ROOT", "Root Node") Chris@0: Chris@0: @child1 = Tree::TreeNode.new("Child1", "Child Node 1") Chris@0: @child2 = Tree::TreeNode.new("Child2", "Child Node 2") Chris@0: @child3 = Tree::TreeNode.new("Child3", "Child Node 3") Chris@0: @child4 = Tree::TreeNode.new("Child31", "Grand Child 1") Chris@0: Chris@0: end Chris@0: Chris@0: # Create this structure for the tests Chris@0: # Chris@0: # +----------+ Chris@0: # | ROOT | Chris@0: # +-+--------+ Chris@0: # | Chris@0: # | +---------------+ Chris@0: # +----+ CHILD1 | Chris@0: # | +---------------+ Chris@0: # | Chris@0: # | +---------------+ Chris@0: # +----+ CHILD2 | Chris@0: # | +---------------+ Chris@0: # | Chris@0: # | +---------------+ +------------------+ Chris@0: # +----+ CHILD3 +---+ CHILD4 | Chris@0: # +---------------+ +------------------+ Chris@0: # Chris@0: def loadChildren Chris@0: @root << @child1 Chris@0: @root << @child2 Chris@0: @root << @child3 << @child4 Chris@0: end Chris@0: Chris@0: def teardown Chris@0: @root = nil Chris@0: end Chris@0: Chris@0: def test_root_setup Chris@0: assert_not_nil(@root, "Root cannot be nil") Chris@0: assert_nil(@root.parent, "Parent of root node should be nil") Chris@0: assert_not_nil(@root.name, "Name should not be nil") Chris@0: assert_equal("ROOT", @root.name, "Name should be 'ROOT'") Chris@0: assert_equal("Root Node", @root.content, "Content should be 'Root Node'") Chris@0: assert(@root.isRoot?, "Should identify as root") Chris@0: assert(!@root.hasChildren?, "Cannot have any children") Chris@0: assert(@root.hasContent?, "This root should have content") Chris@0: assert_equal(1, @root.size, "Number of nodes should be one") Chris@0: assert_nil(@root.siblings, "Root cannot have any children") Chris@0: Chris@0: assert_raise(RuntimeError) { Tree::TreeNode.new(nil) } Chris@0: end Chris@0: Chris@0: def test_root Chris@0: loadChildren Chris@0: Chris@0: assert_same(@root, @root.root, "Root's root is self") Chris@0: assert_same(@root, @child1.root, "Root should be ROOT") Chris@0: assert_same(@root, @child4.root, "Root should be ROOT") Chris@0: end Chris@0: Chris@0: def test_hasContent_eh Chris@0: aNode = Tree::TreeNode.new("A Node") Chris@0: assert_nil(aNode.content, "The node should not have content") Chris@0: assert(!aNode.hasContent?, "The node should not have content") Chris@0: Chris@0: aNode.content = "Something" Chris@0: assert_not_nil(aNode.content, "The node should now have content") Chris@0: assert(aNode.hasContent?, "The node should now have content") Chris@0: end Chris@0: Chris@0: def test_length Chris@0: loadChildren Chris@0: assert_equal(@root.size, @root.length, "Length and size methods should return the same result") Chris@0: end Chris@0: Chris@0: def test_spaceship # Test the <=> operator. Chris@0: firstNode = Tree::TreeNode.new(1) Chris@0: secondNode = Tree::TreeNode.new(2) Chris@0: Chris@0: assert_equal(firstNode <=> nil, +1) Chris@0: assert_equal(firstNode <=> secondNode, -1) Chris@0: Chris@0: secondNode = Tree::TreeNode.new(1) Chris@0: assert_equal(firstNode <=> secondNode, 0) Chris@0: Chris@0: firstNode = Tree::TreeNode.new("ABC") Chris@0: secondNode = Tree::TreeNode.new("XYZ") Chris@0: Chris@0: assert_equal(firstNode <=> nil, +1) Chris@0: assert_equal(firstNode <=> secondNode, -1) Chris@0: Chris@0: secondNode = Tree::TreeNode.new("ABC") Chris@0: assert_equal(firstNode <=> secondNode, 0) Chris@0: end Chris@0: Chris@0: def test_to_s Chris@0: aNode = Tree::TreeNode.new("A Node", "Some Content") Chris@0: Chris@0: expectedString = "Node Name: A Node Content: Some Content Parent: Children: 0 Total Nodes: 1" Chris@0: Chris@0: assert_equal(expectedString, aNode.to_s, "The string representation should be same") Chris@0: end Chris@0: Chris@0: def test_firstSibling Chris@0: loadChildren Chris@0: Chris@0: assert_same(@root, @root.firstSibling, "Root's first sibling is itself") Chris@0: assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself") Chris@0: assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1") Chris@0: assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1") Chris@0: assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself") Chris@0: end Chris@0: Chris@0: def test_isFirstSibling_eh Chris@0: loadChildren Chris@0: Chris@0: assert(@root.isFirstSibling?, "Root's first sibling is itself") Chris@0: assert( @child1.isFirstSibling?, "Child1's first sibling is itself") Chris@0: assert(!@child2.isFirstSibling?, "Child2 is not the first sibling") Chris@0: assert(!@child3.isFirstSibling?, "Child3 is not the first sibling") Chris@0: assert( @child4.isFirstSibling?, "Child4's first sibling is itself") Chris@0: end Chris@0: Chris@0: def test_isLastSibling_eh Chris@0: loadChildren Chris@0: Chris@0: assert(@root.isLastSibling?, "Root's last sibling is itself") Chris@0: assert(!@child1.isLastSibling?, "Child1 is not the last sibling") Chris@0: assert(!@child2.isLastSibling?, "Child2 is not the last sibling") Chris@0: assert( @child3.isLastSibling?, "Child3's last sibling is itself") Chris@0: assert( @child4.isLastSibling?, "Child4's last sibling is itself") Chris@0: end Chris@0: Chris@0: def test_lastSibling Chris@0: loadChildren Chris@0: Chris@0: assert_same(@root, @root.lastSibling, "Root's last sibling is itself") Chris@0: assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3") Chris@0: assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3") Chris@0: assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself") Chris@0: assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself") Chris@0: end Chris@0: Chris@0: def test_siblings Chris@0: loadChildren Chris@0: Chris@0: siblings = [] Chris@0: @child1.siblings { |sibling| siblings << sibling} Chris@0: assert_equal(2, siblings.length, "Should have two siblings") Chris@0: assert(siblings.include?(@child2), "Should have 2nd child as sibling") Chris@0: assert(siblings.include?(@child3), "Should have 3rd child as sibling") Chris@0: Chris@0: siblings.clear Chris@0: siblings = @child1.siblings Chris@0: assert_equal(2, siblings.length, "Should have two siblings") Chris@0: Chris@0: siblings.clear Chris@0: @child4.siblings {|sibling| siblings << sibling} Chris@0: assert(siblings.empty?, "Should not have any children") Chris@0: Chris@0: end Chris@0: Chris@0: def test_isOnlyChild_eh Chris@0: loadChildren Chris@0: Chris@0: assert(!@child1.isOnlyChild?, "Child1 is not the only child") Chris@0: assert(!@child2.isOnlyChild?, "Child2 is not the only child") Chris@0: assert(!@child3.isOnlyChild?, "Child3 is not the only child") Chris@0: assert( @child4.isOnlyChild?, "Child4 is not the only child") Chris@0: end Chris@0: Chris@0: def test_nextSibling Chris@0: loadChildren Chris@0: Chris@0: assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2") Chris@0: assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3") Chris@0: assert_nil(@child3.nextSibling, "Child3 does not have a next sibling") Chris@0: assert_nil(@child4.nextSibling, "Child4 does not have a next sibling") Chris@0: end Chris@0: Chris@0: def test_previousSibling Chris@0: loadChildren Chris@0: Chris@0: assert_nil(@child1.previousSibling, "Child1 does not have previous sibling") Chris@0: assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1") Chris@0: assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2") Chris@0: assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling") Chris@0: end Chris@0: Chris@0: def test_add Chris@0: assert(!@root.hasChildren?, "Should not have any children") Chris@0: Chris@0: @root.add(@child1) Chris@0: Chris@0: @root << @child2 Chris@0: Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: assert_equal(3, @root.size, "Should have three nodes") Chris@0: Chris@0: @root << @child3 << @child4 Chris@0: Chris@0: assert_equal(5, @root.size, "Should have five nodes") Chris@0: assert_equal(2, @child3.size, "Should have two nodes") Chris@0: Chris@0: assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) } Chris@0: Chris@0: end Chris@0: Chris@0: def test_remove_bang Chris@0: @root << @child1 Chris@0: @root << @child2 Chris@0: Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: assert_equal(3, @root.size, "Should have three nodes") Chris@0: Chris@0: @root.remove!(@child1) Chris@0: assert_equal(2, @root.size, "Should have two nodes") Chris@0: @root.remove!(@child2) Chris@0: Chris@0: assert(!@root.hasChildren?, "Should have no children") Chris@0: assert_equal(1, @root.size, "Should have one node") Chris@0: Chris@0: @root << @child1 Chris@0: @root << @child2 Chris@0: Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: assert_equal(3, @root.size, "Should have three nodes") Chris@0: Chris@0: @root.removeAll! Chris@0: Chris@0: assert(!@root.hasChildren?, "Should have no children") Chris@0: assert_equal(1, @root.size, "Should have one node") Chris@0: Chris@0: end Chris@0: Chris@0: def test_removeAll_bang Chris@0: loadChildren Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: @root.removeAll! Chris@0: Chris@0: assert(!@root.hasChildren?, "Should have no children") Chris@0: assert_equal(1, @root.size, "Should have one node") Chris@0: end Chris@0: Chris@0: def test_removeFromParent_bang Chris@0: loadChildren Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: assert(!@root.isLeaf?, "Root is not a leaf here") Chris@0: Chris@0: child1 = @root[0] Chris@0: assert_not_nil(child1, "Child 1 should exist") Chris@0: assert_same(@root, child1.root, "Child 1's root should be ROOT") Chris@0: assert(@root.include?(child1), "root should have child1") Chris@0: child1.removeFromParent! Chris@0: assert_same(child1, child1.root, "Child 1's root should be self") Chris@0: assert(!@root.include?(child1), "root should not have child1") Chris@0: Chris@0: child1.removeFromParent! Chris@0: assert_same(child1, child1.root, "Child 1's root should still be self") Chris@0: end Chris@0: Chris@0: def test_children Chris@0: loadChildren Chris@0: Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: assert_equal(5, @root.size, "Should have four nodes") Chris@0: assert(@child3.hasChildren?, "Should have children") Chris@0: assert(!@child3.isLeaf?, "Should not be a leaf") Chris@0: Chris@0: children = [] Chris@0: for child in @root.children Chris@0: children << child Chris@0: end Chris@0: Chris@0: assert_equal(3, children.length, "Should have three direct children") Chris@0: assert(!children.include?(@root), "Should not have root") Chris@0: assert(children.include?(@child1), "Should have child 1") Chris@0: assert(children.include?(@child2), "Should have child 2") Chris@0: assert(children.include?(@child3), "Should have child 3") Chris@0: assert(!children.include?(@child4), "Should not have child 4") Chris@0: Chris@0: children.clear Chris@0: children = @root.children Chris@0: assert_equal(3, children.length, "Should have three children") Chris@0: Chris@0: end Chris@0: Chris@0: def test_firstChild Chris@0: loadChildren Chris@0: Chris@0: assert_equal(@child1, @root.firstChild, "Root's first child is Child1") Chris@0: assert_nil(@child1.firstChild, "Child1 does not have any children") Chris@0: assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4") Chris@0: Chris@0: end Chris@0: Chris@0: def test_lastChild Chris@0: loadChildren Chris@0: Chris@0: assert_equal(@child3, @root.lastChild, "Root's last child is Child3") Chris@0: assert_nil(@child1.lastChild, "Child1 does not have any children") Chris@0: assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4") Chris@0: Chris@0: end Chris@0: Chris@0: def test_find Chris@0: loadChildren Chris@0: foundNode = @root.find { |node| node == @child2} Chris@0: assert_same(@child2, foundNode, "The node should be Child 2") Chris@0: Chris@0: foundNode = @root.find { |node| node == @child4} Chris@0: assert_same(@child4, foundNode, "The node should be Child 4") Chris@0: Chris@0: foundNode = @root.find { |node| node.name == "Child31" } Chris@0: assert_same(@child4, foundNode, "The node should be Child 4") Chris@0: foundNode = @root.find { |node| node.name == "NOT PRESENT" } Chris@0: assert_nil(foundNode, "The node should not be found") Chris@0: end Chris@0: Chris@0: def test_parentage Chris@0: loadChildren Chris@0: Chris@0: assert_nil(@root.parentage, "Root does not have any parentage") Chris@0: assert_equal([@root], @child1.parentage, "Child1 has Root as its parent") Chris@0: assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors") Chris@0: end Chris@0: Chris@0: def test_each Chris@0: loadChildren Chris@0: assert(@root.hasChildren?, "Should have children") Chris@0: assert_equal(5, @root.size, "Should have five nodes") Chris@0: assert(@child3.hasChildren?, "Should have children") Chris@0: Chris@0: nodes = [] Chris@0: @root.each { |node| nodes << node } Chris@0: Chris@0: assert_equal(5, nodes.length, "Should have FIVE NODES") Chris@0: assert(nodes.include?(@root), "Should have root") Chris@0: assert(nodes.include?(@child1), "Should have child 1") Chris@0: assert(nodes.include?(@child2), "Should have child 2") Chris@0: assert(nodes.include?(@child3), "Should have child 3") Chris@0: assert(nodes.include?(@child4), "Should have child 4") Chris@0: end Chris@0: Chris@0: def test_each_leaf Chris@0: loadChildren Chris@0: Chris@0: nodes = [] Chris@0: @root.each_leaf { |node| nodes << node } Chris@0: Chris@0: assert_equal(3, nodes.length, "Should have THREE LEAF NODES") Chris@0: assert(!nodes.include?(@root), "Should not have root") Chris@0: assert(nodes.include?(@child1), "Should have child 1") Chris@0: assert(nodes.include?(@child2), "Should have child 2") Chris@0: assert(!nodes.include?(@child3), "Should not have child 3") Chris@0: assert(nodes.include?(@child4), "Should have child 4") Chris@0: end Chris@0: Chris@0: def test_parent Chris@0: loadChildren Chris@0: assert_nil(@root.parent, "Root's parent should be nil") Chris@0: assert_equal(@root, @child1.parent, "Parent should be root") Chris@0: assert_equal(@root, @child3.parent, "Parent should be root") Chris@0: assert_equal(@child3, @child4.parent, "Parent should be child3") Chris@0: assert_equal(@root, @child4.parent.parent, "Parent should be root") Chris@0: end Chris@0: Chris@0: def test_indexed_access Chris@0: loadChildren Chris@0: assert_equal(@child1, @root[0], "Should be the first child") Chris@0: assert_equal(@child4, @root[2][0], "Should be the grandchild") Chris@0: assert_nil(@root["TEST"], "Should be nil") Chris@0: assert_raise(RuntimeError) { @root[nil] } Chris@0: end Chris@0: Chris@0: def test_printTree Chris@0: loadChildren Chris@0: #puts Chris@0: #@root.printTree Chris@0: end Chris@0: Chris@0: # Tests the binary dumping mechanism with an Object content node Chris@0: def test_marshal_dump Chris@0: # Setup Test Data Chris@0: test_root = Tree::TreeNode.new("ROOT", "Root Node") Chris@0: test_content = {"KEY1" => "Value1", "KEY2" => "Value2" } Chris@0: test_child = Tree::TreeNode.new("Child", test_content) Chris@0: test_content2 = ["AValue1", "AValue2", "AValue3"] Chris@0: test_grand_child = Tree::TreeNode.new("Grand Child 1", test_content2) Chris@0: test_root << test_child << test_grand_child Chris@0: Chris@0: # Perform the test operation Chris@0: data = Marshal.dump(test_root) # Marshal Chris@0: new_root = Marshal.load(data) # And unmarshal Chris@0: Chris@0: # Test the root node Chris@0: assert_equal(test_root.name, new_root.name, "Must identify as ROOT") Chris@0: assert_equal(test_root.content, new_root.content, "Must have root's content") Chris@0: assert(new_root.isRoot?, "Must be the ROOT node") Chris@0: assert(new_root.hasChildren?, "Must have a child node") Chris@0: Chris@0: # Test the child node Chris@0: new_child = new_root[test_child.name] Chris@0: assert_equal(test_child.name, new_child.name, "Must have child 1") Chris@0: assert(new_child.hasContent?, "Child must have content") Chris@0: assert(new_child.isOnlyChild?, "Child must be the only child") Chris@0: Chris@0: new_child_content = new_child.content Chris@0: assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash") Chris@0: assert_equal(test_child.content.size, new_child_content.size, "The content should have same size") Chris@0: Chris@0: # Test the grand-child node Chris@0: new_grand_child = new_child[test_grand_child.name] Chris@0: assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child") Chris@0: assert(new_grand_child.hasContent?, "Grand-child must have content") Chris@0: assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child") Chris@0: Chris@0: new_grand_child_content = new_grand_child.content Chris@0: assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array") Chris@0: assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size") Chris@0: end Chris@0: Chris@0: # marshal_load and marshal_dump are symmetric methods Chris@0: # This alias is for satisfying ZenTest Chris@0: alias test_marshal_load test_marshal_dump Chris@0: Chris@0: # Test the collect method from the mixed-in Enumerable functionality. Chris@0: def test_collect Chris@0: loadChildren Chris@0: collectArray = @root.collect do |node| Chris@0: node.content = "abc" Chris@0: node Chris@0: end Chris@0: collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")} Chris@0: end Chris@0: Chris@0: # Test freezing the tree Chris@0: def test_freezeTree_bang Chris@0: loadChildren Chris@0: @root.content = "ABC" Chris@0: assert_equal("ABC", @root.content, "Content should be 'ABC'") Chris@0: @root.freezeTree! Chris@0: assert_raise(TypeError) {@root.content = "123"} Chris@0: assert_raise(TypeError) {@root[0].content = "123"} Chris@0: end Chris@0: Chris@0: # Test whether the content is accesible Chris@0: def test_content Chris@0: pers = Person::new("John", "Doe") Chris@0: @root.content = pers Chris@0: assert_same(pers, @root.content, "Content should be the same") Chris@0: end Chris@0: Chris@0: # Test the depth computation algorithm Chris@0: def test_depth Chris@0: assert_equal(1, @root.depth, "A single node's depth is 1") Chris@0: Chris@0: @root << @child1 Chris@0: assert_equal(2, @root.depth, "This should be of depth 2") Chris@0: Chris@0: @root << @child2 Chris@0: assert_equal(2, @root.depth, "This should be of depth 2") Chris@0: Chris@0: @child2 << @child3 Chris@0: assert_equal(3, @root.depth, "This should be of depth 3") Chris@0: assert_equal(2, @child2.depth, "This should be of depth 2") Chris@0: Chris@0: @child3 << @child4 Chris@0: assert_equal(4, @root.depth, "This should be of depth 4") Chris@0: end Chris@0: Chris@0: # Test the breadth computation algorithm Chris@0: def test_breadth Chris@0: assert_equal(1, @root.breadth, "A single node's breadth is 1") Chris@0: Chris@0: @root << @child1 Chris@0: assert_equal(1, @root.breadth, "This should be of breadth 1") Chris@0: Chris@0: @root << @child2 Chris@0: assert_equal(2, @child1.breadth, "This should be of breadth 2") Chris@0: assert_equal(2, @child2.breadth, "This should be of breadth 2") Chris@0: Chris@0: @root << @child3 Chris@0: assert_equal(3, @child1.breadth, "This should be of breadth 3") Chris@0: assert_equal(3, @child2.breadth, "This should be of breadth 3") Chris@0: Chris@0: @child3 << @child4 Chris@0: assert_equal(1, @child4.breadth, "This should be of breadth 1") Chris@0: end Chris@0: Chris@0: # Test the breadth for each Chris@0: def test_breadth_each Chris@0: j = Tree::TreeNode.new("j") Chris@0: f = Tree::TreeNode.new("f") Chris@0: k = Tree::TreeNode.new("k") Chris@0: a = Tree::TreeNode.new("a") Chris@0: d = Tree::TreeNode.new("d") Chris@0: h = Tree::TreeNode.new("h") Chris@0: z = Tree::TreeNode.new("z") Chris@0: Chris@0: # The expected order of response Chris@0: expected_array = [j, Chris@0: f, k, Chris@0: a, h, z, Chris@0: d] Chris@0: Chris@0: # Create the following Tree Chris@0: # j <-- level 0 (Root) Chris@0: # / \ Chris@0: # f k <-- level 1 Chris@0: # / \ \ Chris@0: # a h z <-- level 2 Chris@0: # \ Chris@0: # d <-- level 3 Chris@0: j << f << a << d Chris@0: f << h Chris@0: j << k << z Chris@0: Chris@0: # Create the response Chris@0: result_array = Array.new Chris@0: j.breadth_each { |node| result_array << node.detached_copy } Chris@0: Chris@0: expected_array.each_index do |i| Chris@0: assert_equal(expected_array[i].name, result_array[i].name) # Match only the names. Chris@0: end Chris@0: end Chris@0: Chris@0: Chris@0: def test_preordered_each Chris@0: j = Tree::TreeNode.new("j") Chris@0: f = Tree::TreeNode.new("f") Chris@0: k = Tree::TreeNode.new("k") Chris@0: a = Tree::TreeNode.new("a") Chris@0: d = Tree::TreeNode.new("d") Chris@0: h = Tree::TreeNode.new("h") Chris@0: z = Tree::TreeNode.new("z") Chris@0: Chris@0: # The expected order of response Chris@0: expected_array = [j, f, a, d, h, k, z] Chris@0: Chris@0: # Create the following Tree Chris@0: # j <-- level 0 (Root) Chris@0: # / \ Chris@0: # f k <-- level 1 Chris@0: # / \ \ Chris@0: # a h z <-- level 2 Chris@0: # \ Chris@0: # d <-- level 3 Chris@0: j << f << a << d Chris@0: f << h Chris@0: j << k << z Chris@0: Chris@0: result_array = [] Chris@0: j.preordered_each { |node| result_array << node.detached_copy} Chris@0: Chris@0: expected_array.each_index do |i| Chris@0: # Match only the names. Chris@0: assert_equal(expected_array[i].name, result_array[i].name) Chris@0: end Chris@0: end Chris@0: Chris@0: def test_detached_copy Chris@0: loadChildren Chris@0: Chris@0: assert(@root.hasChildren?, "The root should have children") Chris@0: copy_of_root = @root.detached_copy Chris@0: assert(!copy_of_root.hasChildren?, "The copy should not have children") Chris@0: assert_equal(@root.name, copy_of_root.name, "The names should be equal") Chris@0: Chris@0: # Try the same test with a child node Chris@0: assert(!@child3.isRoot?, "Child 3 is not a root") Chris@0: assert(@child3.hasChildren?, "Child 3 has children") Chris@0: copy_of_child3 = @child3.detached_copy Chris@0: assert(copy_of_child3.isRoot?, "Child 3's copy is a root") Chris@0: assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children") Chris@0: end Chris@0: Chris@0: def test_hasChildren_eh Chris@0: loadChildren Chris@0: assert(@root.hasChildren?, "The Root node MUST have children") Chris@0: end Chris@0: Chris@0: def test_isLeaf_eh Chris@0: loadChildren Chris@0: assert(!@child3.isLeaf?, "Child 3 is not a leaf node") Chris@0: assert(@child4.isLeaf?, "Child 4 is a leaf node") Chris@0: end Chris@0: Chris@0: def test_isRoot_eh Chris@0: loadChildren Chris@0: assert(@root.isRoot?, "The ROOT node must respond as the root node") Chris@0: end Chris@0: Chris@0: def test_content_equals Chris@0: @root.content = nil Chris@0: assert_nil(@root.content, "Root's content should be nil") Chris@0: @root.content = "ABCD" Chris@0: assert_equal("ABCD", @root.content, "Root's content should now be 'ABCD'") Chris@0: end Chris@0: Chris@0: def test_size Chris@0: assert_equal(1, @root.size, "Root's size should be 1") Chris@0: loadChildren Chris@0: assert_equal(5, @root.size, "Root's size should be 5") Chris@0: assert_equal(2, @child3.size, "Child 3's size should be 2") Chris@0: end Chris@0: Chris@0: def test_lt2 # Test the << method Chris@0: @root << @child1 Chris@0: @root << @child2 Chris@0: @root << @child3 << @child4 Chris@0: assert_not_nil(@root['Child1'], "Child 1 should have been added to Root") Chris@0: assert_not_nil(@root['Child2'], "Child 2 should have been added to Root") Chris@0: assert_not_nil(@root['Child3'], "Child 3 should have been added to Root") Chris@0: assert_not_nil(@child3['Child31'], "Child 31 should have been added to Child3") Chris@0: end Chris@0: Chris@0: def test_index # Test the [] method Chris@0: assert_raise(RuntimeError) {@root[nil]} Chris@0: Chris@0: @root << @child1 Chris@0: @root << @child2 Chris@0: assert_equal(@child1.name, @root['Child1'].name, "Child 1 should be returned") Chris@0: assert_equal(@child1.name, @root[0].name, "Child 1 should be returned") Chris@0: assert_equal(@child2.name, @root['Child2'].name, "Child 2 should be returned") Chris@0: assert_equal(@child2.name, @root[1].name, "Child 2 should be returned") Chris@0: Chris@0: assert_nil(@root['Some Random Name'], "Should return nil") Chris@0: assert_nil(@root[99], "Should return nil") Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: __END__ Chris@0: Chris@0: # $Log: test_tree.rb,v $ Chris@0: # Revision 1.6 2007/12/22 00:28:59 anupamsg Chris@0: # Added more test cases, and enabled ZenTest compatibility. Chris@0: # Chris@0: # Revision 1.5 2007/12/19 02:24:18 anupamsg Chris@0: # Updated the marshalling logic to handle non-string contents on the nodes. Chris@0: # Chris@0: # Revision 1.4 2007/10/02 03:38:11 anupamsg Chris@0: # Removed dependency on the redundant "Person" class. Chris@0: # (TC_TreeTest::test_comparator): Added a new test for the spaceship operator. Chris@0: # (TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods. 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/31 01:16:28 anupamsg Chris@0: # Added breadth and pre-order traversals for the tree. Also added a method Chris@0: # to return the detached copy of a node from the tree. Chris@0: # Chris@0: # Revision 1.1 2007/07/21 04:52:38 anupamsg Chris@0: # Renamed the test files. Chris@0: # Chris@0: # Revision 1.13 2007/07/18 22:11:50 anupamsg Chris@0: # Added depth and breadth methods for the TreeNode. Chris@0: # Chris@0: # Revision 1.12 2007/07/18 07:17:34 anupamsg Chris@0: # Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method Chris@0: # has been renamed to TreeNode.parentage. Chris@0: # Chris@0: # Revision 1.11 2007/07/17 03:39:29 anupamsg Chris@0: # Moved the CVS Log keyword to end of the files. Chris@0: #