public static void TestTreeModelCanAddElements() { var root = new StratusTreeElement { name = "Root", depth = -1 }; var listOfElements = new List <StratusTreeElement>(); listOfElements.Add(root); var model = new TreeModel <StratusTreeElement>(listOfElements); model.AddElement(new StratusTreeElement { name = "Element" }, root, 0); model.AddElement(new StratusTreeElement { name = "Element " + root.children.Count }, root, 0); model.AddElement(new StratusTreeElement { name = "Element " + root.children.Count }, root, 0); model.AddElement(new StratusTreeElement { 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 StratusTreeElement.Assert(listOfElements); }
public static void TestTreeToListWorks() { // Arrange TestElement root = new TestElement("root", -1); root.children = new List <StratusTreeElement>(); 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 <StratusTreeElement>(); root.children[1].children.Add(new TestElement("Bchild", 1)); root.children[1].children[0].children = new List <StratusTreeElement>(); root.children[1].children[0].children.Add(new TestElement("Bchildchild", 2)); // Test List <TestElement> result = new List <TestElement>(); StratusTreeElement.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); } StratusTreeElement.Assert(result); }
/// <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 : StratusTreeElement { // Validate input StratusTreeElement.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 <StratusTreeElement> children = null; if (childCount != 0) { children = new List <StratusTreeElement>(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]); }
//------------------------------------------------------------------------/ // Methods: Public //------------------------------------------------------------------------/ public void Assert() { StratusTreeElement.Assert(this.elements); }