3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "util/numeric.h"
23 #include "util/string.h"
25 class TestUtilities : public TestBase {
27 TestUtilities() { TestManager::registerTestModule(this); }
28 const char *getName() { return "TestUtilities"; }
30 void runTests(IGameDef *gamedef);
32 void testAngleWrapAround();
36 void testRemoveStringEnd();
41 void testStringTrim();
42 void testStrToIntConversion();
43 void testStringReplace();
44 void testStringAllowed();
47 void testIsPowerOfTwo();
50 static TestUtilities g_test_instance;
52 void TestUtilities::runTests(IGameDef *gamedef)
54 TEST(testAngleWrapAround);
58 TEST(testRemoveStringEnd);
64 TEST(testStrToIntConversion);
65 TEST(testStringReplace);
66 TEST(testStringAllowed);
69 TEST(testIsPowerOfTwo);
72 ////////////////////////////////////////////////////////////////////////////////
74 inline float ref_WrapDegrees180(float f)
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);
85 inline float ref_WrapDegrees_0_360(float f)
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);
92 return value < 0 ? value + 360 : value;
96 void TestUtilities::testAngleWrapAround()
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);
103 for (float f = -720; f <= -360; f += 0.25) {
104 UASSERT(fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
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);
116 void TestUtilities::testLowercase()
118 UASSERT(lowercase("Foo bAR") == "foo bar");
122 void TestUtilities::testTrim()
124 UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
125 UASSERT(trim("\n \t\r \r\n\t\t ") == "");
129 void TestUtilities::testIsYes()
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);
141 void TestUtilities::testRemoveStringEnd()
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) == "");
151 void TestUtilities::testUrlEncode()
153 UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
154 == "%22Aardvarks%20lurk%2C%20OK%3F%22");
158 void TestUtilities::testUrlDecode()
160 UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
161 == "\"Aardvarks lurk, OK?\"");
165 void TestUtilities::testPadString()
167 UASSERT(padStringRight("hello", 8) == "hello ");
171 void TestUtilities::testStrEqual()
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));
178 void TestUtilities::testStringTrim()
180 UASSERT(trim(" a") == "a");
181 UASSERT(trim(" a ") == "a");
182 UASSERT(trim("a ") == "a");
183 UASSERT(trim("") == "");
187 void TestUtilities::testStrToIntConversion()
189 UASSERT(mystoi("123", 0, 1000) == 123);
190 UASSERT(mystoi("123", 0, 10) == 10);
194 void TestUtilities::testStringReplace()
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");
206 void TestUtilities::testStringAllowed()
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);
215 void TestUtilities::testWrapRows()
217 UASSERT(wrap_rows("12345678",4) == "1234\n5678");
221 void TestUtilities::testIsNumber()
223 UASSERT(is_number("123") == true);
224 UASSERT(is_number("") == false);
225 UASSERT(is_number("123a") == false);
229 void TestUtilities::testIsPowerOfTwo()
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);
240 UASSERT(is_power_of_two((u32)-1) == false);