forked from organicmaps/organicmaps
Routing session tests.
This commit is contained in:
parent
7da94c140f
commit
afa1d62d21
2 changed files with 128 additions and 0 deletions
127
routing/routing_tests/routing_session_test.cpp
Normal file
127
routing/routing_tests/routing_session_test.cpp
Normal file
|
@ -0,0 +1,127 @@
|
|||
#include "testing/testing.hpp"
|
||||
|
||||
#include "routing/route.hpp"
|
||||
#include "routing/router.hpp"
|
||||
#include "routing/routing_session.hpp"
|
||||
|
||||
#include "geometry/point2d.hpp"
|
||||
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include "std/string.hpp"
|
||||
#include "std/vector.hpp"
|
||||
|
||||
namespace routing {
|
||||
// Simple router. It returns route given to him on creation.
|
||||
class DummyRouter : public IRouter
|
||||
{
|
||||
private:
|
||||
Route & m_route;
|
||||
ResultCode m_code;
|
||||
size_t & m_buildCount;
|
||||
|
||||
public:
|
||||
DummyRouter(Route & route, ResultCode code, size_t & buildCounter)
|
||||
: m_route(route), m_code(code), m_buildCount(buildCounter)
|
||||
{
|
||||
}
|
||||
string GetName() const override { return "dummy"; }
|
||||
void ClearState() override {}
|
||||
ResultCode CalculateRoute(m2::PointD const & /* startPoint */,
|
||||
m2::PointD const & /* startDirection */,
|
||||
m2::PointD const & /* finalPoint */,
|
||||
RouterDelegate const & /* delegate */, Route & route) override
|
||||
{
|
||||
++m_buildCount;
|
||||
route = m_route;
|
||||
return m_code;
|
||||
}
|
||||
};
|
||||
|
||||
static vector<m2::PointD> kTestRoute = {{0., 1.}, {0., 2.}, {0., 3.}, {0., 4.}};
|
||||
|
||||
UNIT_TEST(TestRouteBuilding)
|
||||
{
|
||||
RoutingSession session;
|
||||
session.Init(nullptr, nullptr);
|
||||
vector<m2::PointD> routePoints = kTestRoute;
|
||||
Route masterRoute("dummy", routePoints.begin(), routePoints.end());
|
||||
size_t counter = 0;
|
||||
atomic<bool> routeBuilded(false);
|
||||
unique_ptr<DummyRouter> router = make_unique<DummyRouter>(masterRoute, DummyRouter::NoError, counter);
|
||||
session.SetRouter(move(router), nullptr);
|
||||
session.BuildRoute(kTestRoute.front(), kTestRoute.back(),
|
||||
[&routeBuilded](Route const &, IRouter::ResultCode)
|
||||
{
|
||||
routeBuilded = true;
|
||||
},
|
||||
nullptr, 0);
|
||||
while (!routeBuilded)
|
||||
{
|
||||
}
|
||||
TEST_EQUAL(counter, 1, ());
|
||||
}
|
||||
|
||||
UNIT_TEST(TestRouteRebuilding)
|
||||
{
|
||||
Index index;
|
||||
RoutingSession session;
|
||||
session.Init(nullptr, nullptr);
|
||||
vector<m2::PointD> routePoints = kTestRoute;
|
||||
Route masterRoute("dummy", routePoints.begin(), routePoints.end());
|
||||
size_t counter = 0;
|
||||
atomic<bool> routeBuilded(false);
|
||||
unique_ptr<DummyRouter> router = make_unique<DummyRouter>(masterRoute, DummyRouter::NoError, counter);
|
||||
session.SetRouter(move(router), nullptr);
|
||||
|
||||
// Go along the route.
|
||||
session.BuildRoute(kTestRoute.front(), kTestRoute.back(),
|
||||
[&routeBuilded](Route const &, IRouter::ResultCode)
|
||||
{
|
||||
routeBuilded = true;
|
||||
},
|
||||
nullptr, 0);
|
||||
while (!routeBuilded)
|
||||
{
|
||||
}
|
||||
|
||||
location::GpsInfo info;
|
||||
info.m_horizontalAccuracy = 0.01;
|
||||
info.m_verticalAccuracy = 0.01;
|
||||
info.m_longitude = 0.;
|
||||
info.m_latitude = 1.;
|
||||
RoutingSession::State code = session.OnLocationPositionChanged(
|
||||
MercatorBounds::FromLatLon(info.m_latitude, info.m_longitude), info, index);
|
||||
while (info.m_latitude < kTestRoute.back().y)
|
||||
{
|
||||
code = session.OnLocationPositionChanged(
|
||||
MercatorBounds::FromLatLon(info.m_latitude, info.m_longitude), info, index);
|
||||
TEST_EQUAL(code, RoutingSession::State::OnRoute, ());
|
||||
info.m_latitude += 0.01;
|
||||
}
|
||||
TEST_EQUAL(counter, 1, ());
|
||||
|
||||
// Rebuild route and go in opposite direction. So initiate a route rebuilding flag.
|
||||
counter = 0;
|
||||
routeBuilded = false;
|
||||
session.BuildRoute(kTestRoute.front(), kTestRoute.back(),
|
||||
[&routeBuilded](Route const &, IRouter::ResultCode)
|
||||
{
|
||||
routeBuilded = true;
|
||||
},
|
||||
nullptr, 0);
|
||||
info.m_longitude = 0.;
|
||||
info.m_latitude = 1.;
|
||||
while (!routeBuilded)
|
||||
{
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 10; ++i)
|
||||
{
|
||||
code = session.OnLocationPositionChanged(
|
||||
MercatorBounds::FromLatLon(info.m_latitude, info.m_longitude), info, index);
|
||||
info.m_latitude -= 0.1;
|
||||
}
|
||||
TEST_EQUAL(code, RoutingSession::State::RouteNeedRebuild, ());
|
||||
}
|
||||
} // namespace routing
|
|
@ -34,6 +34,7 @@ SOURCES += \
|
|||
road_graph_nearest_edges_test.cpp \
|
||||
route_tests.cpp \
|
||||
routing_mapping_test.cpp \
|
||||
routing_session_test.cpp \
|
||||
turns_generator_test.cpp \
|
||||
turns_sound_test.cpp \
|
||||
turns_tts_text_tests.cpp \
|
||||
|
|
Loading…
Add table
Reference in a new issue