diff --git a/routing/routing_tests/applying_traffic_test.cpp b/routing/routing_tests/applying_traffic_test.cpp index 1adee5658d..adae50868f 100644 --- a/routing/routing_tests/applying_traffic_test.cpp +++ b/routing/routing_tests/applying_traffic_test.cpp @@ -5,6 +5,8 @@ #include "routing/index_graph.hpp" #include "routing/index_graph_starter.hpp" +#include "routing/routing_tests/index_graph_tools.hpp" + #include "traffic/traffic_info.hpp" #include "indexer/classificator_loader.hpp" @@ -20,77 +22,9 @@ namespace { using namespace routing; +using namespace routing_test; using namespace traffic; -// @TODO(bykoianko) When PR with applying restricions is merged all the helper method -// should be used from "routing/routing_tests/index_graph_tools.hpp". -class TestGeometryLoader final : public routing::GeometryLoader -{ -public: - // GeometryLoader overrides: - void Load(uint32_t featureId, routing::RoadGeometry & road) const override; - - void AddRoad(uint32_t featureId, bool oneWay, float speed, - routing::RoadGeometry::Points const & points); - -private: - unordered_map m_roads; -}; - -void TestGeometryLoader::Load(uint32_t featureId, RoadGeometry & road) const -{ - auto it = m_roads.find(featureId); - if (it == m_roads.cend()) - return; - - road = it->second; -} - -void TestGeometryLoader::AddRoad(uint32_t featureId, bool oneWay, float speed, - RoadGeometry::Points const & points) -{ - auto it = m_roads.find(featureId); - CHECK(it == m_roads.end(), ("Already contains feature", featureId)); - m_roads[featureId] = RoadGeometry(oneWay, speed, points); -} - -Joint MakeJoint(vector const & points) -{ - Joint joint; - for (auto const & point : points) - joint.AddPoint(point); - - return joint; -} - -AStarAlgorithm::Result CalculateRoute(IndexGraphStarter & starter, - vector & roadPoints) -{ - AStarAlgorithm algorithm; - RoutingResult routingResult; - auto const resultCode = algorithm.FindPath(starter, starter.GetStartJoint(), - starter.GetFinishJoint(), routingResult, {}, {}); - - starter.RedressRoute(routingResult.path, roadPoints); - return resultCode; -} - -void TestRouteGeometry(IndexGraphStarter & starter, - AStarAlgorithm::Result expectedRouteResult, - vector const & expectedRouteGeom) -{ - vector route; - auto const resultCode = CalculateRoute(starter, route); - TEST_EQUAL(resultCode, expectedRouteResult, ()); - TEST_EQUAL(route.size(), expectedRouteGeom.size(), ()); - for (size_t i = 0; i < route.size(); ++i) - { - RoadGeometry roadGeom = starter.GetGraph().GetGeometry().GetRoad(route[i].GetFeatureId()); - CHECK_LESS(route[i].GetPointId(), roadGeom.GetPointsCount(), ()); - TEST_EQUAL(expectedRouteGeom[i], roadGeom.GetPoint(route[i].GetPointId()), ()); - } -} - // @TODO(bykoianko) When PR with applying restricions is merged BuildXXGraph() // should be moved to "routing/routing_tests/index_graph_tools.hpp" and it should // be used by applying_traffic_test.cpp and cumulative_restriction_test.cpp. diff --git a/routing/routing_tests/index_graph_test.cpp b/routing/routing_tests/index_graph_test.cpp index 4dfb73de64..3fde4305ae 100644 --- a/routing/routing_tests/index_graph_test.cpp +++ b/routing/routing_tests/index_graph_test.cpp @@ -6,6 +6,8 @@ #include "routing/index_graph.hpp" #include "routing/index_graph_starter.hpp" +#include "routing/routing_tests/index_graph_tools.hpp" + #include "geometry/point2d.hpp" #include "base/assert.hpp" @@ -18,49 +20,7 @@ namespace { using namespace routing; - -class TestGeometryLoader final : public GeometryLoader -{ -public: - // GeometryLoader overrides: - void Load(uint32_t featureId, RoadGeometry & road) const override; - - void AddRoad(uint32_t featureId, bool oneWay, float speed, RoadGeometry::Points const & points); - -private: - unordered_map m_roads; -}; - -void TestGeometryLoader::Load(uint32_t featureId, RoadGeometry & road) const -{ - auto it = m_roads.find(featureId); - if (it == m_roads.cend()) - return; - - road = it->second; -} - -void TestGeometryLoader::AddRoad(uint32_t featureId, bool oneWay, float speed, - RoadGeometry::Points const & points) -{ - auto it = m_roads.find(featureId); - CHECK(it == m_roads.end(), ("Already contains feature", featureId)); - m_roads[featureId] = RoadGeometry(oneWay, speed, points); -} - -Joint MakeJoint(vector const & points) -{ - Joint joint; - for (auto const & point : points) - joint.AddPoint(point); - - return joint; -} - -shared_ptr CreateEstimator() -{ - return EdgeEstimator::CreateForCar(*make_shared()->GetVehicleModel()); -} +using namespace routing_test; void TestRoute(IndexGraph & graph, RoadPoint const & start, RoadPoint const & finish, size_t expectedLength, vector const * expectedRoute = nullptr) @@ -68,16 +28,11 @@ void TestRoute(IndexGraph & graph, RoadPoint const & start, RoadPoint const & fi LOG(LINFO, ("Test route", start.GetFeatureId(), ",", start.GetPointId(), "=>", finish.GetFeatureId(), ",", finish.GetPointId())); - AStarAlgorithm algorithm; - RoutingResult routingResult; - IndexGraphStarter starter(graph, start, finish); - auto const resultCode = algorithm.FindPath(starter, starter.GetStartJoint(), - starter.GetFinishJoint(), routingResult, {}, {}); + vector roadPoints; + auto const resultCode = CalculateRoute(starter, roadPoints); TEST_EQUAL(resultCode, AStarAlgorithm::Result::OK, ()); - vector roadPoints; - starter.RedressRoute(routingResult.path, roadPoints); TEST_EQUAL(roadPoints.size(), expectedLength, ()); if (expectedRoute) diff --git a/routing/routing_tests/index_graph_tools.cpp b/routing/routing_tests/index_graph_tools.cpp new file mode 100644 index 0000000000..8296ffe63a --- /dev/null +++ b/routing/routing_tests/index_graph_tools.cpp @@ -0,0 +1,70 @@ +#include "routing/routing_tests/index_graph_tools.hpp" + +#include "testing/testing.hpp" + +#include "routing/car_model.hpp" + +namespace routing_test +{ +using namespace routing; + +void TestGeometryLoader::Load(uint32_t featureId, RoadGeometry & road) const +{ + auto it = m_roads.find(featureId); + if (it == m_roads.cend()) + return; + + road = it->second; +} + +void TestGeometryLoader::AddRoad(uint32_t featureId, bool oneWay, float speed, + RoadGeometry::Points const & points) +{ + auto it = m_roads.find(featureId); + CHECK(it == m_roads.end(), ("Already contains feature", featureId)); + m_roads[featureId] = RoadGeometry(oneWay, speed, points); +} + +Joint MakeJoint(vector const & points) +{ + Joint joint; + for (auto const & point : points) + joint.AddPoint(point); + + return joint; +} + +shared_ptr CreateEstimator() +{ + return EdgeEstimator::CreateForCar(*make_shared()->GetVehicleModel()); +} + +AStarAlgorithm::Result CalculateRoute(IndexGraphStarter & starter, + vector & roadPoints) +{ + AStarAlgorithm algorithm; + RoutingResult routingResult; + auto const resultCode = algorithm.FindPath( + starter, starter.GetStartJoint(), starter.GetFinishJoint(), routingResult, {}, {}); + + starter.RedressRoute(routingResult.path, roadPoints); + return resultCode; +} + +void TestRouteGeometry(IndexGraphStarter & starter, + AStarAlgorithm::Result expectedRouteResult, + vector const & expectedRouteGeom) +{ + vector route; + auto const resultCode = CalculateRoute(starter, route); + TEST_EQUAL(resultCode, expectedRouteResult, ()); + TEST_EQUAL(route.size(), expectedRouteGeom.size(), ()); + for (size_t i = 0; i < route.size(); ++i) + { + // When PR with applying restricions is merged IndexGraph::GetRoad() should be used here instead. + RoadGeometry roadGeom = starter.GetGraph().GetGeometry().GetRoad(route[i].GetFeatureId()); + CHECK_LESS(route[i].GetPointId(), roadGeom.GetPointsCount(), ()); + TEST_EQUAL(expectedRouteGeom[i], roadGeom.GetPoint(route[i].GetPointId()), ()); + } +} +} // namespace routing_test diff --git a/routing/routing_tests/index_graph_tools.hpp b/routing/routing_tests/index_graph_tools.hpp new file mode 100644 index 0000000000..57f98d083f --- /dev/null +++ b/routing/routing_tests/index_graph_tools.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include "routing/edge_estimator.hpp" +#include "routing/index_graph.hpp" +#include "routing/index_graph_starter.hpp" +#include "routing/road_point.hpp" + +#include "routing/base/astar_algorithm.hpp" + +#include "geometry/point2d.hpp" + +#include "std/algorithm.hpp" +#include "std/shared_ptr.hpp" +#include "std/unique_ptr.hpp" +#include "std/unordered_map.hpp" +#include "std/vector.hpp" + +namespace routing_test +{ +class TestGeometryLoader final : public routing::GeometryLoader +{ +public: + // GeometryLoader overrides: + void Load(uint32_t featureId, routing::RoadGeometry & road) const override; + + void AddRoad(uint32_t featureId, bool oneWay, float speed, + routing::RoadGeometry::Points const & points); + +private: + unordered_map m_roads; +}; + +routing::Joint MakeJoint(vector const & points); + +shared_ptr CreateEstimator(); + +routing::AStarAlgorithm::Result CalculateRoute( + routing::IndexGraphStarter & graph, vector & roadPoints); + +void TestRouteSegments( + routing::IndexGraphStarter & starter, + routing::AStarAlgorithm::Result expectedRouteResult, + vector const & expectedRoute); + +void TestRouteGeometry( + routing::IndexGraphStarter & starter, + routing::AStarAlgorithm::Result expectedRouteResult, + vector const & expectedRouteGeom); +} // namespace routing_test diff --git a/routing/routing_tests/routing_tests.pro b/routing/routing_tests/routing_tests.pro index 25c3c5f28f..49ee873ed0 100644 --- a/routing/routing_tests/routing_tests.pro +++ b/routing/routing_tests/routing_tests.pro @@ -29,6 +29,7 @@ SOURCES += \ cross_routing_tests.cpp \ followed_polyline_test.cpp \ index_graph_test.cpp \ + index_graph_tools.cpp \ nearest_edge_finder_tests.cpp \ online_cross_fetcher_test.cpp \ osrm_router_test.cpp \ @@ -43,4 +44,5 @@ SOURCES += \ vehicle_model_test.cpp \ HEADERS += \ + index_graph_tools.hpp \ road_graph_builder.hpp \