1 /*
2  * Copyright (C) 2018 Damir Porobic <damir.porobic@gmx.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "MathHelperTest.h"
21 
TestRoundAngleTo_Should_ReturnAngleRoundedUpToIncrement()22 void MathHelperTest::TestRoundAngleTo_Should_ReturnAngleRoundedUpToIncrement()
23 {
24     QFETCH(qreal, angle);
25     QFETCH(qreal, increment);
26     QFETCH(qreal, result);
27 
28     auto newAngle = MathHelper::roundAngleTo(angle, increment);
29 
30     QCOMPARE(newAngle, result);
31 }
32 
TestRoundAngleTo_Should_ReturnAngleRoundedUpToIncrement_data()33 void MathHelperTest::TestRoundAngleTo_Should_ReturnAngleRoundedUpToIncrement_data()
34 {
35     QTest::addColumn<qreal>("angle");
36     QTest::addColumn<qreal>("increment");
37     QTest::addColumn<qreal>("result");
38 
39     QTest::newRow("set1") << 50.0 << 45.0 << 45.0;
40     QTest::newRow("set2") << 87.0 << 45.0 << 90.0;
41     QTest::newRow("set3") << 0.0 << 45.0 << 0.0;
42     QTest::newRow("set4") << 355.0 << 45.0 << 360.0;
43     QTest::newRow("set5") << 360.0 << 45.0 << 360.0;
44     QTest::newRow("set6") << -2.0 << 45.0 << 0.0;
45 }
46 
TestSmallerValue_Should_AlwaysSmallerOfTwoValues()47 void MathHelperTest::TestSmallerValue_Should_AlwaysSmallerOfTwoValues()
48 {
49     QFETCH(qreal, width);
50     QFETCH(qreal, height);
51     QFETCH(qreal, result);
52 
53     auto smallest = MathHelper::smallerValue(width, height);
54 
55     QCOMPARE(smallest, result);
56 }
57 
TestSmallerValue_Should_AlwaysSmallerOfTwoValues_data()58 void MathHelperTest::TestSmallerValue_Should_AlwaysSmallerOfTwoValues_data()
59 {
60     QTest::addColumn<qreal>("width");
61     QTest::addColumn<qreal>("height");
62     QTest::addColumn<qreal>("result");
63 
64     QTest::newRow("set1") << 50.0 << 49.0 << 49.0;
65     QTest::newRow("set2") << -87.0 << 1.0 << -1.0;
66     QTest::newRow("set3") << 0.0 << -1.0 << 0.0;
67     QTest::newRow("set4") << 400.0 << -500.0 << 400.0;
68     QTest::newRow("set5") << -3.0 << 2.0 << -2.0;
69     QTest::newRow("set6") << -2.0 << 2.0 << -2.0;
70 }
71 
TestDistanceBetweenPoints_Should_ReturnCorrectDistance()72 void MathHelperTest::TestDistanceBetweenPoints_Should_ReturnCorrectDistance()
73 {
74     QFETCH(QPointF, point1);
75     QFETCH(QPointF, point2);
76     QFETCH(double, expected);
77 
78     auto result = MathHelper::distanceBetweenPoints(point1, point2);
79 
80     QCOMPARE(result, expected);
81 }
82 
TestDistanceBetweenPoints_Should_ReturnCorrectDistance_data()83 void MathHelperTest::TestDistanceBetweenPoints_Should_ReturnCorrectDistance_data()
84 {
85     QTest::addColumn<QPointF>("point1");
86     QTest::addColumn<QPointF>("point2");
87     QTest::addColumn<double>("expected");
88 
89     QTest::newRow("set1") << QPointF(10, 10) << QPointF(20, 10) << 10.0;
90     QTest::newRow("set2") << QPointF(10, 10) << QPointF(10, 20) << 10.0;
91     QTest::newRow("set3") << QPointF(-10, 10) << QPointF(10, 10) << 20.0;
92     QTest::newRow("set4") << QPointF(-4, -3) << QPointF(8, 6) << 15.0;
93 }
94 
95 QTEST_MAIN(MathHelperTest);
96