def test_get_codes(self, d):
        """the sum of len(code) * freq_dict[code] is optimal, so it
        must be invariant under permutation of the dictionary"""
        # NB: this also tests huffman_tree indirectly

        t = huffman_tree(d)
        c1 = get_codes(t)
        d2 = list(d.items())
        shuffle(d2)
        d2 = dict(d2)
        t2 = huffman_tree(d2)
        c2 = get_codes(t2)
        self.assertEqual(sum([d[k] * len(c1[k]) for k in d]),
                         sum([d2[k] * len(c2[k]) for k in d2]))
Ejemplo n.º 2
0
    def test_round_trip(self, b):
        """test inverting generate_compressed and generate_uncompressed"""

        orig_text = b
        freq = make_freq_dict(orig_text)
        assume(len(freq) > 1)
        tree = huffman_tree(freq)
        codes = get_codes(tree)
        compressed = generate_compressed(orig_text, codes)
        uncompressed = generate_uncompressed(tree, compressed, len(orig_text))
        assert orig_text == uncompressed
Ejemplo n.º 3
0
    def test_round_trip(self, b):
        """test inverting generate_compressed and generate_uncompressed"""

        orig_text = b
        freq = make_freq_dict(orig_text)
        assume(len(freq) > 1)
        tree = huffman_tree(freq)
        codes = get_codes(tree)
        compressed = generate_compressed(orig_text, codes)
        uncompressed = generate_uncompressed(tree, compressed, len(orig_text))
        assert orig_text == uncompressed  #, '\n'.join([str(list(orig_text)), str(codes), byte_to_bits(compressed[0]), str(list(uncompressed))])
    def test_generate_compressed(self, b):
        """generate_compressed should return a bytes
        object that is no longer than the input bytes, and
        the size of the compressed object should be
        invariant under permuting the input"""
        # NB: this also indirectly tests make_freq_dict, huffman_tree,
        # and get_codes

        d = make_freq_dict(b)
        t = huffman_tree(d)
        c = get_codes(t)
        compressed = generate_compressed(b, c)
        self.assertTrue(isinstance(compressed, bytes))
        self.assertTrue(len(compressed) <= len(b))
        l = list(b)
        shuffle(l)
        b = bytes(l)
        d = make_freq_dict(b)
        t = huffman_tree(d)
        c = get_codes(t)
        compressed2 = generate_compressed(b, c)
        self.assertEqual(len(compressed2), len(compressed))
Ejemplo n.º 5
0
    """  Sorted() by key test
    l = [[2, 3], [6, 7], [3, 34], [24, 64], [1, 43]]
    l = sorted(l, key=getkey)
    print(l)
    """

    """ Test using multiple arguements in for loop
    d = make_freq_dict(bytes([65, 66, 67, 66]))
    for k, v in d.items():
        print(k, 'corresponds to', v)
    """

    # Testing get_codes()
    freq = {2: 6, 3: 4, 4: 13, 5: 17}
    t = huffman_tree(freq)
    d = get_codes(t)
    print(t)
    print(d)

    freqs = {'a': 2}
    d = HuffmanNode()
    print(d)

    print(get_codes(d))

    print(bytes([65, 66, 67, 66]))

    left = HuffmanNode(None, HuffmanNode(3), HuffmanNode(2))
    right = HuffmanNode(None, HuffmanNode(9), HuffmanNode(10))
    tree = HuffmanNode(None, left, right)
    number_nodes(tree)