Skip to content

Commit 7b5ebdb

Browse files
committed
Add real unit tests for core math, util, UID, and shape logic
Replace the placeholder TestMaths (which only exercised Qt's QString::toUpper) with unit tests that cover production code: - TestMaths: degree/radian conversion, wrapAround, distance helpers, xOr - TestUtil: map_float/map_int range mapping and clipping, isNumeric, fileExists/eraseFile - TestUidAllocator: sequential allocation, lowest-id reuse, reserve, free - TestShape: vertex accessors, getCenter, includesPoint, translate, applyTransform, polygon round-trip The test project compiles the real MapMap sources under test and runs all suites from a single binary via a custom QtTest runner. Util.cpp uses glTexCoord2f and glVertex2f, which require -lopengl32 on Windows. The main project already links this via src.pri, but the test project manages its own linkage independently.
1 parent 78d401d commit 7b5ebdb

11 files changed

Lines changed: 507 additions & 15 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Makefile*
4444

4545
# Generated by Qt
4646
mapmap
47+
tests/mapmap_tests
48+
moc_predefs.h
4749
moc_*.cpp
4850
qrc_*.cpp
4951
*.moc

tests/TestMaths.cpp

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
1+
/*
2+
* TestMaths.cpp
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*/
9+
110
#include "TestMaths.h"
211

3-
void TestMaths::toUpper()
12+
#include "Maths.h"
13+
14+
using namespace mmp;
15+
16+
void TestMaths::degreesRadiansRoundTrip()
417
{
5-
QString str = "Hello";
6-
QCOMPARE(str.toUpper(), QString("HELLO"));
18+
QVERIFY(qFuzzyCompare(degreesToRadians(180.0), M_PI));
19+
QVERIFY(qFuzzyCompare(radiansToDegrees(M_PI), 180.0));
20+
QVERIFY(qFuzzyCompare(radiansToDegrees(degreesToRadians(57.3)), 57.3));
721
}
822

9-
QTEST_MAIN(TestMaths)
23+
void TestMaths::wrapAroundInt()
24+
{
25+
// Example taken from the documentation in Maths.h.
26+
QCOMPARE(wrapAround(-1, 3), 2);
27+
QCOMPARE(wrapAround(0, 3), 0);
28+
QCOMPARE(wrapAround(3, 3), 0);
29+
QCOMPARE(wrapAround(5, 3), 2);
30+
QCOMPARE(wrapAround(-4, 3), 2);
31+
}
1032

33+
void TestMaths::wrapAroundReal()
34+
{
35+
QVERIFY(qFuzzyCompare(wrapAround(qreal(-0.5), qreal(1.0)), qreal(0.5)));
36+
QVERIFY(qFuzzyCompare(wrapAround(qreal(1.5), qreal(1.0)), qreal(0.5)));
37+
QVERIFY(qFuzzyCompare(wrapAround(qreal(0.25), qreal(1.0)), qreal(0.25)));
38+
}
39+
40+
void TestMaths::squareAndDistance()
41+
{
42+
QVERIFY(qFuzzyCompare(sq(3.0), 9.0));
43+
44+
QPointF a(0, 0);
45+
QPointF b(3, 4);
46+
QVERIFY(qFuzzyCompare(distSq(a, b), 25.0));
47+
QVERIFY(qFuzzyCompare(dist(a, b), 5.0));
48+
}
49+
50+
void TestMaths::distanceInside()
51+
{
52+
QPointF a(0, 0);
53+
QPointF b(3, 4); // distance 5
54+
QVERIFY(distIsInside(a, b, 6.0));
55+
QVERIFY(!distIsInside(a, b, 5.0)); // strictly inside: equal is outside
56+
QVERIFY(!distIsInside(a, b, 4.0));
57+
}
58+
59+
void TestMaths::booleanXor()
60+
{
61+
QCOMPARE(xOr(true, false), true);
62+
QCOMPARE(xOr(false, true), true);
63+
QCOMPARE(xOr(true, true), false);
64+
QCOMPARE(xOr(false, false), false);
65+
}

tests/TestMaths.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
/*
2+
* TestMaths.h
3+
*
4+
* Unit tests for the math helpers in src/core/Maths.h.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*/
11+
12+
#ifndef TEST_MATHS_H_
13+
#define TEST_MATHS_H_
14+
115
#include <QtTest/QtTest>
216

317
class TestMaths: public QObject
418
{
5-
Q_OBJECT
19+
Q_OBJECT
620

7-
private slots:
8-
void toUpper();
21+
private slots:
22+
void degreesRadiansRoundTrip();
23+
void wrapAroundInt();
24+
void wrapAroundReal();
25+
void squareAndDistance();
26+
void distanceInside();
27+
void booleanXor();
928
};
1029

30+
#endif /* TEST_MATHS_H_ */

tests/TestShape.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* TestShape.cpp
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*/
9+
10+
#include "TestShape.h"
11+
12+
#include "Quad.h"
13+
#include "Triangle.h"
14+
15+
using namespace mmp;
16+
17+
namespace {
18+
// A unit-ish axis-aligned square from (0,0) to (2,2).
19+
Quad makeSquare()
20+
{
21+
return Quad(QPointF(0, 0), QPointF(2, 0), QPointF(2, 2), QPointF(0, 2));
22+
}
23+
24+
bool fuzzyPoint(const QPointF& a, const QPointF& b)
25+
{
26+
return qFuzzyCompare(a.x(), b.x()) && qFuzzyCompare(a.y(), b.y());
27+
}
28+
}
29+
30+
void TestShape::verticesAccessors()
31+
{
32+
// Triangle has 3 vertices and (size <= 3) so setVertex does not constrain.
33+
Triangle tri(QPointF(0, 0), QPointF(4, 0), QPointF(0, 3));
34+
QCOMPARE(tri.nVertices(), 3);
35+
QVERIFY(fuzzyPoint(tri.getVertex(1), QPointF(4, 0)));
36+
37+
tri.setVertex(0, QPointF(1, 1));
38+
QVERIFY(fuzzyPoint(tri.getVertex(0), QPointF(1, 1)));
39+
40+
// setVertices replaces the whole set (deep copy).
41+
QVector<QPointF> newVerts{ QPointF(5, 5), QPointF(6, 6), QPointF(7, 7) };
42+
tri.setVertices(newVerts);
43+
QCOMPARE(tri.nVertices(), 3);
44+
QVERIFY(fuzzyPoint(tri.getVertex(2), QPointF(7, 7)));
45+
}
46+
47+
void TestShape::getCenter()
48+
{
49+
Quad square = makeSquare();
50+
QVERIFY(fuzzyPoint(square.getCenter(), QPointF(1, 1)));
51+
}
52+
53+
void TestShape::includesPoint()
54+
{
55+
Quad square = makeSquare();
56+
QVERIFY(square.includesPoint(QPointF(1, 1)));
57+
QVERIFY(square.includesPoint(QPointF(0.5, 1.5)));
58+
QVERIFY(!square.includesPoint(QPointF(3, 3)));
59+
QVERIFY(!square.includesPoint(QPointF(-1, -1)));
60+
}
61+
62+
void TestShape::translate()
63+
{
64+
Quad square = makeSquare();
65+
square.translate(QPointF(1, 1));
66+
67+
QVERIFY(fuzzyPoint(square.getVertex(0), QPointF(1, 1)));
68+
QVERIFY(fuzzyPoint(square.getVertex(2), QPointF(3, 3)));
69+
QVERIFY(fuzzyPoint(square.getCenter(), QPointF(2, 2)));
70+
}
71+
72+
void TestShape::applyTransform()
73+
{
74+
Quad square = makeSquare();
75+
76+
QTransform t;
77+
t.translate(5, 0);
78+
square.applyTransform(t);
79+
80+
QVERIFY(fuzzyPoint(square.getVertex(0), QPointF(5, 0)));
81+
QVERIFY(fuzzyPoint(square.getVertex(1), QPointF(7, 0)));
82+
}
83+
84+
void TestShape::polygonRoundTrip()
85+
{
86+
Quad square = makeSquare();
87+
88+
QPolygonF poly = square.toPolygon();
89+
QCOMPARE(poly.size(), 4);
90+
QVERIFY(fuzzyPoint(poly.at(2), QPointF(2, 2)));
91+
92+
// Shift every point and feed it back; the shape must reflect the change.
93+
QPolygonF shifted = poly.translated(10, 10);
94+
square.fromPolygon(shifted);
95+
QVERIFY(fuzzyPoint(square.getVertex(0), QPointF(10, 10)));
96+
QVERIFY(fuzzyPoint(square.getCenter(), QPointF(11, 11)));
97+
}

tests/TestShape.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* TestShape.h
3+
*
4+
* Unit tests for the shape geometry in src/shape (MShape / Polygon).
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*/
11+
12+
#ifndef TEST_SHAPE_H_
13+
#define TEST_SHAPE_H_
14+
15+
#include <QtTest/QtTest>
16+
17+
class TestShape: public QObject
18+
{
19+
Q_OBJECT
20+
21+
private slots:
22+
void verticesAccessors();
23+
void getCenter();
24+
void includesPoint();
25+
void translate();
26+
void applyTransform();
27+
void polygonRoundTrip();
28+
};
29+
30+
#endif /* TEST_SHAPE_H_ */

tests/TestUidAllocator.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* TestUidAllocator.cpp
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*/
9+
10+
#include "TestUidAllocator.h"
11+
12+
#include "UidAllocator.h"
13+
14+
using namespace mmp;
15+
16+
void TestUidAllocator::allocateIsSequential()
17+
{
18+
UidAllocator allocator;
19+
QCOMPARE(allocator.allocate(), uid(1));
20+
QCOMPARE(allocator.allocate(), uid(2));
21+
QCOMPARE(allocator.allocate(), uid(3));
22+
23+
QVERIFY(allocator.exists(1));
24+
QVERIFY(allocator.exists(2));
25+
QVERIFY(allocator.exists(3));
26+
QVERIFY(!allocator.exists(4));
27+
QCOMPARE(int(allocator.list().size()), 3);
28+
}
29+
30+
void TestUidAllocator::freeReusesLowestId()
31+
{
32+
UidAllocator allocator;
33+
allocator.allocate(); // 1
34+
allocator.allocate(); // 2
35+
allocator.allocate(); // 3
36+
37+
QVERIFY(allocator.free(2));
38+
QVERIFY(!allocator.exists(2));
39+
40+
// The next allocation should reuse the lowest free id (2).
41+
QCOMPARE(allocator.allocate(), uid(2));
42+
QVERIFY(allocator.exists(2));
43+
}
44+
45+
void TestUidAllocator::reserve()
46+
{
47+
UidAllocator allocator;
48+
49+
// Reserving an unused id succeeds and marks it as existing.
50+
QVERIFY(allocator.reserve(42));
51+
QVERIFY(allocator.exists(42));
52+
53+
// Reserving an already-reserved id fails.
54+
QVERIFY(!allocator.reserve(42));
55+
56+
// A subsequent allocate must skip the reserved id.
57+
QCOMPARE(allocator.allocate(), uid(1));
58+
}
59+
60+
void TestUidAllocator::freeUnknownReturnsFalse()
61+
{
62+
UidAllocator allocator;
63+
QVERIFY(!allocator.free(99));
64+
65+
allocator.allocate(); // 1
66+
QVERIFY(!allocator.free(99));
67+
QVERIFY(allocator.free(1));
68+
QVERIFY(!allocator.free(1)); // already freed
69+
}

tests/TestUidAllocator.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* TestUidAllocator.h
3+
*
4+
* Unit tests for src/core/UidAllocator.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*/
11+
12+
#ifndef TEST_UID_ALLOCATOR_H_
13+
#define TEST_UID_ALLOCATOR_H_
14+
15+
#include <QtTest/QtTest>
16+
17+
class TestUidAllocator: public QObject
18+
{
19+
Q_OBJECT
20+
21+
private slots:
22+
void allocateIsSequential();
23+
void freeReusesLowestId();
24+
void reserve();
25+
void freeUnknownReturnsFalse();
26+
};
27+
28+
#endif /* TEST_UID_ALLOCATOR_H_ */

0 commit comments

Comments
 (0)