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

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

#include "generator/generator_tests_support/routing_helpers.hpp"

#include "generator/osm_id.hpp"
#include "generator/restriction_collector.hpp"

#include "routing/restrictions_serialization.hpp"

#include "platform/platform_tests_support/scoped_dir.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"

#include "platform/platform.hpp"

#include "coding/file_name_utils.hpp"

#include "base/stl_helpers.hpp"

#include "std/string.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"

using namespace generator;
using namespace platform;
using namespace platform::tests_support;

namespace routing
{
string const kRestrictionTestDir = "test-restrictions";

UNIT_TEST(RestrictionTest_ValidCase)
{
  RestrictionCollector restrictionCollector;
  // Adding feature ids.
  restrictionCollector.AddFeatureId(30 /* featureId */, osm::Id::Way(3));
  restrictionCollector.AddFeatureId(10 /* featureId */, osm::Id::Way(1));
  restrictionCollector.AddFeatureId(50 /* featureId */, osm::Id::Way(5));
  restrictionCollector.AddFeatureId(70 /* featureId */, osm::Id::Way(7));
  restrictionCollector.AddFeatureId(20 /* featureId */, osm::Id::Way(2));

  // Adding restrictions.
  TEST(restrictionCollector.AddRestriction(Restriction::Type::No, {osm::Id::Way(1), osm::Id::Way(2)}), ());
  TEST(restrictionCollector.AddRestriction(Restriction::Type::No, {osm::Id::Way(2), osm::Id::Way(3)}), ());
  TEST(restrictionCollector.AddRestriction(Restriction::Type::Only, {osm::Id::Way(5), osm::Id::Way(7)}), ());
  my::SortUnique(restrictionCollector.m_restrictions);

  // Checking the result.
  TEST(restrictionCollector.IsValid(), ());

  RestrictionVec const expectedRestrictions = {{Restriction::Type::No, {10, 20}},
                                               {Restriction::Type::No, {20, 30}},
                                               {Restriction::Type::Only, {50, 70}}};
  TEST_EQUAL(restrictionCollector.m_restrictions, expectedRestrictions, ());
}

UNIT_TEST(RestrictionTest_InvalidCase)
{
  RestrictionCollector restrictionCollector;
  restrictionCollector.AddFeatureId(0 /* featureId */, osm::Id::Way(0));
  restrictionCollector.AddFeatureId(20 /* featureId */, osm::Id::Way(2));

  TEST(!restrictionCollector.AddRestriction(Restriction::Type::No, {osm::Id::Way(0), osm::Id::Way(1)}), ());

  TEST(!restrictionCollector.HasRestrictions(), ());
  TEST(restrictionCollector.IsValid(), ());
}

UNIT_TEST(RestrictionTest_ParseRestrictions)
{
  string const kRestrictionName = "restrictions_in_osm_ids.csv";
  string const kRestrictionPath = my::JoinFoldersToPath(kRestrictionTestDir, kRestrictionName);
  string const kRestrictionContent = R"(No, 1, 1,
                                        Only, 0, 2,
                                        Only, 2, 3,
                                        No, 38028428, 38028428
                                        No, 4, 5,)";

  ScopedDir const scopedDir(kRestrictionTestDir);
  ScopedFile const scopedFile(kRestrictionPath, kRestrictionContent);

  RestrictionCollector restrictionCollector;

  Platform const & platform = Platform();

  TEST(restrictionCollector.ParseRestrictions(
           my::JoinFoldersToPath(platform.WritableDir(), kRestrictionPath)),
       ());
  TEST(!restrictionCollector.HasRestrictions(), ());
}

UNIT_TEST(RestrictionTest_RestrictionCollectorWholeClassTest)
{
  ScopedDir scopedDir(kRestrictionTestDir);

  string const kRestrictionName = "restrictions_in_osm_ids.csv";
  string const kRestrictionPath = my::JoinFoldersToPath(kRestrictionTestDir, kRestrictionName);
  string const kRestrictionContent = R"(No, 10, 10,
                                        Only, 10, 20,
                                        Only, 30, 40,)";
  ScopedFile restrictionScopedFile(kRestrictionPath, kRestrictionContent);

  string const kOsmIdsToFeatureIdsName = "osm_ids_to_feature_ids" OSM2FEATURE_FILE_EXTENSION;
  string const osmIdsToFeatureIdsPath =
      my::JoinFoldersToPath(kRestrictionTestDir, kOsmIdsToFeatureIdsName);
  string const kOsmIdsToFeatureIdsContent = R"(10, 1,
                                               20, 2,
                                               30, 3,
                                               40, 4)";
  Platform const & platform = Platform();
  string const osmIdsToFeatureIdsFullPath =
      my::JoinFoldersToPath(platform.WritableDir(), osmIdsToFeatureIdsPath);
  ReEncodeOsmIdsToFeatureIdsMapping(kOsmIdsToFeatureIdsContent, osmIdsToFeatureIdsFullPath);
  ScopedFile mappingScopedFile(osmIdsToFeatureIdsPath);

  RestrictionCollector restrictionCollector(
      my::JoinFoldersToPath(platform.WritableDir(), kRestrictionPath), osmIdsToFeatureIdsFullPath);
  TEST(restrictionCollector.IsValid(), ());

  RestrictionVec const & restrictions = restrictionCollector.GetRestrictions();
  TEST(is_sorted(restrictions.cbegin(), restrictions.cend()), ());

  RestrictionVec const expectedRestrictions = {{Restriction::Type::No, {1, 1}},
                                               {Restriction::Type::Only, {1, 2}},
                                               {Restriction::Type::Only, {3, 4}}};
  TEST_EQUAL(restrictions, expectedRestrictions, ());
}
}  // namespace routing