From 2f24ae32508fff5f26ad4681def8ef6faedcdbd8 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 7 Dec 2020 17:05:05 -0800 Subject: [PATCH] Update specs. --- spec/heaphash_spec.cr | 65 +++++++++++++++++++++++++++++++++++++++++++ spec/util_spec.cr | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 spec/heaphash_spec.cr diff --git a/spec/heaphash_spec.cr b/spec/heaphash_spec.cr new file mode 100644 index 0000000..e955635 --- /dev/null +++ b/spec/heaphash_spec.cr @@ -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 diff --git a/spec/util_spec.cr b/spec/util_spec.cr index 2097efa..1fd2142 100644 --- a/spec/util_spec.cr +++ b/spec/util_spec.cr @@ -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