annotate .svn/pristine/a7/a771ea7eb3927275538eba94b36787bbcd868180.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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