Skip to content

Commit

Permalink
[routing] Unit tests on A* bidirectional in two threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
bykoianko committed Nov 18, 2020
1 parent f184f1d commit cb3df03
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
23 changes: 19 additions & 4 deletions routing/routing_tests/astar_algorithm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,38 @@ using namespace std;

using Algorithm = AStarAlgorithm<uint32_t, SimpleEdge, double>;

void TestOnRouteGeomAndDistance(
RoutingResult<unsigned /* Vertex */, double /* Weight */> const & actualRoute,
vector<unsigned> const & expectedRoute, double const & expectedDistance)
{
TEST_EQUAL(expectedRoute, actualRoute.m_path, ());
TEST_ALMOST_EQUAL_ULPS(expectedDistance, actualRoute.m_distance, ());
}

void TestAStar(UndirectedGraph & graph, vector<unsigned> const & expectedRoute, double const & expectedDistance)
{
Algorithm algo;

Algorithm::ParamsForTests<> params(graph, 0u /* startVertex */, 4u /* finishVertex */,
nullptr /* prevRoute */);

// Algorithm::FindPath() testing.
RoutingResult<unsigned /* Vertex */, double /* Weight */> actualRoute;
TEST_EQUAL(Algorithm::Result::OK, algo.FindPath(params, actualRoute), ());
TEST_EQUAL(expectedRoute, actualRoute.m_path, ());
TEST_ALMOST_EQUAL_ULPS(expectedDistance, actualRoute.m_distance, ());
TestOnRouteGeomAndDistance(actualRoute, expectedRoute, expectedDistance);

// Algorithm::FindPathBidirectional() in one thread testing.
actualRoute.m_path.clear();
TEST_EQUAL(Algorithm::Result::OK,
algo.FindPathBidirectional(params, actualRoute), ());
TEST_EQUAL(expectedRoute, actualRoute.m_path, ());
TEST_ALMOST_EQUAL_ULPS(expectedDistance, actualRoute.m_distance, ());
TestOnRouteGeomAndDistance(actualRoute, expectedRoute, expectedDistance);

// Algorithm::FindPathBidirectional() in two thread testing.
actualRoute.m_path.clear();
graph.SetTwoThreadsReady(true);
TEST_EQUAL(Algorithm::Result::OK,
algo.FindPathBidirectional(params, actualRoute), ());
TestOnRouteGeomAndDistance(actualRoute, expectedRoute, expectedDistance);
}

UNIT_TEST(AStarAlgorithm_Sample)
Expand Down
1 change: 0 additions & 1 deletion routing/routing_tests/routing_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace routing_test
{
UndirectedGraph::UndirectedGraph()
{
CHECK(!IsTwoThreadsReady(), ());
}

void UndirectedGraph::AddEdge(Vertex u, Vertex v, Weight w)
Expand Down
6 changes: 6 additions & 0 deletions routing/routing_tests/routing_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ class UndirectedGraph : public AStarGraph<uint32_t, SimpleEdge, double>
void GetOutgoingEdgesList(astar::VertexData<Vertex, Weight> const & vertexData,
std::vector<Edge> & adj) override;
double HeuristicCostEstimate(Vertex const & v, Vertex const & w, bool isOutgoing) override;
virtual bool IsTwoThreadsReady() const override { return m_twoThreadsReady; }
// @}

void GetEdgesList(Vertex const & vertex, bool /* isOutgoing */, std::vector<Edge> & adj);
/// \note If |twoThreadsRead| is equal to true it means class should be ready that methods
/// GetIngoingEdgesList(), GetOutgoingEdgesList() and HeuristicCostEstimate() are called
/// from two threads depending on |isOutgoing| parameter.
void SetTwoThreadsReady(bool twoThreadsRead) { m_twoThreadsReady = twoThreadsRead; }

private:
void GetAdjacencyList(Vertex v, std::vector<Edge> & adj) const;

std::map<uint32_t, std::vector<Edge>> m_adjs;
bool m_twoThreadsReady = false;
};

class DirectedGraph
Expand Down

0 comments on commit cb3df03

Please sign in to comment.