From f6fd08f45af27b46494d07419e58fec09b6a943a Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Thu, 25 Jul 2019 15:03:56 +0300 Subject: [PATCH] [routing] Tests on PositionAccumulator. --- routing/routing_tests/CMakeLists.txt | 1 + .../position_accumulator_tests.cpp | 100 ++++++++++++++++++ .../routing/routing.xcodeproj/project.pbxproj | 4 + 3 files changed, 105 insertions(+) create mode 100644 routing/routing_tests/position_accumulator_tests.cpp diff --git a/routing/routing_tests/CMakeLists.txt b/routing/routing_tests/CMakeLists.txt index eeacdf4cda..ab45325546 100644 --- a/routing/routing_tests/CMakeLists.txt +++ b/routing/routing_tests/CMakeLists.txt @@ -20,6 +20,7 @@ set( maxspeeds_tests.cpp nearest_edge_finder_tests.cpp online_cross_fetcher_test.cpp + position_accumulator_tests.cpp restriction_test.cpp road_access_test.cpp road_graph_builder.cpp diff --git a/routing/routing_tests/position_accumulator_tests.cpp b/routing/routing_tests/position_accumulator_tests.cpp new file mode 100644 index 0000000000..b32190a7de --- /dev/null +++ b/routing/routing_tests/position_accumulator_tests.cpp @@ -0,0 +1,100 @@ +#include "testing/testing.hpp" + +#include "routing/position_accumulator.hpp" + +#include "geometry/mercator.hpp" +#include "geometry/point2d.hpp" + +#include "base/math.hpp" + +namespace +{ +double constexpr kEps = 1e-10; +double constexpr kEpsMeters = 0.1; + +class PositionAccumulatorTest +{ +public: + void PushNextPoint(m2::PointD const & point) { m_positionAccumulator.PushNextPoint(point); } + void Clear() { m_positionAccumulator.Clear(); } + m2::PointD GetDirection() const { return m_positionAccumulator.GetDirection(); } + + std::deque const & GetPoints() const + { + return m_positionAccumulator.GetPointsForTesting(); + } + + double GetTrackLengthM() const { return m_positionAccumulator.GetTrackLengthMForTesting(); } + +private: + PositionAccumulator m_positionAccumulator; +}; + +UNIT_CLASS_TEST(PositionAccumulatorTest, Smoke) +{ + PushNextPoint({0.0, 0.0}); + PushNextPoint({0.00001, 0.0}); + TEST_EQUAL(GetPoints().size(), 2, ()); + TEST(m2::AlmostEqualAbs(GetDirection(), m2::PointD(0.00001, 0.0), kEps), ()); + TEST(base::AlmostEqualAbs(GetTrackLengthM(), 1.11, kEpsMeters), (GetTrackLengthM())); +} + +// Test on using good segments. +UNIT_CLASS_TEST(PositionAccumulatorTest, LastGoodSegment) +{ + double constexpr kStepShortButValid = 0.00001; + + for (signed i = -10; i <= 0; ++i) + PushNextPoint({kStepShortButValid * i, 0.0}); + TEST_EQUAL(GetPoints().size(), 11, ()); + TEST(m2::AlmostEqualAbs(GetDirection(), m2::PointD(10 * kStepShortButValid, 0.0), kEps), + (GetDirection())); + TEST(base::AlmostEqualAbs(GetTrackLengthM(), 11.13, kEpsMeters), (GetTrackLengthM())); + + double constexpr kStepGood = 0.0001; + for (size_t i = 0; i < 10; ++i) + PushNextPoint({kStepGood * i, 0.0}); + + TEST_EQUAL(GetPoints().size(), 2, ()); + TEST(m2::AlmostEqualAbs(GetDirection(), m2::PointD(kStepGood, 0.0), kEps), (GetDirection())); + TEST(base::AlmostEqualAbs(GetTrackLengthM(), 11.13, kEpsMeters), (GetTrackLengthM())); + TEST(m2::AlmostEqualAbs({kStepGood * 8, 0.0}, GetPoints()[0], kEps), ()); + TEST(m2::AlmostEqualAbs({kStepGood * 9, 0.0}, GetPoints()[1], kEps), ()); +} + +// Test on removing too outdated elements from the position accumulator. +// Adding short but still valid segments. +UNIT_CLASS_TEST(PositionAccumulatorTest, RemovingOutdated) +{ + double constexpr kStep = 0.00001; + for (size_t i = 0; i < 100; ++i) + PushNextPoint({kStep * i, 0.0}); + + TEST_EQUAL(GetPoints().size(), 64, ()); + TEST(m2::AlmostEqualAbs(GetDirection(), m2::PointD(kStep * 63, 0.0), kEps), (GetDirection())); + + for (size_t i = 36; i < 100; ++i) + { + TEST(m2::AlmostEqualAbs({kStep * i, 0.0}, GetPoints()[i - 36], kEps), + (m2::PointD(kStep * i, 0.0), GetPoints()[i - 36], i)); + } +} + +// Test on adding segments longer than |PositionAccumulator::kMaxValidSegmentLengthM| +// and shorter than |PositionAccumulator::kMinValidSegmentLengthM|. +UNIT_CLASS_TEST(PositionAccumulatorTest, LongSegment) +{ + PushNextPoint({0.0, 0.0}); + PushNextPoint({0.001, 0.0}); // Longer than |MaxValidSegmentLengthM|. + TEST(GetPoints().empty(), ()); + PushNextPoint({0.001001, 0.0}); // Shorter than |kMinValidSegmentLengthM|, but the first one. + PushNextPoint({0.00102, 0.0}); + PushNextPoint({0.00103, 0.0}); + PushNextPoint({0.001031, 0.0}); // Shorter than |kMinValidSegmentLengthM|, so it's ignored. + TEST_EQUAL(GetPoints().size(), 3, ()); + TEST(m2::AlmostEqualAbs(GetDirection(), m2::PointD(0.000029, 0.0), kEps), (GetDirection())); + PushNextPoint({0.00201, 0.0}); // Longer than |PositionAccumulator::kMaxValidSegmentLengthM|. + TEST_EQUAL(GetPoints().size(), 0, ()); + TEST(m2::AlmostEqualAbs(GetDirection(), m2::PointD(0.0, 0.0), kEps), (GetDirection())); +} +} // namespace diff --git a/xcode/routing/routing.xcodeproj/project.pbxproj b/xcode/routing/routing.xcodeproj/project.pbxproj index f07ff42a82..d45c6d86b4 100644 --- a/xcode/routing/routing.xcodeproj/project.pbxproj +++ b/xcode/routing/routing.xcodeproj/project.pbxproj @@ -150,6 +150,7 @@ 56CA630A1F619C1000E6681B /* road_graph_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56CA63061F61206700E6681B /* road_graph_tests.cpp */; }; 56CBED5522E9CE2600D51AF7 /* position_accumulator.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56CBED5322E9CE2500D51AF7 /* position_accumulator.hpp */; }; 56CBED5622E9CE2600D51AF7 /* position_accumulator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56CBED5422E9CE2600D51AF7 /* position_accumulator.cpp */; }; + 56CBED5822E9CFB600D51AF7 /* position_accumulator_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56CBED5722E9CFB600D51AF7 /* position_accumulator_tests.cpp */; }; 56CC5A371E3884960016AC46 /* cross_mwm_index_graph.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56CC5A361E3884960016AC46 /* cross_mwm_index_graph.hpp */; }; 56EA2FD51D8FD8590083F01A /* routing_helpers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56EA2FD41D8FD8590083F01A /* routing_helpers.hpp */; }; 56ED7DBD1F69425700B67156 /* turn_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 567E9F741F5850460064CB96 /* turn_test.cpp */; }; @@ -459,6 +460,7 @@ 56CA63061F61206700E6681B /* road_graph_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = road_graph_tests.cpp; sourceTree = ""; }; 56CBED5322E9CE2500D51AF7 /* position_accumulator.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = position_accumulator.hpp; sourceTree = ""; }; 56CBED5422E9CE2600D51AF7 /* position_accumulator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = position_accumulator.cpp; sourceTree = ""; }; + 56CBED5722E9CFB600D51AF7 /* position_accumulator_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = position_accumulator_tests.cpp; sourceTree = ""; }; 56CC5A361E3884960016AC46 /* cross_mwm_index_graph.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = cross_mwm_index_graph.hpp; sourceTree = ""; }; 56EA2FD41D8FD8590083F01A /* routing_helpers.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = routing_helpers.hpp; sourceTree = ""; }; 56EE14DC1FE812FC0036F20C /* libtransit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libtransit.a; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -710,6 +712,7 @@ 6742ACA01C68A07C009CB89E /* routing_tests */ = { isa = PBXGroup; children = ( + 56CBED5722E9CFB600D51AF7 /* position_accumulator_tests.cpp */, 440BE38722AFB31600C548FB /* uturn_restriction_tests.cpp */, 440BE38322AFB31100C548FB /* world_graph_builder.cpp */, 440BE38422AFB31100C548FB /* world_graph_builder.hpp */, @@ -1306,6 +1309,7 @@ 6742AD291C68A9DF009CB89E /* astar_router_test.cpp in Sources */, 6742AD371C68A9DF009CB89E /* turns_tts_text_tests.cpp in Sources */, 6742AD2C1C68A9DF009CB89E /* followed_polyline_test.cpp in Sources */, + 56CBED5822E9CFB600D51AF7 /* position_accumulator_tests.cpp in Sources */, 6742AD341C68A9DF009CB89E /* routing_session_test.cpp in Sources */, 6742AD261C68A9DF009CB89E /* testingmain.cpp in Sources */, 6742AD361C68A9DF009CB89E /* turns_sound_test.cpp in Sources */,