|
1 | 1 | require "spec"
|
2 | 2 | require "../src/*"
|
3 | 3 |
|
4 |
| -describe "Node" do |
| 4 | +describe "BinarySearchTree" do |
5 | 5 | it "sets the root node" do
|
6 | 6 | root = Node.new(1)
|
7 | 7 | root.value.should eq(1)
|
@@ -66,40 +66,6 @@ describe "Node" do
|
66 | 66 | value.should eq(test_array.shift)
|
67 | 67 | end
|
68 | 68 | end
|
69 |
| - |
70 |
| - # Bonus! |
71 |
| - pending "is an Enumerable" do |
72 |
| - tree = Node.new(1) |
73 |
| - tree.insert(5) |
74 |
| - tree.insert(2) |
75 |
| - tree.should be_a(Enumerable(Int32)) |
76 |
| - mapped_values = tree.map { |value| value * 10 } |
77 |
| - mapped_values.should eq([10, 20, 50]) |
78 |
| - end |
79 |
| - |
80 |
| - # Advanced |
81 |
| - pending "will return an iterator if no block is provided" do |
82 |
| - tree = Node.new(1) |
83 |
| - tree.insert(5) |
84 |
| - tree.insert(2) |
85 |
| - iter = tree.each |
86 |
| - iter.next.should eq 1 |
87 |
| - iter.next.should eq 2 |
88 |
| - iter.next.should eq 5 |
89 |
| - end |
90 |
| - |
91 |
| - # Bonus! |
92 |
| - pending "is Iterable" do |
93 |
| - tree = Node.new(100) |
94 |
| - tree.insert(50) |
95 |
| - tree.insert(20) |
96 |
| - tree.insert(30) |
97 |
| - tree.should be_a(Iterable(Int32)) |
98 |
| - iter = tree.each_cons(2) |
99 |
| - iter.next.should eq([20, 30]) |
100 |
| - iter.next.should eq([30, 50]) |
101 |
| - iter.next.should eq([50, 100]) |
102 |
| - end |
103 | 69 | end
|
104 | 70 |
|
105 | 71 | # Deletion from a binary search tree https://en.wikipedia.org/wiki/Binary_search_tree#Deletion
|
@@ -179,4 +145,46 @@ describe "Node" do
|
179 | 145 | end
|
180 | 146 | end
|
181 | 147 | end
|
| 148 | + |
| 149 | + # The following tests check for additional features to the Binary Search Tree |
| 150 | + # They are not required to implement a complete BST |
| 151 | + # Instead they should be used to dive a little deeper into the Crystal language |
| 152 | + describe "crystal-lang specific" do |
| 153 | + # Make the Binary Search Tree Enumerable |
| 154 | + # See https://crystal-lang.org/api/0.20.3/Enumerable.html |
| 155 | + pending "is an Enumerable" do |
| 156 | + tree = Node.new(1) |
| 157 | + tree.insert(5) |
| 158 | + tree.insert(2) |
| 159 | + tree.should be_a(Enumerable(Int32)) |
| 160 | + mapped_values = tree.map { |value| value * 10 } |
| 161 | + mapped_values.should eq([10, 20, 50]) |
| 162 | + end |
| 163 | + |
| 164 | + # If no block is provided to the each method return an Iterator |
| 165 | + # See https://crystal-lang.org/api/0.20.3/Iterator.html |
| 166 | + pending "will return an iterator if no block is provided" do |
| 167 | + tree = Node.new(1) |
| 168 | + tree.insert(5) |
| 169 | + tree.insert(2) |
| 170 | + iter = tree.each |
| 171 | + iter.next.should eq 1 |
| 172 | + iter.next.should eq 2 |
| 173 | + iter.next.should eq 5 |
| 174 | + end |
| 175 | + |
| 176 | + # Make the Binary Search Tree Iterable |
| 177 | + # See https://crystal-lang.org/api/0.20.3/Iterable.html |
| 178 | + pending "is Iterable" do |
| 179 | + tree = Node.new(100) |
| 180 | + tree.insert(50) |
| 181 | + tree.insert(20) |
| 182 | + tree.insert(30) |
| 183 | + tree.should be_a(Iterable(Int32)) |
| 184 | + iter = tree.each_cons(2) |
| 185 | + iter.next.should eq([20, 30]) |
| 186 | + iter.next.should eq([30, 50]) |
| 187 | + iter.next.should eq([50, 100]) |
| 188 | + end |
| 189 | + end |
182 | 190 | end
|
0 commit comments