diff --git a/generator/generator_tests/restriction_test.cpp b/generator/generator_tests/restriction_test.cpp index 8973866e4d..e16bbe1172 100644 --- a/generator/generator_tests/restriction_test.cpp +++ b/generator/generator_tests/restriction_test.cpp @@ -3,6 +3,7 @@ #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" @@ -12,6 +13,7 @@ #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" @@ -21,7 +23,9 @@ #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; @@ -37,17 +41,47 @@ string const kTestDir = "restriction_generation_test"; // Temporary mwm name for testing. string const kTestMwm = "test"; string const kRestrictionFileName = "restrictions_in_osm_ids.csv"; -string const featureId2OsmIdsName = "feature_id_to_osm_ids.csv"; +string const kOsmIdsToFeatureIdsName = "osm_ids_to_feature_ids" OSM2FEATURE_FILE_EXTENSION; 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 feature ids to osm ids. +/// \param mappingContent comma separated text with with mapping from osm ids to feature ids. void TestRestrictionBuilding(string const & restrictionContent, string const & mappingContent) { Platform & platform = GetPlatform(); @@ -60,16 +94,18 @@ void TestRestrictionBuilding(string const & restrictionContent, string const & m ScopedFile const scopedMwm(mwmRelativePath); BuildEmptyMwm(country); - // Creating files with restrictions. + // Creating a file with restrictions. string const restrictionRelativePath = my::JoinFoldersToPath(kTestDir, kRestrictionFileName); ScopedFile const restrictionScopedFile(restrictionRelativePath, restrictionContent); - string const mappingRelativePath = my::JoinFoldersToPath(kTestDir, featureId2OsmIdsName); - ScopedFile const mappingScopedFile(mappingRelativePath, mappingContent); + // Creating osm ids to feature ids mapping. + string const mappingRelativePath = my::JoinFoldersToPath(kTestDir, kOsmIdsToFeatureIdsName); + ScopedFile const mappingScopedFile(mappingRelativePath); + string const mappingFullPath = my::JoinFoldersToPath(writableDir, mappingRelativePath); + GenerateOsmIdsToFeatureIdsMapping(mappingContent, mappingFullPath); // Adding restriction section to mwm. string const restrictionFullPath = my::JoinFoldersToPath(writableDir, restrictionRelativePath); - string const mappingFullPath = my::JoinFoldersToPath(writableDir, mappingRelativePath); string const mwmFullPath = my::JoinFoldersToPath(writableDir, mwmRelativePath); BuildRoadRestrictions(mwmFullPath, restrictionFullPath, mappingFullPath); @@ -89,22 +125,22 @@ void TestRestrictionBuilding(string const & restrictionContent, string const & m UNIT_TEST(RestrictionGenerationTest_NoRestriction) { string const restrictionContent = ""; - string const featureIdToOsmIdsContent = ""; - TestRestrictionBuilding(restrictionContent, featureIdToOsmIdsContent); + string const osmIdsToFeatureIdsContent = ""; + TestRestrictionBuilding(restrictionContent, osmIdsToFeatureIdsContent); } UNIT_TEST(RestrictionGenerationTest_ZeroId) { string const restrictionContent = R"(Only, 0, 0,)"; - string const featureIdToOsmIdsContent = R"(0, 0,)"; - TestRestrictionBuilding(restrictionContent, featureIdToOsmIdsContent); + string const osmIdsToFeatureIdsContent = R"(0, 0,)"; + TestRestrictionBuilding(restrictionContent, osmIdsToFeatureIdsContent); } UNIT_TEST(RestrictionGenerationTest_OneRestriction) { string const restrictionContent = R"(No, 10, 10,)"; - string const featureIdToOsmIdsContent = R"(1, 10,)"; - TestRestrictionBuilding(restrictionContent, featureIdToOsmIdsContent); + string const osmIdsToFeatureIdsContent = R"(10, 1,)"; + TestRestrictionBuilding(restrictionContent, osmIdsToFeatureIdsContent); } UNIT_TEST(RestrictionGenerationTest_ThreeRestrictions) @@ -112,11 +148,11 @@ UNIT_TEST(RestrictionGenerationTest_ThreeRestrictions) string const restrictionContent = R"(No, 10, 10, Only, 10, 20 Only, 30, 40)"; - string const featureIdToOsmIdsContent = R"(1, 10, - 2, 20 - 3, 30, - 4, 40)"; - TestRestrictionBuilding(restrictionContent, featureIdToOsmIdsContent); + string const osmIdsToFeatureIdsContent = R"(10, 1, + 20, 2 + 30, 3, + 40, 4)"; + TestRestrictionBuilding(restrictionContent, osmIdsToFeatureIdsContent); } UNIT_TEST(RestrictionGenerationTest_SevenRestrictions) @@ -128,11 +164,11 @@ UNIT_TEST(RestrictionGenerationTest_SevenRestrictions) No, 30, 30, No, 40, 40, Only, 30, 40,)"; - string const featureIdToOsmIdsContent = R"(1, 10, - 2, 20, - 3, 30, - 4, 40)"; - TestRestrictionBuilding(restrictionContent, featureIdToOsmIdsContent); + string const osmIdsToFeatureIdsContent = R"(10, 1, + 20, 2, + 30, 3, + 40, 4)"; + TestRestrictionBuilding(restrictionContent, osmIdsToFeatureIdsContent); } UNIT_TEST(RestrictionGenerationTest_ThereAndMoreLinkRestrictions) @@ -141,10 +177,10 @@ UNIT_TEST(RestrictionGenerationTest_ThereAndMoreLinkRestrictions) No, 20, 20, Only, 10, 20, 30, 40 Only, 20, 30, 40)"; - string const featureIdToOsmIdsContent = R"(1, 10, - 2, 20, - 3, 30, - 4, 40)"; - TestRestrictionBuilding(restrictionContent, featureIdToOsmIdsContent); + string const osmIdsToFeatureIdsContent = R"(10, 1, + 20, 2, + 30, 3, + 40, 4)"; + TestRestrictionBuilding(restrictionContent, osmIdsToFeatureIdsContent); } } // namespace diff --git a/generator/restriction_collector.cpp b/generator/restriction_collector.cpp index 3c977fe6e2..58c85e27a6 100644 --- a/generator/restriction_collector.cpp +++ b/generator/restriction_collector.cpp @@ -84,7 +84,7 @@ bool RestrictionCollector::ParseOsmIdToFeatureIdMapping(string const & osmIdsToF } catch (FileReader::Exception const & e) { - LOG(LERROR, ("Exception while reading file:", osmIdsToFeatureIdPath, ". Msg:", e.Msg())); + LOG(LWARNING, ("Exception while reading file:", osmIdsToFeatureIdPath, ". Msg:", e.Msg())); return false; }