diff --git a/examples/AutomatedTest/main.cpp b/examples/AutomatedTest/main.cpp index 2f807226..6fac6f48 100644 --- a/examples/AutomatedTest/main.cpp +++ b/examples/AutomatedTest/main.cpp @@ -7,7 +7,8 @@ using namespace irr; static IrrlichtDevice *device = nullptr; static int test_fail = 0; -extern void test_irr_array(); +void test_irr_array(); +void test_irr_string(); static video::E_DRIVER_TYPE chooseDriver(const char *arg_) { @@ -32,7 +33,12 @@ static inline void check(bool ok, const char *msg) void run_unit_tests() { std::cout << "Running unit tests:" << std::endl; - test_irr_array(); + try { + test_irr_array(); + test_irr_string(); + } catch (const std::exception &e) { + test_fail++; + } } int main(int argc, char *argv[]) @@ -99,7 +105,7 @@ int main(int argc, char *argv[]) while (device->run()) { - if (device->getTimer()->getTime() >= 1300) + if (device->getTimer()->getTime() >= 1000) { device->getTimer()->setTime(0); ++n; diff --git a/examples/AutomatedTest/test_array.cpp b/examples/AutomatedTest/test_array.cpp index 5ec55ee9..4c207ffe 100644 --- a/examples/AutomatedTest/test_array.cpp +++ b/examples/AutomatedTest/test_array.cpp @@ -1,4 +1,4 @@ -#include "irrArray.h" +#include #include "test_helper.h" using namespace irr; diff --git a/examples/AutomatedTest/test_string.cpp b/examples/AutomatedTest/test_string.cpp new file mode 100644 index 00000000..adf4d63f --- /dev/null +++ b/examples/AutomatedTest/test_string.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include +#include "test_helper.h" + +using namespace irr; +using namespace irr::core; + +#define CMPSTR(a, b) (!strcmp(a, b)) +#define UASSERTSTR(actual, expected) UASSERTCMP(CMPSTR, actual.c_str(), expected) + +static void test_basics() +{ + // ctor + stringc s; + UASSERTEQ(s.c_str()[0], '\0'); + s = stringc(0.1234567); + UASSERTSTR(s, "0.123457"); + s = stringc(static_cast(-102400)); + UASSERTSTR(s, "-102400"); + s = stringc(static_cast(102400)); + UASSERTSTR(s, "102400"); + s = stringc(static_cast(-1024000)); + UASSERTSTR(s, "-1024000"); + s = stringc(static_cast(1024000)); + UASSERTSTR(s, "1024000"); + s = stringc("YESno", 3); + UASSERTSTR(s, "YES"); + s = stringc("Hello World!"); + UASSERTSTR(s, "Hello World!"); + // operator= + s = stringw(L"abcdef"); + UASSERTSTR(s, "abcdef"); + s = stringc("test"); + s = s.c_str(); + UASSERTSTR(s, "test"); + s = s.c_str() + 1; + UASSERTSTR(s, "est"); + s = L"abcdef"; + UASSERTSTR(s, "abcdef"); + s = static_cast(nullptr); + UASSERTSTR(s, ""); + // the rest + s = "f"; + UASSERTEQ(s[0], 'f'); + const auto &sref = s; + UASSERTEQ(sref[0], 'f'); + UASSERT(sref == "f"); + UASSERT(sref == stringc("f")); + s = "a"; + UASSERT(sref < stringc("aa")); + UASSERT(sref < stringc("b")); + UASSERT(stringc("Z") < sref); + UASSERT(!(sref < stringc("a"))); + s = "dog"; + UASSERT(sref != "cat"); + UASSERT(sref != stringc("cat")); +} + +static void test_methods() +{ + stringc s; + const auto &sref = s; + s = "irrlicht"; + UASSERTEQ(sref.size(), 8); + UASSERT(!sref.empty()); + s.clear(); + UASSERTEQ(sref.size(), 0); + UASSERT(sref.empty()); + s = "\tAz#`"; + s.make_lower(); + UASSERTSTR(s, "\taz#`"); + s.make_upper(); + UASSERTSTR(s, "\tAZ#`"); + UASSERT(sref.equals_ignore_case("\taz#`")); + UASSERT(sref.equals_substring_ignore_case("Z#`", 2)); + s = "irrlicht"; + UASSERT(sref.equalsn(stringc("irr"), 3)); + UASSERT(sref.equalsn("irr", 3)); + s = "fo"; + s.append('o'); + UASSERTSTR(s, "foo"); + s.append("bar", 1); + UASSERTSTR(s, "foob"); + s.append("ar", 999999); + UASSERTSTR(s, "foobar"); + s = "nyan"; + s.append(stringc("cat")); + UASSERTSTR(s, "nyancat"); + s.append(stringc("sam"), 1); + UASSERTSTR(s, "nyancats"); + s = "fbar"; + s.insert(1, "ooXX", 2); + UASSERTSTR(s, "foobar"); + UASSERTEQ(sref.findFirst('o'), 1); + UASSERTEQ(sref.findFirst('X'), -1); + UASSERTEQ(sref.findFirstChar("abff", 2), 3); + UASSERTEQ(sref.findFirstCharNotInList("fobb", 2), 3); + UASSERTEQ(sref.findLast('o'), 2); + UASSERTEQ(sref.findLast('X'), -1); + UASSERTEQ(sref.findLastChar("abrr", 2), 4); + UASSERTEQ(sref.findLastCharNotInList("rabb", 2), 3); + UASSERTEQ(sref.findNext('o', 2), 2); + UASSERTEQ(sref.findLast('o', 1), 1); + s = "ob-oob"; + UASSERTEQ(sref.find("ob", 1), 4); + UASSERTEQ(sref.find("ob"), 0); + UASSERTEQ(sref.find("?"), -1); + s = "HOMEOWNER"; + stringc s2 = sref.subString(2, 4); + UASSERTSTR(s2, "MEOW"); + s2 = sref.subString(2, 4, true); + UASSERTSTR(s2, "meow"); + s = "land"; + s.replace('l', 's'); + UASSERTSTR(s, "sand"); + s = ">dog<"; + s.replace("dog", "cat"); + UASSERTSTR(s, ">cat<"); + s.replace("cat", "horse"); + UASSERTSTR(s, ">horse<"); + s.replace("horse", "gnu"); + UASSERTSTR(s, ">gnu<"); + s = " h e l p "; + s.remove(' '); + UASSERTSTR(s, "help"); + s.remove("el"); + UASSERTSTR(s, "hp"); + s = "irrlicht"; + s.removeChars("it"); + UASSERTSTR(s, "rrlch"); + s = "\r\nfoo bar "; + s.trim(); + UASSERTSTR(s, "foo bar"); + s = "foxo"; + s.erase(2); + UASSERTSTR(s, "foo"); + s = "a"; + s.append('\0'); + s.append('b'); + UASSERTEQ(s.size(), 3); + s.validate(); + UASSERTEQ(s.size(), 1); + UASSERTEQ(s.lastChar(), 'a'); + std::vector res; + s = "a,,b,c"; + s.split(res, ",aa", 1, true, false); + UASSERTEQ(res.size(), 3); + UASSERTSTR(res[0], "a"); + UASSERTSTR(res[2], "c"); + res.clear(); + s.split(res, ",", 1, false, true); + UASSERTEQ(res.size(), 7); + UASSERTSTR(res[0], "a"); + UASSERTSTR(res[2], ""); + for (int i = 0; i < 3; i++) + UASSERTSTR(res[2*i+1], ","); +} + +static void test_conv() +{ + // assumes Unicode and UTF-8 locale + setlocale(LC_CTYPE, ""); + + stringw out; + multibyteToWString(out, "†††"); + UASSERTEQ(out.size(), 3); + for (int i = 0; i < 3; i++) + UASSERTEQ(static_cast(out[i]), 0x2020); + stringc out2; + wStringToMultibyte(out2, L"†††"); + UASSERTEQ(out2.size(), 9); + for (int i = 0; i < 3; i++) { + UASSERTEQ(static_cast(out2[3*i]), 0xe2); + UASSERTEQ(static_cast(out2[3*i+1]), 0x80); + UASSERTEQ(static_cast(out2[3*i+2]), 0xa0); + } +} + +void test_irr_string() +{ + test_basics(); + test_methods(); + test_conv(); + std::cout << " test_irr_string PASSED" << std::endl; +}