Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions test/file_format_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <algorithm>
// IWYU pragma: no_include <array>
#include <cstddef>
// IWYU pragma: no_include <initializer_list>
#include <limits>
#include <memory>
Expand All @@ -40,10 +41,12 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFlags>
#include <QIODevice>
#include <QLatin1Char>
#include <QLatin1String>
#include <QMetaObject>
#include <QMetaType>
#include <QPageSize>
#include <QPoint>
#include <QPointF>
Expand Down Expand Up @@ -1228,6 +1231,8 @@ struct TestOcdFileImport : public OcdFileImport
}

using OcdFileImport::getObjectText;
using OcdFileImport::fillPathCoords;
using OcdFileImport::OcdImportedPathObject;
};

void FileFormatTest::ocdTextImportTest_data()
Expand Down Expand Up @@ -1306,6 +1311,234 @@ void FileFormatTest::ocdTextImportTest()
}


struct OcdPointsView {
const Ocd::OcdPoint32* data = nullptr;
int size = 0;

OcdPointsView() = default;

template <class T, std::size_t n>
explicit OcdPointsView(T(& t)[n])
: data { t }
, size { int(n) }
{}
};
Q_DECLARE_METATYPE(OcdPointsView)

struct FlagsView {
const int* data = nullptr;
int size = 0;

FlagsView() = default;

template <class T, std::size_t n>
explicit FlagsView(T(& t)[n], int size)
: data { t }
, size { size }
{}

template <class T, std::size_t n>
explicit FlagsView(T(& t)[n])
: data { t }
, size { int(n) }
{}
};
Q_DECLARE_METATYPE(FlagsView)

enum OcdPathPersonality {
Line = 0,
Area = 1
};
Q_DECLARE_METATYPE(OcdPathPersonality)

void FileFormatTest::ocdPathImportTest_data()
{
#define C(x) ((int)((unsigned int)(x)<<8)) // FIXME: Not the same as in export
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we don't check the actual x/y values, it doesn't need action.

constexpr auto ocd_flag_gap = 8; // TODO: implement as Ocd::OcdPoint32::FlagGap
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


QTest::addColumn<OcdPointsView>("points");
QTest::addColumn<OcdPathPersonality>("personality");
QTest::addColumn<FlagsView>("expected");

{
// bezier curve
static Ocd::OcdPoint32 ocd_points[] = {
{ C(-1109), C(212) },
{ C(-1035) | Ocd::OcdPoint32::FlagCtl1, C(302) },
{ C(-1008) | Ocd::OcdPoint32::FlagCtl2, C(519) },
{ C(-926), C(437) } // different from first point
};
static int expected_flags[] = {
MapCoord::CurveStart,
0,
0,
0,
MapCoord::ClosePoint // injected by Mapper
};
QTest::newRow("bezier, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags);
QTest::newRow("bezier, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags, 4);
}

{
// straight segments, corner flag
static Ocd::OcdPoint32 ocd_points[] = {
{ C(-589), C(432) },
{ C(-269), C(845) | Ocd::OcdPoint32::FlagCorner },
{ C(267), C(279) }, // different from first point
};
static int expected_flags[] = {
0,
MapCoord::DashPoint,
0,
MapCoord::ClosePoint // injected by Mapper
};
QTest::newRow("straight, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags);
QTest::newRow("straight, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags, 3);
}

{
// straight segments, virtual gap
static Ocd::OcdPoint32 ocd_points[] = {
{ C(-972), C(-264) },
{ C(-836) | Ocd::OcdPoint32::FlagLeft | ocd_flag_gap, C(-151) | Ocd::OcdPoint32::FlagRight },
{ C(-677), C(-19) },
{ C(-518), C(112) } // different from first point
};
static int expected_flags[] = {
0,
0,
0,
0,
MapCoord::ClosePoint // injected by Mapper
};
QTest::newRow("virtual gap, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags);
QTest::newRow("virtual gap, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags, 4);
}

{
// straight segments, one hole
static Ocd::OcdPoint32 ocd_points[] = {
{ C(100), C(-250) },
{ C(150), C(-260) },
{ C(100), C(-250) }, // same as first point
{ C(200), C(-350) | Ocd::OcdPoint32::FlagHole },
{ C(220), C(-400) },
{ C(200), C(-350) } // same as first point of hole
};
static int expected_flags_area[] = {
0,
0,
MapCoord::ClosePoint | MapCoord::HolePoint,
0,
0,
MapCoord::ClosePoint
};
QTest::newRow("hole, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags_area);
static int expected_flags_line[6] = {};
QTest::newRow("hole, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags_line);
}

{
// straight segments, with an "empty" hole
static Ocd::OcdPoint32 ocd_points[] = {
{ C(100), C(-250) },
{ C(150), C(-260) },
{ C(100), C(-250) }, // same as first point
{ C(120), C(-200) | Ocd::OcdPoint32::FlagHole },
{ C(200), C(-350) | Ocd::OcdPoint32::FlagHole },
{ C(220), C(-400) },
{ C(200), C(-350) } // same as second FlagHole point
};
static int expected_flags_area[] = {
0,
0,
MapCoord::ClosePoint | MapCoord::HolePoint,
MapCoord::ClosePoint | MapCoord::HolePoint,
0,
0,
MapCoord::ClosePoint
};
QTest::newRow("empty hole, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags_area);
static int expected_flags_line[7] = {};
QTest::newRow("empty hole, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags_line);
}

{
// straight segments, with two "empty" holes
static Ocd::OcdPoint32 ocd_points[] = {
{ C(100), C(-250) },
{ C(150), C(-260) },
{ C(100), C(-250) }, // same as first point
{ C(120), C(-200) | Ocd::OcdPoint32::FlagHole },
{ C(140), C(-300) | Ocd::OcdPoint32::FlagHole },
{ C(200), C(-350) | Ocd::OcdPoint32::FlagHole },
{ C(220), C(-400) },
{ C(200), C(-350) }, // same as third FlagHole point
};
static int expected_flags_area[] = {
0,
0,
MapCoord::ClosePoint | MapCoord::HolePoint,
MapCoord::ClosePoint | MapCoord::HolePoint,
MapCoord::ClosePoint | MapCoord::HolePoint,
0,
0,
MapCoord::ClosePoint
};
QTest::newRow("two empty holes, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags_area);
static int expected_flags_line[8] = {};
QTest::newRow("two empty holes, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags_line);
}

{
// straight segments, one hole, not actual ares
static Ocd::OcdPoint32 ocd_points[] = {
{ C(100), C(-250) },
{ C(150), C(-260) },
{ C(200), C(-350) | Ocd::OcdPoint32::FlagHole },
{ C(220), C(-400) }
};
static int expected_flags_area[] = {
0,
0,
MapCoord::ClosePoint | MapCoord::HolePoint, // injected by Mapper
0,
0,
MapCoord::ClosePoint // injected by Mapper
};
QTest::newRow("open areas with hole, area") << OcdPointsView(ocd_points) << Area << FlagsView(expected_flags_area);
static int expected_flags_line[4] = {};
QTest::newRow("open areas with hole, line") << OcdPointsView(ocd_points) << Line << FlagsView(expected_flags_line);
}
}

void FileFormatTest::ocdPathImportTest()
{
QFETCH(OcdPointsView, points);
QFETCH(OcdPathPersonality, personality);
QFETCH(FlagsView, expected);

TestOcdFileImport ocd_v12_import{12};

TestOcdFileImport::OcdImportedPathObject path_object;
ocd_v12_import.fillPathCoords(&path_object, personality == Area, points.size, points.data);
QVERIFY(path_object.getRawCoordinateVector().size() > 0);
Copy link
Member Author

@dg0yt dg0yt Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a time when OcdPointsView and FlagsView returned empty views, turning the test into a no-op. I prefer to leave it in.


// QEXPECT_FAIL("empty hole, area", "", Abort);
QCOMPARE(path_object.getRawCoordinateVector().size(), expected.size);

for (int i = 0; i < expected.size; ++i)
{
// QCOMPARE(path_object.getCoordinate(i).flags(), *expected);
// Provide the current index when failing.
if (path_object.getCoordinate(i).flags() != static_cast<MapCoord::Flags>(expected.data[i]))
{
auto err = QString::fromLatin1("Compared flags are not the same at index %1\n Actual : %2\n Expected: %3")
.arg(QString::number(i), QString::number(path_object.getCoordinate(i).flags()), QString::number(expected.data[i]));
QFAIL(qPrintable(err));
}
}
}

/*
* We don't need a real GUI window.
Expand Down
8 changes: 7 additions & 1 deletion test/file_format_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <QObject>
#include <QString>


/**
* @test Tests concerning the file formats, registry, import and export.
*
Expand Down Expand Up @@ -121,6 +120,13 @@ private slots:
*/
void ocdTextImportTest_data();
void ocdTextImportTest();

/**
* Test path import from OCD.
*/
void ocdPathImportTest_data();
void ocdPathImportTest();

};

#endif // OPENORIENTEERING_FILE_FORMAT_T_H