コード例 #1
0
        public void FileCondense(string path)
        {
            var          w = File.ReadAllText(path);
            HuffmanChain chain;
            HuffmanTree  tree;
            string       cW, cT;

            var condenser = new SingleCondenser();

            (cW, chain) = condenser.Condense(w);
            (cT, tree)  = condenser.CondenseToTree(w);

            Assert.NotNull(chain);
            Assert.NotNull(tree);

            Assert.LessOrEqual(cW.Length, w.Length);
            Assert.LessOrEqual(cT.Length, w.Length);
            Assert.LessOrEqual(cW.Length, cT.Length);
            Assert.LessOrEqual((double)cW.Length / cT.Length, .95);

            var expander = new SingleExpander();
            var expanded = expander.Expand(cW, chain, w.Length);

            TestContext.WriteLine(
                $"Chain vs Tree Efficiency: cW/cT = {(double) cW.Length / cT.Length:P} size decrease");
            TestContext.WriteLine($"Chain Overall Efficiency: cW/w = {(double) cW.Length / w.Length:P} size decrease");
            TestContext.WriteLine($"Tree Overall Efficiency: cW/w = {(double) cT.Length / w.Length:P} size decrease");

            var output = new HuffmanChain.HuffmanChainOutputService().CreateOutput(chain);

            TestContext.WriteLine(output);

            Assert.AreEqual(w, expanded);
        }
コード例 #2
0
        public void ChainStringService(int maxChars, int minChars = 0, char minChar = ' ', char maxChar = '~')
        {
            var dict = HuffmanTreeTests.NumberCharacterGenerator(minChars, maxChars, minChar, maxChar);
            var w    = HuffmanTreeTests.GenerateString(dict);

            HuffmanChain chain;
            var          condenser = new SingleCondenser();

            chain = condenser.Condense(w).Item2;

            var output = new HuffmanChain.HuffmanChainOutputService().CreateOutput(chain);

            TestContext.WriteLine(output);
        }
        public void ChainReproductabilityFromFile(string path)
        {
            var w     = File.ReadAllText(path);
            var chain = new HuffmanChain(w);

            var chainOutputService = new HuffmanChain.HuffmanChainOutputService();
            var chainInputService  = new HuffmanChain.HuffmanChainInputService();

            var output = chainOutputService.CreateOutput(chain);

            TestContext.WriteLine("Original: " + output);
            var recreated = chainInputService.CreateFromInput(output);

            var recreatedString = chainOutputService.CreateOutput(recreated);

            TestContext.WriteLine("Recreated: " + recreatedString);

            Assert.AreEqual(output, recreatedString);
            Assert.IsTrue(chain.Equals(recreated));
        }