Exemple #1
0
        public void Decode_DecodesText_Decoded()
        {
            var tree = new HuffTree();
            var result = tree.Decode("T0h00i100s000 011111101111110100o1000r0000t1010111000a101100m111000p00100l00000e101000011010111000x1000101110011010111011101100n1011100c10110010000d0111100100.");

            Assert.AreEqual("This is short sample text to encode.", result);
        }
Exemple #2
0
        public void Encode_AddsThreeSymbol_Added()
        {
            var tree = new HuffTree();
            tree.Encode('a');
            tree.Encode('a');
            tree.Encode('b');
            tree.Encode('r');

            var r = tree.Root;
            Assert.AreEqual(512, r.Number);
            Assert.AreEqual(4, r.Weight);
            Assert.AreEqual(511, r.Right.Number);
            Assert.AreEqual(2, r.Right.Weight);
            Assert.AreEqual('a', r.Right.Symbol);
            Assert.AreEqual(510, r.Left.Number);
            Assert.AreEqual(2, r.Left.Weight);
            var n510 = r.Left;
            Assert.AreEqual(509, n510.Right.Number);
            Assert.AreEqual(1, n510.Right.Weight);
            Assert.AreEqual('b', n510.Right.Symbol);
            Assert.AreEqual(508, n510.Left.Number);
            Assert.AreEqual(1, n510.Left.Weight);
            var n508 = n510.Left;
            Assert.AreEqual(507, n508.Right.Number);
            Assert.AreEqual(1, n508.Right.Weight);
            Assert.AreEqual('r', n508.Right.Symbol);
            Assert.AreEqual(506, n508.Left.Number);
            Assert.AreEqual(0, n508.Left.Weight);
        }
Exemple #3
0
        public void Encode_AddsTwoOneSymbols_CodeReturned()
        {
            var tree = new HuffTree();

            Assert.AreEqual("a", tree.Encode('a'));
            Assert.AreEqual("1", tree.Encode('a'));
        }
Exemple #4
0
        public void TestHuffaman()
        {
            char[]   chars   = { 'a', 'b', 'c', 'd' };
            double[] weight  = { 7.0, 5.0, 2.0, 4.0 };
            HuffTree huffman = new HuffTree(chars, weight);

            Assert.AreEqual(huffman.Decode(huffman.Encode("abcd")), "abcd");
        }
Exemple #5
0
 public void Constructor_InitializesTree_Initialized()
 {
     var tree = new HuffTree();
     
     Assert.AreEqual(512, tree.Root.Number);
     Assert.AreEqual(0, tree.Root.Weight);
     Assert.IsNull(tree.Root.Right);
     Assert.IsNull(tree.Root.Left);
 }
Exemple #6
0
        public void Encode_AddsThreeSymbol_CodeReturned()
        {
            var tree = new HuffTree();

            Assert.AreEqual("a", tree.Encode('a'));
            Assert.AreEqual("1", tree.Encode('a'));
            Assert.AreEqual("0b", tree.Encode('b'));
            Assert.AreEqual("00r", tree.Encode('r'));
        }
Exemple #7
0
        public void Decode_DecodesFiveCodes_Decoded()
        {
            var tree = new HuffTree();

            Assert.AreEqual("a", tree.Decode("a"));
            Assert.AreEqual("a", tree.Decode("1"));
            Assert.AreEqual("b", tree.Decode("0b"));
            Assert.AreEqual("r", tree.Decode("00r"));
            Assert.AreEqual("d", tree.Decode("000d"));
        }
Exemple #8
0
        public void Encode_AddsZeroSymbol_Added()
        {
            var tree = new HuffTree();
            tree.Encode('z');

            Assert.AreEqual(512, tree.Root.Number);
            Assert.AreEqual(1, tree.Root.Weight);
            Assert.AreEqual(511, tree.Root.Right.Number);
            Assert.AreEqual(1, tree.Root.Right.Weight);
            Assert.AreEqual('z', tree.Root.Right.Symbol);
            Assert.AreEqual(510, tree.Root.Left.Number);
            Assert.AreEqual(0, tree.Root.Left.Weight);
        }
Exemple #9
0
        static void Main(string[] args)
        {
            string   in_f     = "C:/Users/chad/rep/encoder/test/files/ex-1.txt";
            string   out_temp = "C:/Users/chad/rep/encoder/test/files/ex.table";
            HuffTree tree     = new HuffTree();

            tree.Encode(in_f, out_temp);
            ///string out_f = "C:/Users/chad/rep/encoder/test/files/ex.dec";
            //huff.encode(in_f, out_temp);

            //huff.decode(out_temp, out_f);
            //Debug.Assert(FileCompare(in_f, out_f));
        }
Exemple #10
0
        private int setContextInner(int ctxId)
        {
            if (ctxId == currentContext)
            {
                return(currentContext);
            }

            int oldId = currentContext;

            HuffTree hf;

            if (!contexts.TryGetValue(ctxId, out hf))
            {
                hf = new HuffTree(maxSymbol);
                contexts[ctxId] = hf;
            }
            currentContext = ctxId;
            context        = hf;

            return(oldId);
        }
Exemple #11
0
        public void Encode_AddsFourSymbol_Added()
        {
            var tree = new HuffTree();
            tree.Encode('a');
            tree.Encode('a');
            tree.Encode('b');
            tree.Encode('r');
            tree.Encode('d');

            var r = tree.Root;
            Assert.AreEqual(512, r.Number);
            Assert.AreEqual(5, r.Weight);
            Assert.AreEqual(511, r.Right.Number);
            Assert.AreEqual(3, r.Right.Weight);
            Assert.AreEqual(510, r.Left.Number);
            Assert.AreEqual(2, r.Left.Weight);
            Assert.AreEqual('a', r.Left.Symbol);
            var n511 = r.Right;
            Assert.AreEqual(509, n511.Right.Number);
            Assert.AreEqual(2, n511.Right.Weight);
            Assert.AreEqual(508, n511.Left.Number);
            Assert.AreEqual(1, n511.Left.Weight);
            Assert.AreEqual('b', n511.Left.Symbol);
            var n509 = n511.Right;
            Assert.AreEqual(507, n509.Right.Number);
            Assert.AreEqual(1, n509.Right.Weight);
            Assert.AreEqual('r', n509.Right.Symbol);
            Assert.AreEqual(506, n509.Left.Number);
            Assert.AreEqual(1, n509.Left.Weight);
            var n506 = n509.Left;
            Assert.AreEqual(505, n506.Right.Number);
            Assert.AreEqual(1, n506.Right.Weight);
            Assert.AreEqual('d', n506.Right.Symbol);
            Assert.AreEqual(504, n506.Left.Number);
            Assert.AreEqual(0, n506.Left.Weight);
        }
Exemple #12
0
 /// <summary>
 /// Discards all contexts.
 /// </summary>
 protected void clearContexts()
 {
     contexts       = new Dictionary <int, HuffTree>(1);
     currentContext = int.MinValue;
     context        = null;
 }