Files
qelectrotech-source-mirror/tests/qttest/tst_diagramsortkeys.cpp
ispyisail bd5cff4211 Add a unit test locking in Diagram::toXml()'s coordinate sort order
Follow-up to the positionKey() fix merged directly in #779
(b2f4ef5d2): per review request, add a small regression test so this
class of bug (fixed-precision "%.4f" formatting compares out of
numeric order once the integer part's digit count differs) can't
silently reappear.

positionKey()/coordinateKey() move out of diagram.cpp's anonymous
namespace into a small header-only diagramsortkeys.h so the test can
link against the exact same code Diagram::toXml() uses, instead of
duplicating the algorithm. Behavior is unchanged.

tst_diagramsortkeys covers: single- vs double-digit, double- vs
triple-digit, negative-vs-negative, negative-vs-positive, and
negative-vs-zero coordinate pairs, plus sub-precision deltas.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-07 20:54:18 +12:00

40 lines
1.3 KiB
C++

#include <QtTest>
#include "diagramsortkeys.h"
class tst_diagramsortkeys : public QObject
{
Q_OBJECT
private slots:
// positionKey() must sort the same way the underlying coordinates do,
// including across differing integer-part digit widths and across the
// negative/positive boundary. A previous implementation formatted
// coordinates with plain "%.4f" and compared the resulting strings
// directly, which sorted "15.0000" before "5.0000".
void sortsLikeNumbers_data()
{
QTest::addColumn<QPointF>("smaller");
QTest::addColumn<QPointF>("larger");
QTest::newRow("single vs double digit") << QPointF(5.0, 0.0) << QPointF(15.0, 0.0);
QTest::newRow("double vs triple digit") << QPointF(0.0, 99.0) << QPointF(0.0, 100.0);
QTest::newRow("negative vs negative") << QPointF(-15.0, 0.0) << QPointF(-5.0, 0.0);
QTest::newRow("negative vs positive") << QPointF(-1.0, 0.0) << QPointF(1.0, 0.0);
QTest::newRow("negative vs zero") << QPointF(0.0, -0.0001) << QPointF(0.0, 0.0);
QTest::newRow("fractional precision") << QPointF(1.0001, 0.0) << QPointF(1.001, 0.0);
}
void sortsLikeNumbers()
{
QFETCH(QPointF, smaller);
QFETCH(QPointF, larger);
QVERIFY(DiagramSortKeys::positionKey(smaller) < DiagramSortKeys::positionKey(larger));
}
};
QTEST_APPLESS_MAIN(tst_diagramsortkeys)
#include "tst_diagramsortkeys.moc"