#include "tests/StringTests.h" #include "test/Test.h" #include "utils/String.h" // #include "data/HashMap.h" static void testEquality(Core::Test& test) { Core::String s("test"); test.checkTrue(s == "test", "equality with c-string"); test.checkTrue(s == Core::String("test"), "equality with another string"); test.checkTrue("test" == s, "inverse equality with c-string"); test.checkTrue(Core::String("test") == s, "inverse equality with another string"); test.checkTrue(s == s, "equality with itself"); } static void testInequality(Core::Test& test) { Core::String s("test"); test.checkFalse(s != "test", "inequality with c-string"); test.checkFalse(s != Core::String("test"), "inequality with another string"); test.checkFalse("test" != s, "inverse inequality with c-string"); test.checkFalse(Core::String("test") != s, "inverse inequality with another string"); test.checkFalse(s != s, "inequality with itself"); } static void testStringAppend(Core::Test& test) { Core::String s("test"); s.append("22").append("333").append("4444"); test.checkEqual(Core::String("test223334444"), s, "multiple appends"); } static void testCharacters(Core::Test& test) { Core::String s("test"); test.checkEqual('t', s[0], "character read 1"); test.checkEqual('e', s[1], "character read 2"); test.checkEqual('s', s[2], "character read 3"); test.checkEqual('t', s[3], "character read 4"); } static void testLength(Core::Test& test) { Core::String s("test"); test.checkEqual(4, s.getLength(), "length 1"); s.append("aaa"); test.checkEqual(7, s.getLength(), "length 2"); } static void testChar(Core::Test& test) { Core::String s("test"); for(char i = 'a'; i < 'd'; i++) { s.append(i); } test.checkEqual(Core::String("testabc"), s, "char append"); } static void testSignedChar(Core::Test& test) { Core::String s("test"); for(signed char i = 'b'; i < 'e'; i++) { s.append(i); } test.checkEqual(Core::String("testbcd"), s, "signed char append"); } static void testUnsignedChar(Core::Test& test) { Core::String s("test"); for(unsigned char i = 'c'; i < 'f'; i++) { s.append(i); } test.checkEqual(Core::String("testcde"), s, "unsigned char append"); } static void testSignedShort(Core::Test& test) { Core::String s("test"); for(signed short i = 100; i < 103; i++) { s.append(i); } test.checkEqual(Core::String("test100101102"), s, "signed short append"); } static void testUnsignedShort(Core::Test& test) { Core::String s("test"); for(unsigned short i = 101; i < 104; i++) { s.append(i); } test.checkEqual(Core::String("test101102103"), s, "unsigned short append"); } static void testSignedInt(Core::Test& test) { Core::String s("test"); for(signed int i = 102; i < 105; i++) { s.append(i); } test.checkEqual(Core::String("test102103104"), s, "signed int append"); } static void testUnsignedInt(Core::Test& test) { Core::String s("test"); for(unsigned int i = 103; i < 106; i++) { s.append(i); } test.checkEqual(Core::String("test103104105"), s, "unsigned int append"); } static void testSignedLong(Core::Test& test) { Core::String s("test"); for(signed long i = 104; i < 107; i++) { s.append(i); } test.checkEqual(Core::String("test104105106"), s, "signed long append"); } static void testUnsignedLong(Core::Test& test) { Core::String s("test"); for(unsigned long i = 105; i < 108; i++) { s.append(i); } test.checkEqual(Core::String("test105106107"), s, "unsigned long append"); } static void testSignedLongLong(Core::Test& test) { Core::String s("test"); for(signed long long i = 106; i < 109; i++) { s.append(i); } test.checkEqual(Core::String("test106107108"), s, "signed long long append"); } static void testUnsignedLongLong(Core::Test& test) { Core::String s("test"); for(unsigned long long i = 107; i < 110; i++) { s.append(i); } test.checkEqual(Core::String("test107108109"), s, "unsigned long long append"); } static void testFloat(Core::Test& test) { Core::String s("test"); for(float i = 108; i < 111; i++) { s.append(i); } test.checkEqual(Core::String("test108.00109.00110.00"), s, "float append"); } static void testDouble(Core::Test& test) { Core::String s("test"); for(double i = 109; i < 112; i++) { s.append(i); } test.checkEqual(Core::String("test109.00110.00111.00"), s, "double append"); } static void testLongDouble(Core::Test& test) { Core::String s("test"); for(long double i = 110; i < 113; i++) { s.append(i); } test.checkEqual(Core::String("test110.00111.00112.00"), s, "long double append"); } static void testBool(Core::Test& test) { Core::String s("test"); s.append(true).append(false).append(true); test.checkEqual(Core::String("testtruefalsetrue"), s, "bool append"); } static void testUnicode(Core::Test& test) { Core::String s; s.appendUnicode('\u0040'); s.appendUnicode(L'\u0400'); s.appendUnicode(L'\u8000'); s.appendUnicode(U'\U00100000'); test.checkEqual(Core::String("\u0040\u0400\u8000\U00100000"), s, "unicode append"); } static void testClear(Core::Test& test) { Core::String s("test"); s.append(1234).clear().append("wusi").append("1234"); test.checkEqual(Core::String("wusi1234"), s, "clear"); } static void testHashCode(Core::Test& test) { Core::String s; s.append("a").append("bc").append(20).append(25.5f).append(true); test.checkEqual(Core::String("abc2025.50true").hashCode(), s.hashCode(), "string modification recalculates hash 1"); s.clear(); test.checkEqual(Core::String().hashCode(), s.hashCode(), "string modification recalculates hash 2"); } static void testAsHashMapKey(Core::Test& test) { (void)test; /*HashMap map; map.add(String("wusi"), 3) .add(String("hiThere"), 7) .add(String("baum123"), 5); int* a = map.search(String("wusi")); int* b = map.search(String("hiThere")); int* c = map.search(String("baum123")); int* d = map.search(String("423hifd")); test.checkEqual(true, a != nullptr, "strings works as hash key 1"); test.checkEqual(true, b != nullptr, "strings works as hash key 2"); test.checkEqual(true, c != nullptr, "strings works as hash key 3"); test.checkEqual(true, d == nullptr, "strings works as hash key 4"); if(a != nullptr && b != nullptr && c != nullptr) { test.checkEqual(3, *a, "strings works as hash key 1"); test.checkEqual(7, *b, "strings works as hash key 2"); test.checkEqual(5, *c, "strings works as hash key 3"); }*/ } void Core::StringTests::test() { Core::Test test("String"); testEquality(test); testInequality(test); testStringAppend(test); testCharacters(test); testLength(test); testChar(test); testSignedChar(test); testUnsignedChar(test); testSignedShort(test); testUnsignedShort(test); testSignedInt(test); testUnsignedInt(test); testSignedLong(test); testUnsignedLong(test); testSignedLongLong(test); testUnsignedLongLong(test); testFloat(test); testDouble(test); testLongDouble(test); testBool(test); testUnicode(test); testClear(test); testHashCode(test); testAsHashMapKey(test); test.finalize(); }