コード例 #1
0
        public async void Navigate_On_Swarm_Mode_Should_Call_ISwarmCoordinator_GetSwarm_Only_When_There_Are_Forks(int startX, int startY, int destinationX, int destinationY, int numberOfCalls)
        {
            var swarmCoordinator = Substitute.For <ISwarmCoordinator>();

            swarmCoordinator.GetSwarm(Arg.Any <IMazeCrawlerState>()).Returns(new IMazeCrawler[0]);

            var crawlerCoordinator = Substitute.For <IMazeCrawlerCoordinator>();

            crawlerCoordinator.RequestSwarm(Arg.Any <IMazeCrawlerState>()).Returns(swarmCoordinator);

            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(startX, startY),
                Destination   = new Coordinates(destinationX, destinationY),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.OCCPD, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY }
                }),
                NavigationMode = CrawlerNavigationMode.Swarm,
                Coordinator    = crawlerCoordinator
            };
            var crawler = new MazeCrawler(context);

            await crawler.Navigate();

            swarmCoordinator.Received(numberOfCalls).GetSwarm(crawler);
        }
コード例 #2
0
        public async void Navigate_On_Scout_Mode_Should_Recalibrate_After_Each_Fork(int startX, int startY, int destinationX, int destinationY, string expected)
        {
            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(startX, startY),
                Destination   = new Coordinates(destinationX, destinationY),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY, Map.EMPTY }
                }),
                NavigationMode = CrawlerNavigationMode.Scout,
                Coordinator    = Substitute.For <IMazeCrawlerCoordinator>()
            };
            var crawler = new MazeCrawler(context);

            var response = await crawler.Navigate();

            response.Arrived.Should().BeTrue();
            response.PathTaken.Should().Be(expected);
        }
コード例 #3
0
        public async void Navigate_On_Scout_Mode_Should_BackTrack_To_Alternate_Routes()
        {
            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(0, 0),
                Destination   = new Coordinates(1, 7),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.OCCPD, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY }
                }),
                NavigationMode = CrawlerNavigationMode.Scout,
                Coordinator    = Substitute.For <IMazeCrawlerCoordinator>()
            };
            var crawler = new MazeCrawler(context);

            var response = await crawler.Navigate();

            response.Arrived.Should().BeTrue();
            response.PathTaken.Should().Be("EESSSSSSSW");
        }
コード例 #4
0
        public async void Navigate_Should_Handle_Single_Route_Maps(CrawlerNavigationMode mode, int startX, int startY, int destinationX, int destinationY, bool arrived, string path)
        {
            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(startX, startY),
                Destination   = new Coordinates(destinationX, destinationY),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.OCCPD, Map.OCCPD, Map.OCCPD, Map.OCCPD },
                    new [] { Map.OCCPD, Map.OCCPD, Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.OCCPD, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY, Map.EMPTY, Map.EMPTY }
                }),
                NavigationMode = mode,
                Coordinator    = Substitute.For <IMazeCrawlerCoordinator>()
            };
            var crawler = new MazeCrawler(context);

            var response = await crawler.Navigate();

            response.Arrived.Should().Be(arrived);
            response.PathTaken.Should().Be(path);
        }
コード例 #5
0
        public async void Navigate_On_Swarm_Mode_Should_Return_The_First_Task_That_Completes_With_Arrived_True_Result()
        {
            var result1 = new NavigationDetails {
                Arrived = true, PathTaken = "result1"
            };
            var task1    = Task.Run(() => { Task.Delay(300); return(result1); });
            var crawler1 = Substitute.For <IMazeCrawler>();

            crawler1.Navigate().Returns(task1);

            var result2 = new NavigationDetails {
                Arrived = true, PathTaken = "result2"
            };
            var task2    = Task.Run(() => { Task.Delay(500); return(result2); });
            var crawler2 = Substitute.For <IMazeCrawler>();

            crawler2.Navigate().Returns(task2);

            var task3    = Task.Run(() => { Task.Delay(100); return(new NavigationDetails {
                    Arrived = false
                }); });
            var crawler3 = Substitute.For <IMazeCrawler>();

            crawler3.Navigate().Returns(task3);

            var crawlers = new [] { crawler1, crawler2, crawler3 };

            var swarmCoordinator = Substitute.For <ISwarmCoordinator>();

            swarmCoordinator.GetSwarm(Arg.Any <IMazeCrawlerState>()).Returns(crawlers);

            var crawlerCoordinator = Substitute.For <IMazeCrawlerCoordinator>();

            crawlerCoordinator.RequestSwarm(Arg.Any <IMazeCrawlerState>()).Returns(swarmCoordinator);

            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(0, 0),
                Destination   = new Coordinates(0, 1),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD }
                }),
                NavigationMode = CrawlerNavigationMode.Swarm,
                Coordinator    = crawlerCoordinator
            };
            var crawler = new MazeCrawler(context);

            var response = await crawler.Navigate();

            response.Should().BeEquivalentTo(result1);
        }
コード例 #6
0
        public async void Navigate_On_Swarm_Mode_Should_Call_Each_IMazeCrawler_Navigate()
        {
            var crawler1 = Substitute.For <IMazeCrawler>();

            crawler1.Navigate().Returns(Task.FromResult(new NavigationDetails()));
            var crawler2 = Substitute.For <IMazeCrawler>();

            crawler2.Navigate().Returns(Task.FromResult(new NavigationDetails()));
            var crawler3 = Substitute.For <IMazeCrawler>();

            crawler3.Navigate().Returns(Task.FromResult(new NavigationDetails()));
            var crawlers         = new [] { crawler1, crawler2, crawler3 };
            var swarmCoordinator = Substitute.For <ISwarmCoordinator>();

            swarmCoordinator.GetSwarm(Arg.Any <IMazeCrawlerState>()).Returns(crawlers);

            var crawlerCoordinator = Substitute.For <IMazeCrawlerCoordinator>();

            crawlerCoordinator.RequestSwarm(Arg.Any <IMazeCrawlerState>()).Returns(swarmCoordinator);

            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(0, 0),
                Destination   = new Coordinates(0, 1),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD }
                }),
                NavigationMode = CrawlerNavigationMode.Swarm,
                Coordinator    = crawlerCoordinator
            };
            var crawler = new MazeCrawler(context);

            await crawler.Navigate();

            await crawler1.Received(1).Navigate();

            await crawler2.Received(1).Navigate();

            await crawler3.Received(1).Navigate();
        }
コード例 #7
0
        public async void Navigate_On_Swarm_Mode_Should_Call_IMazeCrawlerCoordinator_Debrief(int startX, int startY, int destinationX, int destinationY)
        {
            var task    = Task.Run(() => { Task.Delay(500); return(new NavigationDetails {
                    Arrived = false
                }); });
            var crawler = Substitute.For <IMazeCrawler>();

            crawler.Navigate().Returns(task);

            var crawlers = new [] { crawler };

            var swarmCoordinator = Substitute.For <ISwarmCoordinator>();

            swarmCoordinator.GetSwarm(Arg.Any <IMazeCrawlerState>()).Returns(crawlers);

            var crawlerCoordinator = Substitute.For <IMazeCrawlerCoordinator>();

            crawlerCoordinator.RequestSwarm(Arg.Any <IMazeCrawlerState>()).Returns(swarmCoordinator);

            var context = new MazeCrawlerContext
            {
                Start         = new Coordinates(startX, startY),
                Destination   = new Coordinates(destinationX, destinationY),
                NavigationMap = new Map(new char[][]
                {
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY },
                    new [] { Map.OCCPD, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.OCCPD, Map.EMPTY },
                    new [] { Map.EMPTY, Map.EMPTY, Map.EMPTY }
                }),
                NavigationMode = CrawlerNavigationMode.Swarm,
                Coordinator    = crawlerCoordinator
            };
            var mazeCrawler = new MazeCrawler(context);

            await mazeCrawler.Navigate();

            crawlerCoordinator.Received(1).Debrief(mazeCrawler);
        }