コード例 #1
0
        public void BreadthDepthShouldBeTwoWhenEndNodeIsFoundTest()
        {
            // Arrange
            var start = Tree.Nodes["1"];
            var stop  = Tree.Nodes["5"];

            var  traversal = new BreadthFirstTraversal();
            Node result    = null;
            int  depth     = 0;

            traversal.NodeDiscovered += (sender, args) =>
            {
                if (args.NewNode == stop)
                {
                    args.ContinueExploring = false;
                    args.Abort             = true;
                    result = args.NewNode;
                    depth  = args.Depth;
                }
            };

            // Act
            traversal.Run(start);

            // Assert
            Assert.Equal(stop, result);
            Assert.Equal(2, depth);
        }
コード例 #2
0
        public void TestBFS()
        {
            INode node1 = new Node("1");
            INode node2 = new Node("2");
            INode node3 = new Node("3");
            INode node4 = new Node("4");
            INode node5 = new Node("5");

            IGraph graph = new Graph(false);

            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);
            graph.AddNode(node4);
            graph.AddNode(node5);

            graph.BuildEdge(node1, node2, 0);
            graph.BuildEdge(node1, node3, 0);
            graph.BuildEdge(node2, node4, 0);
            graph.BuildEdge(node2, node5, 0);

            BreadthFirstTraversal bfs             = new BreadthFirstTraversal(graph, node1);
            TraversalResult       traversalResult = bfs.Run();

            string result = string.Join(",", traversalResult.Nodes.Select(node => node.ID));

            Assert.Equal("1,2,3,4,5", result);
        }
コード例 #3
0
        public void BreadthFirstOrderTest()
        {
            var traversal     = new BreadthFirstTraversal();
            var orderRecorder = new TraversalOrderRecorder(traversal);

            traversal.Run(Tree.Nodes["1"]);

            Assert.True(orderRecorder.GetIndex(Tree.Nodes["2"]) < orderRecorder.GetIndex(Tree.Nodes["4A"]));
            Assert.True(orderRecorder.GetIndex(Tree.Nodes["3A"]) < orderRecorder.GetIndex(Tree.Nodes["4A"]));
        }
コード例 #4
0
        public void BreadthFirstDepthShouldBeZeroOnFirstNode()
        {
            // Arrange
            var start = new Node("7");

            Tree.Nodes.Add(start);

            var traversal = new BreadthFirstTraversal();

            traversal.NodeDiscovered += (sender, args) =>
            {
                Assert.Equal(0, args.Depth);
            };

            // Act
            traversal.Run(start);

            // Assert done in NodeDiscoverd event handler.
        }