1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org> |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <qmath.h> |
32 | |
33 | class tst_QMath : public QObject |
34 | { |
35 | Q_OBJECT |
36 | private slots: |
37 | void fastSinCos(); |
38 | void degreesToRadians_data(); |
39 | void degreesToRadians(); |
40 | void radiansToDegrees_data(); |
41 | void radiansToDegrees(); |
42 | void qNextPowerOfTwo32S_data(); |
43 | void qNextPowerOfTwo32S(); |
44 | void qNextPowerOfTwo64S_data(); |
45 | void qNextPowerOfTwo64S(); |
46 | void qNextPowerOfTwo32U_data(); |
47 | void qNextPowerOfTwo32U(); |
48 | void qNextPowerOfTwo64U_data(); |
49 | void qNextPowerOfTwo64U(); |
50 | }; |
51 | |
52 | void tst_QMath::fastSinCos() |
53 | { |
54 | // Test evenly spaced angles from 0 to 2pi radians. |
55 | const int LOOP_COUNT = 100000; |
56 | const qreal loopAngle = 2 * M_PI / (LOOP_COUNT - 1); |
57 | for (int i = 0; i < LOOP_COUNT; ++i) { |
58 | qreal angle = i * loopAngle; |
59 | QVERIFY(qAbs(qSin(angle) - qFastSin(angle)) < 1e-5); |
60 | QVERIFY(qAbs(qCos(angle) - qFastCos(angle)) < 1e-5); |
61 | } |
62 | } |
63 | |
64 | void tst_QMath::degreesToRadians_data() |
65 | { |
66 | QTest::addColumn<float>(name: "degreesFloat" ); |
67 | QTest::addColumn<float>(name: "radiansFloat" ); |
68 | QTest::addColumn<double>(name: "degreesDouble" ); |
69 | QTest::addColumn<double>(name: "radiansDouble" ); |
70 | |
71 | QTest::newRow( dataTag: "pi" ) << 180.0f << float(M_PI) << 180.0 << M_PI; |
72 | QTest::newRow( dataTag: "doublepi" ) << 360.0f << float(2 * M_PI) << 360.0 << 2 * M_PI; |
73 | QTest::newRow( dataTag: "halfpi" ) << 90.0f << float(M_PI_2) << 90.0 << M_PI_2; |
74 | |
75 | QTest::newRow( dataTag: "random" ) << 123.1234567f << 2.1489097058516724f << 123.123456789123456789 << 2.148909707407169856192285627; |
76 | QTest::newRow( dataTag: "bigrandom" ) << 987654321.9876543f << 17237819.79023679f << 987654321987654321.987654321987654321 << 17237819790236794.0; |
77 | |
78 | QTest::newRow( dataTag: "zero" ) << 0.0f << 0.0f << 0.0 << 0.0; |
79 | |
80 | QTest::newRow( dataTag: "minuspi" ) << -180.0f << float(-M_PI) << 180.0 << M_PI; |
81 | QTest::newRow( dataTag: "minusdoublepi" ) << -360.0f << float(-2 * M_PI) << -360.0 << -2 * M_PI; |
82 | QTest::newRow( dataTag: "minushalfpi" ) << -90.0f << float(-M_PI_2) << -90.0 << -M_PI_2; |
83 | |
84 | QTest::newRow( dataTag: "minusrandom" ) << -123.1234567f << -2.1489097058516724f << -123.123456789123456789 << -2.148909707407169856192285627; |
85 | QTest::newRow( dataTag: "minusbigrandom" ) << -987654321.9876543f << -17237819.79023679f << -987654321987654321.987654321987654321 << -17237819790236794.0; |
86 | } |
87 | |
88 | void tst_QMath::degreesToRadians() |
89 | { |
90 | QFETCH(float, degreesFloat); |
91 | QFETCH(float, radiansFloat); |
92 | QFETCH(double, degreesDouble); |
93 | QFETCH(double, radiansDouble); |
94 | |
95 | QCOMPARE(qDegreesToRadians(degreesFloat), radiansFloat); |
96 | QCOMPARE(qDegreesToRadians(degreesDouble), radiansDouble); |
97 | } |
98 | |
99 | void tst_QMath::radiansToDegrees_data() |
100 | { |
101 | QTest::addColumn<float>(name: "radiansFloat" ); |
102 | QTest::addColumn<float>(name: "degreesFloat" ); |
103 | QTest::addColumn<double>(name: "radiansDouble" ); |
104 | QTest::addColumn<double>(name: "degreesDouble" ); |
105 | |
106 | QTest::newRow( dataTag: "pi" ) << float(M_PI) << 180.0f << M_PI << 180.0; |
107 | QTest::newRow( dataTag: "doublepi" ) << float(2 * M_PI) << 360.0f << 2 * M_PI << 360.0; |
108 | QTest::newRow( dataTag: "halfpi" ) << float(M_PI_2) << 90.0f << M_PI_2 << 90.0; |
109 | |
110 | QTest::newRow( dataTag: "random" ) << 123.1234567f << 7054.454427971739f << 123.123456789123456789 << 7054.4544330781363896676339209079742431640625; |
111 | QTest::newRow( dataTag: "bigrandom" ) << 987654321.9876543f << 56588424267.74745f << 987654321987654321.987654321987654321 << 56588424267747450880.0; |
112 | |
113 | QTest::newRow( dataTag: "zero" ) << 0.0f << 0.0f << 0.0 << 0.0; |
114 | |
115 | QTest::newRow( dataTag: "minuspi" ) << float(-M_PI) << -180.0f << -M_PI << -180.0; |
116 | QTest::newRow( dataTag: "minusdoublepi" ) << float(-2 * M_PI) << -360.0f << -2 * M_PI << -360.0; |
117 | QTest::newRow( dataTag: "minushalfpi" ) << float(-M_PI_2) << -90.0f << -M_PI_2 << -90.0; |
118 | |
119 | QTest::newRow( dataTag: "minusrandom" ) << -123.1234567f << -7054.454427971739f << -123.123456789123456789 << -7054.4544330781363896676339209079742431640625; |
120 | QTest::newRow( dataTag: "minusbigrandom" ) << -987654321.9876543f << -56588424267.74745f << -987654321987654321.987654321987654321 << -56588424267747450880.0; |
121 | } |
122 | |
123 | void tst_QMath::radiansToDegrees() |
124 | { |
125 | QFETCH(float, radiansFloat); |
126 | QFETCH(float, degreesFloat); |
127 | QFETCH(double, radiansDouble); |
128 | QFETCH(double, degreesDouble); |
129 | |
130 | QCOMPARE(qRadiansToDegrees(radiansFloat), degreesFloat); |
131 | QCOMPARE(qRadiansToDegrees(radiansDouble), degreesDouble); |
132 | } |
133 | |
134 | |
135 | void tst_QMath::qNextPowerOfTwo32S_data() |
136 | { |
137 | QTest::addColumn<qint32>(name: "input" ); |
138 | QTest::addColumn<quint32>(name: "output" ); |
139 | |
140 | QTest::newRow(dataTag: "0" ) << 0 << 1U; |
141 | QTest::newRow(dataTag: "1" ) << 1 << 2U; |
142 | QTest::newRow(dataTag: "2" ) << 2 << 4U; |
143 | QTest::newRow(dataTag: "17" ) << 17 << 32U; |
144 | QTest::newRow(dataTag: "128" ) << 128 << 256U; |
145 | QTest::newRow(dataTag: "65535" ) << 65535 << 65536U; |
146 | QTest::newRow(dataTag: "65536" ) << 65536 << 131072U; |
147 | QTest::newRow(dataTag: "2^30" ) << (1 << 30) << (1U << 31); |
148 | QTest::newRow(dataTag: "2^30 + 1" ) << (1 << 30) + 1 << (1U << 31); |
149 | QTest::newRow(dataTag: "2^31 - 1" ) << 0x7FFFFFFF << (1U<<31); |
150 | QTest::newRow(dataTag: "-1" ) << -1 << 0U; |
151 | QTest::newRow(dataTag: "-128" ) << -128 << 0U; |
152 | QTest::newRow(dataTag: "-(2^31)" ) << int(0x80000000) << 0U; |
153 | } |
154 | |
155 | void tst_QMath::qNextPowerOfTwo32S() |
156 | { |
157 | QFETCH(qint32, input); |
158 | QFETCH(quint32, output); |
159 | |
160 | QCOMPARE(qNextPowerOfTwo(input), output); |
161 | } |
162 | |
163 | void tst_QMath::qNextPowerOfTwo32U_data() |
164 | { |
165 | QTest::addColumn<quint32>(name: "input" ); |
166 | QTest::addColumn<quint32>(name: "output" ); |
167 | |
168 | QTest::newRow(dataTag: "0" ) << 0U << 1U; |
169 | QTest::newRow(dataTag: "1" ) << 1U << 2U; |
170 | QTest::newRow(dataTag: "2" ) << 2U << 4U; |
171 | QTest::newRow(dataTag: "17" ) << 17U << 32U; |
172 | QTest::newRow(dataTag: "128" ) << 128U << 256U; |
173 | QTest::newRow(dataTag: "65535" ) << 65535U << 65536U; |
174 | QTest::newRow(dataTag: "65536" ) << 65536U << 131072U; |
175 | QTest::newRow(dataTag: "2^30" ) << (1U << 30) << (1U << 31); |
176 | QTest::newRow(dataTag: "2^30 + 1" ) << (1U << 30) + 1 << (1U << 31); |
177 | QTest::newRow(dataTag: "2^31 - 1" ) << 2147483647U << 2147483648U; |
178 | QTest::newRow(dataTag: "2^31" ) << 2147483648U << 0U; |
179 | QTest::newRow(dataTag: "2^31 + 1" ) << 2147483649U << 0U; |
180 | } |
181 | |
182 | void tst_QMath::qNextPowerOfTwo32U() |
183 | { |
184 | QFETCH(quint32, input); |
185 | QFETCH(quint32, output); |
186 | |
187 | QCOMPARE(qNextPowerOfTwo(input), output); |
188 | } |
189 | |
190 | void tst_QMath::qNextPowerOfTwo64S_data() |
191 | { |
192 | QTest::addColumn<qint64>(name: "input" ); |
193 | QTest::addColumn<quint64>(name: "output" ); |
194 | |
195 | QTest::newRow(dataTag: "0" ) << Q_INT64_C(0) << Q_UINT64_C(1); |
196 | QTest::newRow(dataTag: "1" ) << Q_INT64_C(1) << Q_UINT64_C(2); |
197 | QTest::newRow(dataTag: "2" ) << Q_INT64_C(2) << Q_UINT64_C(4); |
198 | QTest::newRow(dataTag: "17" ) << Q_INT64_C(17) << Q_UINT64_C(32); |
199 | QTest::newRow(dataTag: "128" ) << Q_INT64_C(128) << Q_UINT64_C(256); |
200 | QTest::newRow(dataTag: "65535" ) << Q_INT64_C(65535) << Q_UINT64_C(65536); |
201 | QTest::newRow(dataTag: "65536" ) << Q_INT64_C(65536) << Q_UINT64_C(131072); |
202 | QTest::newRow(dataTag: "2^31 - 1" ) << Q_INT64_C(2147483647) << Q_UINT64_C(0x80000000); |
203 | QTest::newRow(dataTag: "2^31" ) << Q_INT64_C(2147483648) << Q_UINT64_C(0x100000000); |
204 | QTest::newRow(dataTag: "2^31 + 1" ) << Q_INT64_C(2147483649) << Q_UINT64_C(0x100000000); |
205 | QTest::newRow(dataTag: "2^63 - 1" ) << Q_INT64_C(0x7FFFFFFFFFFFFFFF) << Q_UINT64_C(0x8000000000000000); |
206 | QTest::newRow(dataTag: "-1" ) << Q_INT64_C(-1) << Q_UINT64_C(0); |
207 | QTest::newRow(dataTag: "-128" ) << Q_INT64_C(-128) << Q_UINT64_C(0); |
208 | QTest::newRow(dataTag: "-(2^31)" ) << -Q_INT64_C(0x80000000) << Q_UINT64_C(0); |
209 | QTest::newRow(dataTag: "-(2^63)" ) << (qint64)Q_INT64_C(0x8000000000000000) << Q_UINT64_C(0); |
210 | } |
211 | |
212 | void tst_QMath::qNextPowerOfTwo64S() |
213 | { |
214 | QFETCH(qint64, input); |
215 | QFETCH(quint64, output); |
216 | |
217 | QCOMPARE(qNextPowerOfTwo(input), output); |
218 | } |
219 | |
220 | void tst_QMath::qNextPowerOfTwo64U_data() |
221 | { |
222 | QTest::addColumn<quint64>(name: "input" ); |
223 | QTest::addColumn<quint64>(name: "output" ); |
224 | |
225 | QTest::newRow(dataTag: "0" ) << Q_UINT64_C(0) << Q_UINT64_C(1); |
226 | QTest::newRow(dataTag: "1" ) << Q_UINT64_C(1) << Q_UINT64_C(2); |
227 | QTest::newRow(dataTag: "2" ) << Q_UINT64_C(2) << Q_UINT64_C(4); |
228 | QTest::newRow(dataTag: "17" ) << Q_UINT64_C(17) << Q_UINT64_C(32); |
229 | QTest::newRow(dataTag: "128" ) << Q_UINT64_C(128) << Q_UINT64_C(256); |
230 | QTest::newRow(dataTag: "65535" ) << Q_UINT64_C(65535) << Q_UINT64_C(65536); |
231 | QTest::newRow(dataTag: "65536" ) << Q_UINT64_C(65536) << Q_UINT64_C(131072); |
232 | QTest::newRow(dataTag: "2^63 - 1" ) << Q_UINT64_C(0x7FFFFFFFFFFFFFFF) << Q_UINT64_C(0x8000000000000000); |
233 | QTest::newRow(dataTag: "2^63" ) << Q_UINT64_C(0x8000000000000000) << Q_UINT64_C(0); |
234 | QTest::newRow(dataTag: "2^63 + 1" ) << Q_UINT64_C(0x8000000000000001) << Q_UINT64_C(0); |
235 | } |
236 | |
237 | void tst_QMath::qNextPowerOfTwo64U() |
238 | { |
239 | QFETCH(quint64, input); |
240 | QFETCH(quint64, output); |
241 | |
242 | QCOMPARE(qNextPowerOfTwo(input), output); |
243 | } |
244 | |
245 | QTEST_APPLESS_MAIN(tst_QMath) |
246 | |
247 | #include "tst_qmath.moc" |
248 | |