annotate vendor/gems/rubytree-0.5.2/test/test_tree.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 #!/usr/bin/env ruby
Chris@0 2
Chris@0 3 # testtree.rb
Chris@0 4 #
Chris@0 5 # $Revision: 1.6 $ by $Author: anupamsg $
Chris@0 6 # $Name: $
Chris@0 7 #
Chris@0 8 # Copyright (c) 2006, 2007 Anupam Sengupta
Chris@0 9 #
Chris@0 10 # All rights reserved.
Chris@0 11 #
Chris@0 12 # Redistribution and use in source and binary forms, with or without modification,
Chris@0 13 # are permitted provided that the following conditions are met:
Chris@0 14 #
Chris@0 15 # - Redistributions of source code must retain the above copyright notice, this
Chris@0 16 # list of conditions and the following disclaimer.
Chris@0 17 #
Chris@0 18 # - Redistributions in binary form must reproduce the above copyright notice, this
Chris@0 19 # list of conditions and the following disclaimer in the documentation and/or
Chris@0 20 # other materials provided with the distribution.
Chris@0 21 #
Chris@0 22 # - Neither the name of the organization nor the names of its contributors may
Chris@0 23 # be used to endorse or promote products derived from this software without
Chris@0 24 # specific prior written permission.
Chris@0 25 #
Chris@0 26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@0 27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@0 28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Chris@0 29 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
Chris@0 30 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Chris@0 31 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Chris@0 32 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Chris@0 33 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@0 35 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 36 #
Chris@0 37
Chris@0 38 require 'test/unit'
Chris@0 39 require 'tree'
Chris@0 40
Chris@0 41 module TestTree
Chris@0 42 # Test class for the Tree node.
Chris@0 43 class TestTreeNode < Test::Unit::TestCase
Chris@0 44
Chris@0 45 Person = Struct::new(:First, :last)
Chris@0 46
Chris@0 47 def setup
Chris@0 48 @root = Tree::TreeNode.new("ROOT", "Root Node")
Chris@0 49
Chris@0 50 @child1 = Tree::TreeNode.new("Child1", "Child Node 1")
Chris@0 51 @child2 = Tree::TreeNode.new("Child2", "Child Node 2")
Chris@0 52 @child3 = Tree::TreeNode.new("Child3", "Child Node 3")
Chris@0 53 @child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
Chris@0 54
Chris@0 55 end
Chris@0 56
Chris@0 57 # Create this structure for the tests
Chris@0 58 #
Chris@0 59 # +----------+
Chris@0 60 # | ROOT |
Chris@0 61 # +-+--------+
Chris@0 62 # |
Chris@0 63 # | +---------------+
Chris@0 64 # +----+ CHILD1 |
Chris@0 65 # | +---------------+
Chris@0 66 # |
Chris@0 67 # | +---------------+
Chris@0 68 # +----+ CHILD2 |
Chris@0 69 # | +---------------+
Chris@0 70 # |
Chris@0 71 # | +---------------+ +------------------+
Chris@0 72 # +----+ CHILD3 +---+ CHILD4 |
Chris@0 73 # +---------------+ +------------------+
Chris@0 74 #
Chris@0 75 def loadChildren
Chris@0 76 @root << @child1
Chris@0 77 @root << @child2
Chris@0 78 @root << @child3 << @child4
Chris@0 79 end
Chris@0 80
Chris@0 81 def teardown
Chris@0 82 @root = nil
Chris@0 83 end
Chris@0 84
Chris@0 85 def test_root_setup
Chris@0 86 assert_not_nil(@root, "Root cannot be nil")
Chris@0 87 assert_nil(@root.parent, "Parent of root node should be nil")
Chris@0 88 assert_not_nil(@root.name, "Name should not be nil")
Chris@0 89 assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
Chris@0 90 assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
Chris@0 91 assert(@root.isRoot?, "Should identify as root")
Chris@0 92 assert(!@root.hasChildren?, "Cannot have any children")
Chris@0 93 assert(@root.hasContent?, "This root should have content")
Chris@0 94 assert_equal(1, @root.size, "Number of nodes should be one")
Chris@0 95 assert_nil(@root.siblings, "Root cannot have any children")
Chris@0 96
Chris@0 97 assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
Chris@0 98 end
Chris@0 99
Chris@0 100 def test_root
Chris@0 101 loadChildren
Chris@0 102
Chris@0 103 assert_same(@root, @root.root, "Root's root is self")
Chris@0 104 assert_same(@root, @child1.root, "Root should be ROOT")
Chris@0 105 assert_same(@root, @child4.root, "Root should be ROOT")
Chris@0 106 end
Chris@0 107
Chris@0 108 def test_hasContent_eh
Chris@0 109 aNode = Tree::TreeNode.new("A Node")
Chris@0 110 assert_nil(aNode.content, "The node should not have content")
Chris@0 111 assert(!aNode.hasContent?, "The node should not have content")
Chris@0 112
Chris@0 113 aNode.content = "Something"
Chris@0 114 assert_not_nil(aNode.content, "The node should now have content")
Chris@0 115 assert(aNode.hasContent?, "The node should now have content")
Chris@0 116 end
Chris@0 117
Chris@0 118 def test_length
Chris@0 119 loadChildren
Chris@0 120 assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
Chris@0 121 end
Chris@0 122
Chris@0 123 def test_spaceship # Test the <=> operator.
Chris@0 124 firstNode = Tree::TreeNode.new(1)
Chris@0 125 secondNode = Tree::TreeNode.new(2)
Chris@0 126
Chris@0 127 assert_equal(firstNode <=> nil, +1)
Chris@0 128 assert_equal(firstNode <=> secondNode, -1)
Chris@0 129
Chris@0 130 secondNode = Tree::TreeNode.new(1)
Chris@0 131 assert_equal(firstNode <=> secondNode, 0)
Chris@0 132
Chris@0 133 firstNode = Tree::TreeNode.new("ABC")
Chris@0 134 secondNode = Tree::TreeNode.new("XYZ")
Chris@0 135
Chris@0 136 assert_equal(firstNode <=> nil, +1)
Chris@0 137 assert_equal(firstNode <=> secondNode, -1)
Chris@0 138
Chris@0 139 secondNode = Tree::TreeNode.new("ABC")
Chris@0 140 assert_equal(firstNode <=> secondNode, 0)
Chris@0 141 end
Chris@0 142
Chris@0 143 def test_to_s
Chris@0 144 aNode = Tree::TreeNode.new("A Node", "Some Content")
Chris@0 145
Chris@0 146 expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
Chris@0 147
Chris@0 148 assert_equal(expectedString, aNode.to_s, "The string representation should be same")
Chris@0 149 end
Chris@0 150
Chris@0 151 def test_firstSibling
Chris@0 152 loadChildren
Chris@0 153
Chris@0 154 assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
Chris@0 155 assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
Chris@0 156 assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
Chris@0 157 assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
Chris@0 158 assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
Chris@0 159 end
Chris@0 160
Chris@0 161 def test_isFirstSibling_eh
Chris@0 162 loadChildren
Chris@0 163
Chris@0 164 assert(@root.isFirstSibling?, "Root's first sibling is itself")
Chris@0 165 assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
Chris@0 166 assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
Chris@0 167 assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
Chris@0 168 assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
Chris@0 169 end
Chris@0 170
Chris@0 171 def test_isLastSibling_eh
Chris@0 172 loadChildren
Chris@0 173
Chris@0 174 assert(@root.isLastSibling?, "Root's last sibling is itself")
Chris@0 175 assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
Chris@0 176 assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
Chris@0 177 assert( @child3.isLastSibling?, "Child3's last sibling is itself")
Chris@0 178 assert( @child4.isLastSibling?, "Child4's last sibling is itself")
Chris@0 179 end
Chris@0 180
Chris@0 181 def test_lastSibling
Chris@0 182 loadChildren
Chris@0 183
Chris@0 184 assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
Chris@0 185 assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
Chris@0 186 assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
Chris@0 187 assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
Chris@0 188 assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
Chris@0 189 end
Chris@0 190
Chris@0 191 def test_siblings
Chris@0 192 loadChildren
Chris@0 193
Chris@0 194 siblings = []
Chris@0 195 @child1.siblings { |sibling| siblings << sibling}
Chris@0 196 assert_equal(2, siblings.length, "Should have two siblings")
Chris@0 197 assert(siblings.include?(@child2), "Should have 2nd child as sibling")
Chris@0 198 assert(siblings.include?(@child3), "Should have 3rd child as sibling")
Chris@0 199
Chris@0 200 siblings.clear
Chris@0 201 siblings = @child1.siblings
Chris@0 202 assert_equal(2, siblings.length, "Should have two siblings")
Chris@0 203
Chris@0 204 siblings.clear
Chris@0 205 @child4.siblings {|sibling| siblings << sibling}
Chris@0 206 assert(siblings.empty?, "Should not have any children")
Chris@0 207
Chris@0 208 end
Chris@0 209
Chris@0 210 def test_isOnlyChild_eh
Chris@0 211 loadChildren
Chris@0 212
Chris@0 213 assert(!@child1.isOnlyChild?, "Child1 is not the only child")
Chris@0 214 assert(!@child2.isOnlyChild?, "Child2 is not the only child")
Chris@0 215 assert(!@child3.isOnlyChild?, "Child3 is not the only child")
Chris@0 216 assert( @child4.isOnlyChild?, "Child4 is not the only child")
Chris@0 217 end
Chris@0 218
Chris@0 219 def test_nextSibling
Chris@0 220 loadChildren
Chris@0 221
Chris@0 222 assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
Chris@0 223 assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
Chris@0 224 assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
Chris@0 225 assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
Chris@0 226 end
Chris@0 227
Chris@0 228 def test_previousSibling
Chris@0 229 loadChildren
Chris@0 230
Chris@0 231 assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
Chris@0 232 assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
Chris@0 233 assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
Chris@0 234 assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
Chris@0 235 end
Chris@0 236
Chris@0 237 def test_add
Chris@0 238 assert(!@root.hasChildren?, "Should not have any children")
Chris@0 239
Chris@0 240 @root.add(@child1)
Chris@0 241
Chris@0 242 @root << @child2
Chris@0 243
Chris@0 244 assert(@root.hasChildren?, "Should have children")
Chris@0 245 assert_equal(3, @root.size, "Should have three nodes")
Chris@0 246
Chris@0 247 @root << @child3 << @child4
Chris@0 248
Chris@0 249 assert_equal(5, @root.size, "Should have five nodes")
Chris@0 250 assert_equal(2, @child3.size, "Should have two nodes")
Chris@0 251
Chris@0 252 assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
Chris@0 253
Chris@0 254 end
Chris@0 255
Chris@0 256 def test_remove_bang
Chris@0 257 @root << @child1
Chris@0 258 @root << @child2
Chris@0 259
Chris@0 260 assert(@root.hasChildren?, "Should have children")
Chris@0 261 assert_equal(3, @root.size, "Should have three nodes")
Chris@0 262
Chris@0 263 @root.remove!(@child1)
Chris@0 264 assert_equal(2, @root.size, "Should have two nodes")
Chris@0 265 @root.remove!(@child2)
Chris@0 266
Chris@0 267 assert(!@root.hasChildren?, "Should have no children")
Chris@0 268 assert_equal(1, @root.size, "Should have one node")
Chris@0 269
Chris@0 270 @root << @child1
Chris@0 271 @root << @child2
Chris@0 272
Chris@0 273 assert(@root.hasChildren?, "Should have children")
Chris@0 274 assert_equal(3, @root.size, "Should have three nodes")
Chris@0 275
Chris@0 276 @root.removeAll!
Chris@0 277
Chris@0 278 assert(!@root.hasChildren?, "Should have no children")
Chris@0 279 assert_equal(1, @root.size, "Should have one node")
Chris@0 280
Chris@0 281 end
Chris@0 282
Chris@0 283 def test_removeAll_bang
Chris@0 284 loadChildren
Chris@0 285 assert(@root.hasChildren?, "Should have children")
Chris@0 286 @root.removeAll!
Chris@0 287
Chris@0 288 assert(!@root.hasChildren?, "Should have no children")
Chris@0 289 assert_equal(1, @root.size, "Should have one node")
Chris@0 290 end
Chris@0 291
Chris@0 292 def test_removeFromParent_bang
Chris@0 293 loadChildren
Chris@0 294 assert(@root.hasChildren?, "Should have children")
Chris@0 295 assert(!@root.isLeaf?, "Root is not a leaf here")
Chris@0 296
Chris@0 297 child1 = @root[0]
Chris@0 298 assert_not_nil(child1, "Child 1 should exist")
Chris@0 299 assert_same(@root, child1.root, "Child 1's root should be ROOT")
Chris@0 300 assert(@root.include?(child1), "root should have child1")
Chris@0 301 child1.removeFromParent!
Chris@0 302 assert_same(child1, child1.root, "Child 1's root should be self")
Chris@0 303 assert(!@root.include?(child1), "root should not have child1")
Chris@0 304
Chris@0 305 child1.removeFromParent!
Chris@0 306 assert_same(child1, child1.root, "Child 1's root should still be self")
Chris@0 307 end
Chris@0 308
Chris@0 309 def test_children
Chris@0 310 loadChildren
Chris@0 311
Chris@0 312 assert(@root.hasChildren?, "Should have children")
Chris@0 313 assert_equal(5, @root.size, "Should have four nodes")
Chris@0 314 assert(@child3.hasChildren?, "Should have children")
Chris@0 315 assert(!@child3.isLeaf?, "Should not be a leaf")
Chris@0 316
Chris@0 317 children = []
Chris@0 318 for child in @root.children
Chris@0 319 children << child
Chris@0 320 end
Chris@0 321
Chris@0 322 assert_equal(3, children.length, "Should have three direct children")
Chris@0 323 assert(!children.include?(@root), "Should not have root")
Chris@0 324 assert(children.include?(@child1), "Should have child 1")
Chris@0 325 assert(children.include?(@child2), "Should have child 2")
Chris@0 326 assert(children.include?(@child3), "Should have child 3")
Chris@0 327 assert(!children.include?(@child4), "Should not have child 4")
Chris@0 328
Chris@0 329 children.clear
Chris@0 330 children = @root.children
Chris@0 331 assert_equal(3, children.length, "Should have three children")
Chris@0 332
Chris@0 333 end
Chris@0 334
Chris@0 335 def test_firstChild
Chris@0 336 loadChildren
Chris@0 337
Chris@0 338 assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
Chris@0 339 assert_nil(@child1.firstChild, "Child1 does not have any children")
Chris@0 340 assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
Chris@0 341
Chris@0 342 end
Chris@0 343
Chris@0 344 def test_lastChild
Chris@0 345 loadChildren
Chris@0 346
Chris@0 347 assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
Chris@0 348 assert_nil(@child1.lastChild, "Child1 does not have any children")
Chris@0 349 assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
Chris@0 350
Chris@0 351 end
Chris@0 352
Chris@0 353 def test_find
Chris@0 354 loadChildren
Chris@0 355 foundNode = @root.find { |node| node == @child2}
Chris@0 356 assert_same(@child2, foundNode, "The node should be Child 2")
Chris@0 357
Chris@0 358 foundNode = @root.find { |node| node == @child4}
Chris@0 359 assert_same(@child4, foundNode, "The node should be Child 4")
Chris@0 360
Chris@0 361 foundNode = @root.find { |node| node.name == "Child31" }
Chris@0 362 assert_same(@child4, foundNode, "The node should be Child 4")
Chris@0 363 foundNode = @root.find { |node| node.name == "NOT PRESENT" }
Chris@0 364 assert_nil(foundNode, "The node should not be found")
Chris@0 365 end
Chris@0 366
Chris@0 367 def test_parentage
Chris@0 368 loadChildren
Chris@0 369
Chris@0 370 assert_nil(@root.parentage, "Root does not have any parentage")
Chris@0 371 assert_equal([@root], @child1.parentage, "Child1 has Root as its parent")
Chris@0 372 assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors")
Chris@0 373 end
Chris@0 374
Chris@0 375 def test_each
Chris@0 376 loadChildren
Chris@0 377 assert(@root.hasChildren?, "Should have children")
Chris@0 378 assert_equal(5, @root.size, "Should have five nodes")
Chris@0 379 assert(@child3.hasChildren?, "Should have children")
Chris@0 380
Chris@0 381 nodes = []
Chris@0 382 @root.each { |node| nodes << node }
Chris@0 383
Chris@0 384 assert_equal(5, nodes.length, "Should have FIVE NODES")
Chris@0 385 assert(nodes.include?(@root), "Should have root")
Chris@0 386 assert(nodes.include?(@child1), "Should have child 1")
Chris@0 387 assert(nodes.include?(@child2), "Should have child 2")
Chris@0 388 assert(nodes.include?(@child3), "Should have child 3")
Chris@0 389 assert(nodes.include?(@child4), "Should have child 4")
Chris@0 390 end
Chris@0 391
Chris@0 392 def test_each_leaf
Chris@0 393 loadChildren
Chris@0 394
Chris@0 395 nodes = []
Chris@0 396 @root.each_leaf { |node| nodes << node }
Chris@0 397
Chris@0 398 assert_equal(3, nodes.length, "Should have THREE LEAF NODES")
Chris@0 399 assert(!nodes.include?(@root), "Should not have root")
Chris@0 400 assert(nodes.include?(@child1), "Should have child 1")
Chris@0 401 assert(nodes.include?(@child2), "Should have child 2")
Chris@0 402 assert(!nodes.include?(@child3), "Should not have child 3")
Chris@0 403 assert(nodes.include?(@child4), "Should have child 4")
Chris@0 404 end
Chris@0 405
Chris@0 406 def test_parent
Chris@0 407 loadChildren
Chris@0 408 assert_nil(@root.parent, "Root's parent should be nil")
Chris@0 409 assert_equal(@root, @child1.parent, "Parent should be root")
Chris@0 410 assert_equal(@root, @child3.parent, "Parent should be root")
Chris@0 411 assert_equal(@child3, @child4.parent, "Parent should be child3")
Chris@0 412 assert_equal(@root, @child4.parent.parent, "Parent should be root")
Chris@0 413 end
Chris@0 414
Chris@0 415 def test_indexed_access
Chris@0 416 loadChildren
Chris@0 417 assert_equal(@child1, @root[0], "Should be the first child")
Chris@0 418 assert_equal(@child4, @root[2][0], "Should be the grandchild")
Chris@0 419 assert_nil(@root["TEST"], "Should be nil")
Chris@0 420 assert_raise(RuntimeError) { @root[nil] }
Chris@0 421 end
Chris@0 422
Chris@0 423 def test_printTree
Chris@0 424 loadChildren
Chris@0 425 #puts
Chris@0 426 #@root.printTree
Chris@0 427 end
Chris@0 428
Chris@0 429 # Tests the binary dumping mechanism with an Object content node
Chris@0 430 def test_marshal_dump
Chris@0 431 # Setup Test Data
Chris@0 432 test_root = Tree::TreeNode.new("ROOT", "Root Node")
Chris@0 433 test_content = {"KEY1" => "Value1", "KEY2" => "Value2" }
Chris@0 434 test_child = Tree::TreeNode.new("Child", test_content)
Chris@0 435 test_content2 = ["AValue1", "AValue2", "AValue3"]
Chris@0 436 test_grand_child = Tree::TreeNode.new("Grand Child 1", test_content2)
Chris@0 437 test_root << test_child << test_grand_child
Chris@0 438
Chris@0 439 # Perform the test operation
Chris@0 440 data = Marshal.dump(test_root) # Marshal
Chris@0 441 new_root = Marshal.load(data) # And unmarshal
Chris@0 442
Chris@0 443 # Test the root node
Chris@0 444 assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
Chris@0 445 assert_equal(test_root.content, new_root.content, "Must have root's content")
Chris@0 446 assert(new_root.isRoot?, "Must be the ROOT node")
Chris@0 447 assert(new_root.hasChildren?, "Must have a child node")
Chris@0 448
Chris@0 449 # Test the child node
Chris@0 450 new_child = new_root[test_child.name]
Chris@0 451 assert_equal(test_child.name, new_child.name, "Must have child 1")
Chris@0 452 assert(new_child.hasContent?, "Child must have content")
Chris@0 453 assert(new_child.isOnlyChild?, "Child must be the only child")
Chris@0 454
Chris@0 455 new_child_content = new_child.content
Chris@0 456 assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
Chris@0 457 assert_equal(test_child.content.size, new_child_content.size, "The content should have same size")
Chris@0 458
Chris@0 459 # Test the grand-child node
Chris@0 460 new_grand_child = new_child[test_grand_child.name]
Chris@0 461 assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
Chris@0 462 assert(new_grand_child.hasContent?, "Grand-child must have content")
Chris@0 463 assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
Chris@0 464
Chris@0 465 new_grand_child_content = new_grand_child.content
Chris@0 466 assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
Chris@0 467 assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size")
Chris@0 468 end
Chris@0 469
Chris@0 470 # marshal_load and marshal_dump are symmetric methods
Chris@0 471 # This alias is for satisfying ZenTest
Chris@0 472 alias test_marshal_load test_marshal_dump
Chris@0 473
Chris@0 474 # Test the collect method from the mixed-in Enumerable functionality.
Chris@0 475 def test_collect
Chris@0 476 loadChildren
Chris@0 477 collectArray = @root.collect do |node|
Chris@0 478 node.content = "abc"
Chris@0 479 node
Chris@0 480 end
Chris@0 481 collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
Chris@0 482 end
Chris@0 483
Chris@0 484 # Test freezing the tree
Chris@0 485 def test_freezeTree_bang
Chris@0 486 loadChildren
Chris@0 487 @root.content = "ABC"
Chris@0 488 assert_equal("ABC", @root.content, "Content should be 'ABC'")
Chris@0 489 @root.freezeTree!
Chris@0 490 assert_raise(TypeError) {@root.content = "123"}
Chris@0 491 assert_raise(TypeError) {@root[0].content = "123"}
Chris@0 492 end
Chris@0 493
Chris@0 494 # Test whether the content is accesible
Chris@0 495 def test_content
Chris@0 496 pers = Person::new("John", "Doe")
Chris@0 497 @root.content = pers
Chris@0 498 assert_same(pers, @root.content, "Content should be the same")
Chris@0 499 end
Chris@0 500
Chris@0 501 # Test the depth computation algorithm
Chris@0 502 def test_depth
Chris@0 503 assert_equal(1, @root.depth, "A single node's depth is 1")
Chris@0 504
Chris@0 505 @root << @child1
Chris@0 506 assert_equal(2, @root.depth, "This should be of depth 2")
Chris@0 507
Chris@0 508 @root << @child2
Chris@0 509 assert_equal(2, @root.depth, "This should be of depth 2")
Chris@0 510
Chris@0 511 @child2 << @child3
Chris@0 512 assert_equal(3, @root.depth, "This should be of depth 3")
Chris@0 513 assert_equal(2, @child2.depth, "This should be of depth 2")
Chris@0 514
Chris@0 515 @child3 << @child4
Chris@0 516 assert_equal(4, @root.depth, "This should be of depth 4")
Chris@0 517 end
Chris@0 518
Chris@0 519 # Test the breadth computation algorithm
Chris@0 520 def test_breadth
Chris@0 521 assert_equal(1, @root.breadth, "A single node's breadth is 1")
Chris@0 522
Chris@0 523 @root << @child1
Chris@0 524 assert_equal(1, @root.breadth, "This should be of breadth 1")
Chris@0 525
Chris@0 526 @root << @child2
Chris@0 527 assert_equal(2, @child1.breadth, "This should be of breadth 2")
Chris@0 528 assert_equal(2, @child2.breadth, "This should be of breadth 2")
Chris@0 529
Chris@0 530 @root << @child3
Chris@0 531 assert_equal(3, @child1.breadth, "This should be of breadth 3")
Chris@0 532 assert_equal(3, @child2.breadth, "This should be of breadth 3")
Chris@0 533
Chris@0 534 @child3 << @child4
Chris@0 535 assert_equal(1, @child4.breadth, "This should be of breadth 1")
Chris@0 536 end
Chris@0 537
Chris@0 538 # Test the breadth for each
Chris@0 539 def test_breadth_each
Chris@0 540 j = Tree::TreeNode.new("j")
Chris@0 541 f = Tree::TreeNode.new("f")
Chris@0 542 k = Tree::TreeNode.new("k")
Chris@0 543 a = Tree::TreeNode.new("a")
Chris@0 544 d = Tree::TreeNode.new("d")
Chris@0 545 h = Tree::TreeNode.new("h")
Chris@0 546 z = Tree::TreeNode.new("z")
Chris@0 547
Chris@0 548 # The expected order of response
Chris@0 549 expected_array = [j,
Chris@0 550 f, k,
Chris@0 551 a, h, z,
Chris@0 552 d]
Chris@0 553
Chris@0 554 # Create the following Tree
Chris@0 555 # j <-- level 0 (Root)
Chris@0 556 # / \
Chris@0 557 # f k <-- level 1
Chris@0 558 # / \ \
Chris@0 559 # a h z <-- level 2
Chris@0 560 # \
Chris@0 561 # d <-- level 3
Chris@0 562 j << f << a << d
Chris@0 563 f << h
Chris@0 564 j << k << z
Chris@0 565
Chris@0 566 # Create the response
Chris@0 567 result_array = Array.new
Chris@0 568 j.breadth_each { |node| result_array << node.detached_copy }
Chris@0 569
Chris@0 570 expected_array.each_index do |i|
Chris@0 571 assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
Chris@0 572 end
Chris@0 573 end
Chris@0 574
Chris@0 575
Chris@0 576 def test_preordered_each
Chris@0 577 j = Tree::TreeNode.new("j")
Chris@0 578 f = Tree::TreeNode.new("f")
Chris@0 579 k = Tree::TreeNode.new("k")
Chris@0 580 a = Tree::TreeNode.new("a")
Chris@0 581 d = Tree::TreeNode.new("d")
Chris@0 582 h = Tree::TreeNode.new("h")
Chris@0 583 z = Tree::TreeNode.new("z")
Chris@0 584
Chris@0 585 # The expected order of response
Chris@0 586 expected_array = [j, f, a, d, h, k, z]
Chris@0 587
Chris@0 588 # Create the following Tree
Chris@0 589 # j <-- level 0 (Root)
Chris@0 590 # / \
Chris@0 591 # f k <-- level 1
Chris@0 592 # / \ \
Chris@0 593 # a h z <-- level 2
Chris@0 594 # \
Chris@0 595 # d <-- level 3
Chris@0 596 j << f << a << d
Chris@0 597 f << h
Chris@0 598 j << k << z
Chris@0 599
Chris@0 600 result_array = []
Chris@0 601 j.preordered_each { |node| result_array << node.detached_copy}
Chris@0 602
Chris@0 603 expected_array.each_index do |i|
Chris@0 604 # Match only the names.
Chris@0 605 assert_equal(expected_array[i].name, result_array[i].name)
Chris@0 606 end
Chris@0 607 end
Chris@0 608
Chris@0 609 def test_detached_copy
Chris@0 610 loadChildren
Chris@0 611
Chris@0 612 assert(@root.hasChildren?, "The root should have children")
Chris@0 613 copy_of_root = @root.detached_copy
Chris@0 614 assert(!copy_of_root.hasChildren?, "The copy should not have children")
Chris@0 615 assert_equal(@root.name, copy_of_root.name, "The names should be equal")
Chris@0 616
Chris@0 617 # Try the same test with a child node
Chris@0 618 assert(!@child3.isRoot?, "Child 3 is not a root")
Chris@0 619 assert(@child3.hasChildren?, "Child 3 has children")
Chris@0 620 copy_of_child3 = @child3.detached_copy
Chris@0 621 assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
Chris@0 622 assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
Chris@0 623 end
Chris@0 624
Chris@0 625 def test_hasChildren_eh
Chris@0 626 loadChildren
Chris@0 627 assert(@root.hasChildren?, "The Root node MUST have children")
Chris@0 628 end
Chris@0 629
Chris@0 630 def test_isLeaf_eh
Chris@0 631 loadChildren
Chris@0 632 assert(!@child3.isLeaf?, "Child 3 is not a leaf node")
Chris@0 633 assert(@child4.isLeaf?, "Child 4 is a leaf node")
Chris@0 634 end
Chris@0 635
Chris@0 636 def test_isRoot_eh
Chris@0 637 loadChildren
Chris@0 638 assert(@root.isRoot?, "The ROOT node must respond as the root node")
Chris@0 639 end
Chris@0 640
Chris@0 641 def test_content_equals
Chris@0 642 @root.content = nil
Chris@0 643 assert_nil(@root.content, "Root's content should be nil")
Chris@0 644 @root.content = "ABCD"
Chris@0 645 assert_equal("ABCD", @root.content, "Root's content should now be 'ABCD'")
Chris@0 646 end
Chris@0 647
Chris@0 648 def test_size
Chris@0 649 assert_equal(1, @root.size, "Root's size should be 1")
Chris@0 650 loadChildren
Chris@0 651 assert_equal(5, @root.size, "Root's size should be 5")
Chris@0 652 assert_equal(2, @child3.size, "Child 3's size should be 2")
Chris@0 653 end
Chris@0 654
Chris@0 655 def test_lt2 # Test the << method
Chris@0 656 @root << @child1
Chris@0 657 @root << @child2
Chris@0 658 @root << @child3 << @child4
Chris@0 659 assert_not_nil(@root['Child1'], "Child 1 should have been added to Root")
Chris@0 660 assert_not_nil(@root['Child2'], "Child 2 should have been added to Root")
Chris@0 661 assert_not_nil(@root['Child3'], "Child 3 should have been added to Root")
Chris@0 662 assert_not_nil(@child3['Child31'], "Child 31 should have been added to Child3")
Chris@0 663 end
Chris@0 664
Chris@0 665 def test_index # Test the [] method
Chris@0 666 assert_raise(RuntimeError) {@root[nil]}
Chris@0 667
Chris@0 668 @root << @child1
Chris@0 669 @root << @child2
Chris@0 670 assert_equal(@child1.name, @root['Child1'].name, "Child 1 should be returned")
Chris@0 671 assert_equal(@child1.name, @root[0].name, "Child 1 should be returned")
Chris@0 672 assert_equal(@child2.name, @root['Child2'].name, "Child 2 should be returned")
Chris@0 673 assert_equal(@child2.name, @root[1].name, "Child 2 should be returned")
Chris@0 674
Chris@0 675 assert_nil(@root['Some Random Name'], "Should return nil")
Chris@0 676 assert_nil(@root[99], "Should return nil")
Chris@0 677 end
Chris@0 678 end
Chris@0 679 end
Chris@0 680
Chris@0 681 __END__
Chris@0 682
Chris@0 683 # $Log: test_tree.rb,v $
Chris@0 684 # Revision 1.6 2007/12/22 00:28:59 anupamsg
Chris@0 685 # Added more test cases, and enabled ZenTest compatibility.
Chris@0 686 #
Chris@0 687 # Revision 1.5 2007/12/19 02:24:18 anupamsg
Chris@0 688 # Updated the marshalling logic to handle non-string contents on the nodes.
Chris@0 689 #
Chris@0 690 # Revision 1.4 2007/10/02 03:38:11 anupamsg
Chris@0 691 # Removed dependency on the redundant "Person" class.
Chris@0 692 # (TC_TreeTest::test_comparator): Added a new test for the spaceship operator.
Chris@0 693 # (TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods.
Chris@0 694 #
Chris@0 695 # Revision 1.3 2007/10/02 03:07:30 anupamsg
Chris@0 696 # * Rakefile: Added an optional task for rcov code coverage.
Chris@0 697 #
Chris@0 698 # * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
Chris@0 699 #
Chris@0 700 # * test/test_tree.rb: Removed dependency on the redundant "Person" class.
Chris@0 701 #
Chris@0 702 # Revision 1.2 2007/08/31 01:16:28 anupamsg
Chris@0 703 # Added breadth and pre-order traversals for the tree. Also added a method
Chris@0 704 # to return the detached copy of a node from the tree.
Chris@0 705 #
Chris@0 706 # Revision 1.1 2007/07/21 04:52:38 anupamsg
Chris@0 707 # Renamed the test files.
Chris@0 708 #
Chris@0 709 # Revision 1.13 2007/07/18 22:11:50 anupamsg
Chris@0 710 # Added depth and breadth methods for the TreeNode.
Chris@0 711 #
Chris@0 712 # Revision 1.12 2007/07/18 07:17:34 anupamsg
Chris@0 713 # Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
Chris@0 714 # has been renamed to TreeNode.parentage.
Chris@0 715 #
Chris@0 716 # Revision 1.11 2007/07/17 03:39:29 anupamsg
Chris@0 717 # Moved the CVS Log keyword to end of the files.
Chris@0 718 #