|
| 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 | +} |
0 commit comments