Example #1
0
        public void TestBuildTree()
        {
            var root = new TestNode("Root");

            Arrange.BuildTestTree(root, 2, 2);
            Arrange.TestTreeIntegrity(root);
        }
        public void TestChildrenAddSelf()
        {
            var root = new TestNode("Root");

            var child0 = new TestNode("0");
            var child1 = new TestNode("1");
            var child2 = new TestNode("2");

            root.Children.Add(child0);
            root.Children.Add(child1);
            root.Children.Add(child2);

            Arrange.VerifyRelationship(root, child0);
            Arrange.VerifyRelationship(root, child1);
            Arrange.VerifyRelationship(root, child2);

            try
            {
                child0.Children.Add(child0);
                Assert.Fail("Node cannot be its own parent.");
            }
            catch (TreeZeroException ex)
            {
                Assert.AreEqual(ExceptionReason.ParentToSelf, ex.Reason);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Wrong exception:{ex}");
            }
        }
        public void TestParentToDescendant()
        {
            var root = new TestNode("Root");

            var child0 = new TestNode("0");
            var child1 = new TestNode("1");
            var child2 = new TestNode("2");

            root.Children.Add(child0);
            child0.Children.Add(child1);
            child1.Children.Add(child2);

            Arrange.VerifyRelationship(root, child0);
            Arrange.VerifyRelationship(child0, child1);
            Arrange.VerifyRelationship(child1, child2);

            try
            {
                child1.Parent = child2;
                Assert.Fail("Cyclic dependency.");
            }
            catch (TreeZeroException ex)
            {
                Assert.AreEqual(ExceptionReason.ParentToDescendant, ex.Reason);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Wrong exception:{ex}");
            }
        }
Example #4
0
        public void TerribleUnitTest(Action <TestNode, TestNode> method0, Action <TestNode, TestNode> method1)
        {
            var root = new TestNode("Root");

            Arrange.BuildTestTree(root, 4, 4);

            // Used to test the tree is correct after we undo every move.
            foreach (TestNode item in TestNode.AllChildren(root))
            {
                item.OriginalParent = item.Parent;
            }

            // Ensure the tree parent/child relationships are correct.
            Arrange.TestTreeIntegrity(root);
            Debug.WriteLine(root.PrintTree());

            // Used to store moves, so we can undo them later.
            Stack <Tuple <TestNode, TestNode> > moves = new Stack <Tuple <TestNode, TestNode> >();

            int nodeCount = Arrange.GetNodeCount(root);

            for (int c = 0; c < nodeCount; c++)
            {
                TestNode child, newParent;
                GetTwoSuitableNodes(root, nodeCount, out child, out newParent);
                Debug.WriteLine($"Moving {child.Name} from {child.Parent.Name} to {newParent.Name}");
                // Store the move, so we can undo it later.
                moves.Push(new Tuple <TestNode, TestNode>(child, child.Parent));
                // Move the child to the new parent.
                method0(child, newParent);
                Assert.AreEqual(newParent, child.Parent, "Wrong parent");
                Assert.AreEqual(nodeCount, Arrange.GetNodeCount(root), "Node has gone missing");
            }
            Arrange.TestTreeIntegrity(root);
            //Debug.WriteLine(root.ToString());
            Assert.AreEqual(nodeCount, Arrange.GetNodeCount(root));

            // Undo every move.
            // NOTE: The tree will be back to its starting shape, but the order of node Children may be different.
            while (moves.TryPop(out var move))
            {
                method1(move.Item1, move.Item2);
            }

            // Ensure the tree has the same shape we started with, allowing for node Children to be in a different order.
            foreach (TestNode item in TestNode.AllChildren(root))
            {
                Assert.AreEqual(item.OriginalParent, item.Parent);
            }

            //Debug.WriteLine(root.PrintTree());
        }
Example #5
0
        public void TestNewChildren()
        {
            ObservableCollection <TestNode> childNodes = new ObservableCollection <TestNode>();

            childNodes.Add(new TestNode("0"));
            childNodes.Add(new TestNode("1"));
            childNodes.Add(new TestNode("2"));

            var root = new TestNode(childNodes, "Root");

            Arrange.VerifyRelationship(root, childNodes[0]);
            Arrange.VerifyRelationship(root, childNodes[1]);
            Arrange.VerifyRelationship(root, childNodes[2]);
        }
Example #6
0
        public void TestBasicTreeByChildren()
        {
            var root = new TestNode("Root");

            var child0 = new TestNode("0");
            var child1 = new TestNode("1");
            var child2 = new TestNode("2");

            root.Children.Add(child0);
            root.Children.Add(child1);
            root.Children.Add(child2);

            Arrange.VerifyRelationship(root, child0);
            Arrange.VerifyRelationship(root, child1);
            Arrange.VerifyRelationship(root, child2);
        }
Example #7
0
        public void TestNestChild()
        {
            var root = new TestNode("Root");

            var child0 = new TestNode("0");
            var child1 = new TestNode("1");
            var child2 = new TestNode("2");

            child0.Parent = root;
            child1.Parent = root;
            child2.Parent = child1;

            Arrange.VerifyRelationship(root, child0);
            Arrange.VerifyRelationship(root, child1);
            Arrange.VerifyRelationship(child1, child2);

            Assert.AreEqual(Arrange.GetNodeCount(root), 4);
        }
Example #8
0
        public void TestBasicTreeByParent()
        {
            var root = new TestNode("Root");

            var child0 = new TestNode("0");
            var child1 = new TestNode("1");
            var child2 = new TestNode("2");


            child0.Parent = root;
            child1.Parent = root;
            child2.Parent = root;
            Arrange.VerifyRelationship(root, child0);
            Arrange.VerifyRelationship(root, child1);
            Arrange.VerifyRelationship(root, child2);

            Assert.AreEqual(Arrange.GetNodeCount(root), 4);

            TestNode s = root;

            Debug.WriteLine(s);
        }
Example #9
0
        public void TestRemoveChild()
        {
            var root = new TestNode("Root");

            var child0 = new TestNode("0");
            var child1 = new TestNode("1");
            var child2 = new TestNode("2");

            child0.Parent = root;
            child1.Parent = root;
            child2.Parent = root;

            Arrange.VerifyRelationship(root, child0);

            Arrange.VerifyRelationship(root, child1);
            root.Children.Remove(child1);
            Assert.AreEqual(0, Arrange.CountChildReferences(root, child1));
            Assert.AreEqual(null, child1.Parent);

            Arrange.VerifyRelationship(root, child2);

            Assert.AreEqual(Arrange.GetNodeCount(root), 3);
        }