]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/unittest/test_utilities.cpp
test_map_settings_manager: Fix Wunused-result warning
[dragonfireclient.git] / src / unittest / test_utilities.cpp
index 9678a81eb18acedb0b7c5b2b8150b57d8b022f3d..d73975b9f84f11076932388f9a7372bc3e6ea4e6 100644 (file)
@@ -43,7 +43,9 @@ class TestUtilities : public TestBase {
        void testStrToIntConversion();
        void testStringReplace();
        void testStringAllowed();
+       void testAsciiPrintableHelper();
        void testUTF8();
+       void testRemoveEscapes();
        void testWrapRows();
        void testIsNumber();
        void testIsPowerOfTwo();
@@ -68,7 +70,9 @@ void TestUtilities::runTests(IGameDef *gamedef)
        TEST(testStrToIntConversion);
        TEST(testStringReplace);
        TEST(testStringAllowed);
+       TEST(testAsciiPrintableHelper);
        TEST(testUTF8);
+       TEST(testRemoveEscapes);
        TEST(testWrapRows);
        TEST(testIsNumber);
        TEST(testIsPowerOfTwo);
@@ -232,6 +236,18 @@ void TestUtilities::testStringAllowed()
        UASSERT(string_allowed_blacklist("hello123", "123") == false);
 }
 
+void TestUtilities::testAsciiPrintableHelper()
+{
+       UASSERT(IS_ASCII_PRINTABLE_CHAR('e') == true);
+       UASSERT(IS_ASCII_PRINTABLE_CHAR('\0') == false);
+
+       // Ensures that there is no cutting off going on...
+       // If there were, 331 would be cut to 75 in this example
+       // and 73 is a valid ASCII char.
+       int ch = 331;
+       UASSERT(IS_ASCII_PRINTABLE_CHAR(ch) == false);
+}
+
 void TestUtilities::testUTF8()
 {
        UASSERT(wide_to_utf8(utf8_to_wide("")) == "");
@@ -239,17 +255,44 @@ void TestUtilities::testUTF8()
                == "the shovel dug a crumbly node!");
 }
 
+void TestUtilities::testRemoveEscapes()
+{
+       UASSERT(unescape_enriched<wchar_t>(
+               L"abc\x1bXdef") == L"abcdef");
+       UASSERT(unescape_enriched<wchar_t>(
+               L"abc\x1b(escaped)def") == L"abcdef");
+       UASSERT(unescape_enriched<wchar_t>(
+               L"abc\x1b((escaped with parenthesis\\))def") == L"abcdef");
+       UASSERT(unescape_enriched<wchar_t>(
+               L"abc\x1b(incomplete") == L"abc");
+       UASSERT(unescape_enriched<wchar_t>(
+               L"escape at the end\x1b") == L"escape at the end");
+       // Nested escapes not supported
+       UASSERT(unescape_enriched<wchar_t>(
+               L"abc\x1b(outer \x1b(inner escape)escape)def") == L"abcescape)def");
+}
+
 void TestUtilities::testWrapRows()
 {
        UASSERT(wrap_rows("12345678",4) == "1234\n5678");
        // test that wrap_rows doesn't wrap inside multibyte sequences
-       const unsigned char s[] = {
-               0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x72, 0x61, 0x70, 0x74, 0x6f,
-               0x72, 0x2f, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x2f,
-               0x6d, 0x69, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x69,
-               0x6e, 0x2f, 0x2e, 0x2e, 0};
-       std::string str((char *)s);
-       UASSERT(utf8_to_wide(wrap_rows(str, 20)) != L"<invalid UTF-8 string>");
+       {
+               const unsigned char s[] = {
+                       0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x72, 0x61, 0x70, 0x74, 0x6f,
+                       0x72, 0x2f, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x2f,
+                       0x6d, 0x69, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x69,
+                       0x6e, 0x2f, 0x2e, 0x2e, 0};
+               std::string str((char *)s);
+               UASSERT(utf8_to_wide(wrap_rows(str, 20)) != L"<invalid UTF-8 string>");
+       };
+       {
+               const unsigned char s[] = {
+                       0x74, 0x65, 0x73, 0x74, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81,
+                       0xd1, 0x82, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82,
+                       0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0};
+               std::string str((char *)s);
+               UASSERT(utf8_to_wide(wrap_rows(str, 8)) != L"<invalid UTF-8 string>");
+       }
 }
 
 
@@ -272,7 +315,7 @@ void TestUtilities::testIsPowerOfTwo()
                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);
+       UASSERT(is_power_of_two(U32_MAX) == false);
 }
 
 void TestUtilities::testMyround()