test/tree.rb in compsci-0.1.1.1 vs test/tree.rb in compsci-0.2.0.1

- old
+ new

@@ -1,24 +1,25 @@ +require 'compsci/node' require 'compsci/tree' require 'minitest/autorun' include CompSci describe Tree do before do - @tree = Tree.new(Node, 42) + @tree = Tree.new(FlexNode, 42) @vals = Array.new(99) { rand 99 } end it "is populated via the root node" do @vals.each { |v| @tree.root.new_child v } @tree.root.children.size.must_equal @vals.size end it "does depth_first search" do vals = (0..30).to_a - tree = Tree.new(Node, vals.shift) + tree = Tree.new(FlexNode, vals.shift) tree.root.new_child vals.shift tree.root.new_child vals.shift tree.root.children.each { |c| c.new_child vals.shift c.new_child vals.shift @@ -38,11 +39,11 @@ visited.must_equal [0, 1, 3, 5, 6, 4, 7, 8, 2, 9, 11, 12, 10, 13, 14] end it "does breadth_first search" do vals = (0..30).to_a - tree = Tree.new(Node, vals.shift) + tree = Tree.new(FlexNode, vals.shift) tree.root.new_child vals.shift tree.root.new_child vals.shift tree.root.children.each { |c| c.new_child vals.shift c.new_child vals.shift @@ -62,13 +63,36 @@ visited.must_equal [0, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8, 11, 12, 13, 14] end end describe NaryTree do - describe "with Node" do + it "must power_of?" do + powers = {} + basemax = 12 + expmax = 10 + 2.upto(basemax) { |base| + 0.upto(expmax) { |exp| + powers[base] ||= [] + powers[base] << base**exp + } + } + + # 12k assertions below! + 2.upto(basemax) { |base| + 1.upto(2**expmax) { |num| + if powers[base].include?(num) + NaryTree.power_of?(num, base).must_equal true + else + NaryTree.power_of?(num, base).must_equal false + end + } + } + end + + describe "with FlexNode" do before do - @tree = NaryTree.new(Node, 42, child_slots: 3) + @tree = NaryTree.new(FlexNode, 42, child_slots: 3) end it "must have an open parent" do @tree.open_parent?(@tree.root).must_equal true @tree.open_parent?(@tree.open_parent).must_equal true @@ -80,13 +104,13 @@ @tree.push 5 @tree.open_parent.children.size.must_equal opc + 1 end end - describe "with ChildNode" do + describe "with ChildFlexNode" do before do - @tree = NaryTree.new(ChildNode, 42, child_slots: 4) + @tree = NaryTree.new(ChildFlexNode, 42, child_slots: 4) end it "must have an open parent" do @tree.open_parent?(@tree.root).must_equal true @tree.open_parent?(@tree.open_parent).must_equal true @@ -101,11 +125,11 @@ end end describe "BinaryTree" do before do - @tree = BinaryTree.new(Node, 42) + @tree = BinaryTree.new(FlexNode, 42) end it "must have 2 child_slots" do @tree.child_slots.must_equal 2 end @@ -119,10 +143,10 @@ line_count.must_equal Math.log(item_count + 1, 2).ceil end describe "searching" do before do - @tree = NaryTree.new(Node, 42, child_slots: 2) + @tree = NaryTree.new(FlexNode, 42, child_slots: 2) 99.times { |i| @tree.push i } end it "must find 42 quickly" do count = 0