Update specs.

This commit is contained in:
Danila Fedorin 2020-12-07 17:05:05 -08:00
parent 44b60114a1
commit 2f24ae3250
2 changed files with 129 additions and 0 deletions

65
spec/heaphash_spec.cr Normal file
View File

@ -0,0 +1,65 @@
require "./spec_helper"
describe HeapHash do
describe "#[]" do
hh = HeapHash(String, Int32).new
it "correctly assigns new keys" do
hh["a"] = 5
hh["b"] = 3
hh["c"] = 10
hh["a"].should eq(5)
hh["b"].should eq(3)
hh["c"].should eq(10)
hh.size.should eq(3)
end
it "correctly updates the value of a key" do
hh["a"] = 0
hh["a"].should eq(0)
hh.size.should eq(3)
end
it "returns nil for non-existent keys" do
hh["?"]?.should be_nil
end
it "throws for non-existent keys" do
expect_raises(KeyError) do
hh["?"]
end
end
end
describe "#pop" do
it "pops keys in the right oder" do
hh = HeapHash(String, Int32).new
expect_raises(IndexError) do
hh.pop
end
hh["last"] = 1
hh["first"] = 10
hh["medium"] = 5
hh.pop.should eq({"first", 10})
hh.pop.should eq({"medium", 5})
hh.pop.should eq({"last", 1})
end
end
describe "#delete" do
it "doesn't break the heap property" do
hh = HeapHash(String, Int32).new
hh["last"] = 1
hh["first"] = 10
hh["medium"] = 5
popped = hh.delete "last"
popped.should eq({"last", 1})
hh.pop.should eq({"first", 10})
hh.pop.should eq({"medium", 5})
end
end
end

View File

@ -27,3 +27,67 @@ describe Array do
end
end
end
describe Tuple do
describe "#add" do
it "works for pairs" do
{1,2}.add({2,3}).should eq({3,5})
end
it "works for triples" do
{1,2,3}.add({3,4,5}).should eq({4, 6, 8})
end
end
describe "#neg" do
it "works for pairs" do
{1,2}.neg.should eq({-1,-2})
end
it "works for triples" do
{1,2,3}.neg.should eq({-1, -2, -3})
end
end
describe "#dot" do
it "works for pairs" do
{1,2}.dot({2,3}).should eq(8)
end
it "works for triples" do
{1,2,3}.dot({2,3,4}).should eq(20)
end
end
describe "#reduce_fraction" do
it "works with nonzero numbers" do
[{1, 3}, {2, 6}, {3, 9}].each(&.reduce_fraction.should eq({1,3}))
end
it "works when the denominator is zero" do
# This is technically not a fraction, but it helps to have
# consistent behavior
{5, 0}.reduce_fraction.should eq({1,0})
end
it "works when the numerator is zero" do
{0, 5}.reduce_fraction.should eq({0, 1})
end
end
end
describe Number do
describe "#signum" do
it "works properly for positive numbers" do
[1,2,3,4,5].each(&.signum.should eq(1))
end
it "works properly for negative numbers" do
[-1,-2,-3,-4].each(&.signum.should eq(-1))
end
it "works properly for zero" do
0.signum.should eq(0)
end
end
end