Пример #1
0
        public void BuildCodingTableTest5()
        {
            HuffmanEncoder encoder = new HuffmanEncoder();
            HuffmanTree    tree    = HuffmanTreeHelper.Builder().NewInternalNode(44)
                                     .WithLeft(19, 'C')
                                     .WithRightInternal(25)
                                     .GoToRight()
                                     .WithLeft(11, 'B')
                                     .WithRightInternal(14)
                                     .GoToRight()
                                     .WithRight(9, 'E')
                                     .WithLeftInternal(5)
                                     .GoToLeft()
                                     .WithLeft(2, 'D')
                                     .WithRight(3, 'A')
                                     .CreateTree();

            CodingTable codingTable = encoder.BuildCodingTable(tree);

            Assert.IsNotNull(codingTable);

            Pair <byte, BitSequence>[] expected =
            {
                new Pair <byte, BitSequence>((byte)'A', BitSequence.FromString("1101")),
                new Pair <byte, BitSequence>((byte)'B', BitSequence.FromString("10")),
                new Pair <byte, BitSequence>((byte)'C', BitSequence.FromString("0")),
                new Pair <byte, BitSequence>((byte)'D', BitSequence.FromString("1100")),
                new Pair <byte, BitSequence>((byte)'E', BitSequence.FromString("111")),
            };
            CollectionAssert.AreEquivalent(expected, codingTable);
        }
Пример #2
0
        public void BuildCodingTableTest4()
        {
            HuffmanEncoder encoder = new HuffmanEncoder();
            HuffmanTree    tree    = HuffmanTreeHelper.Builder().NewInternalNode(133)
                                     .WithRight(85, 'f')
                                     .WithLeftInternal(48)
                                     .GoToLeft()
                                     .WithLeftInternal(21)
                                     .WithRightInternal(27)
                                     .GoToRight()
                                     .WithLeft(13, 'd')
                                     .WithRight(14, 'e')
                                     .GoToSibling()
                                     .WithRight(12, 'a')
                                     .WithLeftInternal(9)
                                     .GoToLeft()
                                     .WithRight(7, 'c')
                                     .WithLeft(2, 'b')
                                     .CreateTree();

            CodingTable codingTable = encoder.BuildCodingTable(tree);

            Assert.IsNotNull(codingTable);

            Pair <byte, BitSequence>[] expected =
            {
                new Pair <byte, BitSequence>((byte)'a', BitSequence.FromString("001")),
                new Pair <byte, BitSequence>((byte)'b', BitSequence.FromString("0000")),
                new Pair <byte, BitSequence>((byte)'c', BitSequence.FromString("0001")),
                new Pair <byte, BitSequence>((byte)'d', BitSequence.FromString("010")),
                new Pair <byte, BitSequence>((byte)'e', BitSequence.FromString("011")),
                new Pair <byte, BitSequence>((byte)'f', BitSequence.FromString("1")),
            };
            CollectionAssert.AreEquivalent(expected, codingTable);
        }
Пример #3
0
        public void BuildCodingTableTest2()
        {
            HuffmanEncoder encoder     = new HuffmanEncoder();
            HuffmanTree    tree        = HuffmanTreeHelper.Builder().NewNode(1, 'X').CreateTree();
            CodingTable    codingTable = encoder.BuildCodingTable(tree);

            Assert.IsNotNull(codingTable);

            Pair <byte, BitSequence>[] expected =
            {
                new Pair <byte, BitSequence>((byte)'X', BitSequence.FromString("0"))
            };
            CollectionAssert.AreEquivalent(expected, codingTable);
        }
Пример #4
0
        public void BuildCodingTableTest3()
        {
            HuffmanEncoder encoder = new HuffmanEncoder();
            HuffmanTree    tree    = HuffmanTreeHelper.Builder().NewInternalNode(15)
                                     .WithLeft(5, 'A')
                                     .WithRight(10, 'B')
                                     .CreateTree();

            CodingTable codingTable = encoder.BuildCodingTable(tree);

            Assert.IsNotNull(codingTable);

            Pair <byte, BitSequence>[] expected =
            {
                new Pair <byte, BitSequence>((byte)'A', BitSequence.FromString("0")),
                new Pair <byte, BitSequence>((byte)'B', BitSequence.FromString("1")),
            };
            CollectionAssert.AreEquivalent(expected, codingTable);
        }