public void PreOrderTraversal_NullNode_ArgumentNullExceptionThrown()
        {
            // Create a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // Assert that 'PreOrderTraversal' throws an 'ArgumentNullException' when the node is null.
            Assert.Throws <ArgumentNullException>(
                "node",
                () => walker.PreOrderTraversal(null).ToArray());
        }
        public void PreOrderTraversal()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // Create arrays of the results expected from each node.
            int[] node0ExpectedResult = new int[] { 0, 1, 2, 3, 4, 5, 6 };
            int[] node1ExpectedResult = new int[] { 1, 2, 3 };
            int[] node2ExpectedResult = new int[] { 2 };
            int[] node3ExpectedResult = new int[] { 3 };
            int[] node4ExpectedResult = new int[] { 4, 5, 6 };
            int[] node5ExpectedResult = new int[] { 5, 6 };
            int[] node6ExpectedResult = new int[] { 6 };

            // For each node in the tree assert that 'PreOrderTraversal' returns the correct
            // elements.

            // Node 0:
            Assert.Equal(
                node0ExpectedResult,
                walker.PreOrderTraversal(tree).Select(x => x.Value));

            // Node 1:
            Assert.Equal(
                node1ExpectedResult,
                walker.PreOrderTraversal(tree[0]).Select(x => x.Value));

            // Node 2:
            Assert.Equal(
                node2ExpectedResult,
                walker.PreOrderTraversal(tree[0][0]).Select(x => x.Value));

            // Node 3:
            Assert.Equal(
                node3ExpectedResult,
                walker.PreOrderTraversal(tree[0][1]).Select(x => x.Value));

            // Node 4:
            Assert.Equal(
                node4ExpectedResult,
                walker.PreOrderTraversal(tree[1]).Select(x => x.Value));

            // Node 5:
            Assert.Equal(
                node5ExpectedResult,
                walker.PreOrderTraversal(tree[1][0]).Select(x => x.Value));

            // Node 6:
            Assert.Equal(
                node6ExpectedResult,
                walker.PreOrderTraversal(tree[1][0][0]).Select(x => x.Value));
        }
Ejemplo n.º 3
0
        public void GetRoot()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // For each node in the tree assert the 'GetRoot' returns the root of the tree.
            foreach (Node <int> node in walker.PreOrderTraversal(tree))
            {
                Assert.Equal(tree.Value, walker.GetRoot(node).Value);
            }
        }
        public void PreOrderTraversal_NullWalker_ArgumentNullExceptionThrown()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Create a null ITreeWalker.
            NodeWalker <int> walker = null;

            // Assert that 'PreOrderTraversal' throws an 'ArgumentNullException' when the tree
            // walker is null.
            Assert.Throws <ArgumentNullException>(
                "walker",
                () => walker.PreOrderTraversal(tree).ToArray());
        }
Ejemplo n.º 5
0
        public void GetChildrenByKey_ByKeyTests()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Create a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            foreach (Node <int> node in walker.PreOrderTraversal(tree))
            {
                foreach (Node <int> key in walker.PreOrderTraversal(tree))
                {
                    IEnumerable <Node <int> > result = walker.GetChildren(node, key).ToArray();
                    if (walker.GetChildren(node).Contains(key))
                    {
                        Assert.Equal(result, new Node <int>[] { key });
                    }
                    else
                    {
                        Assert.Empty(result);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void GetDegree()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // For each node in the tree assert the 'GetDegree' returns the number of children
            // that the node has.
            foreach (Node <int> node in walker.PreOrderTraversal(tree))
            {
                Assert.Equal(node.Children.Count, walker.GetDegree(node));
            }
        }
        public void PreOrderTraversal_SingleNode(
            ExcludeOption excludeOption,
            int[] expectedResults)
        {
            // Get a valid tree.
            var tree = Node.Create(0);

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // Assert that the correct sequence is returned.
            Assert.Equal(
                expectedResults,
                walker.PreOrderTraversal(tree, (n, i) => n.Value == 0, excludeOption).Select(x => x.Value));
        }
        public void PreOrderTraversal_ShortCircuitRootNode(
            ExcludeOption excludeOption,
            int[] expectedResults)
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // Assert that the correct sequence is returned.
            Assert.Equal(
                expectedResults,
                walker.PreOrderTraversal(tree, (n, i) => n.Value == 0, excludeOption).Select(x => x.Value));
        }
        public void GetDescendants_ByKey_NullComparer()
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Create a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            foreach (Node <int> key in walker.PreOrderTraversal(tree).Skip(1))
            {
                IEnumerable <Node <int> > result = walker.GetDescendants(tree, key);
                Assert.Equal(1, result.Count());
                Assert.Equal(key, result.First());
            }
        }
Ejemplo n.º 10
0
        public void GetBranches_SingleBranch()
        {
            // Get a valid tree.
            var tree =
                Node.Create(0).AddChildren(
                    Node.Create(1).AddChildren(
                        Node.Create(2)));

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // Assert.
            Assert.Equal(
                EnumerableEx.Return(walker.PreOrderTraversal(tree).ToArray()).ToArray(),
                walker.GetBranches(tree).Select(x => x.ToArray()).ToArray());
        }
        public void PreOrderTraversal_ShortCircuitOddNumbers(
            int[] traversalToStartNode,
            ExcludeOption excludeOption,
            int[] expectedResults)
        {
            // Get a valid tree.
            var tree = TestTreeFactory.GetSimpleTree();

            // Get a valid ITreeWalker.
            NodeWalker <int> walker = new NodeWalker <int>();

            // Get the node to begin traversing from.
            var startNode = tree;

            foreach (int i in traversalToStartNode)
            {
                startNode = startNode[i];
            }

            // Assert that the correct sequence is returned.
            Assert.Equal(
                expectedResults,
                walker.PreOrderTraversal(startNode, (n, i) => n.Value % 2 == 1, excludeOption).Select(x => x.Value));
        }