Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Makefile*

# Generated by Qt
mapmap
tests/mapmap_tests
moc_predefs.h
moc_*.cpp
qrc_*.cpp
*.moc
Expand Down
63 changes: 59 additions & 4 deletions tests/TestMaths.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
/*
* TestMaths.cpp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

#include "TestMaths.h"

void TestMaths::toUpper()
#include "Maths.h"

using namespace mmp;

void TestMaths::degreesRadiansRoundTrip()
{
QString str = "Hello";
QCOMPARE(str.toUpper(), QString("HELLO"));
QVERIFY(qFuzzyCompare(degreesToRadians(180.0), M_PI));
QVERIFY(qFuzzyCompare(radiansToDegrees(M_PI), 180.0));
QVERIFY(qFuzzyCompare(radiansToDegrees(degreesToRadians(57.3)), 57.3));
}

QTEST_MAIN(TestMaths)
void TestMaths::wrapAroundInt()
{
// Example taken from the documentation in Maths.h.
QCOMPARE(wrapAround(-1, 3), 2);
QCOMPARE(wrapAround(0, 3), 0);
QCOMPARE(wrapAround(3, 3), 0);
QCOMPARE(wrapAround(5, 3), 2);
QCOMPARE(wrapAround(-4, 3), 2);
}

void TestMaths::wrapAroundReal()
{
QVERIFY(qFuzzyCompare(wrapAround(qreal(-0.5), qreal(1.0)), qreal(0.5)));
QVERIFY(qFuzzyCompare(wrapAround(qreal(1.5), qreal(1.0)), qreal(0.5)));
QVERIFY(qFuzzyCompare(wrapAround(qreal(0.25), qreal(1.0)), qreal(0.25)));
}

void TestMaths::squareAndDistance()
{
QVERIFY(qFuzzyCompare(sq(3.0), 9.0));

QPointF a(0, 0);
QPointF b(3, 4);
QVERIFY(qFuzzyCompare(distSq(a, b), 25.0));
QVERIFY(qFuzzyCompare(dist(a, b), 5.0));
}

void TestMaths::distanceInside()
{
QPointF a(0, 0);
QPointF b(3, 4); // distance 5
QVERIFY(distIsInside(a, b, 6.0));
QVERIFY(!distIsInside(a, b, 5.0)); // strictly inside: equal is outside
QVERIFY(!distIsInside(a, b, 4.0));
}

void TestMaths::booleanXor()
{
QCOMPARE(xOr(true, false), true);
QCOMPARE(xOr(false, true), true);
QCOMPARE(xOr(true, true), false);
QCOMPARE(xOr(false, false), false);
}
26 changes: 23 additions & 3 deletions tests/TestMaths.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
/*
* TestMaths.h
*
* Unit tests for the math helpers in src/core/Maths.h.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

#ifndef TEST_MATHS_H_
#define TEST_MATHS_H_

#include <QtTest/QtTest>

class TestMaths: public QObject
{
Q_OBJECT
Q_OBJECT

private slots:
void toUpper();
private slots:
void degreesRadiansRoundTrip();
void wrapAroundInt();
void wrapAroundReal();
void squareAndDistance();
void distanceInside();
void booleanXor();
};

#endif /* TEST_MATHS_H_ */
97 changes: 97 additions & 0 deletions tests/TestShape.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* TestShape.cpp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

#include "TestShape.h"

#include "Quad.h"
#include "Triangle.h"

using namespace mmp;

namespace {
// A unit-ish axis-aligned square from (0,0) to (2,2).
Quad makeSquare()
{
return Quad(QPointF(0, 0), QPointF(2, 0), QPointF(2, 2), QPointF(0, 2));
}

bool fuzzyPoint(const QPointF& a, const QPointF& b)
{
return qFuzzyCompare(a.x(), b.x()) && qFuzzyCompare(a.y(), b.y());
}
}

void TestShape::verticesAccessors()
{
// Triangle has 3 vertices and (size <= 3) so setVertex does not constrain.
Triangle tri(QPointF(0, 0), QPointF(4, 0), QPointF(0, 3));
QCOMPARE(tri.nVertices(), 3);
QVERIFY(fuzzyPoint(tri.getVertex(1), QPointF(4, 0)));

tri.setVertex(0, QPointF(1, 1));
QVERIFY(fuzzyPoint(tri.getVertex(0), QPointF(1, 1)));

// setVertices replaces the whole set (deep copy).
QVector<QPointF> newVerts{ QPointF(5, 5), QPointF(6, 6), QPointF(7, 7) };
tri.setVertices(newVerts);
QCOMPARE(tri.nVertices(), 3);
QVERIFY(fuzzyPoint(tri.getVertex(2), QPointF(7, 7)));
}

void TestShape::getCenter()
{
Quad square = makeSquare();
QVERIFY(fuzzyPoint(square.getCenter(), QPointF(1, 1)));
}

void TestShape::includesPoint()
{
Quad square = makeSquare();
QVERIFY(square.includesPoint(QPointF(1, 1)));
QVERIFY(square.includesPoint(QPointF(0.5, 1.5)));
QVERIFY(!square.includesPoint(QPointF(3, 3)));
QVERIFY(!square.includesPoint(QPointF(-1, -1)));
}

void TestShape::translate()
{
Quad square = makeSquare();
square.translate(QPointF(1, 1));

QVERIFY(fuzzyPoint(square.getVertex(0), QPointF(1, 1)));
QVERIFY(fuzzyPoint(square.getVertex(2), QPointF(3, 3)));
QVERIFY(fuzzyPoint(square.getCenter(), QPointF(2, 2)));
}

void TestShape::applyTransform()
{
Quad square = makeSquare();

QTransform t;
t.translate(5, 0);
square.applyTransform(t);

QVERIFY(fuzzyPoint(square.getVertex(0), QPointF(5, 0)));
QVERIFY(fuzzyPoint(square.getVertex(1), QPointF(7, 0)));
}

void TestShape::polygonRoundTrip()
{
Quad square = makeSquare();

QPolygonF poly = square.toPolygon();
QCOMPARE(poly.size(), 4);
QVERIFY(fuzzyPoint(poly.at(2), QPointF(2, 2)));

// Shift every point and feed it back; the shape must reflect the change.
QPolygonF shifted = poly.translated(10, 10);
square.fromPolygon(shifted);
QVERIFY(fuzzyPoint(square.getVertex(0), QPointF(10, 10)));
QVERIFY(fuzzyPoint(square.getCenter(), QPointF(11, 11)));
}
30 changes: 30 additions & 0 deletions tests/TestShape.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* TestShape.h
*
* Unit tests for the shape geometry in src/shape (MShape / Polygon).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

#ifndef TEST_SHAPE_H_
#define TEST_SHAPE_H_

#include <QtTest/QtTest>

class TestShape: public QObject
{
Q_OBJECT

private slots:
void verticesAccessors();
void getCenter();
void includesPoint();
void translate();
void applyTransform();
void polygonRoundTrip();
};

#endif /* TEST_SHAPE_H_ */
69 changes: 69 additions & 0 deletions tests/TestUidAllocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* TestUidAllocator.cpp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

#include "TestUidAllocator.h"

#include "UidAllocator.h"

using namespace mmp;

void TestUidAllocator::allocateIsSequential()
{
UidAllocator allocator;
QCOMPARE(allocator.allocate(), uid(1));
QCOMPARE(allocator.allocate(), uid(2));
QCOMPARE(allocator.allocate(), uid(3));

QVERIFY(allocator.exists(1));
QVERIFY(allocator.exists(2));
QVERIFY(allocator.exists(3));
QVERIFY(!allocator.exists(4));
QCOMPARE(int(allocator.list().size()), 3);
}

void TestUidAllocator::freeReusesLowestId()
{
UidAllocator allocator;
allocator.allocate(); // 1
allocator.allocate(); // 2
allocator.allocate(); // 3

QVERIFY(allocator.free(2));
QVERIFY(!allocator.exists(2));

// The next allocation should reuse the lowest free id (2).
QCOMPARE(allocator.allocate(), uid(2));
QVERIFY(allocator.exists(2));
}

void TestUidAllocator::reserve()
{
UidAllocator allocator;

// Reserving an unused id succeeds and marks it as existing.
QVERIFY(allocator.reserve(42));
QVERIFY(allocator.exists(42));

// Reserving an already-reserved id fails.
QVERIFY(!allocator.reserve(42));

// A subsequent allocate must skip the reserved id.
QCOMPARE(allocator.allocate(), uid(1));
}

void TestUidAllocator::freeUnknownReturnsFalse()
{
UidAllocator allocator;
QVERIFY(!allocator.free(99));

allocator.allocate(); // 1
QVERIFY(!allocator.free(99));
QVERIFY(allocator.free(1));
QVERIFY(!allocator.free(1)); // already freed
}
28 changes: 28 additions & 0 deletions tests/TestUidAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* TestUidAllocator.h
*
* Unit tests for src/core/UidAllocator.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

#ifndef TEST_UID_ALLOCATOR_H_
#define TEST_UID_ALLOCATOR_H_

#include <QtTest/QtTest>

class TestUidAllocator: public QObject
{
Q_OBJECT

private slots:
void allocateIsSequential();
void freeReusesLowestId();
void reserve();
void freeUnknownReturnsFalse();
};

#endif /* TEST_UID_ALLOCATOR_H_ */
Loading
Loading