public static void FindCommonAncestorsWithinListWorks()
        {
            // Arrange
            var list = new List <TestElement>();

            list.Add(new TestElement("root", -1));
            list.Add(new TestElement("A", 0));
            var b0 = new TestElement("B", 0);
            var b1 = new TestElement("Bchild", 1);
            var b2 = new TestElement("Bchildchild", 2);

            list.Add(b0);
            list.Add(b1);
            list.Add(b2);

            var c0 = new TestElement("C", 0);

            list.Add(c0);

            var f0 = new TestElement("F", 0);
            var f1 = new TestElement("Fchild", 1);
            var f2 = new TestElement("Fchildchild", 2);

            list.Add(f0);
            list.Add(f1);
            list.Add(f2);

            // Init tree structure: set Children and Parent properties
            TreeElementUtility.ListToTree(list);


            // Single element
            TestElement[] input          = { b1 };
            TestElement[] expectedResult = { b1 };
            var           result         = TreeElementUtility.FindCommonAncestorsWithinList(input).ToArray();

            Assert.IsTrue(ArrayUtility.ArrayEquals(expectedResult, result), "Single input should return single output");

            // Single sub tree
            input          = new[] { b1, b2 };
            expectedResult = new[] { b1 };
            result         = TreeElementUtility.FindCommonAncestorsWithinList(input).ToArray();
            Assert.IsTrue(ArrayUtility.ArrayEquals(expectedResult, result), "Common ancestor should only be b1 ");

            // Multiple sub trees
            input          = new[] { b0, b2, f0, f2, c0 };
            expectedResult = new[] { b0, f0, c0 };
            result         = TreeElementUtility.FindCommonAncestorsWithinList(input).ToArray();
            Assert.IsTrue(ArrayUtility.ArrayEquals(expectedResult, result),
                          "Common ancestor should only be b0, f0, c0");
        }
Пример #2
0
        private void Initialize(IList <T> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "Input data is null. Ensure input is a non-null list.");
            }

            _data = data;
            if (_data.Count > 0)
            {
                Root = TreeElementUtility.ListToTree(data);
            }
            _maxId = _data.Max(e => e.Id);
        }
        public static void TestListToTreeWorks()
        {
            // Arrange
            var list = new List <TestElement>();

            list.Add(new TestElement("root", -1));
            list.Add(new TestElement("A", 0));
            list.Add(new TestElement("B", 0));
            list.Add(new TestElement("Bchild", 1));
            list.Add(new TestElement("Bchildchild", 2));
            list.Add(new TestElement("C", 0));

            // Test
            var root = TreeElementUtility.ListToTree(list);

            // Assert
            Assert.AreEqual("root", root.Name);
            Assert.AreEqual(3, root.Children.Count);
            Assert.AreEqual("C", root.Children[2].Name);
            Assert.AreEqual("Bchildchild", root.Children[1].Children[0].Children[0].Name);
        }
        public static void TestListToTreeThrowsExceptionIfRootIsInvalidDepth()
        {
            // Arrange
            var list = new List <TestElement>();

            list.Add(new TestElement("root", 0));
            list.Add(new TestElement("A", 1));
            list.Add(new TestElement("B", 1));
            list.Add(new TestElement("Bchild", 2));

            // Test
            var catchedException = false;

            try {
                TreeElementUtility.ListToTree(list);
            }
            catch (Exception) {
                catchedException = true;
            }

            // Assert
            Assert.IsTrue(catchedException, "We require the root.Depth to be -1, here it is: " + list[0].Depth);
        }