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

osrm_router_test.cpp « routing_tests « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d706cf78a337c06b21fd9b0b2db3d7c39b8fee6f (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
#include "testing/testing.hpp"

#include "routing/car_router.hpp"

#include "indexer/features_offsets_table.hpp"
#include "geometry/mercator.hpp"

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

#include "platform/platform_tests_support/scoped_mwm.hpp"

#include "coding/file_writer.hpp"

#include "defines.hpp"

#include "base/scope_guard.hpp"

#include "std/bind.hpp"
#include "std/unique_ptr.hpp"
#include "std/vector.hpp"

using namespace routing;
using platform::CountryIndexes;

namespace
{

typedef vector<OsrmFtSegMappingBuilder::FtSegVectorT> InputDataT;
typedef vector< vector<TOsrmNodeId> > NodeIdDataT;
typedef vector< pair<size_t, size_t> > RangeDataT;
typedef OsrmMappingTypes::FtSeg SegT;

void TestNodeId(OsrmFtSegMapping const & mapping, NodeIdDataT const & test)
{
  for (TOsrmNodeId nodeId = 0; nodeId < test.size(); ++nodeId)
  {
    for (auto idx : test[nodeId])
      TEST_EQUAL(nodeId, mapping.GetNodeId(idx), ());
  }
}

void TestSegmentRange(OsrmFtSegMapping const & mapping, RangeDataT const & test)
{
  for (TOsrmNodeId nodeId = 0; nodeId < test.size(); ++nodeId)
  {
    // Input test range is { start, count } but we should pass [start, end).
    auto const & r = test[nodeId];
    TEST_EQUAL(mapping.GetSegmentsRange(nodeId), make_pair(r.first, r.first + r.second), ());
  }
}

void TestMapping(InputDataT const & data,
                 NodeIdDataT const & nodeIds,
                 RangeDataT const & ranges)
{
  platform::CountryFile country("TestCountry");
  platform::LocalCountryFile localFile(GetPlatform().WritableDir(), country, 0 /* version */);
  localFile.SyncWithDisk();
  platform::tests_support::ScopedMwm mapMwm(
      platform::GetFileName(localFile.GetCountryFile().GetName(), MapOptions::Map,
                            version::FOR_TESTING_TWO_COMPONENT_MWM1));
  static string const ftSegsPath = GetPlatform().WritablePathForFile("test1.tmp");

  platform::CountryIndexes::PreparePlaceOnDisk(localFile);
  string const & featuresOffsetsTablePath =
    CountryIndexes::GetPath(localFile, CountryIndexes::Index::Offsets);
  MY_SCOPE_GUARD(ftSegsFileDeleter, bind(FileWriter::DeleteFileX, ftSegsPath));
  MY_SCOPE_GUARD(featuresOffsetsTableFileDeleter,
                 bind(FileWriter::DeleteFileX, featuresOffsetsTablePath));
  MY_SCOPE_GUARD(indexesDeleter, bind(&CountryIndexes::DeleteFromDisk, localFile));

  {
    // Prepare fake features offsets table for input data, because
    // OsrmFtSegMapping::Load() loads routing index and creates
    // additional helper indexes and some of them require
    // FeatureOffsetsTable existence.
    //
    // As instantiation of FeatureOffsetsTable requires complete MWM
    // file with features or at least already searialized
    // FeatureOffsetsTable, the purpose of this code is to prepare a
    // file with serialized FeatureOffsetsTable and feed it to
    // OsrmFtSegMapping.
    feature::FeaturesOffsetsTable::Builder tableBuilder;
    for (auto const & segVector : data)
    {
      for (auto const & seg : segVector)
        tableBuilder.PushOffset(seg.m_fid);
    }
    unique_ptr<feature::FeaturesOffsetsTable> table =
        feature::FeaturesOffsetsTable::Build(tableBuilder);
    table->Save(featuresOffsetsTablePath);
  }

  OsrmFtSegMappingBuilder builder;
  for (TOsrmNodeId nodeId = 0; nodeId < data.size(); ++nodeId)
    builder.Append(nodeId, data[nodeId]);

  TestNodeId(builder, nodeIds);
  TestSegmentRange(builder, ranges);

  {
    FilesContainerW w(ftSegsPath);
    builder.Save(w);
  }

  {
    FilesMappingContainer cont(ftSegsPath);
    OsrmFtSegMapping mapping;
    mapping.Load(cont, localFile);
    mapping.Map(cont);

    TestNodeId(mapping, nodeIds);
    TestSegmentRange(mapping, ranges);

    for (size_t i = 0; i < mapping.GetSegmentsCount(); ++i)
    {
      TOsrmNodeId const node = mapping.GetNodeId(i);
      size_t count = 0;
      mapping.ForEachFtSeg(node, [&] (OsrmMappingTypes::FtSeg const & s)
      {
        TEST_EQUAL(s, data[node][count++], ());
      });
      TEST_EQUAL(count, data[node].size(), ());
    }
  }
}

bool TestFtSeg(SegT const & s)
{
  return (SegT(s.Store()) == s);
}

}

UNIT_TEST(FtSeg_Smoke)
{
  SegT arr[] = {
    { 5, 1, 2 },
    { 666, 0, 17 },
  };

  for (size_t i = 0; i < ARRAY_SIZE(arr); ++i)
    TEST(TestFtSeg(arr[i]), (arr[i].Store()));
}

UNIT_TEST(OsrmFtSegMappingBuilder_Smoke)
{
  {
    InputDataT data =
    {
      { {0, 0, 1} },
      { {1, 0, 1} },
      { {2, 0, 1}, {3, 0, 1} },
      { {4, 0, 1} },
      { {5, 0, 1}, {6, 0, 1}, {7, 0, 1} },
      { {8, 0, 1}, {9, 0, 1}, {10, 0, 1}, {11, 0, 1} },
      { {12, 0, 1} }
    };

    NodeIdDataT nodeIds =
    {
      { 0 },
      { 1 },
      { 2, 3 },
      { 4 },
      { 5, 6, 7 },
      { 8, 9, 10, 11 },
      { 12 }
    };

    RangeDataT ranges =
    {
      { 0, 1 },
      { 1, 1 },
      { 2, 2 },
      { 4, 1 },
      { 5, 3 },
      { 8, 4 },
      { 12, 1 },
    };

    TestMapping(data, nodeIds, ranges);
  }


  {
    InputDataT data =
    {
      { {0, 0, 1} },
      { {1, 0, 1} },
      { {2, 0, 1} },
      { {3, 0, 1} },
      { {4, 0, 1} },
      { {5, 0, 1}, {6, 0, 1} },
      { {7, 0, 1} },
      { {8, 0, 1}, {9, 0, 1}, {10, 0, 1} },
      { {11, 0, 1}, {12, 0, 1}, {13, 0, 1} }
    };

    NodeIdDataT nodeIds =
    {
      { 0 },
      { 1 },
      { 2 },
      { 3 },
      { 4 },
      { 5, 6 },
      { 7 },
      { 8, 9, 10 },
      { 11, 12, 13 }
    };

    RangeDataT ranges =
    {
      { 0, 1 },
      { 1, 1 },
      { 2, 1 },
      { 3, 1 },
      { 4, 1 },
      { 5, 2 },
      { 7, 1 },
      { 8, 3 },
      { 11, 3 },
    };

    TestMapping(data, nodeIds, ranges);
  }


  {
    InputDataT data =
    {
      { {0, 0, 1}, {1, 2, 3} },
      { },
      { {3, 6, 7} },
      { {4, 8, 9}, {5, 10, 11} },
    };

    NodeIdDataT nodeIds =
    {
      { 0, 1 },
      { },
      { 3 },
      { 4, 5 },
    };

    RangeDataT ranges =
    {
      { 0, 2 },
      { 2, 1 },
      { 3, 1 },
      { 4, 2 },
    };

    TestMapping(data, nodeIds, ranges);
  }
}