$LOAD_PATH.unshift(File.dirname(__FILE__)) require 'test_helper' include BitLab class TestBits < Test::Unit::TestCase def test_00_banner print "\nBitLab" end # Make some Code objects def test_01_codes c0 = 10.code assert_equal c0.to_s, "1010" assert_equal c0.length, 4 assert_equal c0.value, 10 assert_equal c0.base, :binary c1 = 10.code(6) assert_equal c1.to_s, "001010" assert_equal c1.length, 6 assert_equal c1.value, 10 assert_equal c1.base, :binary c2 = 10.code(:hex) assert_equal c2.to_s, "A" assert_equal c2.length, 4 assert_equal c2.value, 10 assert_equal c2.base, :hex c3 = 10.code(:hex, 2) assert_equal c3.to_s, "0A" assert_equal c3.length, 8 assert_equal c3.value, 10 assert_equal c3.base, :hex end # Parity bit tests def test_02_parity a = ?A.code(8) assert_equal a.chr, "A" assert a.even_parity? assert_equal a.parity_bit, 0 a.add_parity_bit assert_equal a.length, 9 assert a.even_parity? c = ?C.code(8) assert_equal c.chr, "C" assert ! c.even_parity? assert_equal c.parity_bit, 1 c.add_parity_bit assert_equal c.length, 9 assert c.even_parity? c.flip(7) assert_equal c.to_s, "010000101" assert ! c.even_parity? end # Make an encoding scheme for 5 items -- we should get 5 binary codes from 000 to 100 def test_03_make_codes a = TestArray.new(5, :cars) assert_equal a.length, 5 c = make_codes(a) assert_equal c.length, 5 codes = c.values.sort assert_equal codes[0].length, 3 assert_equal codes[0].value, 0 assert_equal codes[-1].value, 4 end # Make some Message objects def test_04_messages s = "hello" msg1 = encode(s, :ascii) assert_equal msg1.length, 8 * s.length msg2 = encode(s, :parity) assert_equal msg2.length, 9 * s.length bang = ?!.code(8) msg1 << bang assert_equal decode(msg1, :ascii), s + "!" bang.add_parity_bit msg2 << bang assert_equal decode(msg2, :parity), s + "!" msg3 = msg1.copy assert_not_equal msg3.object_id, msg1.object_id msg3 << bang assert msg3.length > msg1.length end # Tree nodes def test_05_nodes n0 = Node.new("A", 0.2) assert_equal n0.char, "A" assert_equal n0.freq, 0.2 assert_nil n0.left assert_nil n0.right assert n0.leaf? n1 = Node.new("B", 0.4) n2 = Node.combine(n0, n1) assert_nil n2.char assert_equal n2.freq, n0.freq + n1.freq assert_equal n2.left, n0 assert_equal n2.right, n1 assert ! n2.leaf? end # Priority queue -- uses the class defined in RubyLabs, but adds some "hooks" def test_06_priority_queue n0 = Node.new("A", 0.2) n1 = Node.new("B", 0.4) n2 = Node.new("C", 0.3) pq = PriorityQueue.new [n0,n1,n2].each { |x| pq << x } assert_nil pq.on_canvas assert_equal pq.length, 3 assert_equal pq.first, n0 assert_equal pq.last, n1 pq << Node.combine(pq.shift, pq.shift) assert_equal pq.length, 2 assert_equal pq.first, n1 assert_equal pq.last.freq, n0.freq + n2.freq assert ! pq.last.leaf? end # Huffman tree and encoding def test_07_huffman freq = read_frequencies(:hafreq) tree = build_tree(freq) assert_equal tree.freq, 1.0 codes = assign_codes(tree) assert_equal codes["A"].to_s, "10" assert_equal codes["W"].to_s, "110010" msg = encode("ALOHA", tree) assert_equal msg.to_s, "100000010000110" assert_equal decode(msg, tree), "ALOHA" end end