123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #include "tests/ArrayStringTests.h"
- #include "data/HashMap.h"
- #include "test/Test.h"
- #include "utils/ArrayString.h"
- using String = Core::ArrayString<128>;
- static String build(Core::Test& test, const char* cs) {
- String s;
- test.checkFalse(s.append(cs), "append works");
- return s;
- }
- static void testEquality(Core::Test& test) {
- String s = build(test, "test");
- test.checkTrue(s == "test", "equality with c-string");
- test.checkTrue(s == build(test, "test"), "equality with another string");
- test.checkTrue("test" == s, "inverse equality with c-string");
- test.checkTrue(build(test, "test") == s,
- "inverse equality with another string");
- test.checkTrue(s == s, "equality with itself");
- }
- static void testUnicodeEquality(Core::Test& test) {
- const char* cs = "\u0040\u0400\u8000\U00100000";
- String s = build(test, cs);
- test.checkTrue(s == cs, "unicode equality with c-string");
- test.checkTrue(s == build(test, cs),
- "unicode equality with another string");
- test.checkTrue(cs == s, "unicode inverse equality with c-string");
- test.checkTrue(build(test, cs) == s,
- "unicode inverse equality with another string");
- test.checkTrue(s == s, "unicode equality with itself");
- }
- static void testInequality(Core::Test& test) {
- String s = build(test, "test");
- test.checkFalse(s != "test", "inequality with c-string");
- test.checkFalse(s != build(test, "test"), "inequality with another string");
- test.checkFalse("test" != s, "inverse inequality with c-string");
- test.checkFalse(build(test, "test") != s,
- "inverse inequality with another string");
- test.checkFalse(s != s, "inequality with itself");
- }
- static void testStringAppend(Core::Test& test) {
- String s = build(test, "test");
- test.checkFalse(s.append("22"), "multiple append works");
- test.checkFalse(s.append("333"), "multiple append works");
- test.checkFalse(s.append("4444"), "multiple append works");
- test.checkEqual(build(test, "test223334444"), s, "multiple appends");
- }
- static void testStringAppendOverflow(Core::Test& test) {
- Core::ArrayString<20> s;
- test.checkFalse(s.append("te"), "overflow test 1");
- test.checkTrue(s.append("23334444"), "overflow test 2");
- test.checkTrue(build(test, "te23334444") != s, "overflow test 3");
- }
- static void testCharacters(Core::Test& test) {
- String s = build(test, "test");
- test.checkEqual<u32>('t', s[0], "character read 1");
- test.checkEqual<u32>('e', s[1], "character read 2");
- test.checkEqual<u32>('s', s[2], "character read 3");
- test.checkEqual<u32>('t', s[3], "character read 4");
- }
- static void testLength(Core::Test& test) {
- String s = build(test, "test");
- test.checkEqual(4, s.getLength(), "length 1");
- test.checkFalse(s.append("aaa"), "length append works");
- test.checkEqual(7, s.getLength(), "length 2");
- }
- static void testChar(Core::Test& test) {
- String s = build(test, "test");
- for(char i = 'a'; i < 'd'; i++) {
- test.checkFalse(s.append(i), "char append");
- }
- test.checkEqual(build(test, "testabc"), s, "char append");
- }
- static void testSignedChar(Core::Test& test) {
- String s = build(test, "test");
- for(signed char i = 'b'; i < 'e'; i++) {
- test.checkFalse(s.append(i), "signed char append");
- }
- test.checkEqual(build(test, "testbcd"), s, "signed char append");
- }
- static void testUnsignedChar(Core::Test& test) {
- String s = build(test, "test");
- for(unsigned char i = 'c'; i < 'f'; i++) {
- test.checkFalse(s.append(i), "unsigned char append");
- }
- test.checkEqual(build(test, "testcde"), s, "unsigned char append");
- }
- static void testSignedShort(Core::Test& test) {
- String s = build(test, "test");
- for(signed short i = 100; i < 103; i++) {
- test.checkFalse(s.append(i), "signed short append");
- }
- test.checkEqual(build(test, "test100101102"), s, "signed short append");
- }
- static void testUnsignedShort(Core::Test& test) {
- String s = build(test, "test");
- for(unsigned short i = 101; i < 104; i++) {
- test.checkFalse(s.append(i), "unsigned short append");
- }
- test.checkEqual(build(test, "test101102103"), s, "unsigned short append");
- }
- static void testSignedInt(Core::Test& test) {
- String s = build(test, "test");
- for(signed int i = 102; i < 105; i++) {
- test.checkFalse(s.append(i), "signed int append");
- }
- test.checkEqual(build(test, "test102103104"), s, "signed int append");
- }
- static void testUnsignedInt(Core::Test& test) {
- String s = build(test, "test");
- for(unsigned int i = 103; i < 106; i++) {
- test.checkFalse(s.append(i), "unsigned int append");
- }
- test.checkEqual(build(test, "test103104105"), s, "unsigned int append");
- }
- static void testSignedLong(Core::Test& test) {
- String s = build(test, "test");
- for(signed long i = 104; i < 107; i++) {
- test.checkFalse(s.append(i), "signed long append");
- }
- test.checkEqual(build(test, "test104105106"), s, "signed long append");
- }
- static void testUnsignedLong(Core::Test& test) {
- String s = build(test, "test");
- for(unsigned long i = 105; i < 108; i++) {
- test.checkFalse(s.append(i), "unsigned long append");
- }
- test.checkEqual(build(test, "test105106107"), s, "unsigned long append");
- }
- static void testSignedLongLong(Core::Test& test) {
- String s = build(test, "test");
- for(signed long long i = 106; i < 109; i++) {
- test.checkFalse(s.append(i), "signed long long append");
- }
- test.checkEqual(build(test, "test106107108"), s, "signed long long append");
- }
- static void testUnsignedLongLong(Core::Test& test) {
- String s = build(test, "test");
- for(unsigned long long i = 107; i < 110; i++) {
- test.checkFalse(s.append(i), "unsigned long long append");
- }
- test.checkEqual(build(test, "test107108109"), s,
- "unsigned long long append");
- }
- static void testFloat(Core::Test& test) {
- String s = build(test, "test");
- for(float i = 108; i < 111; i++) {
- test.checkFalse(s.append(i), "float append");
- }
- test.checkEqual(build(test, "test108.00109.00110.00"), s, "float append");
- }
- static void testDouble(Core::Test& test) {
- String s = build(test, "test");
- for(double i = 109; i < 112; i++) {
- test.checkFalse(s.append(i), "double append");
- }
- test.checkEqual(build(test, "test109.00110.00111.00"), s, "double append");
- }
- static void testLongDouble(Core::Test& test) {
- String s = build(test, "test");
- for(long double i = 110; i < 113; i++) {
- test.checkFalse(s.append(i), "long double append");
- }
- test.checkEqual(build(test, "test110.00111.00112.00"), s,
- "long double append");
- }
- static void testBool(Core::Test& test) {
- String s = build(test, "test");
- test.checkFalse(s.append(true), "bool append 1");
- test.checkFalse(s.append(false), "bool append 2");
- test.checkFalse(s.append(true), "bool append 3");
- test.checkEqual(build(test, "testtruefalsetrue"), s, "bool append 4");
- }
- static void testIntOverflow(Core::Test& test) {
- Core::ArrayString<20> s;
- test.checkTrue(s.append(123456), "int overflow");
- String o;
- for(int i = 0; i < s.getCapacity(); i++) {
- test.checkFalse(o.append(i + 1), "int overflow compare build");
- }
- test.checkTrue(o == s, "int overflow compare");
- }
- static void testUnicode(Core::Test& test) {
- String s;
- test.checkFalse(s.appendUnicode('\u0040'), "unicode 1");
- test.checkFalse(s.appendUnicode(L'\u0400'), "unicode 2");
- test.checkFalse(s.appendUnicode(L'\u8000'), "unicode 3");
- test.checkFalse(s.appendUnicode(U'\U00100000'), "unicode 4");
- test.checkEqual(build(test, "\u0040\u0400\u8000\U00100000"), s,
- "unicode append");
- }
- static void testClear(Core::Test& test) {
- String s = build(test, "test");
- test.checkFalse(s.append(1234), "clear 1");
- s.clear();
- test.checkFalse(s.append("wusi"), "clear 2");
- test.checkFalse(s.append("1234"), "clear 3");
- test.checkEqual(build(test, "wusi1234"), s, "clear 4");
- }
- static void testHashCode(Core::Test& test) {
- String s;
- test.checkFalse(s.append("a"), "hash code 1");
- test.checkFalse(s.append("bc"), "hash code 2");
- test.checkFalse(s.append(20), "hash code 3");
- test.checkFalse(s.append(25.5f), "hash code 4");
- test.checkFalse(s.append(true), "hash code 5");
- test.checkEqual(build(test, "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 testAddSelf(Core::Test& test) {
- String s;
- test.checkFalse(s.append("test1"), "add self 1");
- test.checkFalse(s.append(s), "add self 2");
- test.checkFalse(s.append(s), "add self 3");
- test.checkEqual(build(test, "test1test1test1test1"), s, "add self 4");
- }
- static void testAsHashMapKey(Core::Test& test) {
- Core::HashMap<String, int> map;
- test.checkFalse(map.add(build(test, "wusi"), 3), "as hashmap key 1");
- test.checkFalse(map.add(build(test, "hiThere"), 7), "as hashmap key 2");
- test.checkFalse(map.add(build(test, "baum123"), 5), "as hashmap key 3");
- int* a = map.search(build(test, "wusi"));
- int* b = map.search(build(test, "hiThere"));
- int* c = map.search(build(test, "baum123"));
- int* d = map.search(build(test, "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::ArrayStringTests::test() {
- Core::Test test("String");
- testEquality(test);
- testUnicodeEquality(test);
- testInequality(test);
- testStringAppend(test);
- testStringAppendOverflow(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);
- testIntOverflow(test);
- testUnicode(test);
- testClear(test);
- testHashCode(test);
- testAddSelf(test);
- testAsHashMapKey(test);
- test.finalize();
- }
|