Example #1
0
        public void BuildTree_SimpleTree()
        {
            //Arrange
            var processor = new InputProcessorMockup();
            var leafTree  = processor.BuildSimpleTree();
            var builder   = new TreeBuilder(leafTree);

            //Act
            builder.BuildTree();

            //Assert
            Assert.AreEqual(builder.tree.Count, 1);
            var root = builder.tree.First();

            Assert.IsTrue(root.GetType() != typeof(Leaf));
            Assert.AreEqual(root.frequency, 6);
            Assert.AreEqual(root.creationId, 2);

            Assert.IsTrue(root.left.GetType() == typeof(Leaf));
            Assert.AreEqual(root.left.character, Convert.ToByte('a'));
            Assert.AreEqual(root.left.frequency, 3);

            Assert.IsTrue(root.right.GetType() != typeof(Leaf));
            Assert.AreEqual(root.right.frequency, 3);
            Assert.AreEqual(root.right.creationId, 1);

            Assert.IsTrue(root.right.left.GetType() == typeof(Leaf));
            Assert.AreEqual(root.right.left.character, Convert.ToByte('c'));
            Assert.AreEqual(root.right.left.frequency, 1);

            Assert.IsTrue(root.right.right.GetType() == typeof(Leaf));
            Assert.AreEqual(root.right.right.character, Convert.ToByte('b'));
            Assert.AreEqual(root.right.right.frequency, 2);
        }
Example #2
0
        public void PrintTree_PrintSimpleTree()
        {
            //Arrange
            var processor = new InputProcessorMockup();
            var leafTree  = processor.BuildSimpleTree();
            var builder   = new TreeBuilder(leafTree);

            using (StringWriter stringWriter = new StringWriter())
            {
                //Act
                builder.BuildTree();
                Console.SetOut(stringWriter);
                builder.PrintTree(builder.tree.First());

                //Assert
                Assert.AreEqual("6 *97:3 3 *99:1 *98:2", stringWriter.ToString());
            }
        }
Example #3
0
        public void InitializeTree_ValidDictionary()
        {
            //Arrange
            var processor = new InputProcessorMockup();
            var dict      = processor.GetUniqueBytes();

            //Act
            var leafTree = processor.InitializeTree(dict);

            //Assert
            Assert.AreEqual(leafTree.Count, 10);

            int c = (byte)'a';
            int i = 0;

            foreach (var node in leafTree)
            {
                Assert.AreEqual(node.character, Convert.ToByte(c + i));
                Assert.AreEqual(node.frequency, 4);
                Assert.IsTrue(node.GetType() == typeof(Leaf));
                i++;
            }
        }