123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- #include "../Tests.hpp"
- #include "core/data/HashMap.hpp"
- #include "core/utils/ArrayString.hpp"
- #include "core/utils/Error.hpp"
- template class Core::ArrayString<128>;
- using String = Core::ArrayString<128>;
- static String build(const char* cs) {
- String s;
- s.append(cs);
- return s;
- }
- static void testEquality() {
- String s = build("test");
- CORE_TEST_TRUE(s == "test");
- CORE_TEST_TRUE(s == build("test"));
- CORE_TEST_TRUE("test" == s);
- CORE_TEST_TRUE(build("test") == s);
- CORE_TEST_FALSE(build("tes2") == s);
- CORE_TEST_TRUE(s == s);
- }
- static void testUnicodeEquality() {
- const char* cs = "\u0040\u0400\u8000\U00100000";
- String s = build(cs);
- CORE_TEST_TRUE(s == cs);
- CORE_TEST_TRUE(s == build(cs));
- CORE_TEST_TRUE(cs == s);
- CORE_TEST_TRUE(build(cs) == s);
- CORE_TEST_TRUE(s == s);
- }
- static void testInequality() {
- String s = build("test");
- CORE_TEST_FALSE(s != "test");
- CORE_TEST_FALSE(s != build("test"));
- CORE_TEST_FALSE("test" != s);
- CORE_TEST_FALSE(build("test") != s);
- CORE_TEST_FALSE(s != s);
- }
- static void testStringAppend() {
- String s = build("test");
- s.append("22").append("333").append("4444");
- CORE_TEST_EQUAL(build("test223334444"), s);
- }
- static void testStringAppendOverflow() {
- Core::ArrayString<6> s;
- s.append("te").append("23334444");
- CORE_TEST_TRUE(build("te23334444") != s);
- }
- static void testCharacters() {
- String s = build("test");
- CORE_TEST_EQUAL('t', s[0]);
- CORE_TEST_EQUAL('e', s[1]);
- CORE_TEST_EQUAL('s', s[2]);
- CORE_TEST_EQUAL('t', s[3]);
- }
- static void testLength() {
- String s = build("test");
- CORE_TEST_EQUAL(4, s.getLength());
- s.append("aaa");
- CORE_TEST_EQUAL(7, s.getLength());
- }
- static void testChar() {
- String s = build("test");
- for(char i = 'a'; i < 'd'; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("testabc"), s);
- }
- static void testSignedChar() {
- String s = build("test");
- for(signed char i = 'b'; i < 'e'; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("testbcd"), s);
- }
- static void testUnsignedChar() {
- String s = build("test");
- for(unsigned char i = 'c'; i < 'f'; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("testcde"), s);
- }
- static void testSignedShort() {
- String s = build("test");
- for(signed short i = 100; i < 103; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test100101102"), s);
- }
- static void testUnsignedShort() {
- String s = build("test");
- for(unsigned short i = 101; i < 104; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test101102103"), s);
- }
- static void testSignedInt() {
- String s = build("test");
- for(signed int i = 102; i < 105; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test102103104"), s);
- }
- static void testUnsignedInt() {
- String s = build("test");
- for(unsigned int i = 103; i < 106; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test103104105"), s);
- }
- static void testSignedLong() {
- String s = build("test");
- for(signed long i = 104; i < 107; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test104105106"), s);
- }
- static void testUnsignedLong() {
- String s = build("test");
- for(unsigned long i = 105; i < 108; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test105106107"), s);
- }
- static void testSignedLongLong() {
- String s = build("test");
- for(signed long long i = 106; i < 109; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test106107108"), s);
- }
- static void testUnsignedLongLong() {
- String s = build("test");
- for(unsigned long long i = 107; i < 110; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test107108109"), s);
- }
- static void testFloat() {
- String s = build("test");
- for(float i = 108; i < 111; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test108.00109.00110.00"), s);
- }
- static void testDouble() {
- String s = build("test");
- for(double i = 109; i < 112; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test109.00110.00111.00"), s);
- }
- static void testLongDouble() {
- String s = build("test");
- for(long double i = 110; i < 113; i++) {
- s.append(i);
- }
- CORE_TEST_EQUAL(build("test110.00111.00112.00"), s);
- }
- static void testBool() {
- String s = build("test");
- s.append(true).append(false).append(true);
- CORE_TEST_EQUAL(build("testtruefalsetrue"), s);
- }
- static void testIntOverflow() {
- Core::ArrayString<4> s;
- s.append(123456);
- String o;
- for(size_t i = 0; i < s.getCapacity(); i++) {
- o.append(i + 1);
- }
- CORE_TEST_TRUE(o == s);
- }
- static void testUnicode() {
- String s;
- s.append('\u0040');
- s.append(L'\u0400');
- s.append(L'\u8000');
- s.append(U'\U00100000');
- CORE_TEST_EQUAL(build("\u0040\u0400\u8000\U00100000"), s);
- }
- static void testClear() {
- String s = build("test");
- s.append(1234);
- s.clear();
- s.append("wusi");
- s.append("1234");
- CORE_TEST_EQUAL(build("wusi1234"), s);
- }
- static void testAddSelf() {
- String s;
- s.append("test1").append(s).append(s);
- CORE_TEST_EQUAL(build("test1test1test1test1"), s);
- }
- static void testStartsWith() {
- String s;
- s.append("0123456789");
- String s2;
- s2.append("123");
- String s3;
- s3.append("234");
- String s4;
- s4.append("789");
- String s5;
- s5.append("124");
- String s6;
- String s7;
- s7.append("7891");
- CORE_TEST_FALSE(s.startsWith(s2));
- CORE_TEST_TRUE(s.startsWith(s2, 1));
- CORE_TEST_FALSE(s.startsWith(s3));
- CORE_TEST_TRUE(s.startsWith(s3, 2));
- CORE_TEST_FALSE(s.startsWith(s4));
- CORE_TEST_TRUE(s.startsWith(s4, 7));
- CORE_TEST_FALSE(s.startsWith(s5));
- CORE_TEST_FALSE(s.startsWith(s5, 3));
- CORE_TEST_TRUE(s.startsWith(s6));
- CORE_TEST_TRUE(s.startsWith(s6, 3));
- CORE_TEST_FALSE(s.startsWith(s7));
- CORE_TEST_FALSE(s.startsWith(s7, 7));
- }
- static void testSearch() {
- String s;
- s.append("0123456789");
- String s2;
- s2.append("123");
- String s3;
- s3.append("234");
- String s4;
- s4.append("789");
- String s5;
- s5.append("124");
- String s6;
- String s7;
- s7.append("7891");
- CORE_TEST_EQUAL(1, s.search(s2));
- CORE_TEST_EQUAL(2, s.search(s3));
- CORE_TEST_EQUAL(7, s.search(s4));
- CORE_TEST_EQUAL(SIZE_MAX, s.search(s5));
- CORE_TEST_EQUAL(0, s.search(s6));
- CORE_TEST_EQUAL(SIZE_MAX, s.search(s7));
- CORE_TEST_EQUAL(SIZE_MAX, s.search(s2, 3));
- CORE_TEST_EQUAL(SIZE_MAX, s.search(s3, 3));
- CORE_TEST_EQUAL(7, s.search(s4, 3));
- CORE_TEST_EQUAL(SIZE_MAX, s.search(s5, 3));
- CORE_TEST_EQUAL(3, s.search(s6, 3));
- CORE_TEST_EQUAL(SIZE_MAX, s.search(s7, 3));
- }
- static void testContains() {
- String s;
- s.append("0123456789");
- String s2;
- s2.append("123");
- String s3;
- s3.append("234");
- String s4;
- s4.append("789");
- String s5;
- s5.append("124");
- String s6;
- String s7;
- s7.append("7891");
- CORE_TEST_TRUE(s.contains(s2));
- CORE_TEST_TRUE(s.contains(s3));
- CORE_TEST_TRUE(s.contains(s4));
- CORE_TEST_FALSE(s.contains(s5));
- CORE_TEST_TRUE(s.contains(s6));
- CORE_TEST_FALSE(s.contains(s7));
- }
- static void testSearchChar() {
- String s;
- s.append("01üää3ä");
- CORE_TEST_EQUAL(0, s.search('0'));
- CORE_TEST_EQUAL(1, s.search('1'));
- CORE_TEST_EQUAL(8, s.search('3'));
- }
- static void testContainsChar() {
- String s;
- s.append("01üää3ä");
- CORE_TEST_TRUE(s.contains('0'));
- CORE_TEST_TRUE(s.contains('1'));
- CORE_TEST_TRUE(s.contains('3'));
- CORE_TEST_FALSE(s.contains('a'));
- }
- static void testSubString() {
- String s;
- s.append("01üää3ä");
- String sub;
- s.substring(sub, 0);
- CORE_TEST_STRING("01üää3ä", sub);
- s.substring(sub, 2);
- CORE_TEST_STRING("üää3ä", sub);
- s.substring(sub, 4);
- CORE_TEST_STRING("ää3ä", sub);
- s.substring(sub, 6);
- CORE_TEST_STRING("ä3ä", sub);
- s.substring(sub, 0, 10);
- CORE_TEST_STRING("01üää3ä", sub);
- s.substring(sub, 1, 8);
- CORE_TEST_STRING("1üää3", sub);
- s.substring(sub, 2, 7);
- CORE_TEST_STRING("üää", sub);
- s.substring(sub, 4, 5);
- CORE_TEST_STRING("ä", sub);
- s.substring(sub, 4, 2);
- CORE_TEST_STRING("", sub);
- s.substring(sub, 6, 23);
- CORE_TEST_STRING("ä3ä", sub);
- }
- static void testReplace() {
- String s;
- s.append("0äääää1üää3ä");
- String search;
- search.append("ää");
- String replace;
- replace.append("ABCD");
- s.replace(search, replace);
- CORE_TEST_STRING("0ABCDABCDä1üABCD3ä", s);
- }
- static void testReplaceChar() {
- String s;
- s.append("01YXX3X");
- s.replace('0', 'A');
- CORE_TEST_STRING("A1YXX3X", s);
- s.replace('1', 'B');
- CORE_TEST_STRING("ABYXX3X", s);
- s.replace('Y', 'C');
- CORE_TEST_STRING("ABCXX3X", s);
- s.replace('X', 'D');
- CORE_TEST_STRING("ABCDD3D", s);
- s.replace('3', 'E');
- CORE_TEST_STRING("ABCDDED", s);
- }
- static void testCastAppendSelf() {
- String s;
- s.append("abc");
- s.append(s);
- s.append(static_cast<const char*>(s));
- CORE_TEST_STRING("abcabcabcabc", s);
- }
- static void testCompareWithShorter() {
- String s;
- s.append("abc");
- CORE_TEST_FALSE(s == "ab");
- }
- static void testAppendSignedChar() {
- const signed char buffer[] = {'a', 'b', 'c', '\0'};
- String s;
- s.append(buffer);
- CORE_TEST_TRUE(s == "abc");
- }
- static void testAppendUnsignedChar() {
- const unsigned char buffer[] = {'a', 'b', 'c', '\0'};
- String s;
- s.append(buffer);
- CORE_TEST_TRUE(s == "abc");
- }
- static void testAppendError() {
- String s;
- s.append(Core::ErrorCode::NONE);
- CORE_TEST_STRING("0", s);
- }
- static void testPrint() {
- String s;
- s.append('\u0040');
- s.append(L'\u0400');
- s.append(L'\u8000');
- s.append(U'\U00100000');
- CORE_TEST_EQUAL(build("\u0040\u0400\u8000\U00100000"), s);
- s.append('\n');
- s.print();
- }
- static void testKeepHash() {
- String s;
- s.append("a ## test #### #####");
- s.format(1, 2, 3, 4, 5, 6, 7, 8, 9);
- CORE_TEST_STRING("a # test ## ##123456789", s);
- }
- static void testFormatWithoutArguments() {
- String s;
- s.append("wusi");
- s.format();
- CORE_TEST_STRING("wusi", s);
- }
- static void testUnicodeString() {
- String s;
- s.append(U"_üö§äab");
- CORE_TEST_STRING("_üö§äab", s);
- }
- static void testInvalidUnicodeString() {
- String s;
- s.append(static_cast<c32>(0xFFFFFFFF));
- CORE_TEST_STRING("?", s);
- }
- static void testConversion() {
- const c32* a = U"öüewfde_§$§%$ädsf";
- const char* b = "öüewfde_§$§%$ädsf";
- String s;
- s.append(a);
- String s2;
- s2.append(b);
- CORE_TEST_STRING(s, s2);
- }
- void Core::testArrayString() {
- testEquality();
- testUnicodeEquality();
- testInequality();
- testStringAppend();
- testStringAppendOverflow();
- testCharacters();
- testLength();
- testChar();
- testSignedChar();
- testUnsignedChar();
- testSignedShort();
- testUnsignedShort();
- testSignedInt();
- testUnsignedInt();
- testSignedLong();
- testUnsignedLong();
- testSignedLongLong();
- testUnsignedLongLong();
- testFloat();
- testDouble();
- testLongDouble();
- testBool();
- testIntOverflow();
- testUnicode();
- testClear();
- testAddSelf();
- testStartsWith();
- testSearch();
- testContains();
- testSearchChar();
- testContainsChar();
- testSubString();
- testReplace();
- testReplaceChar();
- testCastAppendSelf();
- testCompareWithShorter();
- testAppendSignedChar();
- testAppendUnsignedChar();
- testAppendError();
- testPrint();
- testKeepHash();
- testFormatWithoutArguments();
- testUnicodeString();
- testInvalidUnicodeString();
- testConversion();
- }
|