# Copyright (c) 2011 David Love # # Based on the file testtree.rb, part of the RubyTree package. # Copyright (c) 2006, 2007, 2008, 2009, 2010 Anupam Sengupta. # # Modified for use with the Riot test framework, and integration into the other # tests # # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # - Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # - Redistributions in binary form must reproduce the above copyright notice, this # list of conditions and the following disclaimer in the documentation and/or # other materials provided with the distribution. # # - Neither the name of the organization nor the names of its contributors may # be used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # require 'tree' module Test::DataStructures # Create this structure for the tests # # +----------+ # | ROOT | # +-+--------+ # | # | +---------------+ # +----+ CHILD1 | # | +---------------+ # | # | +---------------+ # +----+ CHILD2 | # | +---------------+ # | # | +---------------+ +------------------+ # +----+ CHILD3 +---+ CHILD4 | # +---------------+ +------------------+ # # This test is for the root alone - without any children being linked context "Sanity tests for the Basic Tree root node" do setup { root = Tree::TreeNode.new("ROOT", "Root Node") } denies("root node"){ topic }.nil asserts("parent of root node") { topic.parent }.nil denies("root name") { topic.name }.nil asserts("root name") { topic.name }.equals("ROOT") asserts("root contents") { topic.content }.equals("Root Node") asserts("root node declares as the root node") { topic.is_root? } denies("root cannot have any children") { topic.has_children? } asserts("root has contents") { topic.has_content? } asserts("number of nodes should be one") { topic.size }.equals(1) asserts("root does not have any children") { topic.siblings.nil? } asserts("root should have an in-degree that") { topic.in_degree }.equals(0) asserts("root's height before adding any children") { topic.node_height }.equals(0) end context "Sanity tests for the Basic Tree after the children are linked to the root" do setup { @root = Tree::TreeNode.new("ROOT", "Root Node") @child1 = Tree::TreeNode.new("Child1", "Child Node 1") @child2 = Tree::TreeNode.new("Child2", "Child Node 2") @child3 = Tree::TreeNode.new("Child3", "Child Node 3") @child4 = Tree::TreeNode.new("Child4", "Grand Child 1") @child5 = Tree::TreeNode.new("Child5", "Child Node 4") @root << @child1 @root << @child2 @root << @child3 << @child4 @root } asserts("root's root is self") { topic.root == topic } asserts("child 1's root should be ROOT") { @child1.root == topic } asserts("child 4's root should be ROOT") { @child4.root == topic } asserts("root's height after adding the children") { topic.node_height }.equals(2) end context "Test the new nodes have no content" do setup { a_node = Tree::TreeNode.new("A Node") } asserts("the node should not have content") { topic.content }.nil denies("the node should not have content") { topic.has_content? } end context "Test the presence of content in the nodes" do setup { a_node = Tree::TreeNode.new("A Node") a_node.content = "Something" a_node } denies("the node should now have content") { topic.content }.nil asserts("the node should now have content") { topic.has_content? } end end