예제 #1
0
        public void Should_BreadthFirstSearch_Throw_If_Out_Of_Range_Index()
        {
            //arrange
            var graph = new MyGraph <int>(5);

            //act
            Action actLower  = () => graph.BreadthFirstSearch(-1).ToArray();
            Action actHigher = () => graph.BreadthFirstSearch(6).ToArray();

            //assert
            actLower.ShouldThrow <ArgumentOutOfRangeException>();
            actHigher.ShouldThrow <ArgumentOutOfRangeException>();

            graph.Capacity.ShouldBeEquivalentTo(5);
            graph.Count.ShouldBeEquivalentTo(0);
        }
예제 #2
0
        public void Should_Remove()
        {
            //arrange
            var graph = new MyGraph <int>(4);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);

            graph.AddEdge(0, 2);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 1);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 2);

            //act
            graph.Remove(2);

            var    first  = graph.BreadthFirstSearch(0).ToArray();
            var    second = graph.BreadthFirstSearch(1).ToArray();
            var    third  = graph.BreadthFirstSearch(3).ToArray();
            Action act    = () => graph.BreadthFirstSearch(2).ToArray();

            //assert
            first.Length.ShouldBeEquivalentTo(1);
            second.Length.ShouldBeEquivalentTo(1);
            third.Length.ShouldBeEquivalentTo(1);

            first[0].ShouldBeEquivalentTo(0);
            second[0].ShouldBeEquivalentTo(1);
            third[0].ShouldBeEquivalentTo(3);

            act.ShouldThrow <ArgumentException>();

            graph.Capacity.ShouldBeEquivalentTo(4);
            graph.Count.ShouldBeEquivalentTo(3);
        }
예제 #3
0
        public void Should_BreadthFirstSearch_Throw_If_Node_Is_Null()
        {
            //arrange
            var graph = new MyGraph <int>(5);

            //act
            Action act = () => graph.BreadthFirstSearch(1).ToArray();

            //assert
            act.ShouldThrow <ArgumentException>();

            graph.Capacity.ShouldBeEquivalentTo(5);
            graph.Count.ShouldBeEquivalentTo(0);
        }
예제 #4
0
        public void BFSQueueWithGivenStartingPoint_Equals_ExpectedQueue()
        {
            MyGraph <GraphNodeTestClass> myGraph = ArrangeAndConnectMyGraphNodes();

            MyQueue bfsQueue      = myGraph.BreadthFirstSearch(myGraph[2]);
            MyQueue expectedQueue = new MyQueue();

            expectedQueue.Enqueue(myGraph[2]);
            expectedQueue.Enqueue(myGraph[1]);
            expectedQueue.Enqueue(myGraph[3]);
            expectedQueue.Enqueue(myGraph[5]);
            expectedQueue.Enqueue(myGraph[0]);
            expectedQueue.Enqueue(myGraph[4]);

            Assert.Equal(expectedQueue, bfsQueue);
        }
예제 #5
0
        public void Should_BreadthFirstSearch_Length_One()
        {
            //arrange
            var graph = new MyGraph <int>(6);

            graph.AddVertex(0, 0);

            //act
            var result = graph.BreadthFirstSearch(0).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(1);
            result[0].ShouldBeEquivalentTo(0);

            graph.Capacity.ShouldBeEquivalentTo(6);
            graph.Count.ShouldBeEquivalentTo(1);
        }
예제 #6
0
        public void Should_RemoveEdge()
        {
            //arrange
            var graph = new MyGraph <int>(6);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);
            graph.AddVertex(4, 4);
            graph.AddVertex(5, 5);

            graph.AddEdge(0, 5);
            graph.AddEdge(0, 1);
            graph.AddEdge(0, 4);
            graph.AddEdge(1, 3);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 1);
            graph.AddEdge(3, 2);
            graph.AddEdge(3, 4);

            var resBfs = new[] { 0, 5, 4 };

            //act
            graph.RemoveEdge(0, 1);
            var result = graph.BreadthFirstSearch(0).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(resBfs.Length);
            for (int i = 0; i < result.Length; i++)
            {
                result[i].ShouldBeEquivalentTo(resBfs[i]);
            }

            graph.Capacity.ShouldBeEquivalentTo(6);
            graph.Count.ShouldBeEquivalentTo(6);
        }