diff --git a/generator/generator_tests/CMakeLists.txt b/generator/generator_tests/CMakeLists.txt index 09293a8..5563eed 100644 --- a/generator/generator_tests/CMakeLists.txt +++ b/generator/generator_tests/CMakeLists.txt @@ -25,6 +25,7 @@ set( source_to_element_test.cpp street_geometry_tests.cpp street_regions_tracing_tests.cpp + streets_builder_tests.cpp tag_admixer_test.cpp translation_test.cpp types_helper.hpp diff --git a/generator/generator_tests/feature_builder_test.cpp b/generator/generator_tests/feature_builder_test.cpp index b91c696..5962c7d 100644 --- a/generator/generator_tests/feature_builder_test.cpp +++ b/generator/generator_tests/feature_builder_test.cpp @@ -272,6 +272,55 @@ UNIT_CLASS_TEST(TestWithClassificator, FeatureBuilder_SerializeLocalityObjectFor }); } +UNIT_CLASS_TEST(TestWithClassificator, FeatureBuilder_SerializeLocalityObjectForLine) +{ + FeatureBuilder fb; + FeatureParams params; + + char const * arr1[][2] = { + { "highway", "residential"}, + }; + AddTypes(params, arr1); + params.FinishAddingTypes(); + params.AddName("default", "Arbat Street"); + + fb.AddOsmId(base::MakeOsmNode(1)); + fb.SetParams(params); + fb.SetLinear(); + fb.AddPoint(m2::PointD(10.1, 15.8)); + fb.AddPoint(m2::PointD(10.2, 15.9)); + fb.AddPoint(m2::PointD(10.4, 15.9)); + + TEST(fb.RemoveInvalidTypes(), ()); + Check(fb); + + feature::DataHeader header; + header.SetGeometryCodingParams(serial::GeometryCodingParams()); + header.SetScales({scales::GetUpperScale()}); + feature::GeometryHolder holder(fb, header, + std::numeric_limits::max() /* maxTrianglesNumber */); + holder.AddPoints(holder.GetSourcePoints(), 0 /* scaleIndex */); + + auto & buffer = holder.GetBuffer(); + auto preserialize = fb.PreSerializeAndRemoveUselessNamesForMwm(buffer); + CHECK(preserialize, ()); + fb.SerializeLocalityObject(serial::GeometryCodingParams(), buffer); + + using indexer::LocalityObject; + LocalityObject object; + object.Deserialize(buffer.m_buffer.data()); + + TEST_EQUAL(LocalityObject::FromStoredId(object.GetStoredId()), base::MakeOsmNode(1), ()); + auto localityObjectsPoints = std::vector{}; + object.ForEachPoint([&] (auto && point) { + localityObjectsPoints.push_back(point); + }); + TEST_EQUAL(localityObjectsPoints.size(), 3, ()); + TEST(base::AlmostEqualAbs(localityObjectsPoints[0], {10.1, 15.8}, 1e-6), ()); + TEST(base::AlmostEqualAbs(localityObjectsPoints[1], {10.2, 15.9}, 1e-6), ()); + TEST(base::AlmostEqualAbs(localityObjectsPoints[2], {10.4, 15.9}, 1e-6), ()); +} + UNIT_TEST(FeatureBuilder_SerializeAccuratelyForIntermediate) { FeatureBuilder fb1; diff --git a/generator/generator_tests/streets_builder_tests.cpp b/generator/generator_tests/streets_builder_tests.cpp new file mode 100644 index 0000000..1a936d5 --- /dev/null +++ b/generator/generator_tests/streets_builder_tests.cpp @@ -0,0 +1,112 @@ +#include "testing/testing.hpp" + +#include "generator/feature_builder.hpp" +#include "generator/generator_tests/common.hpp" +#include "generator/key_value_storage.hpp" +#include "generator/streets/streets_builder.hpp" + +#include "geometry/point2d.hpp" + +#include "3party/jansson/myjansson.hpp" + +#include +#include +#include +#include + +using namespace generator_tests; +using namespace generator; +using namespace generator::streets; +using namespace base; + +std::shared_ptr RussiaGetter(uint64_t id) +{ + auto const russiaId = base::MakeOsmNode(424314830).GetEncodedId(); + std::shared_ptr russiaValue = std::make_shared(LoadFromString( + R"#({ + "type": "Feature", + "properties": { + "locales": { + "default": { + "address": { + "country": "Russia" + }, + "name": "Russia" + } + }, + "rank": 1, + "code": "RU" + } + })#")); + return id == russiaId ? russiaValue : std::shared_ptr{}; +} + +StreetsBuilder::RegionFinder RussiaFinder() +{ + auto finder = [](auto && /* point */, auto && selector) -> boost::optional { + static auto const russiaId = base::MakeOsmNode(424314830).GetEncodedId(); + static auto const russiaValue = RussiaGetter(russiaId); + CHECK(russiaValue, ()); + if (selector({russiaId, russiaValue})) + return std::make_pair(russiaId, russiaValue); + return {}; + }; + return finder; +} + +UNIT_TEST(StreetsBuilderTest_AggreatedStreetsInKv) +{ + auto const osmElements = std::vector{ + {1, {{"name", "Arbat Street"}, {"highway", "residential"}}, {{1.001, 2.001}, {1.002, 2.001}}, + {}}, + {2, {{"name", "Arbat Street"}, {"highway", "residential"}}, {{1.002, 2.001}, {1.002, 2.002}}, + {}}, + {3, {{"name", "New Arbat Street"}, {"highway", "residential"}}, + {{1.001, 2.002}, {1.002, 2.002}}, {}}}; + ScopedFile const streetsFeatures{"streets.mwm", ScopedFile::Mode::DoNotCreate}; + WriteFeatures(osmElements, streetsFeatures); + + StreetsBuilder streetsBuilder{RussiaFinder()}; + streetsBuilder.AssembleStreets(streetsFeatures.GetFullPath()); + ScopedFile const streetsJsonlFile{"streets.jsonl", ScopedFile::Mode::DoNotCreate}; + std::ofstream streetsJsonlStream(streetsJsonlFile.GetFullPath()); + streetsBuilder.SaveStreetsKv(RussiaGetter, streetsJsonlStream); + streetsJsonlStream.flush(); + + KeyValueStorage streetsStorage{streetsJsonlFile.GetFullPath(), 1000 /* cacheValuesCountLimit */}; + TEST_EQUAL(streetsStorage.Size(), 2, ()); + TEST(bool(streetsStorage.Find(MakeOsmWay(1).GetEncodedId())) != + bool(streetsStorage.Find(MakeOsmWay(2).GetEncodedId())), + ()); + TEST(streetsStorage.Find(MakeOsmWay(3).GetEncodedId()), ()); +} + +UNIT_TEST(StreetsBuilderTest_AggreatedStreetsInFeatures) +{ + auto const osmElements = std::vector{ + {1, {{"name", "Arbat Street"}, {"highway", "residential"}}, {{1.001, 2.001}, {1.002, 2.001}}, + {}}, + {2, {{"name", "Arbat Street"}, {"highway", "residential"}}, {{1.002, 2.001}, {1.002, 2.002}}, + {}}, + {3, {{"name", "New Arbat Street"}, {"highway", "residential"}}, + {{1.001, 2.002}, {1.002, 2.002}}, {}}}; + ScopedFile const streetsFeatures{"streets.mwm", ScopedFile::Mode::DoNotCreate}; + WriteFeatures(osmElements, streetsFeatures); + + StreetsBuilder streetsBuilder{RussiaFinder()}; + streetsBuilder.AssembleStreets(streetsFeatures.GetFullPath()); + streetsBuilder.RegenerateAggreatedStreetsFeatures(streetsFeatures.GetFullPath()); + + std::vector features; + std::unordered_set featureIds; + feature::ForEachFromDatRawFormat(streetsFeatures.GetFullPath(), [&](auto && fb, ...) { + features.emplace_back(fb); + featureIds.insert(fb.GetMostGenericOsmId()); + }); + TEST_EQUAL(features.size(), 3, ()); + TEST_EQUAL(featureIds.size(), 2, ()); + TEST((featureIds.find(MakeOsmWay(1)) != featureIds.end()) != + (featureIds.find(MakeOsmWay(2)) != featureIds.end()), + ()); + TEST(featureIds.find(MakeOsmWay(3)) != featureIds.end(), ()); +}