aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_utilities.cpp
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2015-04-26 01:24:19 -0400
committerkwolekr <kwolekr@minetest.net>2015-04-26 15:08:54 -0400
commit7220ca906dfc804bd508336f5bed252bcec62ae0 (patch)
tree6a61f536227510a57b023019271084450042a7ad /src/unittest/test_utilities.cpp
parent45a77c8bf1a744edc1642d717579281cf988f8dd (diff)
downloadminetest-7220ca906dfc804bd508336f5bed252bcec62ae0.tar.gz
minetest-7220ca906dfc804bd508336f5bed252bcec62ae0.tar.bz2
minetest-7220ca906dfc804bd508336f5bed252bcec62ae0.zip
Tests: Modularize unit testing
Split unit tests into separate files under src/unittest/ Give better unittest diagnostics Clean up some code
Diffstat (limited to 'src/unittest/test_utilities.cpp')
-rw-r--r--src/unittest/test_utilities.cpp241
1 files changed, 241 insertions, 0 deletions
diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp
new file mode 100644
index 000000000..caf4515c8
--- /dev/null
+++ b/src/unittest/test_utilities.cpp
@@ -0,0 +1,241 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "test.h"
+
+#include "util/numeric.h"
+#include "util/string.h"
+
+class TestUtilities : public TestBase {
+public:
+ TestUtilities() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestUtilities"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testAngleWrapAround();
+ void testLowercase();
+ void testTrim();
+ void testIsYes();
+ void testRemoveStringEnd();
+ void testUrlEncode();
+ void testUrlDecode();
+ void testPadString();
+ void testStrEqual();
+ void testStringTrim();
+ void testStrToIntConversion();
+ void testStringReplace();
+ void testStringAllowed();
+ void testWrapRows();
+ void testIsNumber();
+ void testIsPowerOfTwo();
+};
+
+static TestUtilities g_test_instance;
+
+void TestUtilities::runTests(IGameDef *gamedef)
+{
+ TEST(testAngleWrapAround);
+ TEST(testLowercase);
+ TEST(testTrim);
+ TEST(testIsYes);
+ TEST(testRemoveStringEnd);
+ TEST(testUrlEncode);
+ TEST(testUrlDecode);
+ TEST(testPadString);
+ TEST(testStrEqual);
+ TEST(testStringTrim);
+ TEST(testStrToIntConversion);
+ TEST(testStringReplace);
+ TEST(testStringAllowed);
+ TEST(testWrapRows);
+ TEST(testIsNumber);
+ TEST(testIsPowerOfTwo);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+inline float ref_WrapDegrees180(float f)
+{
+ // This is a slower alternative to the wrapDegrees_180() function;
+ // used as a reference for testing
+ float value = fmodf(f + 180, 360);
+ if (value < 0)
+ value += 360;
+ return value - 180;
+}
+
+
+inline float ref_WrapDegrees_0_360(float f)
+{
+ // This is a slower alternative to the wrapDegrees_0_360() function;
+ // used as a reference for testing
+ float value = fmodf(f, 360);
+ if (value < 0)
+ value += 360;
+ return value < 0 ? value + 360 : value;
+}
+
+
+void TestUtilities::testAngleWrapAround()
+{
+ UASSERT(fabs(modulo360f(100.0) - 100.0) < 0.001);
+ UASSERT(fabs(modulo360f(720.5) - 0.5) < 0.001);
+ UASSERT(fabs(modulo360f(-0.5) - (-0.5)) < 0.001);
+ UASSERT(fabs(modulo360f(-365.5) - (-5.5)) < 0.001);
+
+ for (float f = -720; f <= -360; f += 0.25) {
+ UASSERT(fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
+ }
+
+ for (float f = -1440; f <= 1440; f += 0.25) {
+ UASSERT(fabs(modulo360f(f) - fmodf(f, 360)) < 0.001);
+ UASSERT(fabs(wrapDegrees_180(f) - ref_WrapDegrees180(f)) < 0.001);
+ UASSERT(fabs(wrapDegrees_0_360(f) - ref_WrapDegrees_0_360(f)) < 0.001);
+ UASSERT(wrapDegrees_0_360(fabs(wrapDegrees_180(f) - wrapDegrees_0_360(f))) < 0.001);
+ }
+}
+
+
+void TestUtilities::testLowercase()
+{
+ UASSERT(lowercase("Foo bAR") == "foo bar");
+}
+
+
+void TestUtilities::testTrim()
+{
+ UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
+ UASSERT(trim("\n \t\r \r\n\t\t ") == "");
+}
+
+
+void TestUtilities::testIsYes()
+{
+ UASSERT(is_yes("YeS") == true);
+ UASSERT(is_yes("") == false);
+ UASSERT(is_yes("FAlse") == false);
+ UASSERT(is_yes("-1") == true);
+ UASSERT(is_yes("0") == false);
+ UASSERT(is_yes("1") == true);
+ UASSERT(is_yes("2") == true);
+}
+
+
+void TestUtilities::testRemoveStringEnd()
+{
+ const char *ends[] = {"abc", "c", "bc", "", NULL};
+ UASSERT(removeStringEnd("abc", ends) == "");
+ UASSERT(removeStringEnd("bc", ends) == "b");
+ UASSERT(removeStringEnd("12c", ends) == "12");
+ UASSERT(removeStringEnd("foo", ends) == "");
+}
+
+
+void TestUtilities::testUrlEncode()
+{
+ UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
+ == "%22Aardvarks%20lurk%2C%20OK%3F%22");
+}
+
+
+void TestUtilities::testUrlDecode()
+{
+ UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
+ == "\"Aardvarks lurk, OK?\"");
+}
+
+
+void TestUtilities::testPadString()
+{
+ UASSERT(padStringRight("hello", 8) == "hello ");
+}
+
+
+void TestUtilities::testStrEqual()
+{
+ UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc")));
+ UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true));
+}
+
+
+void TestUtilities::testStringTrim()
+{
+ UASSERT(trim(" a") == "a");
+ UASSERT(trim(" a ") == "a");
+ UASSERT(trim("a ") == "a");
+ UASSERT(trim("") == "");
+}
+
+
+void TestUtilities::testStrToIntConversion()
+{
+ UASSERT(mystoi("123", 0, 1000) == 123);
+ UASSERT(mystoi("123", 0, 10) == 10);
+}
+
+
+void TestUtilities::testStringReplace()
+{
+ std::string test_str;
+ test_str = "Hello there";
+ str_replace(test_str, "there", "world");
+ UASSERT(test_str == "Hello world");
+ test_str = "ThisAisAaAtest";
+ str_replace(test_str, 'A', ' ');
+ UASSERT(test_str == "This is a test");
+}
+
+
+void TestUtilities::testStringAllowed()
+{
+ UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
+ UASSERT(string_allowed("123", "abcdefghijklmno") == false);
+ UASSERT(string_allowed_blacklist("hello", "123") == true);
+ UASSERT(string_allowed_blacklist("hello123", "123") == false);
+}
+
+
+void TestUtilities::testWrapRows()
+{
+ UASSERT(wrap_rows("12345678",4) == "1234\n5678");
+}
+
+
+void TestUtilities::testIsNumber()
+{
+ UASSERT(is_number("123") == true);
+ UASSERT(is_number("") == false);
+ UASSERT(is_number("123a") == false);
+}
+
+
+void TestUtilities::testIsPowerOfTwo()
+{
+ UASSERT(is_power_of_two(0) == false);
+ UASSERT(is_power_of_two(1) == true);
+ UASSERT(is_power_of_two(2) == true);
+ UASSERT(is_power_of_two(3) == false);
+ for (int exponent = 2; exponent <= 31; ++exponent) {
+ UASSERT(is_power_of_two((1 << exponent) - 1) == false);
+ UASSERT(is_power_of_two((1 << exponent)) == true);
+ UASSERT(is_power_of_two((1 << exponent) + 1) == false);
+ }
+ UASSERT(is_power_of_two((u32)-1) == false);
+}