From 06d2506c41b27aa45743a97efffb53d776384ccc Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Fri, 14 Apr 2017 00:38:41 +0300 Subject: [PATCH] Review fixes. --- .../cumulative_restriction_test.cpp | 1 + routing/routing_tests/index_graph_test.cpp | 99 ++++++++++--------- routing/routing_tests/index_graph_tools.cpp | 32 +++--- routing/routing_tests/index_graph_tools.hpp | 7 +- 4 files changed, 75 insertions(+), 64 deletions(-) diff --git a/routing/routing_tests/cumulative_restriction_test.cpp b/routing/routing_tests/cumulative_restriction_test.cpp index 668e318dbe..1dfbbc0b5d 100644 --- a/routing/routing_tests/cumulative_restriction_test.cpp +++ b/routing/routing_tests/cumulative_restriction_test.cpp @@ -17,6 +17,7 @@ namespace { using namespace routing; using namespace routing_test; +using namespace std; // Finish // 3 * diff --git a/routing/routing_tests/index_graph_test.cpp b/routing/routing_tests/index_graph_test.cpp index 17f98e2fa7..e8bce04a1e 100644 --- a/routing/routing_tests/index_graph_test.cpp +++ b/routing/routing_tests/index_graph_test.cpp @@ -25,11 +25,15 @@ #include "std/unordered_map.hpp" #include "std/vector.hpp" +using namespace std; + namespace { using namespace routing; using namespace routing_test; +using Edge = TestIndexGraphTopology::Edge; + void TestRoute(IndexGraphStarter::FakeVertex const & start, IndexGraphStarter::FakeVertex const & finish, size_t expectedLength, vector const * expectedRoute, WorldGraph & graph) @@ -85,6 +89,24 @@ void TestIngoingEdges(IndexGraph & graph, Segment const & segment, } uint32_t AbsDelta(uint32_t v0, uint32_t v1) { return v0 > v1 ? v0 - v1 : v1 - v0; } + +void TestTopologyGraph(TestIndexGraphTopology const & graph, TestIndexGraphTopology::Vertex from, + TestIndexGraphTopology::Vertex to, bool expectedPathFound, + double const expectedWeight, vector const & expectedEdges) +{ + double const kEpsilon = 1e-6; + + double pathWeight = 0.0; + vector pathEdges; + bool const pathFound = graph.FindPath(from, to, pathWeight, pathEdges); + TEST_EQUAL(pathFound, expectedPathFound, ()); + if (!pathFound) + return; + + TEST(my::AlmostEqualAbs(pathWeight, expectedWeight, kEpsilon), + (pathWeight, expectedWeight, pathEdges)); + TEST_EQUAL(pathEdges, expectedEdges, ()); +} } // namespace namespace routing_test @@ -506,63 +528,46 @@ UNIT_CLASS_TEST(RestrictionTest, LoopGraph) TestRouteTime(*m_starter, AStarAlgorithm::Result::OK, kExpectedRouteTimeSec); } -UNIT_TEST(IndexGraph_OnlyTopology) +UNIT_TEST(IndexGraph_OnlyTopology_1) { - double const kEpsilon = 1e-6; - using Vertex = TestIndexGraphTopology::Vertex; - using Edge = TestIndexGraphTopology::Edge; // Add edges to the graph in the following format: (from, to, weight). - auto const testGraph = [&](TestIndexGraphTopology & graph, Vertex from, Vertex to, - bool expectedPathFound, double const expectedWeight, - vector const & expectedEdges) { - double pathWeight = 0.0; - vector pathEdges; - bool const pathFound = graph.FindPath(from, to, pathWeight, pathEdges); - TEST_EQUAL(pathFound, expectedPathFound, ()); - if (!pathFound) - return; + uint32_t const numVertices = 5; + TestIndexGraphTopology graph(numVertices); + graph.AddDirectedEdge(0, 1, 1.0); + graph.AddDirectedEdge(0, 2, 2.0); + graph.AddDirectedEdge(1, 3, 1.0); + graph.AddDirectedEdge(2, 3, 2.0); - TEST(my::AlmostEqualAbs(pathWeight, expectedWeight, kEpsilon), - (pathWeight, expectedWeight, pathEdges)); - TEST_EQUAL(pathEdges, expectedEdges, ()); - }; + double const expectedWeight = 2.0; + vector const expectedEdges = {{0, 1}, {1, 3}}; - { - uint32_t const numVertices = 5; - TestIndexGraphTopology graph(numVertices); - graph.AddDirectedEdge(0, 1, 1.0); - graph.AddDirectedEdge(0, 2, 2.0); - graph.AddDirectedEdge(1, 3, 1.0); - graph.AddDirectedEdge(2, 3, 2.0); + TestTopologyGraph(graph, 0, 3, true /* pathFound */, expectedWeight, expectedEdges); + TestTopologyGraph(graph, 0, 4, false /* pathFound */, 0.0, {}); +} - double const expectedWeight = 2.0; - vector const expectedEdges = {{0, 1}, {1, 3}}; +UNIT_TEST(IndexGraph_OnlyTopology_2) +{ + uint32_t const numVertices = 1; + TestIndexGraphTopology graph(numVertices); + graph.AddDirectedEdge(0, 0, 100.0); - testGraph(graph, 0, 3, true /* pathFound */, expectedWeight, expectedEdges); - testGraph(graph, 0, 4, false /* pathFound */, 0.0, {}); - } + double const expectedWeight = 0.0; + vector const expectedEdges = {}; - { - uint32_t const numVertices = 1; - TestIndexGraphTopology graph(numVertices); - graph.AddDirectedEdge(0, 0, 100.0); + TestTopologyGraph(graph, 0, 0, true /* pathFound */, expectedWeight, expectedEdges); +} - double const expectedWeight = 0.0; - vector const expectedEdges = {}; +UNIT_TEST(IndexGraph_OnlyTopology_3) +{ + uint32_t const numVertices = 2; + TestIndexGraphTopology graph(numVertices); - testGraph(graph, 0, 0, true /* pathFound */, expectedWeight, expectedEdges); - } + graph.AddDirectedEdge(0, 1, 1.0); + graph.AddDirectedEdge(1, 0, 1.0); + double const expectedWeight = 1.0; + vector const expectedEdges = {{0, 1}}; - { - uint32_t const numVertices = 2; - TestIndexGraphTopology graph(numVertices); - graph.AddDirectedEdge(0, 1, 1.0); - graph.AddDirectedEdge(1, 0, 1.0); - double const expectedWeight = 1.0; - vector const expectedEdges = {{0, 1}}; - - testGraph(graph, 0, 1, true /* pathFound */, expectedWeight, expectedEdges); - } + TestTopologyGraph(graph, 0, 1, true /* pathFound */, expectedWeight, expectedEdges); } } // namespace routing_test diff --git a/routing/routing_tests/index_graph_tools.cpp b/routing/routing_tests/index_graph_tools.cpp index 6ec344d3df..c2c8fd4565 100644 --- a/routing/routing_tests/index_graph_tools.cpp +++ b/routing/routing_tests/index_graph_tools.cpp @@ -5,7 +5,6 @@ #include "routing_common/car_model.hpp" #include "base/assert.hpp" -#include "base/scope_guard.hpp" namespace routing_test { @@ -78,12 +77,11 @@ TestIndexGraphTopology::TestIndexGraphTopology(uint32_t numVertices) : m_numVert void TestIndexGraphTopology::AddDirectedEdge(Vertex from, Vertex to, double weight) { - uint32_t const id = static_cast(m_edgeRequests.size()); - m_edgeRequests.emplace_back(id, from, to, weight); + AddDirectedEdge(m_edgeRequests, from, to, weight); } bool TestIndexGraphTopology::FindPath(Vertex start, Vertex finish, double & pathWeight, - vector & pathEdges) + vector & pathEdges) const { CHECK_LESS(start, m_numVertices, ()); CHECK_LESS(finish, m_numVertices, ()); @@ -95,19 +93,16 @@ bool TestIndexGraphTopology::FindPath(Vertex start, Vertex finish, double & path return true; } + auto edgeRequests = m_edgeRequests; // Edges of the index graph are segments, so we need a loop at finish // for the end of our path and another loop at start for the bidirectional search. - auto const startFeatureId = static_cast(m_edgeRequests.size()); - AddDirectedEdge(start, start, 0.0); - auto const finishFeatureId = static_cast(m_edgeRequests.size()); - AddDirectedEdge(finish, finish, 0.0); - MY_SCOPE_GUARD(cleanup, [&]() { - m_edgeRequests.pop_back(); - m_edgeRequests.pop_back(); - }); + auto const startFeatureId = static_cast(edgeRequests.size()); + AddDirectedEdge(edgeRequests, start, start, 0.0); + auto const finishFeatureId = static_cast(edgeRequests.size()); + AddDirectedEdge(edgeRequests, finish, finish, 0.0); Builder builder(m_numVertices); - builder.BuildGraphFromRequests(m_edgeRequests); + builder.BuildGraphFromRequests(edgeRequests); auto const worldGraph = builder.PrepareIndexGraph(); CHECK(worldGraph != nullptr, ()); @@ -153,6 +148,13 @@ bool TestIndexGraphTopology::FindPath(Vertex start, Vertex finish, double & path return true; } +void TestIndexGraphTopology::AddDirectedEdge(vector & edgeRequests, Vertex from, + Vertex to, double weight) const +{ + uint32_t const id = static_cast(edgeRequests.size()); + edgeRequests.emplace_back(id, from, to, weight); +} + // TestIndexGraphTopology::Builder ----------------------------------------------------------------- unique_ptr TestIndexGraphTopology::Builder::PrepareIndexGraph() { @@ -174,7 +176,7 @@ void TestIndexGraphTopology::Builder::BuildJoints() for (auto const & segment : m_outgoingSegments[i]) joint.AddPoint(RoadPoint(segment.GetFeatureId(), segment.GetPointId(false /* front */))); - for (auto const & segment : m_incomingSegments[i]) + for (auto const & segment : m_ingoingSegments[i]) joint.AddPoint(RoadPoint(segment.GetFeatureId(), segment.GetPointId(true /* front */))); } } @@ -197,7 +199,7 @@ void TestIndexGraphTopology::Builder::BuildSegmentFromEdge(EdgeRequest const & r m_segmentWeights[segment] = request.m_weight; m_segmentToEdge[segment] = edge; m_outgoingSegments[request.m_from].push_back(segment); - m_incomingSegments[request.m_to].push_back(segment); + m_ingoingSegments[request.m_to].push_back(segment); } // Functions --------------------------------------------------------------------------------------- diff --git a/routing/routing_tests/index_graph_tools.hpp b/routing/routing_tests/index_graph_tools.hpp index 459c1542ed..f904672ecc 100644 --- a/routing/routing_tests/index_graph_tools.hpp +++ b/routing/routing_tests/index_graph_tools.hpp @@ -126,7 +126,7 @@ public: void AddDirectedEdge(Vertex from, Vertex to, double weight); // Finds a path between the start and finish vertices. Returns true iff a path exists. - bool FindPath(Vertex start, Vertex finish, double & pathWeight, vector & pathEdges); + bool FindPath(Vertex start, Vertex finish, double & pathWeight, vector & pathEdges) const; private: struct EdgeRequest @@ -156,10 +156,13 @@ private: map m_segmentWeights; map m_segmentToEdge; map> m_outgoingSegments; - map> m_incomingSegments; + map> m_ingoingSegments; vector m_joints; }; + void AddDirectedEdge(vector & edgeRequests, Vertex from, Vertex to, + double weight) const; + uint32_t const m_numVertices; vector m_edgeRequests; };