Welcome to mirror list, hosted at ThFree Co, Russian Federation.

routing_test_tools.cpp « routing_integration_tests « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 688115296a6328f97525871276ca389e92b21d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "routing/routing_integration_tests/routing_test_tools.hpp"

#include "routing/routing_tests/index_graph_tools.hpp"

#include "testing/testing.hpp"

#include "map/feature_vec_model.hpp"

#include "storage/routing_helpers.hpp"

#include "routing/index_router.hpp"
#include "routing/online_absent_fetcher.hpp"
#include "routing/online_cross_fetcher.hpp"
#include "routing/route.hpp"
#include "routing/router_delegate.hpp"
#include "routing/routing_callbacks.hpp"

#include "indexer/data_source.hpp"

#include "storage/country_parent_getter.hpp"

#include "platform/local_country_file.hpp"
#include "platform/local_country_file_utils.hpp"
#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"

#include "geometry/distance_on_sphere.hpp"
#include "geometry/latlon.hpp"

#include "base/math.hpp"
#include "base/stl_add.hpp"

#include "std/functional.hpp"
#include "std/limits.hpp"

#include "private.h"

#include <sys/resource.h>

using namespace routing;
using namespace routing_test;

using TRouterFactory =
    function<unique_ptr<IRouter>(DataSource & dataSource, TCountryFileFn const & countryFileFn,
                                 shared_ptr<NumMwmIds> numMwmIds)>;

namespace
{
double constexpr kErrorMeters = 1.0;
double constexpr kErrorSeconds = 1.0;

void ChangeMaxNumberOfOpenFiles(size_t n)
{
  struct rlimit rlp;
  getrlimit(RLIMIT_NOFILE, &rlp);
  rlp.rlim_cur = n;
  setrlimit(RLIMIT_NOFILE, &rlp);
}
}  // namespace

namespace integration
{
  shared_ptr<model::FeaturesFetcher> CreateFeaturesFetcher(vector<LocalCountryFile> const & localFiles)
  {
    size_t const maxOpenFileNumber = 4096;
    ChangeMaxNumberOfOpenFiles(maxOpenFileNumber);
    shared_ptr<model::FeaturesFetcher> featuresFetcher(new model::FeaturesFetcher);
    featuresFetcher->InitClassificator();

    for (LocalCountryFile const & localFile : localFiles)
    {
      auto p = featuresFetcher->RegisterMap(localFile);
      if (p.second != MwmSet::RegResult::Success)
      {
        ASSERT(false, ("Can't register", localFile));
        return nullptr;
      }
    }
    return featuresFetcher;
  }

  unique_ptr<storage::CountryInfoGetter> CreateCountryInfoGetter()
  {
    Platform const & platform = GetPlatform();
    return storage::CountryInfoReader::CreateCountryInfoReader(platform);
  }

  unique_ptr<IndexRouter> CreateVehicleRouter(DataSource & dataSource,
                                              storage::CountryInfoGetter const & infoGetter,
                                              traffic::TrafficCache const & trafficCache,
                                              vector<LocalCountryFile> const & localFiles,
                                              VehicleType vehicleType)
  {
    auto const countryFileGetter = [&infoGetter](m2::PointD const & pt) {
      return infoGetter.GetRegionCountryId(pt);
    };

    auto const getMwmRectByName = [&infoGetter](string const & countryId) -> m2::RectD {
      return infoGetter.GetLimitRectForLeaf(countryId);
    };

    auto numMwmIds = make_shared<NumMwmIds>();
    for (auto const & f : localFiles)
    {
      auto const & countryFile = f.GetCountryFile();
      auto const mwmId = dataSource.GetMwmIdByCountryFile(countryFile);
      CHECK(mwmId.IsAlive(), ());
      if (mwmId.GetInfo()->GetType() == MwmInfo::COUNTRY && countryFile.GetName() != "minsk-pass")
        numMwmIds->RegisterFile(countryFile);
    }

    auto countryParentGetter = my::make_unique<storage::CountryParentGetter>();
    CHECK(countryParentGetter, ());

    auto indexRouter = make_unique<IndexRouter>(vehicleType, false /* load altitudes*/,
                                                *countryParentGetter, countryFileGetter,
                                                getMwmRectByName, numMwmIds,
                                                MakeNumMwmTree(*numMwmIds, infoGetter), trafficCache, dataSource);

    return indexRouter;
  }

  unique_ptr<IRouter> CreateAStarRouter(DataSource & dataSource,
                                        storage::CountryInfoGetter const & infoGetter,
                                        vector<LocalCountryFile> const & localFiles,
                                        TRouterFactory const & routerFactory)
  {
    // |infoGetter| should be a reference to an object which exists while the
    // result of the function is used.
    auto countryFileGetter = [&infoGetter](m2::PointD const & pt)
    {
      return infoGetter.GetRegionCountryId(pt);
    };

    auto numMwmIds = make_shared<NumMwmIds>();
    for (auto const & file : localFiles)
      numMwmIds->RegisterFile(file.GetCountryFile());

    unique_ptr<IRouter> router = routerFactory(dataSource, countryFileGetter, numMwmIds);
    return unique_ptr<IRouter>(move(router));
  }

  void GetAllLocalFiles(vector<LocalCountryFile> & localFiles)
  {
    // Setting stored paths from testingmain.cpp
    Platform & pl = GetPlatform();
    CommandLineOptions const & options = GetTestingOptions();
    if (options.m_dataPath)
      pl.SetWritableDirForTests(options.m_dataPath);
    if (options.m_resourcePath)
      pl.SetResourceDir(options.m_resourcePath);

    platform::migrate::SetMigrationFlag();
    platform::FindAllLocalMapsAndCleanup(numeric_limits<int64_t>::max() /* latestVersion */,
                                         localFiles);
    for (auto & file : localFiles)
      file.SyncWithDisk();
  }

  shared_ptr<VehicleRouterComponents> CreateAllMapsComponents(VehicleType vehicleType)
  {
    vector<LocalCountryFile> localFiles;
    GetAllLocalFiles(localFiles);
    ASSERT(!localFiles.empty(), ());
    return make_shared<VehicleRouterComponents>(localFiles, vehicleType);
  }

  TRouteResult CalculateRoute(IRouterComponents const & routerComponents,
                              m2::PointD const & startPoint, m2::PointD const & startDirection,
                              m2::PointD const & finalPoint)
  {
    RouterDelegate delegate;
    shared_ptr<Route> route(new Route("mapsme"));
    RouterResultCode result = routerComponents.GetRouter().CalculateRoute(
        Checkpoints(startPoint, finalPoint), startDirection, false /* adjust */, delegate, *route);
    ASSERT(route, ());
    return TRouteResult(route, result);
  }

  void TestTurnCount(routing::Route const & route, uint32_t expectedTurnCount)
  {
    // We use -1 for ignoring the "ReachedYourDestination" turn record.
    vector<turns::TurnItem> turns;
    route.GetTurnsForTesting(turns);
    TEST_EQUAL(turns.size() - 1, expectedTurnCount, ());
  }

  void TestCurrentStreetName(routing::Route const & route, string const & expectedStreetName)
  {
    string streetName;
    route.GetCurrentStreetName(streetName);
    TEST_EQUAL(streetName, expectedStreetName, ());
  }

  void TestNextStreetName(routing::Route const & route, string const & expectedStreetName)
  {
    string streetName;
    double distance;
    turns::TurnItem turn;
    route.GetCurrentTurn(distance, turn);
    route.GetStreetNameAfterIdx(turn.m_index, streetName);
    TEST_EQUAL(streetName, expectedStreetName, ());
  }

  void TestRouteLength(Route const & route, double expectedRouteMeters,
                       double relativeError)
  {
    double const delta = max(expectedRouteMeters * relativeError, kErrorMeters);
    double const routeMeters = route.GetTotalDistanceMeters();
    TEST(my::AlmostEqualAbs(routeMeters, expectedRouteMeters, delta),
        ("Route length test failed. Expected:", expectedRouteMeters, "have:", routeMeters, "delta:", delta));
  }

  void TestRouteTime(Route const & route, double expectedRouteSeconds, double relativeError)
  {
    double const delta = max(expectedRouteSeconds * relativeError, kErrorSeconds);
    double const routeSeconds = route.GetTotalTimeSec();
    TEST(my::AlmostEqualAbs(routeSeconds, expectedRouteSeconds, delta),
        ("Route time test failed. Expected:", expectedRouteSeconds, "have:", routeSeconds, "delta:", delta));
  }

  void TestRoutePointsNumber(Route const & route, size_t expectedPointsNumber, double relativeError)
  {
    CHECK_GREATER_OR_EQUAL(relativeError, 0.0, ());
    size_t const routePoints = route.GetPoly().GetSize();
    TEST(my::AlmostEqualRel(static_cast<double>(routePoints),
                            static_cast<double>(expectedPointsNumber), relativeError),
         ("Route points test failed. Expected:", expectedPointsNumber, "have:", routePoints,
          "relative error:", relativeError));
  }

  void CalculateRouteAndTestRouteLength(IRouterComponents const & routerComponents,
                                        m2::PointD const & startPoint,
                                        m2::PointD const & startDirection,
                                        m2::PointD const & finalPoint, double expectedRouteMeters,
                                        double relativeError)
  {
    TRouteResult routeResult =
        CalculateRoute(routerComponents, startPoint, startDirection, finalPoint);
    RouterResultCode const result = routeResult.second;
    TEST_EQUAL(result, RouterResultCode::NoError, ());
    TestRouteLength(*routeResult.first, expectedRouteMeters, relativeError);
  }

  const TestTurn & TestTurn::TestValid() const
  {
    TEST(m_isValid, ());
    return *this;
  }

  const TestTurn & TestTurn::TestNotValid() const
  {
    TEST(!m_isValid, ());
    return *this;
  }

  const TestTurn & TestTurn::TestPoint(m2::PointD const & expectedPoint, double inaccuracyMeters) const
  {
    double const distanceMeters = ms::DistanceOnEarth(expectedPoint.y, expectedPoint.x, m_point.y, m_point.x);
    TEST_LESS(distanceMeters, inaccuracyMeters, ());
    return *this;
  }

  const TestTurn & TestTurn::TestDirection(routing::turns::CarDirection expectedDirection) const
  {
    TEST_EQUAL(m_direction, expectedDirection, (m_direction));
    return *this;
  }

  const TestTurn & TestTurn::TestOneOfDirections(
      set<routing::turns::CarDirection> const & expectedDirections) const
  {
    TEST(expectedDirections.find(m_direction) != expectedDirections.cend(), (m_direction));
    return *this;
  }

  const TestTurn & TestTurn::TestRoundAboutExitNum(uint32_t expectedRoundAboutExitNum) const
  {
    TEST_EQUAL(m_roundAboutExitNum, expectedRoundAboutExitNum, ());
    return *this;
  }

  TestTurn GetNthTurn(routing::Route const & route, uint32_t turnNumber)
  {
    vector<turns::TurnItem> turns;
    route.GetTurnsForTesting(turns);
    if (turnNumber >= turns.size())
      return TestTurn();

    TurnItem const & turn = turns[turnNumber];
    return TestTurn(route.GetPoly().GetPoint(turn.m_index), turn.m_turn, turn.m_exitNum);
  }

  void TestOnlineFetcher(ms::LatLon const & startPoint, ms::LatLon const & finalPoint,
                         vector<string> const & expected, IRouterComponents & routerComponents)
  {
    auto countryFileGetter = [&routerComponents](m2::PointD const & p) -> string
    {
      return routerComponents.GetCountryInfoGetter().GetRegionCountryId(p);
    };
    auto localFileChecker = [](string const & /* countryFile */) -> bool {
      // Always returns that the file is absent.
      return false;
    };
    routing::OnlineAbsentCountriesFetcher fetcher(countryFileGetter, localFileChecker);
    fetcher.GenerateRequest(Checkpoints(MercatorBounds::FromLatLon(startPoint),
                                        MercatorBounds::FromLatLon(finalPoint)));
    vector<string> absent;
    fetcher.GetAbsentCountries(absent);
    if (expected.size() < 2)
    {
      // Single MWM case. Do not use online routing.
      TEST(absent.empty(), ());
      return;
    }
    TEST_EQUAL(absent.size(), expected.size(), ());
    for (string const & name : expected)
      TEST(find(absent.begin(), absent.end(), name) != absent.end(), ("Can't find", name));
  }

  void TestOnlineCrosses(ms::LatLon const & startPoint, ms::LatLon const & finalPoint,
                         vector<string> const & expected,
                         IRouterComponents & routerComponents)
  {
    TCountryFileFn const countryFileGetter = [&](m2::PointD const & p) {
      return routerComponents.GetCountryInfoGetter().GetRegionCountryId(p);
    };
    routing::OnlineCrossFetcher fetcher(countryFileGetter, OSRM_ONLINE_SERVER_URL,
                                        Checkpoints(MercatorBounds::FromLatLon(startPoint),
                                                    MercatorBounds::FromLatLon(finalPoint)));
    fetcher.Do();
    vector<m2::PointD> const & points = fetcher.GetMwmPoints();
    set<string> foundMwms;

    for (m2::PointD const & point : points)
    {
      string const mwmName = routerComponents.GetCountryInfoGetter().GetRegionCountryId(point);
      TEST(find(expected.begin(), expected.end(), mwmName) != expected.end(),
           ("Can't find ", mwmName));
      foundMwms.insert(mwmName);
    }
    TEST_EQUAL(expected.size(), foundMwms.size(), ());
  }
}