]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_utilities.cpp
Fix several MSVC issues numeric.h
[dragonfireclient.git] / src / unittest / test_utilities.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21
22 #include "util/numeric.h"
23 #include "util/string.h"
24
25 class TestUtilities : public TestBase {
26 public:
27         TestUtilities() { TestManager::registerTestModule(this); }
28         const char *getName() { return "TestUtilities"; }
29
30         void runTests(IGameDef *gamedef);
31
32         void testAngleWrapAround();
33         void testLowercase();
34         void testTrim();
35         void testIsYes();
36         void testRemoveStringEnd();
37         void testUrlEncode();
38         void testUrlDecode();
39         void testPadString();
40         void testStrEqual();
41         void testStringTrim();
42         void testStrToIntConversion();
43         void testStringReplace();
44         void testStringAllowed();
45         void testWrapRows();
46         void testIsNumber();
47         void testIsPowerOfTwo();
48         void testMyround();
49 };
50
51 static TestUtilities g_test_instance;
52
53 void TestUtilities::runTests(IGameDef *gamedef)
54 {
55         TEST(testAngleWrapAround);
56         TEST(testLowercase);
57         TEST(testTrim);
58         TEST(testIsYes);
59         TEST(testRemoveStringEnd);
60         TEST(testUrlEncode);
61         TEST(testUrlDecode);
62         TEST(testPadString);
63         TEST(testStrEqual);
64         TEST(testStringTrim);
65         TEST(testStrToIntConversion);
66         TEST(testStringReplace);
67         TEST(testStringAllowed);
68         TEST(testWrapRows);
69         TEST(testIsNumber);
70         TEST(testIsPowerOfTwo);
71         TEST(testMyround);
72 }
73
74 ////////////////////////////////////////////////////////////////////////////////
75
76 inline float ref_WrapDegrees180(float f)
77 {
78         // This is a slower alternative to the wrapDegrees_180() function;
79         // used as a reference for testing
80         float value = fmodf(f + 180, 360);
81         if (value < 0)
82                 value += 360;
83         return value - 180;
84 }
85
86
87 inline float ref_WrapDegrees_0_360(float f)
88 {
89         // This is a slower alternative to the wrapDegrees_0_360() function;
90         // used as a reference for testing
91         float value = fmodf(f, 360);
92         if (value < 0)
93                 value += 360;
94         return value < 0 ? value + 360 : value;
95 }
96
97
98 void TestUtilities::testAngleWrapAround()
99 {
100         UASSERT(fabs(modulo360f(100.0) - 100.0) < 0.001);
101         UASSERT(fabs(modulo360f(720.5) - 0.5) < 0.001);
102         UASSERT(fabs(modulo360f(-0.5) - (-0.5)) < 0.001);
103         UASSERT(fabs(modulo360f(-365.5) - (-5.5)) < 0.001);
104
105         for (float f = -720; f <= -360; f += 0.25) {
106                 UASSERT(fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
107         }
108
109         for (float f = -1440; f <= 1440; f += 0.25) {
110                 UASSERT(fabs(modulo360f(f) - fmodf(f, 360)) < 0.001);
111                 UASSERT(fabs(wrapDegrees_180(f) - ref_WrapDegrees180(f)) < 0.001);
112                 UASSERT(fabs(wrapDegrees_0_360(f) - ref_WrapDegrees_0_360(f)) < 0.001);
113                 UASSERT(wrapDegrees_0_360(fabs(wrapDegrees_180(f) - wrapDegrees_0_360(f))) < 0.001);
114         }
115 }
116
117
118 void TestUtilities::testLowercase()
119 {
120         UASSERT(lowercase("Foo bAR") == "foo bar");
121 }
122
123
124 void TestUtilities::testTrim()
125 {
126         UASSERT(trim("\n \t\r  Foo bAR  \r\n\t\t  ") == "Foo bAR");
127         UASSERT(trim("\n \t\r    \r\n\t\t  ") == "");
128 }
129
130
131 void TestUtilities::testIsYes()
132 {
133         UASSERT(is_yes("YeS") == true);
134         UASSERT(is_yes("") == false);
135         UASSERT(is_yes("FAlse") == false);
136         UASSERT(is_yes("-1") == true);
137         UASSERT(is_yes("0") == false);
138         UASSERT(is_yes("1") == true);
139         UASSERT(is_yes("2") == true);
140 }
141
142
143 void TestUtilities::testRemoveStringEnd()
144 {
145         const char *ends[] = {"abc", "c", "bc", "", NULL};
146         UASSERT(removeStringEnd("abc", ends) == "");
147         UASSERT(removeStringEnd("bc", ends) == "b");
148         UASSERT(removeStringEnd("12c", ends) == "12");
149         UASSERT(removeStringEnd("foo", ends) == "");
150 }
151
152
153 void TestUtilities::testUrlEncode()
154 {
155         UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
156                         == "%22Aardvarks%20lurk%2C%20OK%3F%22");
157 }
158
159
160 void TestUtilities::testUrlDecode()
161 {
162         UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
163                         == "\"Aardvarks lurk, OK?\"");
164 }
165
166
167 void TestUtilities::testPadString()
168 {
169         UASSERT(padStringRight("hello", 8) == "hello   ");
170 }
171
172
173 void TestUtilities::testStrEqual()
174 {
175         UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc")));
176         UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true));
177 }
178
179
180 void TestUtilities::testStringTrim()
181 {
182         UASSERT(trim("  a") == "a");
183         UASSERT(trim("   a  ") == "a");
184         UASSERT(trim("a   ") == "a");
185         UASSERT(trim("") == "");
186 }
187
188
189 void TestUtilities::testStrToIntConversion()
190 {
191         UASSERT(mystoi("123", 0, 1000) == 123);
192         UASSERT(mystoi("123", 0, 10) == 10);
193 }
194
195
196 void TestUtilities::testStringReplace()
197 {
198         std::string test_str;
199         test_str = "Hello there";
200         str_replace(test_str, "there", "world");
201         UASSERT(test_str == "Hello world");
202         test_str = "ThisAisAaAtest";
203         str_replace(test_str, 'A', ' ');
204         UASSERT(test_str == "This is a test");
205 }
206
207
208 void TestUtilities::testStringAllowed()
209 {
210         UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
211         UASSERT(string_allowed("123", "abcdefghijklmno") == false);
212         UASSERT(string_allowed_blacklist("hello", "123") == true);
213         UASSERT(string_allowed_blacklist("hello123", "123") == false);
214 }
215
216
217 void TestUtilities::testWrapRows()
218 {
219         UASSERT(wrap_rows("12345678",4) == "1234\n5678");
220 }
221
222
223 void TestUtilities::testIsNumber()
224 {
225         UASSERT(is_number("123") == true);
226         UASSERT(is_number("") == false);
227         UASSERT(is_number("123a") == false);
228 }
229
230
231 void TestUtilities::testIsPowerOfTwo()
232 {
233         UASSERT(is_power_of_two(0) == false);
234         UASSERT(is_power_of_two(1) == true);
235         UASSERT(is_power_of_two(2) == true);
236         UASSERT(is_power_of_two(3) == false);
237         for (int exponent = 2; exponent <= 31; ++exponent) {
238                 UASSERT(is_power_of_two((1 << exponent) - 1) == false);
239                 UASSERT(is_power_of_two((1 << exponent)) == true);
240                 UASSERT(is_power_of_two((1 << exponent) + 1) == false);
241         }
242         UASSERT(is_power_of_two((u32)-1) == false);
243 }
244
245 void TestUtilities::testMyround()
246 {
247         UASSERT(myround(4.6f) == 5);
248         UASSERT(myround(1.2f) == 1);
249         UASSERT(myround(-3.1f) == -3);
250         UASSERT(myround(-6.5f) == -7);
251 }
252