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();
51 static TestUtilities g_test_instance;
53 void TestUtilities::runTests(IGameDef *gamedef)
55 TEST(testAngleWrapAround);
59 TEST(testRemoveStringEnd);
65 TEST(testStrToIntConversion);
66 TEST(testStringReplace);
67 TEST(testStringAllowed);
70 TEST(testIsPowerOfTwo);
74 ////////////////////////////////////////////////////////////////////////////////
76 inline float ref_WrapDegrees180(float f)
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);
87 inline float ref_WrapDegrees_0_360(float f)
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);
94 return value < 0 ? value + 360 : value;
98 void TestUtilities::testAngleWrapAround()
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);
105 for (float f = -720; f <= -360; f += 0.25) {
106 UASSERT(fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
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);
118 void TestUtilities::testLowercase()
120 UASSERT(lowercase("Foo bAR") == "foo bar");
124 void TestUtilities::testTrim()
126 UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
127 UASSERT(trim("\n \t\r \r\n\t\t ") == "");
131 void TestUtilities::testIsYes()
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);
143 void TestUtilities::testRemoveStringEnd()
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) == "");
153 void TestUtilities::testUrlEncode()
155 UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
156 == "%22Aardvarks%20lurk%2C%20OK%3F%22");
160 void TestUtilities::testUrlDecode()
162 UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
163 == "\"Aardvarks lurk, OK?\"");
167 void TestUtilities::testPadString()
169 UASSERT(padStringRight("hello", 8) == "hello ");
173 void TestUtilities::testStrEqual()
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));
180 void TestUtilities::testStringTrim()
182 UASSERT(trim(" a") == "a");
183 UASSERT(trim(" a ") == "a");
184 UASSERT(trim("a ") == "a");
185 UASSERT(trim("") == "");
189 void TestUtilities::testStrToIntConversion()
191 UASSERT(mystoi("123", 0, 1000) == 123);
192 UASSERT(mystoi("123", 0, 10) == 10);
196 void TestUtilities::testStringReplace()
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");
208 void TestUtilities::testStringAllowed()
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);
217 void TestUtilities::testWrapRows()
219 UASSERT(wrap_rows("12345678",4) == "1234\n5678");
223 void TestUtilities::testIsNumber()
225 UASSERT(is_number("123") == true);
226 UASSERT(is_number("") == false);
227 UASSERT(is_number("123a") == false);
231 void TestUtilities::testIsPowerOfTwo()
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);
242 UASSERT(is_power_of_two((u32)-1) == false);
245 void TestUtilities::testMyround()
247 UASSERT(myround(4.6f) == 5);
248 UASSERT(myround(1.2f) == 1);
249 UASSERT(myround(-3.1f) == -3);
250 UASSERT(myround(-6.5f) == -7);