Exemplo n.º 1
0
        public void TestReparentChild()
        {
            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;

            child0.Parent = child2;

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

            Assert.AreEqual(Arrange.GetNodeCount(root), 4);
        }
Exemplo n.º 2
0
        public static string VerifyRelationship(TestNode parent, TestNode child)
        {
            string reason = string.Empty;

            if (child.Parent != parent)
            {
                reason += "Child not pointing to parent";
            }
            int count = CountChildReferences(parent, child);

            if (count == 0)
            {
                reason += "Child not contained by parent";
            }
            if (count > 1)
            {
                reason += $"Child contained by parent {count} times";
            }


            return(reason);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void TestNewChildrenFail()
        {
            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");

            try
            {
                var root2 = new TestNode(childNodes, "Root");
                Assert.Fail("Added same node to two roots");
            }
            catch (TreeZeroException ex)
            {
                Assert.AreEqual(ExceptionReason.CollectionItemAlreadyParented, ex.Reason);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Wrong exception:{ex}");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Pick a random node and a suitable node we can move it to.
        /// We cannot move a node to itself or to a descendant of itself.
        /// </summary>
        /// <param name="root">The root of the tree</param>
        /// <param name="nodeCount">Number of nodes in the tree</param>
        /// <param name="child">out the node that can be moved</param>
        /// <param name="newParent">out the Parent the node can be moved to</param>
        private void GetTwoSuitableNodes(TestNode root, int nodeCount, out TestNode child, out TestNode newParent)
        {
            do
            {
                int first  = _rand.Next(nodeCount);
                int second = _rand.Next(nodeCount);

                child     = GetItemAtIndex(root, first);
                newParent = GetItemAtIndex(root, second);
            }while ((newParent.IsChildOf(child)) || (child == newParent) || (child.Parent == newParent));
        }
Exemplo n.º 7
0
 public static void BuildTestTree(TestNode root, int depth, int childNodeCount)
 {
     _createdIndex = 0;
     _BuildTestTree(root, depth, childNodeCount);
 }