require 'test/unit' require 'histogram' class TC_Histogram < Test::Unit::TestCase def test_empty histogram = Histogram.new assert(histogram.count == 0) assert(histogram.keys == []) end def test_loading h1 = Histogram.new "hello" h1.add "goodbye",2 assert(h1.count == 3) assert(h1.count("hello") == 1) assert(h1.count("goodbye") == 2) assert(h1.count("everyone") == 0) assert(h1.key_count == 2) h2 = Histogram.new [["hello", 4], "goodbye", "everyone"] assert(h2.count == 6) assert(h2.count("hello") == 4) assert(h2.count("goodbye") == 1) assert(h2.count("everyone") == 1) assert(h2.key_count == 3) h1.add h2 assert(h1.count == 9) assert(h1.count("hello") == 5) assert(h1.count("goodbye") == 3) assert(h1.count("everyone") == 1) assert(h1.key_count == 3) assert(h1.has_key?("hello")) assert(h1.has_key?("goodbye")) assert(h1.has_key?("everyone")) assert(!(h1.has_key? "help")) end def test_subset h = Histogram.new [["hello", 4], ["goodbye", 3], "everyone"] h1 = Histogram.new [["hello", 4], ["goodbye", 3]] assert((h >= 3) == h1) h2 = Histogram.new [["hello", 4]] assert((h > 3) == h2) assert((h == 4) == h2) h3 = Histogram.new [["goodbye", 3], "everyone"] assert((h <= 3) == h3) h4 = Histogram.new "everyone" assert((h < 3) == h4) end end