]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_utilities.cpp
Tests: Modularize unit testing
[minetest.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 };
49
50 static TestUtilities g_test_instance;
51
52 void TestUtilities::runTests(IGameDef *gamedef)
53 {
54         TEST(testAngleWrapAround);
55         TEST(testLowercase);
56         TEST(testTrim);
57         TEST(testIsYes);
58         TEST(testRemoveStringEnd);
59         TEST(testUrlEncode);
60         TEST(testUrlDecode);
61         TEST(testPadString);
62         TEST(testStrEqual);
63         TEST(testStringTrim);
64         TEST(testStrToIntConversion);
65         TEST(testStringReplace);
66         TEST(testStringAllowed);
67         TEST(testWrapRows);
68         TEST(testIsNumber);
69         TEST(testIsPowerOfTwo);
70 }
71
72 ////////////////////////////////////////////////////////////////////////////////
73
74 inline float ref_WrapDegrees180(float f)
75 {
76         // This is a slower alternative to the wrapDegrees_180() function;
77         // used as a reference for testing
78         float value = fmodf(f + 180, 360);
79         if (value < 0)
80                 value += 360;
81         return value - 180;
82 }
83
84
85 inline float ref_WrapDegrees_0_360(float f)
86 {
87         // This is a slower alternative to the wrapDegrees_0_360() function;
88         // used as a reference for testing
89         float value = fmodf(f, 360);
90         if (value < 0)
91                 value += 360;
92         return value < 0 ? value + 360 : value;
93 }
94
95
96 void TestUtilities::testAngleWrapAround()
97 {
98         UASSERT(fabs(modulo360f(100.0) - 100.0) < 0.001);
99         UASSERT(fabs(modulo360f(720.5) - 0.5) < 0.001);
100         UASSERT(fabs(modulo360f(-0.5) - (-0.5)) < 0.001);
101         UASSERT(fabs(modulo360f(-365.5) - (-5.5)) < 0.001);
102
103         for (float f = -720; f <= -360; f += 0.25) {
104                 UASSERT(fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
105         }
106
107         for (float f = -1440; f <= 1440; f += 0.25) {
108                 UASSERT(fabs(modulo360f(f) - fmodf(f, 360)) < 0.001);
109                 UASSERT(fabs(wrapDegrees_180(f) - ref_WrapDegrees180(f)) < 0.001);
110                 UASSERT(fabs(wrapDegrees_0_360(f) - ref_WrapDegrees_0_360(f)) < 0.001);
111                 UASSERT(wrapDegrees_0_360(fabs(wrapDegrees_180(f) - wrapDegrees_0_360(f))) < 0.001);
112         }
113 }
114
115
116 void TestUtilities::testLowercase()
117 {
118         UASSERT(lowercase("Foo bAR") == "foo bar");
119 }
120
121
122 void TestUtilities::testTrim()
123 {
124         UASSERT(trim("\n \t\r  Foo bAR  \r\n\t\t  ") == "Foo bAR");
125         UASSERT(trim("\n \t\r    \r\n\t\t  ") == "");
126 }
127
128
129 void TestUtilities::testIsYes()
130 {
131         UASSERT(is_yes("YeS") == true);
132         UASSERT(is_yes("") == false);
133         UASSERT(is_yes("FAlse") == false);
134         UASSERT(is_yes("-1") == true);
135         UASSERT(is_yes("0") == false);
136         UASSERT(is_yes("1") == true);
137         UASSERT(is_yes("2") == true);
138 }
139
140
141 void TestUtilities::testRemoveStringEnd()
142 {
143         const char *ends[] = {"abc", "c", "bc", "", NULL};
144         UASSERT(removeStringEnd("abc", ends) == "");
145         UASSERT(removeStringEnd("bc", ends) == "b");
146         UASSERT(removeStringEnd("12c", ends) == "12");
147         UASSERT(removeStringEnd("foo", ends) == "");
148 }
149
150
151 void TestUtilities::testUrlEncode()
152 {
153         UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
154                         == "%22Aardvarks%20lurk%2C%20OK%3F%22");
155 }
156
157
158 void TestUtilities::testUrlDecode()
159 {
160         UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
161                         == "\"Aardvarks lurk, OK?\"");
162 }
163
164
165 void TestUtilities::testPadString()
166 {
167         UASSERT(padStringRight("hello", 8) == "hello   ");
168 }
169
170
171 void TestUtilities::testStrEqual()
172 {
173         UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc")));
174         UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true));
175 }
176
177
178 void TestUtilities::testStringTrim()
179 {
180         UASSERT(trim("  a") == "a");
181         UASSERT(trim("   a  ") == "a");
182         UASSERT(trim("a   ") == "a");
183         UASSERT(trim("") == "");
184 }
185
186
187 void TestUtilities::testStrToIntConversion()
188 {
189         UASSERT(mystoi("123", 0, 1000) == 123);
190         UASSERT(mystoi("123", 0, 10) == 10);
191 }
192
193
194 void TestUtilities::testStringReplace()
195 {
196         std::string test_str;
197         test_str = "Hello there";
198         str_replace(test_str, "there", "world");
199         UASSERT(test_str == "Hello world");
200         test_str = "ThisAisAaAtest";
201         str_replace(test_str, 'A', ' ');
202         UASSERT(test_str == "This is a test");
203 }
204
205
206 void TestUtilities::testStringAllowed()
207 {
208         UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
209         UASSERT(string_allowed("123", "abcdefghijklmno") == false);
210         UASSERT(string_allowed_blacklist("hello", "123") == true);
211         UASSERT(string_allowed_blacklist("hello123", "123") == false);
212 }
213
214
215 void TestUtilities::testWrapRows()
216 {
217         UASSERT(wrap_rows("12345678",4) == "1234\n5678");
218 }
219
220
221 void TestUtilities::testIsNumber()
222 {
223         UASSERT(is_number("123") == true);
224         UASSERT(is_number("") == false);
225         UASSERT(is_number("123a") == false);
226 }
227
228
229 void TestUtilities::testIsPowerOfTwo()
230 {
231         UASSERT(is_power_of_two(0) == false);
232         UASSERT(is_power_of_two(1) == true);
233         UASSERT(is_power_of_two(2) == true);
234         UASSERT(is_power_of_two(3) == false);
235         for (int exponent = 2; exponent <= 31; ++exponent) {
236                 UASSERT(is_power_of_two((1 << exponent) - 1) == false);
237                 UASSERT(is_power_of_two((1 << exponent)) == true);
238                 UASSERT(is_power_of_two((1 << exponent) + 1) == false);
239         }
240         UASSERT(is_power_of_two((u32)-1) == false);
241 }