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