#include "tests/StringBufferTests.h" #include "data/HashMap.h" #include "tests/Test.h" #include "utils/StringBuffer.h" typedef StringBuffer<20> String; static void testEquality(Test& test) { String s("test"); test.checkEqual(true, s == "test", "equality with c-string"); test.checkEqual(true, s == String("test"), "equality with another string"); test.checkEqual(true, "test" == s, "inverse equality with c-string"); test.checkEqual(true, String("test") == s, "inverse equality with another string"); test.checkEqual(true, s == s, "equality with itself"); } static void testInequality(Test& test) { String s("test"); test.checkEqual(false, s != "test", "inequality with c-string"); test.checkEqual(false, s != String("test"), "inequality with another string"); test.checkEqual(false, "test" != s, "inverse inequality with c-string"); test.checkEqual(false, String("test") != s, "inverse inequality with another string"); test.checkEqual(false, s != s, "inequality with itself"); } static void testStringAppend(Test& test) { String s("test"); s.append("22").append("333").append("4444"); test.checkEqual(String("test223334444"), s, "multiple appends"); } static void testStringAppendOverflow(Test& test) { StringBuffer<5> s("te"); s.append("2").append("333").append("4444"); test.checkEqual(StringBuffer<5>("te23"), s, "multiple appends with overflow"); test.checkEqual(4, s.getLength(), "length after multiple appends with overflow"); } static void testCharacters(Test& test) { 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(Test& test) { String s("test"); test.checkEqual(4, s.getLength(), "length 1"); s.append("aaa"); test.checkEqual(7, s.getLength(), "length 2"); } static void testInt(Test& test) { String s("test"); s.append(524).append(-37); test.checkEqual(String("test524-37"), s, "int append"); } static void testUnsignedInt(Test& test) { String s("test"); s.append(524u).append(4294967295u); test.checkEqual(String("test5244294967295"), s, "unsigned int append"); } static void testOverflowConstructor(Test& test) { StringBuffer<5> s("12345678"); test.checkEqual(StringBuffer<5>("1234"), s, "overflow constructor"); test.checkEqual(4, s.getLength(), "length after overflow constructor"); } static void testIntOverflow(Test& test) { StringBuffer<5> s("te"); s.append(123456); test.checkEqual(StringBuffer<5>("te12"), s, "int append with overflow"); test.checkEqual(4, s.getLength(), "length after int append with overflow"); } static void testFloat(Test& test) { String s("test"); s.append(5.2378f).append(-500.4321f); test.checkEqual(String("test5.24-500.43"), s, "float append"); } static void testBool(Test& test) { String s("test"); s.append(true).append(false).append(true); test.checkEqual(String("testtruefalsetrue"), s, "bool append"); } static void testUnicode(Test& test) { String s(""); s.appendUnicode('\u0040') .appendUnicode(L'\u0400') .appendUnicode(L'\u8000') .appendUnicode(U'\U00100000'); test.checkEqual(String("\u0040\u0400\u8000\U00100000"), s, "unicode append"); } static void testClear(Test& test) { String s("test"); s.append(1234).clear().append("wusi").append("1234"); test.checkEqual(String("wusi1234"), s, "clear"); } static void testAppendChar(Test& test) { StringBuffer<5> s(""); for(int i = 0; i < 20; i++) { s.append('a'); } test.checkEqual(StringBuffer<5>("aaaa"), s, "char append with overflow"); test.checkEqual(4, s.getLength(), "length after char append with overflow"); } static void testHashCode(Test& test) { String s; s.append("a").append("bc").append(20).append(25.5f).append(true); test.checkEqual(String("abc2025.50true").hashCode(), s.hashCode(), "string modification recalculates hash 1"); s.clear(); test.checkEqual(String().hashCode(), s.hashCode(), "string modification recalculates hash 2"); } static void testAsHashMapKey(Test& 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 StringBufferTests::test() { Test test("StringBuffer"); testEquality(test); testInequality(test); testStringAppend(test); testStringAppendOverflow(test); testCharacters(test); testLength(test); testInt(test); testUnsignedInt(test); testOverflowConstructor(test); testIntOverflow(test); testFloat(test); testBool(test); testUnicode(test); testClear(test); testAppendChar(test); testHashCode(test); testAsHashMapKey(test); test.finalize(); }