public void TestThatGraphCanBeCreatedWithTwoConnectedNodes()
 {
     var root = new SimpleNode("root");
     root.AddChild(new SimpleNode("child"));
     var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);
     Assert.AreEqual(2, graph.Order);
 }
Exemple #2
0
 public void TestAddedNodeIsReturnedInChildEnumeration()
 {
     var child = new SimpleNode("child");
     var sn1 = new SimpleNode("test");
     sn1.AddChild(child);
     CollectionAssert.Contains(new List<SimpleNode>(sn1.GetChildren()), child);
 }
Exemple #3
0
 public void TestThatNodeTailsAreReachableThroughEnumeration()
 {
     var root = new SimpleNode("root");
     var child = new SimpleNode("child");
     root.AddChild(child);
     var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);
     CollectionAssert.Contains(new List<SimpleNode>(graph.TailsFor(child)), root);
 }
 public void TestThatBuilderHandlesCycle()
 {
     var root = new SimpleNode("root");
     var child = new SimpleNode("child");
     root.AddChild(child);
     child.AddChild(root);
     var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);
     Assert.AreEqual(2, graph.Order);
 }
Exemple #5
0
        public void TestThatHeadsForRootWithTwoEdgesToChildIncludesChildTwice()
        {
            var root = new SimpleNode("root");
            var child = new SimpleNode("child");
            root.AddChild(child);
            root.AddChild(child);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            Assert.AreEqual(2, graph.HeadsFor(root).Count());
        }
Exemple #6
0
        public void TestThatGraphCanBeWalkedFromRoot()
        {
            var root = new SimpleNode("root");
            var child = new SimpleNode("child");
            root.AddChild(child);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var result = graph.Walk().Aggregate("", (str, node) => str + node.ToString());
            Assert.AreEqual("rootchild", result);
        }
Exemple #7
0
        public void TestThatHeadsAreOrderedBasedOnInput()
        {
            var root = new SimpleNode("root");
            var child1 = new SimpleNode("child1");
            var child2 = new SimpleNode("child2");
            root.AddChild(child1);
            root.AddChild(child2);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            Assert.AreSame(child1, graph.HeadByIndex(root, 0));
        }
Exemple #8
0
        public void ThatThatThereIsSinglePathBetweenNodeAndItself()
        {
            var root = new SimpleNode("root");
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var paths = graph.FindPaths(root, root);
            Assert.AreEqual("root", DescribePaths(paths));
        }
Exemple #9
0
        public void ThatThatPathBetweenNodeAndChildContainsNodeAndChild()
        {
            var root = new SimpleNode("root");
            var child = new SimpleNode("child");
            root.AddChild(child);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var paths = graph.FindPaths(root, child);
            Assert.AreEqual("root child", DescribePaths(paths));
        }
Exemple #10
0
        public void TestThatWalkingIsDepthFirst()
        {
            var root = new SimpleNode("root");
            var child11 = new SimpleNode("child11");
            var child12 = new SimpleNode("child12");
            var child111 = new SimpleNode("child111");
            root.AddChild(child11);
            root.AddChild(child12);
            child11.AddChild(child111);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var result = graph.Walk().Aggregate("", (str, node) => str + node.ToString());
            Assert.AreEqual("rootchild11child111child12", result);
        }
Exemple #11
0
        public void TestThatTwoPathsAreFoundInDiamondGraph()
        {
            var root = new SimpleNode("root");
            var child1 = new SimpleNode("child1");
            var child2 = new SimpleNode("child2");
            var end = new SimpleNode("end");
            root.AddChild(child1);
            root.AddChild(child2);
            child1.AddChild(end);
            child2.AddChild(end);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var paths = graph.FindPaths(root, end);
            Assert.AreEqual("root child1 end\nroot child2 end", DescribePaths(paths));
        }
Exemple #12
0
 public void TestNodesWithSameValueHaveSameHashCode()
 {
     var sn1 = new SimpleNode("test");
     var sn2 = new SimpleNode("test");
     Assert.AreEqual(sn1.GetHashCode(), sn2.GetHashCode());
 }
Exemple #13
0
 public void TestNodeHasValue()
 {
     var sn = new SimpleNode("test");
     Assert.AreEqual("test", sn.Value);
 }
Exemple #14
0
 public void AddChild(SimpleNode child)
 {
     _children.Add(child);
 }
Exemple #15
0
 public void TestThatRootHasOutDegree()
 {
     var root = new SimpleNode("root");
     var child = new SimpleNode("child");
     root.AddChild(child);
     var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);
     Assert.AreEqual(1, graph.OutDegreeOf(root));
 }
Exemple #16
0
 public void TestNodesWithDifferentValuesAreNotEqual()
 {
     var sn1 = new SimpleNode("test");
     var sn2 = new SimpleNode("test2");
     Assert.AreNotEqual(sn1, sn2);
 }
Exemple #17
0
 public void TestNodesWithDifferentValuesHaveDifferentHashCodes()
 {
     var sn1 = new SimpleNode("test");
     var sn2 = new SimpleNode("test2");
     Assert.AreNotEqual(sn1.GetHashCode(), sn2.GetHashCode());
 }
Exemple #18
0
 public void TestNodeToStringIsValue()
 {
     var sn = new SimpleNode("test");
     Assert.AreEqual("test", sn.ToString());
 }
Exemple #19
0
 private static IEnumerable<SimpleNode> GetChildren(SimpleNode node)
 {
     return node.GetChildren();
 }
Exemple #20
0
 public void TestChildListIsInitiallyEmpty()
 {
     var sn1 = new SimpleNode("test");
     Assert.AreEqual(0, sn1.GetChildren().Count());
 }
Exemple #21
0
 public void TestThatGraphExposesRootNode()
 {
     var root = new SimpleNode("root");
     var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);
     Assert.AreSame(root, graph.Root);
 }
Exemple #22
0
        public void TestThatTwoPathsAreFoundForTwoEdgesBetweenVertices()
        {
            var root = new SimpleNode("root");
            var child = new SimpleNode("child");
            root.AddChild(child);
            root.AddChild(child);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var paths = graph.FindPaths(root, child);
            Assert.AreEqual("root child\nroot child", DescribePaths(paths));
        }
Exemple #23
0
 public void TestThatRootHeadsAreReachableThroughIndex()
 {
     var root = new SimpleNode("root");
     var child = new SimpleNode("child");
     root.AddChild(child);
     var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);
     Assert.AreSame(child, graph.HeadByIndex(root, 0));
 }
Exemple #24
0
 public void TestNodesWithSameValueAreEqual()
 {
     var sn1 = new SimpleNode("test");
     var sn2 = new SimpleNode("test");
     Assert.AreEqual(sn1, sn2);
 }
Exemple #25
0
        public void TestThatPathsCanBeFoundWhenThereIsACycle()
        {
            var root = new SimpleNode("root");
            var child = new SimpleNode("child");
            var target = new SimpleNode("target");
            var back = new SimpleNode("back");

            root.AddChild(child);
            child.AddChild(target);
            child.AddChild(back);
            back.AddChild(root);

            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            var paths = graph.FindPaths(root, target);

            var result = string.Join("|", paths.Select(path => string.Join(",", path)));
            Assert.AreEqual("root,child,target|root,child,back,root,child,target", result);
        }
Exemple #26
0
        public void TestThatTailsForChildWithTwoEdgesToRootIncludesRootTwice()
        {
            var root = new SimpleNode("root");
            var child = new SimpleNode("child");
            root.AddChild(child);
            root.AddChild(child);
            var graph = new GraphBuilder<SimpleNode>(GetChildren).Build(root);

            Assert.AreEqual(2, graph.TailsFor(child).Count());
        }