Пример #1
0
 def test_encode_bits_as_strings_nothing(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     output = huffman.encode_bits_as_strings(test_huffman[1], Bits())
     self.assertIsNotNone(output)
     self.assertEqual(output[0], Bits())
     self.assertEqual(output[1], "")
Пример #2
0
 def test_encode_bits_as_strings_padded(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     output = huffman.encode_bits_as_strings(test_huffman[1],
                                             Bits(bin="011100011010110"))
     self.assertIsNotNone(output)
     self.assertEqual("stegaanalynalysstegastegaganal", output[1])
Пример #3
0
 def test_serialise_tree_codes(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     serial_tree = test_huffman[1].__dict__()
     self.assertIsInstance(serial_tree, dict)
     self.assertIsInstance(serial_tree.get("left"), dict)
     path_code = serial_tree.get("left").get("path_code")
     self.assertEqual("0", path_code)
Пример #4
0
 def test_tree_has_nodes(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     test_passed = test_huffman is not None
     self.assertTrue(test_passed, "Huffman tree was empty")
     test_passed = test_passed and is_correct_tree(test_huffman)
     self.assertTrue(
         test_passed,
         "Huffman tree was not recursively defined by at least 1 subtree")
Пример #5
0
 def test_has_given_symbol_length(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     self.assertFalse(huffman.has_given_symbol_length(test_huffman, 4))
     self.assertFalse(huffman.has_given_symbol_length(test_huffman, 6))
     self.assertTrue(huffman.has_given_symbol_length(test_huffman, 5))
     self.assertRaises(ValueError, huffman.has_given_symbol_length, None, 1)
     self.assertRaises(ValueError, huffman.has_given_symbol_length,
                       [0, None], 1)
Пример #6
0
    def test_tree_has_non_leaf_values(self):
        test_huffman = huffman.create_tree(self.string_definitions)
        test_huffman[1].value = (1, "value")

        test_passed = has_correct_leaves(test_huffman)
        self.assertFalse(
            test_passed,
            "Huffman tree was recognised as correct even though a non-leaf had a value"
        )
Пример #7
0
 def test_serialise_tree(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     serial_tree = test_huffman[1].__dict__()
     self.assertIsInstance(serial_tree, dict)
     self.assertEqual(4, len(serial_tree))
     self.assertEqual(None, serial_tree.get("value"))
     self.assertEqual(None, serial_tree.get("path_code"))
     self.assertIsInstance(serial_tree.get("left"), dict)
     self.assertIsInstance(serial_tree.get("right"), dict)
Пример #8
0
 def test_search_tree_for_symbol(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     bits = huffman.search_tree_for_symbol(test_huffman[1], "alysi")
     self.assertEqual(bits, Bits(bin="0010"))
     bits = huffman.search_tree_for_symbol(test_huffman[1], "ysis ")
     self.assertEqual(bits, Bits(bin="1111"))
     bits = huffman.search_tree_for_symbol(test_huffman[1], "sis 0")
     self.assertEqual(bits, Bits(bin="11100"))
Пример #9
0
    def test_tree_missing_leaf_values(self):
        test_huffman = huffman.create_tree(self.string_definitions)

        node = test_huffman[1]
        while node.left is not None:
            node = node.left[1]
        node.value = None

        test_passed = has_correct_leaves(test_huffman)
        self.assertFalse(
            test_passed,
            "Huffman tree was recognised as correct even a leaf node was missing a value"
        )
Пример #10
0
    def test_tree_to_symbols(self):
        test_huffman = huffman.create_tree(self.string_definitions)
        huffman.allocate_path_bits(test_huffman)
        flattened_tree = huffman.tree_to_symbols(test_huffman)
        self.assertIsNotNone(flattened_tree)

        sorted_list = list(self.string_definitions)
        sorted_list.sort(key=lambda x: x[0])
        self.assertEqual(len(flattened_tree), len(sorted_list))
        for i in range(0, len(sorted_list)):
            # We are only interested in the string and frequency from tree nodes.
            this_tuple = flattened_tree[i][0], flattened_tree[i][1]
            self.assertTupleEqual(this_tuple, sorted_list[i])
Пример #11
0
    def test_variable_length_tree(self):
        self.string_definitions = set()
        self.string_definitions.add(("a", 15))
        self.string_definitions.add(("ba", 4))
        self.string_definitions.add(("abc", 33))
        self.string_definitions.add(("dcba", 17))
        self.string_definitions.add(("abcde", 10))

        test_huffman = huffman.create_tree(self.string_definitions)
        test_passed = has_correct_leaves(test_huffman)
        self.assertTrue(test_passed,
                        "Huffman tree did not have values for all leaves")
        huffman.allocate_path_bits(test_huffman)
        self.assertTrue(
            has_correct_bits(test_huffman, Bits()),
            "Huffman tree did not have correct bits for every node")
Пример #12
0
 def test_get_tree_leaf_codes(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     path_codes = huffman.get_tree_leaf_codes(test_huffman)
     self.assertNotEqual(set(), path_codes)
     self.assertEqual(10, len(path_codes))
Пример #13
0
 def test_allocate_path_bits(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     self.assertTrue(
         has_correct_bits(test_huffman, Bits()),
         "Huffman tree did not have correct bits for every node")
Пример #14
0
 def test_tree_has_leaves(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     test_passed = has_correct_leaves(test_huffman)
     self.assertTrue(test_passed,
                     "Huffman tree did not have values for all leaves")
Пример #15
0
 def test_tree_created(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     self.assertIsInstance(test_huffman[0], int)
     self.assertIsInstance(test_huffman[1], HuffmanTree)
Пример #16
0
 def test_encode_string_as_bits(self):
     test_huffman = huffman.create_tree(self.string_definitions)
     huffman.allocate_path_bits(test_huffman)
     bits = huffman.encode_string_as_bits(test_huffman[1],
                                          "stegaalysilysissis 0tegan", 5)
     self.assertEqual(bits, Bits(bin="0b0100101110111100000"))