Exemplo n.º 1
0
        public static void TestTreeToListWorks()
        {
            // Arrange
            TestElement root = new TestElement("root", -1);

            root.children = new List <TreeElement>();
            root.children.Add(new TestElement("A", 0));
            root.children.Add(new TestElement("B", 0));
            root.children.Add(new TestElement("C", 0));

            root.children[1].children = new List <TreeElement>();
            root.children[1].children.Add(new TestElement("Bchild", 1));

            root.children[1].children[0].children = new List <TreeElement>();
            root.children[1].children[0].children.Add(new TestElement("Bchildchild", 2));

            // Test
            List <TestElement> result = new List <TestElement>();

            TreeElement.TreeToList(root, result);

            // Assert
            string[] namesInCorrectOrder = { "root", "A", "B", "Bchild", "Bchildchild", "C" };
            Assert.AreEqual(namesInCorrectOrder.Length, result.Count, "Result count is not match");
            for (int i = 0; i < namesInCorrectOrder.Length; ++i)
            {
                Assert.AreEqual(namesInCorrectOrder[i], result[i].name);
            }
            TreeElement.Assert(result);
        }
Exemplo n.º 2
0
        public static void TestTreeModelCanAddElements()
        {
            var root = new TreeElement {
                name = "Root", depth = -1
            };
            var listOfElements = new List <TreeElement>();

            listOfElements.Add(root);

            var model = new TreeModel <TreeElement>(listOfElements);

            model.AddElement(new TreeElement {
                name = "Element"
            }, root, 0);
            model.AddElement(new TreeElement {
                name = "Element " + root.children.Count
            }, root, 0);
            model.AddElement(new TreeElement {
                name = "Element " + root.children.Count
            }, root, 0);
            model.AddElement(new TreeElement {
                name = "Sub Element"
            }, root.children[1], 0);

            // Assert order is correct
            string[] namesInCorrectOrder = { "Root", "Element 2", "Element 1", "Sub Element", "Element" };
            Assert.AreEqual(namesInCorrectOrder.Length, listOfElements.Count, "Result count does not match");
            for (int i = 0; i < namesInCorrectOrder.Length; ++i)
            {
                Assert.AreEqual(namesInCorrectOrder[i], listOfElements[i].name);
            }

            // Assert depths are valid
            TreeElement.Assert(listOfElements);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the root of the tree parsed from the list (always the first element)
        /// Note: The first element is requried to have a depth value of -1, with the rest
        /// of the elements at a depth >= 0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static T ListToTree <T>(IList <T> list) where T : TreeElement
        {
            // Validate input
            TreeElement.Assert(list);

            // Clear old state
            foreach (var element in list)
            {
                element.parent   = null;
                element.children = null;
            }

            // Set child and parent references using depth info
            for (int parentIndex = 0; parentIndex < list.Count; parentIndex++)
            {
                T parent = list[parentIndex];

                // Been visited before
                bool alreadyHasValidChildren = parent.children != null;
                if (alreadyHasValidChildren)
                {
                    continue;
                }

                // Count children based depth value, lookign at children until its
                // the same depth of this element
                int parentDepth = parent.depth;
                int childCount  = 0;
                for (int i = parentIndex + 1; i < list.Count; i++)
                {
                    int depth = list[i].depth;
                    if (depth == parentDepth + 1)
                    {
                        childCount++;
                    }
                    else if (depth <= parentDepth)
                    {
                        break;
                    }
                }

                // Fill the child array for this element
                List <TreeElement> children = null;
                if (childCount != 0)
                {
                    children   = new List <TreeElement>(childCount);
                    childCount = 0;

                    for (int i = parentIndex + 1; i < list.Count; i++)
                    {
                        int depth = list[i].depth;
                        if (depth == parentDepth + 1)
                        {
                            list[i].parent = parent;
                            children.Add(list[i]);
                            childCount++;
                        }

                        if (depth <= parentDepth)
                        {
                            break;
                        }
                    }
                }

                parent.children = children;
            }

            // Now return the root
            return(list[0]);
        }
Exemplo n.º 4
0
 //------------------------------------------------------------------------/
 // Methods: Public
 //------------------------------------------------------------------------/
 public void Assert()
 {
     TreeElement.Assert(this.elements);
 }