diff --git a/src/core/virtual_path.cpp b/src/core/virtual_path.cpp index cc9933d56..d3bdea811 100644 --- a/src/core/virtual_path.cpp +++ b/src/core/virtual_path.cpp @@ -89,18 +89,17 @@ VirtualCoordVector::size_type PathCoordVector::update(VirtualCoordVector::size_t { Q_ASSERT(virtual_coords.size() == flags.size()); + clear(); + if (flags[part_start].isHolePoint()) { auto pos = virtual_coords[0]; qWarning("PathCoordVector at %g %g (mm) has an invalid hole at index %d.", pos.x(), -pos.y(), part_start); + return part_start; } - clear(); - if (empty() || (part_start > 0 && flags[part_start-1].isHolePoint())) - { - emplace_back(virtual_coords[part_start], part_start, 0.0, 0.0); - } + emplace_back(virtual_coords[part_start], part_start, 0.0, 0.0); for (auto index = part_start + 1; index <= part_end; ++index) { diff --git a/test/path_object_t.cpp b/test/path_object_t.cpp index 1613a6af1..a5b458ede 100644 --- a/test/path_object_t.cpp +++ b/test/path_object_t.cpp @@ -22,13 +22,14 @@ #include -#include "global.h" #include "core/map.h" #include "core/objects/object.h" #include "core/symbols/line_symbol.h" using namespace OpenOrienteering; +Q_DECLARE_METATYPE(MapCoord*) + namespace QTest { @@ -1150,6 +1151,63 @@ void PathObjectTest::atypicalPathTest() +void PathObjectTest::recalculatePartsTest_data() +{ + static MapCoord coords[] = { + { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }, { 0.0, 0.0, MapCoord::HolePoint }, + { 2.0, 2.0 }, { 2.0, 3.0 }, { 3.0, 3.0, MapCoord::HolePoint }, + { 4.0, 4.0, MapCoord::HolePoint }, + { 6.0, 6.0 }, { 6.0, 5.0 }, + }; + QTest::addColumn("first"); + QTest::addColumn("last"); // STL sense + QTest::addColumn("expected_size"); + + QTest::newRow("0.") << coords+0 << coords+0 << 0; + QTest::newRow("1.") << coords+0 << coords+1 << 1; // maybe not desired + QTest::newRow("2.") << coords+0 << coords+2 << 1; // maybe not desired for areas + QTest::newRow("3.") << coords+0 << coords+3 << 1; + QTest::newRow("4.") << coords+0 << coords+4 << 1; + QTest::newRow("5.") << coords+0 << coords+5 << 1; + QTest::newRow("5;1.") << coords+0 << coords+6 << 2; // maybe not desired + QTest::newRow("5;2.") << coords+0 << coords+7 << 2; // maybe not desired for areas + QTest::newRow("5;3.") << coords+0 << coords+8 << 2; + QTest::newRow("5;3;1.") << coords+0 << coords+9 << 3; // maybe not desired + QTest::newRow("5;3;1;1.") << coords+0 << coords+10 << 4; // maybe not desired +} + +void PathObjectTest::recalculatePartsTest() +{ + QFETCH(MapCoord*, first); + QFETCH(MapCoord*, last); + QFETCH(int, expected_size); + + // The constructor calls PathObject::recalculateParts(). + PathObject path { nullptr, MapCoordVector(first, last), nullptr }; + { + auto& initial_parts = path.parts(); + QCOMPARE(initial_parts.size(), expected_size); + + for (auto& initial_part : initial_parts) + { + QVERIFY(initial_part.first_index <= initial_part.last_index); + } + } + + path.updatePathCoords(); + { + auto& updated_parts = path.parts(); + QCOMPARE(updated_parts.size(), expected_size); + + for (auto& updated_part : updated_parts) + { + QVERIFY(updated_part.first_index <= updated_part.last_index); + } + } +} + + + /* * We don't need a real GUI window. */ diff --git a/test/path_object_t.h b/test/path_object_t.h index b44a3aca7..0a9c07382 100644 --- a/test/path_object_t.h +++ b/test/path_object_t.h @@ -75,6 +75,9 @@ private slots: /** Tests PathCoord and SplitPathCoord for a non-trivial zero-length path. */ void atypicalPathTest(); + /** Tests recalculation of path parts from input coords. */ + void recalculatePartsTest(); + void recalculatePartsTest_data(); }; #endif