Commit 5a5a092
Changed files (11)
spec
spec/data_structures/avl_tree_spec.rb
@@ -1,156 +1,156 @@
require "spec_helper"
describe AVLTree do
- let(:sut) { AVLTree.new }
+ subject { AVLTree.new }
context "when empty" do
it "should have a size of 0" do
- expect(sut.size).to eq(0)
+ expect(subject.size).to eq(0)
end
it "should have have a height of 0" do
- expect(sut.height).to eq(0)
+ expect(subject.height).to eq(0)
end
end
context "when a single item is added" do
before :each do
- sut.push("A")
+ subject.push("A")
end
it "should have a size of 1" do
- expect(sut.size).to eq(1)
+ expect(subject.size).to eq(1)
end
it "should have a height of 1" do
- expect(sut.height).to eq(1)
+ expect(subject.height).to eq(1)
end
end
context "when two items are added" do
before :each do
- sut.add("a")
- sut.add("b")
+ subject.add("a")
+ subject.add("b")
end
it "should have a size of 2" do
- expect(sut.size).to eq(2)
+ expect(subject.size).to eq(2)
end
it "should have a height of 2" do
- expect(sut.height).to eq(2)
+ expect(subject.height).to eq(2)
end
end
context "when a balanced tree has 3 items" do
before :each do
- sut.add("b")
- sut.add("a")
- sut.add("c")
+ subject.add("b")
+ subject.add("a")
+ subject.add("c")
end
it "should have a size of 3" do
- expect(sut.size).to eq(3)
+ expect(subject.size).to eq(3)
end
it "should have a height of 2" do
- expect(sut.height).to eq(2)
+ expect(subject.height).to eq(2)
end
end
context "when the tree is unbalanced" do
context "with a right-right case" do
before :each do
- sut.add("a")
- sut.add("b")
- sut.add("c")
+ subject.add("a")
+ subject.add("b")
+ subject.add("c")
end
it "should re-balance it self" do
- expect(sut.height).to eq(2)
+ expect(subject.height).to eq(2)
end
it "should have a new root" do
- expect(sut.root.data).to eq("b")
+ expect(subject.root.data).to eq("b")
end
it "should change the left side" do
- expect(sut.root.left.data).to eq("a")
+ expect(subject.root.left.data).to eq("a")
end
it "should change the right side" do
- expect(sut.root.right.data).to eq("c")
+ expect(subject.root.right.data).to eq("c")
end
end
context "with a left-left case" do
before :each do
- sut.add("c")
- sut.add("b")
- sut.add("a")
+ subject.add("c")
+ subject.add("b")
+ subject.add("a")
end
it "should re-balance it self" do
- expect(sut.height).to eq(2)
+ expect(subject.height).to eq(2)
end
it "should have a new root" do
- expect(sut.root.data).to eq("b")
+ expect(subject.root.data).to eq("b")
end
it "should change the left side" do
- expect(sut.root.left.data).to eq("a")
+ expect(subject.root.left.data).to eq("a")
end
it "should change the right side" do
- expect(sut.root.right.data).to eq("c")
+ expect(subject.root.right.data).to eq("c")
end
end
context "with a left-right case" do
before :each do
- sut.add(5)
- sut.add(3)
- sut.add(4)
+ subject.add(5)
+ subject.add(3)
+ subject.add(4)
end
it "should adjust the height" do
- expect(sut.height).to eq(2)
+ expect(subject.height).to eq(2)
end
it "should have a new root" do
- expect(sut.root.data).to eq(4)
+ expect(subject.root.data).to eq(4)
end
it "should have a proper left side" do
- expect(sut.root.left.data).to eq(3)
+ expect(subject.root.left.data).to eq(3)
end
it "should have a proper right side" do
- expect(sut.root.right.data).to eq(5)
+ expect(subject.root.right.data).to eq(5)
end
end
context "with a right-left case" do
before :each do
- sut.add(3)
- sut.add(5)
- sut.add(4)
+ subject.add(3)
+ subject.add(5)
+ subject.add(4)
end
it "should adjust the height" do
- expect(sut.height).to eq(2)
+ expect(subject.height).to eq(2)
end
it "should have a new root" do
- expect(sut.root.data).to eq(4)
+ expect(subject.root.data).to eq(4)
end
it "should have a proper left side" do
- expect(sut.root.left.data).to eq(3)
+ expect(subject.root.left.data).to eq(3)
end
it "should have a proper right side" do
- expect(sut.root.right.data).to eq(5)
+ expect(subject.root.right.data).to eq(5)
end
end
end
spec/data_structures/binary_tree_spec.rb
@@ -4,27 +4,27 @@ require "spec_helper"
#each node has at most two children
#nodes with children are parent nodes.
describe BinaryTree do
- let(:sut) { BinaryTree.new }
+ subject { BinaryTree.new }
context "when there are no items in the tree" do
it "should have a size of 0" do
- expect(sut.size).to eq(0)
+ expect(subject.size).to eq(0)
end
end
context "when many items are pushed on to the tree" do
before :each do
10.times do |n|
- sut.push(rand(n))
+ subject.push(rand(n))
end
end
it "should increase the size" do
- expect(sut.size).to eq(10)
+ expect(subject.size).to eq(10)
end
it "can iterate through each item" do
- sut.each do |item|
+ subject.each do |item|
expect(item).not_to be_nil
end
end
spec/data_structures/dynamic_array_stack_spec.rb
@@ -1,29 +1,29 @@
require "spec_helper"
describe DynamicArrayStack do
- let(:sut) { DynamicArrayStack.new }
+ subject { DynamicArrayStack.new }
context "when there is nothing on the stack" do
it "should be able to pop off nil" do
- expect(sut.pop).to be_nil
+ expect(subject.pop).to be_nil
end
end
context "when there is one item on the stack" do
it "should be able to pop it off" do
n = rand
- sut.push(n)
- expect(sut.pop).to eq(n)
+ subject.push(n)
+ expect(subject.pop).to eq(n)
end
end
context "when there are multiple items on the stack" do
it "should pop each one off in the right order" do
(0..10).each do |n|
- sut.push(n)
+ subject.push(n)
end
(10..0).each do |n|
- expect(sut.pop).to eq(n)
+ expect(subject.pop).to eq(n)
end
end
end
spec/data_structures/hash_table_spec.rb
@@ -1,14 +1,14 @@
require "spec_helper"
describe HashTable do
- let(:sut) { HashTable.new }
+ subject { HashTable.new }
it "returns a default value" do
- expect(sut[:unkonwn]).to be_nil
+ expect(subject[:unkonwn]).to be_nil
end
it "can return the value associated to a key" do
- sut[:name] = 'mo'
- expect(sut[:name]).to eq('mo')
+ subject[:name] = 'mo'
+ expect(subject[:name]).to eq('mo')
end
end
spec/data_structures/linked_list_stack_spec.rb
@@ -1,10 +1,10 @@
require "spec_helper"
describe LinkedListStack do
- let(:sut) { LinkedListStack.new }
+ subject { LinkedListStack.new }
context "when no items are pushed on to the stack" do
- let(:result) { sut.pop }
+ let(:result) { subject.pop }
it "should pop nil if there is nothing on the stack" do
expect(result).to be_nil
@@ -16,21 +16,21 @@ describe LinkedListStack do
before :each do
(1..n).each do |n|
- sut.push(n)
+ subject.push(n)
end
end
it "should pop the last item pushed on to the stack" do
- expect(sut.pop).to eq(n)
+ expect(subject.pop).to eq(n)
end
it "should pop off each item in reverse order of how they were put on" do
(n..1).each do |n|
- expect(sut.pop).to eq(n)
+ expect(subject.pop).to eq(n)
end
end
it "should have the correct number of items" do
- expect(sut.count).to eq(n)
+ expect(subject.count).to eq(n)
end
end
end
spec/practice/fizzbuzz_spec.rb
@@ -17,26 +17,26 @@ class FizzBuzz
end
describe FizzBuzz do
- let(:sut) { FizzBuzz.new }
+ subject { FizzBuzz.new }
let(:printer) { StringIO.new }
it "should return Fizz for multiples of 3" do
- sut.print(3, printer)
+ subject.print(3, printer)
expect(printer.string).to eq("Fizz")
end
it "should return Buzz for multiples of 5" do
- sut.print(5, printer)
+ subject.print(5, printer)
expect(printer.string).to eq("Buzz")
end
it "should return FizzBuzz for multiples of 3 and 5" do
- sut.print(15, printer)
+ subject.print(15, printer)
expect(printer.string).to eq("FizzBuzz")
end
it "should return the number for everything else" do
- sut.print(16, printer)
+ subject.print(16, printer)
expect(printer.string).to eq("16")
end
end
spec/sorting/bubble_sort_spec.rb
@@ -1,21 +1,21 @@
require "spec_helper"
describe BubbleSort do
- let(:sut) { BubbleSort.new }
+ subject { BubbleSort.new }
it "should be able to sort an empty array" do
- expect(sut.sort([])).to eq([])
+ expect(subject.sort([])).to eq([])
end
it "should be able to sort an array with one item" do
- expect(sut.sort([1])).to eq([1])
+ expect(subject.sort([1])).to eq([1])
end
it "should be able to sort an array of numbers" do
n = 500
numbers = Array.new(n) { rand(n) }
Benchmark.bmbm do |x|
- x.report("bubble sort") { expect(sut.sort(numbers)).to eq(numbers.sort) }
+ x.report("bubble sort") { expect(subject.sort(numbers)).to eq(numbers.sort) }
end
end
end
spec/sorting/insertion_sort_spec.rb
@@ -1,21 +1,21 @@
require "spec_helper"
describe InsertionSort do
- let(:sut) { InsertionSort.new }
+ subject { InsertionSort.new }
it "should sort an empty array" do
- expect(sut.sort([])).to eq([])
+ expect(subject.sort([])).to eq([])
end
it "should sort an array with one item" do
- expect(sut.sort([1])).to eq([1])
+ expect(subject.sort([1])).to eq([1])
end
it "should sort an array of numbers" do
n = 200
numbers = Array.new(n) { rand(n) }
Benchmark.bmbm do |x|
- x.report("insertion sort") { expect(sut.sort(numbers)).to eq(numbers.sort) }
+ x.report("insertion sort") { expect(subject.sort(numbers)).to eq(numbers.sort) }
end
end
end
spec/sorting/merge_sort_spec.rb
@@ -1,20 +1,20 @@
require "spec_helper"
describe MergeSort do
- let(:sut) { MergeSort.new }
+ subject { MergeSort.new }
it "should sort an empty array" do
- expect(sut.sort([])).to eq([])
+ expect(subject.sort([])).to eq([])
end
it "should sort an array with one item" do
- expect(sut.sort([2])).to eq([2])
+ expect(subject.sort([2])).to eq([2])
end
it "should sort an array of numbers" do
n = 601
numbers = Array.new(n) { rand(n) }
sorted_numbers = numbers.sort
- expect(sut.sort(numbers)).to eq(sorted_numbers)
+ expect(subject.sort(numbers)).to eq(sorted_numbers)
end
end
spec/sorting/quick_sort_spec.rb
@@ -10,14 +10,14 @@ require "spec_helper"
# 3. recursively split up lists until the base case
# 4. combine the lesser list with the greater list
describe QuickSort do
- let(:sut) { QuickSort.new }
+ subject { QuickSort.new }
it "should sort an empty array" do
- expect(sut.sort([])).to eq([])
+ expect(subject.sort([])).to eq([])
end
it "should sort an array with one item" do
- expect(sut.sort([1])).to eq([1])
+ expect(subject.sort([1])).to eq([1])
end
it "should sort an array of numbers" do
@@ -25,7 +25,7 @@ describe QuickSort do
numbers = Array.new(n) { rand(n) }
sorted_numbers = numbers.sort
Benchmark.bmbm do |x|
- x.report("quick sort") { expect(sut.sort(numbers)).to eq(sorted_numbers) }
+ x.report("quick sort") { expect(subject.sort(numbers)).to eq(sorted_numbers) }
end
end
end
spec/traversal/in_order_traversal_spec.rb
@@ -1,7 +1,7 @@
require "spec_helper"
describe "inorder traversal" do
- let(:sut) { InOrderTraversal.new }
+ subject { InOrderTraversal.new }
let(:node) { double }
let(:left_node) { double }
let(:right_node) { double }
@@ -10,7 +10,7 @@ describe "inorder traversal" do
before :each do
allow(node).to receive(:left).and_return(left_node)
allow(node).to receive(:right).and_return(right_node)
- sut.traverse(node, visitor)
+ subject.traverse(node, visitor)
end
it "should visit the left node first" do