From 649d4f4bedaf509549084860514824909df3c66d Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Tue, 15 Nov 2016 10:12:32 +0300 Subject: [PATCH] Fixing restrictions_collector_test and moving osm ids to feature ids generation to generator_tests_support. --- .../restriction_collector_test.cpp | 76 +++++++------------ .../generator_tests/restriction_test.cpp | 37 +-------- .../generator_tests_support.pro | 2 + .../restrcion_support.cpp | 36 +++++++++ .../restrcion_support.hpp | 16 ++++ generator/restriction_collector.hpp | 6 +- generator/restriction_generator.hpp | 13 +--- 7 files changed, 89 insertions(+), 97 deletions(-) create mode 100644 generator/generator_tests_support/restrcion_support.cpp create mode 100644 generator/generator_tests_support/restrcion_support.hpp diff --git a/generator/generator_tests/restriction_collector_test.cpp b/generator/generator_tests/restriction_collector_test.cpp index 838a9a65e1..852f7193b6 100644 --- a/generator/generator_tests/restriction_collector_test.cpp +++ b/generator/generator_tests/restriction_collector_test.cpp @@ -1,5 +1,7 @@ #include "testing/testing.hpp" +#include "generator/generator_tests_support/restrcion_support.hpp" + #include "generator/osm_id.hpp" #include "generator/restriction_collector.hpp" @@ -18,6 +20,7 @@ #include "std/utility.hpp" #include "std/vector.hpp" +using namespace generator; using namespace platform; using namespace platform::tests_support; @@ -27,13 +30,13 @@ string const kRestrictionTestDir = "test-restrictions"; UNIT_TEST(RestrictionTest_ValidCase) { - RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* featureIdToOsmIdsPath */); + RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* osmIdsToFeatureIdsPath */); // Adding feature ids. - restrictionCollector.AddFeatureId(30 /* featureId */, {3} /* osmIds */); - restrictionCollector.AddFeatureId(10 /* featureId */, {1} /* osmIds */); - restrictionCollector.AddFeatureId(50 /* featureId */, {5} /* osmIds */); - restrictionCollector.AddFeatureId(70 /* featureId */, {7} /* osmIds */); - restrictionCollector.AddFeatureId(20 /* featureId */, {2} /* osmIds */); + restrictionCollector.AddFeatureId(30 /* featureId */, 3 /* osmId */); + restrictionCollector.AddFeatureId(10 /* featureId */, 1 /* osmId */); + restrictionCollector.AddFeatureId(50 /* featureId */, 5 /* osmId */); + restrictionCollector.AddFeatureId(70 /* featureId */, 7 /* osmId */); + restrictionCollector.AddFeatureId(20 /* featureId */, 2 /* osmId */); // Adding restrictions. TEST(restrictionCollector.AddRestriction(Restriction::Type::No, {1, 2} /* osmIds */), ()); @@ -52,9 +55,9 @@ UNIT_TEST(RestrictionTest_ValidCase) UNIT_TEST(RestrictionTest_InvalidCase) { - RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* featureIdToOsmIdsPath */); - restrictionCollector.AddFeatureId(0 /* featureId */, {0} /* osmIds */); - restrictionCollector.AddFeatureId(20 /* featureId */, {2} /* osmIds */); + RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* osmIdsToFeatureIdsPath */); + restrictionCollector.AddFeatureId(0 /* featureId */, 0 /* osmId */); + restrictionCollector.AddFeatureId(20 /* featureId */, 2 /* osmId */); TEST(!restrictionCollector.AddRestriction(Restriction::Type::No, {0, 1} /* osmIds */), ()); @@ -75,7 +78,7 @@ UNIT_TEST(RestrictionTest_ParseRestrictions) ScopedDir const scopedDir(kRestrictionTestDir); ScopedFile const scopedFile(kRestrictionPath, kRestrictionContent); - RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* featureIdToOsmIdsPath */); + RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* osmIdsToFeatureIdsPath */); Platform const & platform = Platform(); @@ -85,35 +88,6 @@ UNIT_TEST(RestrictionTest_ParseRestrictions) TEST(!restrictionCollector.HasRestrictions(), ()); } -UNIT_TEST(RestrictionTest_ParseFeatureId2OsmIdsMapping) -{ - string const kFeatureIdToOsmIdsName = "feature_id_to_osm_ids.csv"; - string const kFeatureIdToOsmIdsPath = - my::JoinFoldersToPath(kRestrictionTestDir, kFeatureIdToOsmIdsName); - string const kFeatureIdToOsmIdsContent = R"(1, 10, - 2, 20 - 779703, 5423239545, - 3, 30)"; - - ScopedDir const scopedDir(kRestrictionTestDir); - ScopedFile const scopedFile(kFeatureIdToOsmIdsPath, kFeatureIdToOsmIdsContent); - - RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* featureIdToOsmIdsPath */); - - Platform const & platform = Platform(); - restrictionCollector.ParseOsmIdToFeatureIdMapping( - my::JoinFoldersToPath(platform.WritableDir(), kFeatureIdToOsmIdsPath)); - - vector> const expectedOsmIds2FeatureId = { - {10, 1}, {20, 2}, {30, 3}, {5423239545, 779703}}; - vector> osmIds2FeatureId( - restrictionCollector.m_osmIdToFeatureId.cbegin(), - restrictionCollector.m_osmIdToFeatureId.cend()); - sort(osmIds2FeatureId.begin(), osmIds2FeatureId.end(), - my::LessBy(&pair::first)); - TEST_EQUAL(osmIds2FeatureId, expectedOsmIds2FeatureId, ()); -} - UNIT_TEST(RestrictionTest_RestrictionCollectorWholeClassTest) { string const kRestrictionName = "restrictions_in_osm_ids.csv"; @@ -122,22 +96,24 @@ UNIT_TEST(RestrictionTest_RestrictionCollectorWholeClassTest) Only, 10, 20, Only, 30, 40,)"; - string const kFeatureIdToOsmIdsName = "feature_id_to_osm_ids.csv"; - string const kFeatureIdToOsmIdsPath = - my::JoinFoldersToPath(kRestrictionTestDir, kFeatureIdToOsmIdsName); - string const kFeatureIdToOsmIdsContent = R"(1, 10, - 2, 20, - 3, 30, - 4, 40)"; + 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); + GenerateOsmIdsToFeatureIdsMapping(kOsmIdsToFeatureIdsContent, osmIdsToFeatureIdsFullPath); ScopedDir scopedDir(kRestrictionTestDir); ScopedFile restrictionScopedFile(kRestrictionPath, kRestrictionContent); - ScopedFile mappingScopedFile(kFeatureIdToOsmIdsPath, kFeatureIdToOsmIdsContent); + ScopedFile mappingScopedFile(osmIdsToFeatureIdsPath); - Platform const & platform = Platform(); RestrictionCollector restrictionCollector( - my::JoinFoldersToPath(platform.WritableDir(), kRestrictionPath), - my::JoinFoldersToPath(platform.WritableDir(), kFeatureIdToOsmIdsPath)); + my::JoinFoldersToPath(platform.WritableDir(), kRestrictionPath), osmIdsToFeatureIdsFullPath); TEST(restrictionCollector.IsValid(), ()); RestrictionVec const & restrictions = restrictionCollector.GetRestrictions(); diff --git a/generator/generator_tests/restriction_test.cpp b/generator/generator_tests/restriction_test.cpp index e16bbe1172..efd6e5ff96 100644 --- a/generator/generator_tests/restriction_test.cpp +++ b/generator/generator_tests/restriction_test.cpp @@ -1,9 +1,9 @@ #include "testing/testing.hpp" +#include "generator/generator_tests_support/restrcion_support.hpp" #include "generator/generator_tests_support/test_feature.hpp" #include "generator/generator_tests_support/test_mwm_builder.hpp" -#include "generator/gen_mwm_info.hpp" #include "generator/restriction_collector.hpp" #include "generator/restriction_generator.hpp" @@ -13,7 +13,6 @@ #include "indexer/mwm_set.hpp" #include "coding/file_name_utils.hpp" -#include "coding/file_writer.hpp" #include "platform/platform_tests_support/scoped_dir.hpp" #include "platform/platform_tests_support/scoped_file.hpp" @@ -23,9 +22,7 @@ #include "base/logging.hpp" #include "base/scope_guard.hpp" -#include "base/string_utils.hpp" -#include "std/utility.hpp" #include "std/string.hpp" using namespace feature; @@ -48,40 +45,10 @@ void BuildEmptyMwm(LocalCountryFile & country) generator::tests_support::TestMwmBuilder builder(country, feature::DataHeader::country); } -/// \brief Generates a binary file with by a string with mapping from osm ids to feature ids. -/// \param mappingContent a string with lines with mapping from osm id to feature id (one to one). -/// For example -/// 10, 1, -/// 20, 2 -/// 30, 3, -/// 40, 4 -/// \parma outputFilePath full path to an output file where the mapping is saved. -void GenerateOsmIdsToFeatureIdsMapping(string const & mappingContent, string const & outputFilePath) -{ - strings::SimpleTokenizer lineIter(mappingContent, "\n\r" /* string delimiter */); - - gen::Accumulator> osmIdsToFeatureIds; - for (; lineIter; ++lineIter) - { - strings::SimpleTokenizer idIter(*lineIter, ", \t" /* id delimiter */); - uint64_t osmId = 0; - TEST(strings::to_uint64(*idIter, osmId), ("Cannot covert to uint64_t:", *idIter)); - TEST(idIter, ("Wrong feature ids to osm ids mapping.")); - ++idIter; - - uint32_t featureId = 0; - TEST(strings::to_uint(*idIter, featureId), ("Cannot covert to uint:", *idIter)); - osmIdsToFeatureIds.Add(make_pair(osmId, featureId)); - } - - FileWriter osm2ftWriter(outputFilePath); - osmIdsToFeatureIds.Flush(osm2ftWriter); -} - /// \brief Generates a restriction section, adds it to an empty mwm, /// loads the restriction section and test loaded restrictions. /// \param restrictionContent comma separated text with restrictions in osm id terms. -/// \param mappingContent comma separated text with with mapping from osm ids to feature ids. +/// \param mappingContent comma separated text with mapping from osm ids to feature ids. void TestRestrictionBuilding(string const & restrictionContent, string const & mappingContent) { Platform & platform = GetPlatform(); diff --git a/generator/generator_tests_support/generator_tests_support.pro b/generator/generator_tests_support/generator_tests_support.pro index 380dc89064..af3ff085e2 100644 --- a/generator/generator_tests_support/generator_tests_support.pro +++ b/generator/generator_tests_support/generator_tests_support.pro @@ -7,9 +7,11 @@ ROOT_DIR = ../.. include($$ROOT_DIR/common.pri) SOURCES += \ + restrcion_support.cpp \ test_feature.cpp \ test_mwm_builder.cpp \ HEADERS += \ + restrcion_support.hpp \ test_feature.hpp \ test_mwm_builder.hpp \ diff --git a/generator/generator_tests_support/restrcion_support.cpp b/generator/generator_tests_support/restrcion_support.cpp new file mode 100644 index 0000000000..678cc37fd4 --- /dev/null +++ b/generator/generator_tests_support/restrcion_support.cpp @@ -0,0 +1,36 @@ +#include "generator/generator_tests_support/restrcion_support.hpp" + +#include "testing/testing.hpp" + +#include "generator/gen_mwm_info.hpp" + +#include "coding/file_writer.hpp" + +#include "base/string_utils.hpp" + +#include "std/utility.hpp" + +namespace generator +{ +void GenerateOsmIdsToFeatureIdsMapping(string const & mappingContent, string const & outputFilePath) +{ + strings::SimpleTokenizer lineIter(mappingContent, "\n\r" /* string delimiter */); + + gen::Accumulator> osmIdsToFeatureIds; + for (; lineIter; ++lineIter) + { + strings::SimpleTokenizer idIter(*lineIter, ", \t" /* id delimiter */); + uint64_t osmId = 0; + TEST(strings::to_uint64(*idIter, osmId), ("Cannot covert to uint64_t:", *idIter)); + TEST(idIter, ("Wrong feature ids to osm ids mapping.")); + ++idIter; + + uint32_t featureId = 0; + TEST(strings::to_uint(*idIter, featureId), ("Cannot covert to uint:", *idIter)); + osmIdsToFeatureIds.Add(make_pair(osmId, featureId)); + } + + FileWriter osm2ftWriter(outputFilePath); + osmIdsToFeatureIds.Flush(osm2ftWriter); +} +} // namespace generator diff --git a/generator/generator_tests_support/restrcion_support.hpp b/generator/generator_tests_support/restrcion_support.hpp new file mode 100644 index 0000000000..2268158c5a --- /dev/null +++ b/generator/generator_tests_support/restrcion_support.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "std/string.hpp" + +namespace generator +{ +/// \brief Generates a binary file with by a string with mapping from osm ids to feature ids. +/// \param mappingContent a string with lines with mapping from osm id to feature id (one to one). +/// For example +/// 10, 1, +/// 20, 2 +/// 30, 3, +/// 40, 4 +/// \parma outputFilePath full path to an output file where the mapping is saved. +void GenerateOsmIdsToFeatureIdsMapping(string const & mappingContent, string const & outputFilePath); +} // namespace generator diff --git a/generator/restriction_collector.hpp b/generator/restriction_collector.hpp index 155e84973c..432acdc159 100644 --- a/generator/restriction_collector.hpp +++ b/generator/restriction_collector.hpp @@ -17,8 +17,8 @@ class RestrictionCollector { public: /// \param restrictionPath full path to file with road restrictions in osm id terms. - /// \param featureIdToOsmIdsPath full path to file with mapping from feature id to osm id. - RestrictionCollector(string const & restrictionPath, string const & featureIdToOsmIdsPath); + /// \param osmIdsToFeatureIdsPath full path to file with mapping from osm ids to feature ids. + RestrictionCollector(string const & restrictionPath, string const & osmIdsToFeatureIdsPath); bool HasRestrictions() const { return !m_restrictions.empty(); } @@ -53,7 +53,7 @@ private: /// Only, 335049632, 49356687, /// No, 157616940, 157616940, /// No, 157616940, 157617107, - /// \param featureIdToOsmIdsPath path to the text file. + /// \param path path to the text file with restrictions. bool ParseRestrictions(string const & path); /// \brief Adds feature id and corresponding |osmId| to |m_osmIdToFeatureId|. diff --git a/generator/restriction_generator.hpp b/generator/restriction_generator.hpp index 17636f6fc8..d960f8149c 100644 --- a/generator/restriction_generator.hpp +++ b/generator/restriction_generator.hpp @@ -12,14 +12,9 @@ namespace routing /// For example: /// Only, 335049632, 49356687, /// No, 157616940, 157616940, -/// \param featureIdToOsmIdsPath comma separated (csv like) file with mapping from feature id to osm -/// ids -/// in following format: -/// , , , and so -/// on -/// For example: -/// 137999, 5170186, -/// 138000, 5170209, +/// \param osmIdsToFeatureIdsPath a binary file with mapping form osm ids to feature ids. +/// One osm id is mapped to one feature is. The file should be saved with the help of +/// OsmID2FeatureID class or using a similar way. bool BuildRoadRestrictions(string const & mwmPath, string const & restrictionPath, - string const & featureIdToOsmIdsPath); + string const & osmIdsToFeatureIdsPath); } // namespace routing