def test_number_nodes(self, d): """if the root is an interior node, it must be numbered two less than the number of symbols""" # a complete tree has one fewer interior nodes than # it has leaves, and we are numbering from 0 # NB: this also tests huffman_tree indirectly t = huffman_tree(d) assume(not t.is_leaf()) count = len(d) number_nodes(t) self.assertEqual(count, t.number + 2)
def test_num_nodes_to_bytes(self, b): """num_nodes_to_bytes returns a bytes object that has length 1 (since the number of internal nodes cannot exceed 256)""" # NB: also indirectly tests make_freq_dict and huffman_tree d = make_freq_dict(b) assume(len(d) > 1) t = huffman_tree(d) number_nodes(t) n = num_nodes_to_bytes(t) self.assertTrue(isinstance(n, bytes)) self.assertEqual(len(n), 1)
def test_tree_to_bytes(self, b): """tree_to_bytes generates a bytes representation of a post-order traversal of a trees internal nodes""" # Since each internal node requires 4 bytes to represent, # and there are 1 fewer internal node than distinct symbols, # the length of the bytes produced should be 4 times the # length of the frequency dictionary, minus 4""" # NB: also indirectly tests make_freq_dict, huffman_tree, and # number_nodes d = make_freq_dict(b) assume(len(d) > 1) t = huffman_tree(d) number_nodes(t) output_bytes = tree_to_bytes(t) dictionary_length = len(d) leaf_count = dictionary_length self.assertEqual(4 * (leaf_count - 1), len(output_bytes))