public void GetDepth() { // 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 that 'GetDepth' returns the correct elements. Assert.Equal( 0, walker.GetDepth(tree)); Assert.Equal( 1, walker.GetDepth(tree[0])); Assert.Equal( 2, walker.GetDepth(tree[0][0])); Assert.Equal( 2, walker.GetDepth(tree[0][1])); Assert.Equal( 1, walker.GetDepth(tree[1])); Assert.Equal( 2, walker.GetDepth(tree[1][0])); Assert.Equal( 3, walker.GetDepth(tree[1][0][0])); }
public void LevelOrderTraversal() { // 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, 4, 2, 3, 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 'LevelOrderTraversal' returns the correct // elements. // Node 0: Assert.Equal( node0ExpectedResult, walker.LevelOrderTraversal(tree).Select(x => x.Value)); // Node 1: Assert.Equal( node1ExpectedResult, walker.LevelOrderTraversal(tree[0]).Select(x => x.Value)); // Node 2: Assert.Equal( node2ExpectedResult, walker.LevelOrderTraversal(tree[0][0]).Select(x => x.Value)); // Node 3: Assert.Equal( node3ExpectedResult, walker.LevelOrderTraversal(tree[0][1]).Select(x => x.Value)); // Node 4: Assert.Equal( node4ExpectedResult, walker.LevelOrderTraversal(tree[1]).Select(x => x.Value)); // Node 5: Assert.Equal( node5ExpectedResult, walker.LevelOrderTraversal(tree[1][0]).Select(x => x.Value)); // Node 6: Assert.Equal( node6ExpectedResult, walker.LevelOrderTraversal(tree[1][0][0]).Select(x => x.Value)); }
public void GetLevel_NegativeDepth_ArgumentExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Assert that 'GetLevel' throws an 'ArgumentNullException' when the node is null. Assert.Throws <ArgumentException>("depth", () => walker.GetLevel(tree, -1).ToArray()); }
public void GetDepth_NullWalker_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a null ITreeWalker. NodeWalker <int> walker = null; // Assert that 'GetDepth' throws an 'ArgumentNullException' when the tree walker // is null. Assert.Throws <ArgumentNullException>("walker", () => walker.GetDepth(tree)); }
public void GetPrecedingSiblingsAndSelf_NullWalker_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a null ITreeWalker. NodeWalker <int> walker = null; // Assert that 'GetPrecedingSiblingsAndSelf' throws an 'ArgumentNullException' when the tree walker // is null. Assert.Throws <ArgumentNullException>("walker", () => walker.GetPrecedingSiblingsAndSelf(tree).ToArray()); }
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 LevelOrderTraversal_NullWalker_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a null ITreeWalker. NodeWalker <int> walker = null; // Assert that 'LevelOrderTraversal' throws an 'ArgumentNullException' when the tree // walker is null. Assert.Throws <ArgumentNullException>( "walker", () => walker.LevelOrderTraversal(tree).ToArray()); }
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_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()); } }
public void GetChildrenByPredicate_NullPredicate_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Assert that 'GetChildren' throws an 'ArgumentNullException' when the predicate // is null. Assert.Throws <ArgumentNullException>( "predicate", () => walker.GetChildren(new Node <int>[] { tree }, (Func <Node <int>, bool>)null).ToArray()); Assert.Throws <ArgumentNullException>( "predicate", () => walker.GetChildren(tree, (Func <Node <int>, bool>)null).ToArray()); }
public void GetChildrenByPredicate_NullWalker_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a null ITreeWalker. NodeWalker <int> walker = null; // Assert that 'GetChildren' throws an 'ArgumentNullException' when the tree // walker is null. Assert.Throws <ArgumentNullException>( "walker", () => walker.GetChildren(new Node <int>[] { tree }, (n) => true).ToArray()); Assert.Throws <ArgumentNullException>( "walker", () => walker.GetChildren(tree, (n) => true).ToArray()); }
public void HasChildren() { // 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 'HasChildren' returns the correct value. Assert.Equal(true, walker.HasChildren(tree)); Assert.Equal(true, walker.HasChildren(tree[0])); Assert.Equal(false, walker.HasChildren(tree[0][0])); Assert.Equal(false, walker.HasChildren(tree[0][1])); Assert.Equal(true, walker.HasChildren(tree[1])); Assert.Equal(true, walker.HasChildren(tree[1][0])); Assert.Equal(false, walker.HasChildren(tree[1][0][0])); }
public void GetPrecedingSiblingsAndSelf(int[] path, int[] expectedResult) { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Get a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); foreach (int i in path) { tree = tree[i]; } // For each node in the tree assert that 'GetPrecedingSiblingsAndSelf' returns the // correct elements. Assert.Equal( expectedResult, walker.GetPrecedingSiblingsAndSelf(tree).Select(x => x.Value)); }
public void GetLeaves(int[] testPath, int[] expected) { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Get a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); var node = tree; foreach (int i in testPath) { node = walker.GetChildAt(node, i); } Assert.Equal( expected, walker.GetLeaves(node).Select(x => x.Value)); }
public void GetBranches_MultipleBranches() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Get a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Assert. Assert.Equal( walker .GetLeaves(tree) .Select(x => EnumerableEx .Return(x) .Concat(walker.GetAncestors(x)) .Reverse() .ToArray()) .ToArray(), walker.GetBranches(tree).Select(x => x.ToArray()).ToArray()); }
public void GetChildrenByPredicate_PredicateTests() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Create test predicates and the expected results. var testCases = new[] { new { Predicate = new Func <Node <int>, bool>(i => i.Value % 2 == 0), ExpectedResults = new int[] { 4 } }, new { Predicate = new Func <Node <int>, bool>(i => i.Value % 2 == 1), ExpectedResults = new int[] { 1 } }, new { Predicate = new Func <Node <int>, bool>(i => i.Value >= 2), ExpectedResults = new int[] { 4 } }, }; // Assert that 'GetChildren' returns the expected results for each predicate. foreach (var testCase in testCases) { Assert.Equal( walker .GetChildren(tree, testCase.Predicate) .Select(x => x.Value), testCase.ExpectedResults); Assert.Equal( walker .GetChildren(new Node <int>[] { tree }, testCase.Predicate) .Select(x => x.Value), testCase.ExpectedResults); } }
public void GetLevel() { // 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 that 'GetLevel' returns the correct elements. Assert.Equal( new int[] { 0 }, walker.GetLevel(tree, 0).Select(x => x.Value)); Assert.Equal( new int[] { 1, 4 }, walker.GetLevel(tree, 1).Select(x => x.Value)); Assert.Equal( new int[] { 2, 3, 5 }, walker.GetLevel(tree, 2).Select(x => x.Value)); Assert.Equal( new int[] { 6 }, walker.GetLevel(tree, 3).Select(x => x.Value)); }
public void GetDescendants_ValuePredicateTests( Func <Node <int>, bool> predicate, int[] expectedResult) { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Assert that 'GetDescendants' returns the expected results for each predicate. Assert.Equal( expectedResult, walker .GetDescendants(tree, predicate) .Select(x => x.Value)); Assert.Equal( expectedResult, walker .GetDescendants(new Node <int>[] { tree }, predicate) .Select(x => x.Value)); }
public void GetChildrenByKey_NullKey_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Assert that 'GetChildren' throws an 'ArgumentNullException' when the key // is null. Assert.Throws <ArgumentNullException>( "key", () => walker.GetChildren(new Node <int>[] { tree }, (Node <int>)null).ToArray()); Assert.Throws <ArgumentNullException>( "key", () => walker.GetChildren(tree, (Node <int>)null).ToArray()); Assert.Throws <ArgumentNullException>( "key", () => walker.GetChildren(new Node <int>[] { tree }, (Node <int>)null, EqualityComparer <Node <int> > .Default).ToArray()); Assert.Throws <ArgumentNullException>( "key", () => walker.GetChildren(tree, (Node <int>)null, EqualityComparer <Node <int> > .Default).ToArray()); }
public void GetDescendants_ByKey_NullWalker_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a null ITreeWalker. NodeWalker <int> walker = null; // Assert that 'GetDescendants' throws an 'ArgumentNullException' when the tree // walker is null. Assert.Throws <ArgumentNullException>( "walker", () => walker.GetDescendants(new Node <int>[] { tree }, default(Node <int>))); Assert.Throws <ArgumentNullException>( "walker", () => walker.GetDescendants(new Node <int>[] { tree }, default(Node <int>), EqualityComparer <Node <int> > .Default)); Assert.Throws <ArgumentNullException>( "walker", () => walker.GetDescendants(tree, default(Node <int>)).ToArray()); Assert.Throws <ArgumentNullException>( "walker", () => walker.GetDescendants(tree, default(Node <int>), EqualityComparer <Node <int> > .Default).ToArray()); }
public void GetDescendants_ByKey_NullKey_ArgumentNullExceptionThrown() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Assert that 'GetDescendants' throws an 'ArgumentNullException' when the predicate // is null. Assert.Throws <ArgumentNullException>( "key", () => walker.GetDescendants(new Node <int>[] { tree }, (Node <int>)null).ToArray()); Assert.Throws <ArgumentNullException>( "key", () => walker.GetDescendants(new Node <int>[] { tree }, (Node <int>)null).ToArray()); Assert.Throws <ArgumentNullException>( "key", () => walker.GetDescendants(tree, (Node <int>)null).ToArray()); Assert.Throws <ArgumentNullException>( "key", () => walker.GetDescendants(tree, (Node <int>)null).ToArray()); }
public void Descendants_ByKey_ArgumentComparerIsInvoked() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a mock IComparer. Mock <IEqualityComparer <Node <int> > > mockComparer = new Mock <IEqualityComparer <Node <int> > >(); mockComparer .Setup(mock => mock.Equals(It.IsAny <Node <int> >(), It.IsAny <Node <int> >())) .Returns(true); // Create a VirtualForest. IVirtualForest <Node <int> > vt = VirtualForest.New(new NodeWalker <int>(), tree); // Create a key to use for comparison. Any key is fine. Node <int> key = new Node <int>(0); // Execute Descendants. vt.Descendants(key, mockComparer.Object).Roots.ToArray(); // Verify that the VirtualForest's comparer was used. //mockComparer.Verify(x => x.Equals(It.IsAny<Node<int>>(), It.IsAny<Node<int>>()), Times.Exactly(vt.GetDegree())); }
public void LevelOrderTraversal_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.LevelOrderTraversal(startNode, (n, i) => n.Value % 2 == 1, excludeOption).Select(x => x.Value)); }
public void GetChildrenByKey_ArgumentComparerIsInvoked() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a mock IComparer. Mock <IEqualityComparer <Node <int> > > mockComparer = new Mock <IEqualityComparer <Node <int> > >(); mockComparer .Setup(mock => mock.Equals(It.IsAny <Node <int> >(), It.IsAny <Node <int> >())) .Returns(true); // Create a VirtualTree. VirtualTree <Node <int> > vt = VirtualTree.New(new NodeWalker <int>(), tree); // Create a key to use for comparison. Any key is fine. Node <int> key = new Node <int>(0); // Execute the GetChildren. vt.GetChildren(key, mockComparer.Object).ToArray(); // Verify that the comparer argument was used. mockComparer.Verify(x => x.Equals(key, It.IsAny <Node <int> >()), Times.AtLeastOnce); }
public void GetDescendants_ByKey_ComparerIsInvoked() { // Get a valid tree. var tree = TestTreeFactory.GetSimpleTree(); // Create a valid ITreeWalker. NodeWalker <int> walker = new NodeWalker <int>(); // Create a mock IComparer. Mock <IEqualityComparer <Node <int> > > mockComparer = new Mock <IEqualityComparer <Node <int> > >(); mockComparer .Setup(mock => mock.Equals(It.IsAny <Node <int> >(), It.IsAny <Node <int> >())) .Returns(true); // Create a key to use for comparison. Any key is fine. Node <int> key = new Node <int>(0); // Execute GetDescendants. walker.GetDescendants(tree, key, mockComparer.Object).ToArray(); // It should be called the same number of times as children of the node being evaluated. mockComparer.Verify(x => x.Equals(It.IsAny <Node <int> >(), It.IsAny <Node <int> >()), Times.Exactly(walker.GetDegree(tree))); }
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); } } } }