Example #1
0
        public void TestFindPathTo()
        {
            /*
             * Тестовый город дает нам несколько дорог и, соответственно, построек.
             * Тип построек подбирается случайным образом, но так как мы задаем конкретный сид рандома
             * (в функции GenerateTestCityGraph), то первой постройкой (и соответственно вершиной в графе),
             * будет являться госпиталь.
             *
             * В первую очередь нужно убедиться в том, что выполняется условие «стартовая точка не может
             * оказаться конечной». Для этого пытаемся найти путь до точки того же типа, что и стартовая.
             */
            Graph    graph    = DataGenerator.GenerateTestCityGraph();
            PathInfo pathInfo = Finder.FindPathTo(graph, graph.Vertices.First(), InstitutionType.Hospital);

            Assert.IsFalse(pathInfo.IsEmptyPath());

            // Проверяем ситуацию, когда весь город состоит из искомых объектов.
            City  cityWithSameInstitutes  = DataGenerator.GenerateTestCity(new PostOfficeBuilder());
            Graph graphWithSameInstitutes = Finder.ConvertCityToGraph(cityWithSameInstitutes);

            pathInfo = Finder.FindPathTo(graphWithSameInstitutes,
                                         graphWithSameInstitutes.Vertices.First(), InstitutionType.PostOffice);

            Assert.IsFalse(pathInfo.IsEmptyPath());

            // Проверяем ситуацию, когда в городе нет искомого объекта.
            pathInfo = Finder.FindPathTo(graphWithSameInstitutes,
                                         graphWithSameInstitutes.Vertices.First(), InstitutionType.Hospital);

            Assert.IsTrue(pathInfo.IsEmptyPath());
        }
Example #2
0
        public void TestFindPathToOverload()
        {
            City city = DataGenerator.GenerateTestCity();

            // Проверка перегрузки метода поиска, который принимает город и объект учреждения, а не граф.
            // Результаты должны быть идентичны версии с графом.

            Graph    graph         = DataGenerator.GenerateTestCityGraph();
            PathInfo graphPathInfo = Finder.FindPathTo(graph, graph.Vertices.First(),
                                                       InstitutionType.Hospital);
            CityPathInfo cityPathInfo = Finder.FindPathTo(city, city.Institutions.First(),
                                                          InstitutionType.Hospital);

            Assert.AreEqual(graphPathInfo.Path.Length, cityPathInfo.Path.Length);
            Assert.AreEqual(graphPathInfo.TotalLength, cityPathInfo.TotalLength);
        }
Example #3
0
        public void TestConvertCityToGraph()
        {
            City  testCity = DataGenerator.GenerateTestCity();
            Graph graph    = Finder.ConvertCityToGraph(testCity);

            /**
             * Так как мы создаем тестовый город, в котором изначально уже знаем количество
             * пересечений дорог, то можно проверить количество вершин и ребер.
             */

            List <int> validationValues = new List <int>()
            {
                2, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 3, 4, 4, 4
            };

            Assert.AreEqual(15, graph.Vertices.Count);
            for (int i = 0; i < graph.Vertices.Count; i++)
            {
                Assert.AreEqual(validationValues[i], graph.Vertices[i].Edges.Count);
            }
        }
Example #4
0
        public void TestAddRoad()
        {
            City city = DataGenerator.GenerateTestCity();

            Dictionary <Point2D, int> validationValues = new Dictionary <Point2D, int>()
            {
                { new Point2D(0, 0), 2 },
                { new Point2D(2, 0), 3 },
                { new Point2D(5, 0), 3 },
                { new Point2D(8, 0), 3 },
                { new Point2D(10, 0), 2 },
                { new Point2D(0, 4), 3 },
                { new Point2D(0, 8), 3 },
                { new Point2D(10, 10), 3 },
                { new Point2D(0, 11), 2 },
                { new Point2D(5, 11), 3 },
                { new Point2D(7, 11), 3 },
                { new Point2D(10, 11), 2 },
            };

            Assert.AreEqual(15, city.Institutions.Count);

            foreach (Institution institution in city.Institutions)
            {
                if (validationValues.ContainsKey(institution.Location))
                {
                    Assert.AreEqual(validationValues[institution.Location], institution.Neighbours.Count,
                                    $"{institution.Location}");
                }
            }

            // Проверим город, который пока без дорог. Но так как границы тоже дороги, то должно быть 4 учреждения.
            City city2 = new City(new Point2D(0, 0), new Point2D(10, 10),
                                  new RandomBuilder(1337));

            Assert.AreEqual(4, city2.Institutions.Count);

            Dictionary <Point2D, int> city2ValidationValues = new Dictionary <Point2D, int>()
            {
                { new Point2D(0, 0), 2 },
                { new Point2D(10, 0), 2 },
                { new Point2D(10, 10), 2 },
                { new Point2D(0, 10), 2 },
            };

            foreach (Institution institution in city2.Institutions)
            {
                if (city2ValidationValues.ContainsKey(institution.Location))
                {
                    Assert.AreEqual(city2ValidationValues[institution.Location], institution.Neighbours.Count,
                                    $"{institution.Location}");
                }
            }

            // Проверяем ситуацию, когда дорога проходит ровно по центру пересечения других двух дорог.
            city2.AddRoad(new Road(new Point2D(-2, -2), new Point2D(14, 14)));

            foreach (Institution institution in city2.Institutions)
            {
                Console.WriteLine(institution.Location);
            }

            // Если дорога прошла между другого пересечения, то новых построек не будет.
            Assert.AreEqual(4, city2.Institutions.Count);

            /*
             * Тем не менее, если дорога прошла сквозь другие пересечения, то тогда она должна связать те учреждения,
             * которые до этого не были связаны.
             * То есть, например, те, что по углам располагались. Прямой связи между левым верхним и правым нижним
             * углом не было, а теперь есть. А значит нужно проверить соседей.
             */

            Dictionary <Point2D, int> city2ValidationValuesAfterRoad = new Dictionary <Point2D, int>()
            {
                { new Point2D(0, 0), 3 },
                { new Point2D(10, 0), 2 },
                { new Point2D(10, 10), 3 },
                { new Point2D(0, 10), 2 },
            };

            foreach (Institution institution in city2.Institutions)
            {
                if (city2ValidationValuesAfterRoad.ContainsKey(institution.Location))
                {
                    Assert.AreEqual(city2ValidationValuesAfterRoad[institution.Location], institution.Neighbours.Count,
                                    $"{institution.Location}");
                }
            }
        }