Example #1
0
 def test_compare_many_chars(self):
     a = sut.Node("a", 1)
     b = sut.Node("b", 1)
     c = sut.Node("c", 2)
     ab = sut.Node(a, b)
     self.assertGreater(0, sut.is_left_ok(ab, c))
     self.assertLess(0, sut.is_left_ok(c, ab))
Example #2
0
 def test_compare_tree(self):
     a = sut.Node("a", 2)
     b = sut.Node("b", 1)
     c = sut.Node("c", 1)
     bc = sut.Node(b, c)
     self.assertGreater(0, sut.is_left_ok(bc, a))
     self.assertLess(0, sut.is_left_ok(a, bc))
Example #3
0
    def test_sort_nodes_1(self):
        a = sut.Node("a", 1)
        b = sut.Node("b", 1)
        c = sut.Node("c", 1)

        ref = [a, b, c]
        output = sut.sort_nodes([b, a, c])
        self.assertEqual(ref, output)
Example #4
0
 def test_build_huffman_tree(self):
     a = sut.Node("a", 1)
     b = sut.Node("b", 2)
     ab = sut.Node(a, b)
     c = sut.Node("c", 4)
     abc = sut.Node(ab, c)
     self.assertEqual(ab, sut.build_huffman_tree({"a": 1, "b": 2}))
     self.assertEqual(abc, sut.build_huffman_tree({"a": 1, "b": 2, "c": 4}))
Example #5
0
 def test_init_ancestor(self):
     a = sut.Node("a", 42)
     b = sut.Node("b", 1337)
     node = sut.Node(a, b)
     self.assertEqual("ab", node.value())
     self.assertEqual(42 + 1337, node.priority())
     self.assertEqual(a, node._leftChild)
     self.assertEqual(b, node._rightChild)
Example #6
0
    def test_sort_nodes_2(self):
        b = sut.Node("b", 1)
        c = sut.Node("c", 2)
        a = sut.Node("a", 4)
        z = sut.Node("z", 1)

        ref = [b, z, c, a]
        output = sut.sort_nodes([b, z, c, a])
        self.assertEqual(ref, output)
Example #7
0
 def test_make_trees_from_dict(self):
     d1 = []
     self.assertEqual(d1, sut.make_trees_from_dict({}))
     d2 = [sut.Node("a", 1)]
     self.assertEqual(d2, sut.make_trees_from_dict({"a": 1}))
     d3 = [sut.Node("a", 1), sut.Node(
         "b",
         2,
     )]
     self.assertEqual(d3, sut.make_trees_from_dict({"a": 1, "b": 2}))
Example #8
0
    def test_left_most_leaf(self):
        a = sut.Node("a", 1)
        self.assertEqual(a, a.left_most_leaf())

        b = sut.Node("b", 1)
        ba = sut.Node(b, a)
        self.assertEqual(b, ba.left_most_leaf())

        c = sut.Node("c", 1)
        bac = sut.Node(ba, c)
        self.assertEqual(b, bac.left_most_leaf())
Example #9
0
 def test_get_codes_for_nodes_leaf_nodes_alphabetical_when_equal_weight(
         self):
     a = sut.Node("a", 1)
     b = sut.Node("b", 1)
     c = sut.Node("c", 2)
     ab = sut.Node(a, b)
     abc = sut.Node(ab, c)
     self.assertEqual({
         "a": "00",
         "b": "01",
         "c": "1"
     }, abc.get_codes_for_nodes())
Example #10
0
    def test_get_codes_for_nodes_leaf_nodes_with_different_weight(self):
        a = sut.Node("a", 1)
        b = sut.Node("b", 2)
        c = sut.Node("c", 3)
        ab = sut.Node(a, b)
        abc = sut.Node(ab, c)

        self.assertEqual({
            "a": "00",
            "b": "01",
            "c": "1"
        }, abc.get_codes_for_nodes())
Example #11
0
    def test_sort_nodes_3(self):
        b = sut.Node("b", 1)
        c = sut.Node("c", 2)
        a = sut.Node("a", 4)
        z = sut.Node("z", 1)

        bc = sut.Node(b, c)
        zbc = sut.Node(z, bc)

        ref = [zbc, a]
        output = sut.sort_nodes([a, zbc])
        self.assertEqual(ref, output)
Example #12
0
 def test_make_trees_from_dict_ilove(self):
     loveNodes = []
     loveNodes.append(sut.Node("i", 1))
     loveNodes.append(sut.Node("d", 1))
     loveNodes.append(sut.Node("c", 1))
     loveNodes.append(sut.Node("a", 1))
     loveNodes.append(sut.Node("v", 2))
     loveNodes.append(sut.Node("o", 2))
     loveNodes.append(sut.Node("l", 2))
     loveNodes.append(sut.Node("e", 3))
     loveDict = {
         "i": 1,
         "l": 2,
         "o": 2,
         "v": 2,
         "e": 3,
         "c": 1,
         "d": 1,
         "a": 1
     }
     generated = sorted(sut.make_trees_from_dict(loveDict),
                        key=functools.cmp_to_key(sut.is_left_ok))
     reference = sorted(loveNodes, key=functools.cmp_to_key(sut.is_left_ok))
     self.assertEqual(reference, generated)
Example #13
0
 def test_init_leaf(self):
     node = sut.Node("a", 42)
     self.assertEqual("a", node.value())
     self.assertEqual(42, node.priority())
     self.assertIsNone(node._leftChild)
     self.assertIsNone(node._rightChild)
Example #14
0
 def test_build_huffman_tree_alphabetical_if_equal_weight(self):
     a = sut.Node("a", 1)
     b = sut.Node("b", 1)
     ab = sut.Node(a, b)
     self.assertEqual(ab, sut.build_huffman_tree({"a": 1, "b": 1}))
Example #15
0
    def test_build_huffman_tree_ilove(self):
        loveDict = {
            "i": 1,
            "l": 2,
            "o": 2,
            "v": 2,
            "e": 3,
            "c": 1,
            "d": 1,
            "a": 1
        }
        a = sut.Node("a", 1)
        c = sut.Node("c", 1)
        d = sut.Node("d", 1)
        i = sut.Node("i", 1)
        l = sut.Node("l", 2)
        o = sut.Node("o", 2)
        v = sut.Node("v", 2)
        e = sut.Node("e", 3)

        di = sut.Node(d, i)
        ac = sut.Node(a, c)
        acdi = sut.Node(ac, di)
        lo = sut.Node(l, o)
        ve = sut.Node(v, e)
        acdilo = sut.Node(acdi, lo)
        veacdilo = sut.Node(ve, acdilo)
        loveTree = veacdilo

        self.assertEqual(loveTree, sut.build_huffman_tree(loveDict))
Example #16
0
    def test_sort_nodes_4(self):
        # gptucpgcggtqiwbq
        b = sut.Node("b", 1)
        i = sut.Node("i", 1)
        u = sut.Node("u", 1)
        w = sut.Node("w", 1)
        c = sut.Node("c", 2)
        p = sut.Node("p", 2)
        q = sut.Node("q", 2)
        t = sut.Node("t", 2)
        g = sut.Node("g", 4)

        bi = sut.Node(b, i)
        uw = sut.Node(u, w)
        biuw = sut.Node(bi, uw)
        cp = sut.Node(c, p)
        qt = sut.Node(q, t)
        biuwcp = sut.Node(biuw, cp)
        qtg = sut.Node(qt, g)
        biuwcpqtg = sut.Node(biuwcp, qtg)

        ref = [b, i, u, w, c, p, q, t, g]
        output = sut.sort_nodes([c, p, w, u, q, t, i, b, g])
        self.assertEqual(ref, output)

        ref = [bi, uw, c, p, q, t, g]
        output = sut.sort_nodes([g, t, q, p, c, uw, bi])
        self.assertEqual(ref, output)

        ref = [qt, g, biuwcp]
        output = sut.sort_nodes([biuwcp, qt, g])
        self.assertEqual(ref, output)
Example #17
0
 def test_compare_leafs(self):
     a = sut.Node("a", 1)
     b = sut.Node("b", 1)
     self.assertGreater(0, sut.is_left_ok(a, b))
     self.assertLess(0, sut.is_left_ok(b, a))