require 'pqueue' require 'test/unit' class TC_PQueue < Test::Unit::TestCase ARY_TEST = [2,6,1,3,8,15,0,-4,7,8,10] ARY_TEST_2 = [25,10,5,13,16,9,16,12] def test_initialize assert_nothing_raised { PQueue.new } assert_nothing_raised { PQueue.new([3]) } assert_nothing_raised { PQueue.new(ARY_TEST) } assert_nothing_raised { PQueue.new {|a,b| a", PQueue.new(ARY_TEST).inspect) end def test_to_a q = PQueue.new(ARY_TEST) assert_equal(ARY_TEST.sort.reverse, q.sort) q = PQueue.new(0..4) assert_equal([4,3,2,1,0], q.sort) end def pop_array q = PQueue.new(ARY_TEST) assert_equal(ARY_TEST.sort.reverse[0..5], q.pop_array(5)) q = PQueue.new(ARY_TEST) assert_equal(ARY_TEST.sort.reverse, q.pop_array) end def test_include q = PQueue.new(ARY_TEST + [21] + ARY_TEST_2) assert_equal(true, q.include?(21)) q = PQueue.new(ARY_TEST - [15]) assert_equal(false, q.include?(15)) end def test_assert_equal assert_equal(PQueue.new, PQueue.new) assert_equal(PQueue.new(ARY_TEST), PQueue.new(ARY_TEST.sort_by{rand})) end def test_replace_top q = PQueue.new assert_nil(q.replace_top(6)) assert_equal(6, q.top) q = PQueue.new(ARY_TEST) h = PQueue.new(ARY_TEST) q.pop; q.push(11) h.replace_top(11) assert_equal(q, h) end def test_dup q = PQueue.new(ARY_TEST) assert_equal(q, q.dup) end def test_array_copied ary = ARY_TEST.dup q = PQueue.new(ary) q.pop assert_equal(ARY_TEST, ary) ary = ARY_TEST.dup q = PQueue.new q.replace(ary) q.pop assert_equal(ARY_TEST, ary) ary = ARY_TEST.dup q = PQueue.new([1]) q.push_all(ary) q.pop assert_equal(ARY_TEST, ary) q = PQueue.new(ARY_TEST) r = q.dup q.pop assert_not_equal(q, r) end end