ArrayStringTests.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. #include "../Tests.hpp"
  2. #include "core/data/HashMap.hpp"
  3. #include "core/utils/ArrayString.hpp"
  4. template class Core::ArrayString<128, char>;
  5. template class Core::ArrayString<128, char32_t>;
  6. using String8 = Core::String8<128>;
  7. using String32 = Core::String32<128>;
  8. static String8 build(const char* cs) {
  9. String8 s;
  10. CORE_TEST_ERROR(s.append(cs));
  11. return s;
  12. }
  13. static void testEquality8() {
  14. String8 s = build("test");
  15. CORE_TEST_TRUE(s == "test");
  16. CORE_TEST_TRUE(s == build("test"));
  17. CORE_TEST_TRUE("test" == s);
  18. CORE_TEST_TRUE(build("test") == s);
  19. CORE_TEST_TRUE(s == s);
  20. }
  21. static void testUnicodeEquality8() {
  22. const char* cs = "\u0040\u0400\u8000\U00100000";
  23. String8 s = build(cs);
  24. CORE_TEST_TRUE(s == cs);
  25. CORE_TEST_TRUE(s == build(cs));
  26. CORE_TEST_TRUE(cs == s);
  27. CORE_TEST_TRUE(build(cs) == s);
  28. CORE_TEST_TRUE(s == s);
  29. }
  30. static void testInequality8() {
  31. String8 s = build("test");
  32. CORE_TEST_FALSE(s != "test");
  33. CORE_TEST_FALSE(s != build("test"));
  34. CORE_TEST_FALSE("test" != s);
  35. CORE_TEST_FALSE(build("test") != s);
  36. CORE_TEST_FALSE(s != s);
  37. }
  38. static void testStringAppend8() {
  39. String8 s = build("test");
  40. CORE_TEST_ERROR(s.append("22"));
  41. CORE_TEST_ERROR(s.append("333"));
  42. CORE_TEST_ERROR(s.append("4444"));
  43. CORE_TEST_EQUAL(build("test223334444"), s);
  44. }
  45. static void testStringAppendOverflow8() {
  46. Core::ArrayString<6, char> s;
  47. CORE_TEST_ERROR(s.append("te"));
  48. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append("23334444"));
  49. CORE_TEST_TRUE(build("te23334444") != s);
  50. }
  51. static void testCharacters8() {
  52. String8 s = build("test");
  53. CORE_TEST_EQUAL('t', s[0]);
  54. CORE_TEST_EQUAL('e', s[1]);
  55. CORE_TEST_EQUAL('s', s[2]);
  56. CORE_TEST_EQUAL('t', s[3]);
  57. }
  58. static void testLength8() {
  59. String8 s = build("test");
  60. CORE_TEST_EQUAL(4, s.getLength());
  61. CORE_TEST_ERROR(s.append("aaa"));
  62. CORE_TEST_EQUAL(7, s.getLength());
  63. }
  64. static void testChar8() {
  65. String8 s = build("test");
  66. for(char i = 'a'; i < 'd'; i++) {
  67. CORE_TEST_ERROR(s.append(i));
  68. }
  69. CORE_TEST_EQUAL(build("testabc"), s);
  70. }
  71. static void testSignedChar8() {
  72. String8 s = build("test");
  73. for(signed char i = 'b'; i < 'e'; i++) {
  74. CORE_TEST_ERROR(s.append(i));
  75. }
  76. CORE_TEST_EQUAL(build("testbcd"), s);
  77. }
  78. static void testUnsignedChar8() {
  79. String8 s = build("test");
  80. for(unsigned char i = 'c'; i < 'f'; i++) {
  81. CORE_TEST_ERROR(s.append(i));
  82. }
  83. CORE_TEST_EQUAL(build("testcde"), s);
  84. }
  85. static void testSignedShort8() {
  86. String8 s = build("test");
  87. for(signed short i = 100; i < 103; i++) {
  88. CORE_TEST_ERROR(s.append(i));
  89. }
  90. CORE_TEST_EQUAL(build("test100101102"), s);
  91. }
  92. static void testUnsignedShort8() {
  93. String8 s = build("test");
  94. for(unsigned short i = 101; i < 104; i++) {
  95. CORE_TEST_ERROR(s.append(i));
  96. }
  97. CORE_TEST_EQUAL(build("test101102103"), s);
  98. }
  99. static void testSignedInt8() {
  100. String8 s = build("test");
  101. for(signed int i = 102; i < 105; i++) {
  102. CORE_TEST_ERROR(s.append(i));
  103. }
  104. CORE_TEST_EQUAL(build("test102103104"), s);
  105. }
  106. static void testUnsignedInt8() {
  107. String8 s = build("test");
  108. for(unsigned int i = 103; i < 106; i++) {
  109. CORE_TEST_ERROR(s.append(i));
  110. }
  111. CORE_TEST_EQUAL(build("test103104105"), s);
  112. }
  113. static void testSignedLong8() {
  114. String8 s = build("test");
  115. for(signed long i = 104; i < 107; i++) {
  116. CORE_TEST_ERROR(s.append(i));
  117. }
  118. CORE_TEST_EQUAL(build("test104105106"), s);
  119. }
  120. static void testUnsignedLong8() {
  121. String8 s = build("test");
  122. for(unsigned long i = 105; i < 108; i++) {
  123. CORE_TEST_ERROR(s.append(i));
  124. }
  125. CORE_TEST_EQUAL(build("test105106107"), s);
  126. }
  127. static void testSignedLongLong8() {
  128. String8 s = build("test");
  129. for(signed long long i = 106; i < 109; i++) {
  130. CORE_TEST_ERROR(s.append(i));
  131. }
  132. CORE_TEST_EQUAL(build("test106107108"), s);
  133. }
  134. static void testUnsignedLongLong8() {
  135. String8 s = build("test");
  136. for(unsigned long long i = 107; i < 110; i++) {
  137. CORE_TEST_ERROR(s.append(i));
  138. }
  139. CORE_TEST_EQUAL(build("test107108109"), s);
  140. }
  141. static void testFloat8() {
  142. String8 s = build("test");
  143. for(float i = 108; i < 111; i++) {
  144. CORE_TEST_ERROR(s.append(i));
  145. }
  146. CORE_TEST_EQUAL(build("test108.00109.00110.00"), s);
  147. }
  148. static void testDouble8() {
  149. String8 s = build("test");
  150. for(double i = 109; i < 112; i++) {
  151. CORE_TEST_ERROR(s.append(i));
  152. }
  153. CORE_TEST_EQUAL(build("test109.00110.00111.00"), s);
  154. }
  155. static void testLongDouble8() {
  156. String8 s = build("test");
  157. for(long double i = 110; i < 113; i++) {
  158. CORE_TEST_ERROR(s.append(i));
  159. }
  160. CORE_TEST_EQUAL(build("test110.00111.00112.00"), s);
  161. }
  162. static void testBool8() {
  163. String8 s = build("test");
  164. CORE_TEST_ERROR(s.append(true));
  165. CORE_TEST_ERROR(s.append(false));
  166. CORE_TEST_ERROR(s.append(true));
  167. CORE_TEST_EQUAL(build("testtruefalsetrue"), s);
  168. }
  169. static void testIntOverflow8() {
  170. Core::ArrayString<4, char> s;
  171. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
  172. String8 o;
  173. for(int i = 0; i < s.getCapacity(); i++) {
  174. CORE_TEST_ERROR(o.append(i + 1));
  175. }
  176. CORE_TEST_TRUE(o == s);
  177. }
  178. static void testUnicode8() {
  179. String8 s;
  180. CORE_TEST_ERROR(s.append('\u0040'));
  181. CORE_TEST_ERROR(s.append(L'\u0400'));
  182. CORE_TEST_ERROR(s.append(L'\u8000'));
  183. CORE_TEST_ERROR(s.append(U'\U00100000'));
  184. CORE_TEST_EQUAL(build("\u0040\u0400\u8000\U00100000"), s);
  185. }
  186. static void testClear8() {
  187. String8 s = build("test");
  188. CORE_TEST_ERROR(s.append(1234));
  189. s.clear();
  190. CORE_TEST_ERROR(s.append("wusi"));
  191. CORE_TEST_ERROR(s.append("1234"));
  192. CORE_TEST_EQUAL(build("wusi1234"), s);
  193. }
  194. static void testHashCode8() {
  195. String8 s;
  196. CORE_TEST_ERROR(s.append("a"));
  197. CORE_TEST_ERROR(s.append("bc"));
  198. CORE_TEST_ERROR(s.append(20));
  199. CORE_TEST_ERROR(s.append(25.5f));
  200. CORE_TEST_ERROR(s.append(true));
  201. CORE_TEST_EQUAL(build("abc2025.50true").hashCode(), s.hashCode());
  202. s.clear();
  203. CORE_TEST_EQUAL(String8().hashCode(), s.hashCode());
  204. }
  205. static void testAddSelf8() {
  206. String8 s;
  207. CORE_TEST_ERROR(s.append("test1"));
  208. CORE_TEST_ERROR(s.append(s));
  209. CORE_TEST_ERROR(s.append(s));
  210. CORE_TEST_EQUAL(build("test1test1test1test1"), s);
  211. }
  212. static void testAsHashMapKey8() {
  213. Core::HashMap<String8, int> map;
  214. CORE_TEST_ERROR(map.add(build("wusi"), 3));
  215. CORE_TEST_ERROR(map.add(build("hiThere"), 7));
  216. CORE_TEST_ERROR(map.add(build("baum123"), 5));
  217. int* a = map.search(build("wusi"));
  218. int* b = map.search(build("hiThere"));
  219. int* c = map.search(build("baum123"));
  220. int* d = map.search(build("423hifd"));
  221. CORE_TEST_NOT_NULL(a);
  222. CORE_TEST_NOT_NULL(b);
  223. CORE_TEST_NOT_NULL(c);
  224. CORE_TEST_NULL(d);
  225. if(a != nullptr && b != nullptr && c != nullptr) {
  226. CORE_TEST_EQUAL(3, *a);
  227. CORE_TEST_EQUAL(7, *b);
  228. CORE_TEST_EQUAL(5, *c);
  229. }
  230. }
  231. static void testStartsWith8() {
  232. String8 s;
  233. CORE_TEST_ERROR(s.append("0123456789"));
  234. String8 s2;
  235. CORE_TEST_ERROR(s2.append("123"));
  236. String8 s3;
  237. CORE_TEST_ERROR(s3.append("234"));
  238. String8 s4;
  239. CORE_TEST_ERROR(s4.append("789"));
  240. String8 s5;
  241. CORE_TEST_ERROR(s5.append("124"));
  242. String8 s6;
  243. String8 s7;
  244. CORE_TEST_ERROR(s7.append("7891"));
  245. CORE_TEST_FALSE(s.startsWidth(s2));
  246. CORE_TEST_TRUE(s.startsWidth(s2, 1));
  247. CORE_TEST_FALSE(s.startsWidth(s3));
  248. CORE_TEST_TRUE(s.startsWidth(s3, 2));
  249. CORE_TEST_FALSE(s.startsWidth(s4));
  250. CORE_TEST_TRUE(s.startsWidth(s4, 7));
  251. CORE_TEST_FALSE(s.startsWidth(s5));
  252. CORE_TEST_FALSE(s.startsWidth(s5, 3));
  253. CORE_TEST_TRUE(s.startsWidth(s6));
  254. CORE_TEST_TRUE(s.startsWidth(s6, 3));
  255. CORE_TEST_FALSE(s.startsWidth(s7));
  256. CORE_TEST_FALSE(s.startsWidth(s7, 7));
  257. }
  258. static void testSearch8() {
  259. String8 s;
  260. CORE_TEST_ERROR(s.append("0123456789"));
  261. String8 s2;
  262. CORE_TEST_ERROR(s2.append("123"));
  263. String8 s3;
  264. CORE_TEST_ERROR(s3.append("234"));
  265. String8 s4;
  266. CORE_TEST_ERROR(s4.append("789"));
  267. String8 s5;
  268. CORE_TEST_ERROR(s5.append("124"));
  269. String8 s6;
  270. String8 s7;
  271. CORE_TEST_ERROR(s7.append("7891"));
  272. CORE_TEST_EQUAL(1, s.search(s2));
  273. CORE_TEST_EQUAL(2, s.search(s3));
  274. CORE_TEST_EQUAL(7, s.search(s4));
  275. CORE_TEST_EQUAL(-1, s.search(s5));
  276. CORE_TEST_EQUAL(0, s.search(s6));
  277. CORE_TEST_EQUAL(-1, s.search(s7));
  278. CORE_TEST_EQUAL(-1, s.search(s2, 3));
  279. CORE_TEST_EQUAL(-1, s.search(s3, 3));
  280. CORE_TEST_EQUAL(7, s.search(s4, 3));
  281. CORE_TEST_EQUAL(-1, s.search(s5, 3));
  282. CORE_TEST_EQUAL(3, s.search(s6, 3));
  283. CORE_TEST_EQUAL(-1, s.search(s7, 3));
  284. }
  285. static void testContains8() {
  286. String8 s;
  287. CORE_TEST_ERROR(s.append("0123456789"));
  288. String8 s2;
  289. CORE_TEST_ERROR(s2.append("123"));
  290. String8 s3;
  291. CORE_TEST_ERROR(s3.append("234"));
  292. String8 s4;
  293. CORE_TEST_ERROR(s4.append("789"));
  294. String8 s5;
  295. CORE_TEST_ERROR(s5.append("124"));
  296. String8 s6;
  297. String8 s7;
  298. CORE_TEST_ERROR(s7.append("7891"));
  299. CORE_TEST_TRUE(s.contains(s2));
  300. CORE_TEST_TRUE(s.contains(s3));
  301. CORE_TEST_TRUE(s.contains(s4));
  302. CORE_TEST_FALSE(s.contains(s5));
  303. CORE_TEST_TRUE(s.contains(s6));
  304. CORE_TEST_FALSE(s.contains(s7));
  305. }
  306. static void testSearchChar8() {
  307. String8 s;
  308. CORE_TEST_ERROR(s.append("01üää3ä"));
  309. CORE_TEST_EQUAL(0, s.search('0'));
  310. CORE_TEST_EQUAL(1, s.search('1'));
  311. CORE_TEST_EQUAL(8, s.search('3'));
  312. }
  313. static void testContainsChar8() {
  314. String8 s;
  315. CORE_TEST_ERROR(s.append("01üää3ä"));
  316. CORE_TEST_TRUE(s.contains('0'));
  317. CORE_TEST_TRUE(s.contains('1'));
  318. CORE_TEST_TRUE(s.contains('3'));
  319. CORE_TEST_FALSE(s.contains('a'));
  320. }
  321. static void testSubString8() {
  322. String8 s;
  323. CORE_TEST_ERROR(s.append("01üää3ä"));
  324. CORE_TEST_STRING("01üää3ä", s.substring(-2));
  325. CORE_TEST_STRING("1üää3ä", s.substring(1));
  326. CORE_TEST_STRING("üää3ä", s.substring(2));
  327. CORE_TEST_STRING("ää3ä", s.substring(4));
  328. CORE_TEST_STRING("ä3ä", s.substring(6));
  329. CORE_TEST_STRING("01üää3ä", s.substring(0, 10));
  330. CORE_TEST_STRING("1üää3", s.substring(1, 8));
  331. CORE_TEST_STRING("üää", s.substring(2, 7));
  332. CORE_TEST_STRING("ä", s.substring(4, 5));
  333. CORE_TEST_STRING("", s.substring(4, 2));
  334. CORE_TEST_STRING("ä3ä", s.substring(6, 23));
  335. }
  336. static void testReplace8() {
  337. String8 s;
  338. CORE_TEST_ERROR(s.append("0äääää1üää3ä"));
  339. String8 search;
  340. CORE_TEST_ERROR(search.append("ää"));
  341. String8 replace;
  342. CORE_TEST_ERROR(replace.append("ABCD"));
  343. CORE_TEST_ERROR(s.replace(search, replace));
  344. CORE_TEST_STRING("0ABCDABCDä1üABCD3ä", s);
  345. String8 s2;
  346. CORE_TEST_ERROR(s2.append("0ABCDABCDä1üABCD3ä"));
  347. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  348. }
  349. static void testReplaceChar8() {
  350. String8 s;
  351. CORE_TEST_ERROR(s.append("01YXX3X"));
  352. s.replace('0', 'A');
  353. CORE_TEST_STRING("A1YXX3X", s);
  354. s.replace('1', 'B');
  355. CORE_TEST_STRING("ABYXX3X", s);
  356. s.replace('Y', 'C');
  357. CORE_TEST_STRING("ABCXX3X", s);
  358. s.replace('X', 'D');
  359. CORE_TEST_STRING("ABCDD3D", s);
  360. s.replace('3', 'E');
  361. CORE_TEST_STRING("ABCDDED", s);
  362. String8 s2;
  363. CORE_TEST_ERROR(s2.append("ABCDDED"));
  364. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  365. }
  366. static void testCastAppendSelf8() {
  367. String8 s;
  368. CORE_TEST_ERROR(s.append("abc"));
  369. CORE_TEST_ERROR(s.append(s));
  370. CORE_TEST_ERROR(s.append(static_cast<const char*>(s)));
  371. CORE_TEST_STRING("abcabcabcabc", s);
  372. }
  373. static void testCompareWithShorter8() {
  374. String8 s;
  375. CORE_TEST_ERROR(s.append("abc"));
  376. CORE_TEST_FALSE(s == "ab");
  377. }
  378. static void testAppendSignedChar8() {
  379. const signed char buffer[] = {'a', 'b', 'c', '\0'};
  380. String8 s;
  381. CORE_TEST_ERROR(s.append(buffer));
  382. CORE_TEST_TRUE(s == "abc");
  383. }
  384. static void testAppendUnsignedChar8() {
  385. const unsigned char buffer[] = {'a', 'b', 'c', '\0'};
  386. String8 s;
  387. CORE_TEST_ERROR(s.append(buffer));
  388. CORE_TEST_TRUE(s == "abc");
  389. }
  390. static void testAppendError8() {
  391. String8 s;
  392. CORE_TEST_ERROR(s.append(Core::Error::NONE));
  393. CORE_TEST_TRUE(s == "NONE");
  394. }
  395. static void testPrint8() {
  396. String8 s;
  397. CORE_TEST_ERROR(s.append('\u0040'));
  398. CORE_TEST_ERROR(s.append(L'\u0400'));
  399. CORE_TEST_ERROR(s.append(L'\u8000'));
  400. CORE_TEST_ERROR(s.append(U'\U00100000'));
  401. CORE_TEST_EQUAL(build("\u0040\u0400\u8000\U00100000"), s);
  402. CORE_TEST_ERROR(s.print());
  403. }
  404. static void testKeepHash8() {
  405. String8 s;
  406. CORE_TEST_ERROR(s.append("a ## test #### #####"));
  407. CORE_TEST_ERROR(s.format(1, 2, 3, 4, 5, 6, 7, 8, 9));
  408. CORE_TEST_STRING("a # test ## ##123456789", s);
  409. }
  410. static String32 build(const c32* cs) {
  411. String32 s;
  412. CORE_TEST_ERROR(s.append(cs));
  413. return s;
  414. }
  415. static void testEquality32() {
  416. String32 s = build(U"test");
  417. CORE_TEST_TRUE(s == U"test");
  418. CORE_TEST_TRUE(s == build(U"test"));
  419. CORE_TEST_TRUE(U"test" == s);
  420. CORE_TEST_TRUE(build(U"test") == s);
  421. CORE_TEST_TRUE(s == s);
  422. }
  423. static void testUnicodeEquality32() {
  424. const c32* cs = U"\u0040\u0400\u8000\U00100000";
  425. String32 s = build(cs);
  426. CORE_TEST_TRUE(s == cs);
  427. CORE_TEST_TRUE(s == build(cs));
  428. CORE_TEST_TRUE(cs == s);
  429. CORE_TEST_TRUE(build(cs) == s);
  430. CORE_TEST_TRUE(s == s);
  431. }
  432. static void testInequality32() {
  433. String32 s = build(U"test");
  434. CORE_TEST_FALSE(s != U"test");
  435. CORE_TEST_FALSE(s != build(U"test"));
  436. CORE_TEST_FALSE(U"test" != s);
  437. CORE_TEST_FALSE(build(U"test") != s);
  438. CORE_TEST_FALSE(s != s);
  439. }
  440. static void testStringAppend32() {
  441. String32 s = build(U"test");
  442. CORE_TEST_ERROR(s.append(U"22"));
  443. CORE_TEST_ERROR(s.append(U"333"));
  444. CORE_TEST_ERROR(s.append(U"4444"));
  445. CORE_TEST_EQUAL(build(U"test223334444"), s);
  446. }
  447. static void testStringAppendOverflow32() {
  448. Core::ArrayString<6, c32> s;
  449. CORE_TEST_ERROR(s.append(U"te"));
  450. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(U"23334444"));
  451. CORE_TEST_TRUE(build(U"te23334444") != s);
  452. }
  453. static void testCharacters32() {
  454. String32 s = build(U"test");
  455. CORE_TEST_EQUAL('t', s[0]);
  456. CORE_TEST_EQUAL('e', s[1]);
  457. CORE_TEST_EQUAL('s', s[2]);
  458. CORE_TEST_EQUAL('t', s[3]);
  459. }
  460. static void testLength32() {
  461. String32 s = build(U"test");
  462. CORE_TEST_EQUAL(4, s.getLength());
  463. CORE_TEST_ERROR(s.append(U"aaa"));
  464. CORE_TEST_EQUAL(7, s.getLength());
  465. }
  466. static void testChar32() {
  467. String32 s = build(U"test");
  468. for(char i = 'a'; i < 'd'; i++) {
  469. CORE_TEST_ERROR(s.append(i));
  470. }
  471. CORE_TEST_EQUAL(build(U"testabc"), s);
  472. }
  473. static void testSignedChar32() {
  474. String32 s = build(U"test");
  475. for(signed char i = 'b'; i < 'e'; i++) {
  476. CORE_TEST_ERROR(s.append(i));
  477. }
  478. CORE_TEST_EQUAL(build(U"testbcd"), s);
  479. }
  480. static void testUnsignedChar32() {
  481. String32 s = build(U"test");
  482. for(unsigned char i = 'c'; i < 'f'; i++) {
  483. CORE_TEST_ERROR(s.append(i));
  484. }
  485. CORE_TEST_EQUAL(build(U"testcde"), s);
  486. }
  487. static void testSignedShort32() {
  488. String32 s = build(U"test");
  489. for(signed short i = 100; i < 103; i++) {
  490. CORE_TEST_ERROR(s.append(i));
  491. }
  492. CORE_TEST_EQUAL(build(U"test100101102"), s);
  493. }
  494. static void testUnsignedShort32() {
  495. String32 s = build(U"test");
  496. for(unsigned short i = 101; i < 104; i++) {
  497. CORE_TEST_ERROR(s.append(i));
  498. }
  499. CORE_TEST_EQUAL(build(U"test101102103"), s);
  500. }
  501. static void testSignedInt32() {
  502. String32 s = build(U"test");
  503. for(signed int i = 102; i < 105; i++) {
  504. CORE_TEST_ERROR(s.append(i));
  505. }
  506. CORE_TEST_EQUAL(build(U"test102103104"), s);
  507. }
  508. static void testUnsignedInt32() {
  509. String32 s = build(U"test");
  510. for(unsigned int i = 103; i < 106; i++) {
  511. CORE_TEST_ERROR(s.append(i));
  512. }
  513. CORE_TEST_EQUAL(build(U"test103104105"), s);
  514. }
  515. static void testSignedLong32() {
  516. String32 s = build(U"test");
  517. for(signed long i = 104; i < 107; i++) {
  518. CORE_TEST_ERROR(s.append(i));
  519. }
  520. CORE_TEST_EQUAL(build(U"test104105106"), s);
  521. }
  522. static void testUnsignedLong32() {
  523. String32 s = build(U"test");
  524. for(unsigned long i = 105; i < 108; i++) {
  525. CORE_TEST_ERROR(s.append(i));
  526. }
  527. CORE_TEST_EQUAL(build(U"test105106107"), s);
  528. }
  529. static void testSignedLongLong32() {
  530. String32 s = build(U"test");
  531. for(signed long long i = 106; i < 109; i++) {
  532. CORE_TEST_ERROR(s.append(i));
  533. }
  534. CORE_TEST_EQUAL(build(U"test106107108"), s);
  535. }
  536. static void testUnsignedLongLong32() {
  537. String32 s = build(U"test");
  538. for(unsigned long long i = 107; i < 110; i++) {
  539. CORE_TEST_ERROR(s.append(i));
  540. }
  541. CORE_TEST_EQUAL(build(U"test107108109"), s);
  542. }
  543. static void testFloat32() {
  544. String32 s = build(U"test");
  545. for(float i = 108; i < 111; i++) {
  546. CORE_TEST_ERROR(s.append(i));
  547. }
  548. CORE_TEST_EQUAL(build(U"test108.00109.00110.00"), s);
  549. }
  550. static void testDouble32() {
  551. String32 s = build(U"test");
  552. for(double i = 109; i < 112; i++) {
  553. CORE_TEST_ERROR(s.append(i));
  554. }
  555. CORE_TEST_EQUAL(build(U"test109.00110.00111.00"), s);
  556. }
  557. static void testLongDouble32() {
  558. String32 s = build(U"test");
  559. for(long double i = 110; i < 113; i++) {
  560. CORE_TEST_ERROR(s.append(i));
  561. }
  562. CORE_TEST_EQUAL(build(U"test110.00111.00112.00"), s);
  563. }
  564. static void testBool32() {
  565. String32 s = build(U"test");
  566. CORE_TEST_ERROR(s.append(true));
  567. CORE_TEST_ERROR(s.append(false));
  568. CORE_TEST_ERROR(s.append(true));
  569. CORE_TEST_EQUAL(build(U"testtruefalsetrue"), s);
  570. }
  571. static void testIntOverflow32() {
  572. Core::ArrayString<4, c32> s;
  573. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
  574. String32 o;
  575. for(int i = 0; i < s.getCapacity(); i++) {
  576. CORE_TEST_ERROR(o.append(i + 1));
  577. }
  578. CORE_TEST_TRUE(o == s);
  579. }
  580. static void testUnicode32() {
  581. String32 s;
  582. CORE_TEST_ERROR(s.append('\u0040'));
  583. CORE_TEST_ERROR(s.append(L'\u0400'));
  584. CORE_TEST_ERROR(s.append(L'\u8000'));
  585. CORE_TEST_ERROR(s.append(U'\U00100000'));
  586. CORE_TEST_EQUAL(build(U"\u0040\u0400\u8000\U00100000"), s);
  587. }
  588. static void testClear32() {
  589. String32 s = build(U"test");
  590. CORE_TEST_ERROR(s.append(1234));
  591. s.clear();
  592. CORE_TEST_ERROR(s.append(U"wusi"));
  593. CORE_TEST_ERROR(s.append(U"1234"));
  594. CORE_TEST_EQUAL(build(U"wusi1234"), s);
  595. }
  596. static void testHashCode32() {
  597. String32 s;
  598. CORE_TEST_ERROR(s.append(U"a"));
  599. CORE_TEST_ERROR(s.append(U"bc"));
  600. CORE_TEST_ERROR(s.append(20));
  601. CORE_TEST_ERROR(s.append(25.5f));
  602. CORE_TEST_ERROR(s.append(true));
  603. CORE_TEST_EQUAL(build(U"abc2025.50true").hashCode(), s.hashCode());
  604. s.clear();
  605. CORE_TEST_EQUAL(String32().hashCode(), s.hashCode());
  606. }
  607. static void testAddSelf32() {
  608. String32 s;
  609. CORE_TEST_ERROR(s.append(U"test1"));
  610. CORE_TEST_ERROR(s.append(s));
  611. CORE_TEST_ERROR(s.append(s));
  612. CORE_TEST_EQUAL(build(U"test1test1test1test1"), s);
  613. }
  614. static void testAsHashMapKey32() {
  615. Core::HashMap<String32, int> map;
  616. CORE_TEST_ERROR(map.add(build(U"wusi"), 3));
  617. CORE_TEST_ERROR(map.add(build(U"hiThere"), 7));
  618. CORE_TEST_ERROR(map.add(build(U"baum123"), 5));
  619. int* a = map.search(build(U"wusi"));
  620. int* b = map.search(build(U"hiThere"));
  621. int* c = map.search(build(U"baum123"));
  622. int* d = map.search(build(U"423hifd"));
  623. CORE_TEST_NOT_NULL(a);
  624. CORE_TEST_NOT_NULL(b);
  625. CORE_TEST_NOT_NULL(c);
  626. CORE_TEST_NULL(d);
  627. if(a != nullptr && b != nullptr && c != nullptr) {
  628. CORE_TEST_EQUAL(3, *a);
  629. CORE_TEST_EQUAL(7, *b);
  630. CORE_TEST_EQUAL(5, *c);
  631. }
  632. }
  633. static void testStartsWith32() {
  634. String32 s;
  635. CORE_TEST_ERROR(s.append(U"0123456789"));
  636. String32 s2;
  637. CORE_TEST_ERROR(s2.append(U"123"));
  638. String32 s3;
  639. CORE_TEST_ERROR(s3.append(U"234"));
  640. String32 s4;
  641. CORE_TEST_ERROR(s4.append(U"789"));
  642. String32 s5;
  643. CORE_TEST_ERROR(s5.append(U"124"));
  644. String32 s6;
  645. String32 s7;
  646. CORE_TEST_ERROR(s7.append(U"7891"));
  647. CORE_TEST_FALSE(s.startsWidth(s2));
  648. CORE_TEST_TRUE(s.startsWidth(s2, 1));
  649. CORE_TEST_FALSE(s.startsWidth(s3));
  650. CORE_TEST_TRUE(s.startsWidth(s3, 2));
  651. CORE_TEST_FALSE(s.startsWidth(s4));
  652. CORE_TEST_TRUE(s.startsWidth(s4, 7));
  653. CORE_TEST_FALSE(s.startsWidth(s5));
  654. CORE_TEST_FALSE(s.startsWidth(s5, 3));
  655. CORE_TEST_TRUE(s.startsWidth(s6));
  656. CORE_TEST_TRUE(s.startsWidth(s6, 3));
  657. CORE_TEST_FALSE(s.startsWidth(s7));
  658. CORE_TEST_FALSE(s.startsWidth(s7, 7));
  659. }
  660. static void testSearch32() {
  661. String32 s;
  662. CORE_TEST_ERROR(s.append(U"0123456789"));
  663. String32 s2;
  664. CORE_TEST_ERROR(s2.append(U"123"));
  665. String32 s3;
  666. CORE_TEST_ERROR(s3.append(U"234"));
  667. String32 s4;
  668. CORE_TEST_ERROR(s4.append(U"789"));
  669. String32 s5;
  670. CORE_TEST_ERROR(s5.append(U"124"));
  671. String32 s6;
  672. String32 s7;
  673. CORE_TEST_ERROR(s7.append(U"7891"));
  674. CORE_TEST_EQUAL(1, s.search(s2));
  675. CORE_TEST_EQUAL(2, s.search(s3));
  676. CORE_TEST_EQUAL(7, s.search(s4));
  677. CORE_TEST_EQUAL(-1, s.search(s5));
  678. CORE_TEST_EQUAL(0, s.search(s6));
  679. CORE_TEST_EQUAL(-1, s.search(s7));
  680. CORE_TEST_EQUAL(-1, s.search(s2, 3));
  681. CORE_TEST_EQUAL(-1, s.search(s3, 3));
  682. CORE_TEST_EQUAL(7, s.search(s4, 3));
  683. CORE_TEST_EQUAL(-1, s.search(s5, 3));
  684. CORE_TEST_EQUAL(3, s.search(s6, 3));
  685. CORE_TEST_EQUAL(-1, s.search(s7, 3));
  686. }
  687. static void testContains32() {
  688. String32 s;
  689. CORE_TEST_ERROR(s.append(U"0123456789"));
  690. String32 s2;
  691. CORE_TEST_ERROR(s2.append(U"123"));
  692. String32 s3;
  693. CORE_TEST_ERROR(s3.append(U"234"));
  694. String32 s4;
  695. CORE_TEST_ERROR(s4.append(U"789"));
  696. String32 s5;
  697. CORE_TEST_ERROR(s5.append(U"124"));
  698. String32 s6;
  699. String32 s7;
  700. CORE_TEST_ERROR(s7.append(U"7891"));
  701. CORE_TEST_TRUE(s.contains(s2));
  702. CORE_TEST_TRUE(s.contains(s3));
  703. CORE_TEST_TRUE(s.contains(s4));
  704. CORE_TEST_FALSE(s.contains(s5));
  705. CORE_TEST_TRUE(s.contains(s6));
  706. CORE_TEST_FALSE(s.contains(s7));
  707. }
  708. static void testSearchChar32() {
  709. String32 s;
  710. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  711. CORE_TEST_EQUAL(0, s.search('0'));
  712. CORE_TEST_EQUAL(1, s.search('1'));
  713. CORE_TEST_EQUAL(5, s.search('3'));
  714. CORE_TEST_EQUAL(2, s.search(U'ü'));
  715. CORE_TEST_EQUAL(3, s.search(U'ä'));
  716. CORE_TEST_EQUAL(4, s.search(U'ä', 4));
  717. CORE_TEST_EQUAL(6, s.search(U'ä', 5));
  718. }
  719. static void testContainsChar32() {
  720. String32 s;
  721. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  722. CORE_TEST_TRUE(s.contains(U'0'));
  723. CORE_TEST_TRUE(s.contains(U'1'));
  724. CORE_TEST_TRUE(s.contains(U'3'));
  725. CORE_TEST_FALSE(s.contains(U'a'));
  726. CORE_TEST_TRUE(s.contains(U'0'));
  727. CORE_TEST_TRUE(s.contains(U'1'));
  728. CORE_TEST_TRUE(s.contains(U'ü'));
  729. CORE_TEST_TRUE(s.contains(U'ä'));
  730. CORE_TEST_FALSE(s.contains(U'ö'));
  731. }
  732. static void testSubString32() {
  733. String32 s;
  734. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  735. CORE_TEST_STRING(U"01üää3ä", s.substring(-2));
  736. CORE_TEST_STRING(U"1üää3ä", s.substring(1));
  737. CORE_TEST_STRING(U"üää3ä", s.substring(2));
  738. CORE_TEST_STRING(U"ää3ä", s.substring(3));
  739. CORE_TEST_STRING(U"ä3ä", s.substring(4));
  740. CORE_TEST_STRING(U"01üää3ä", s.substring(0, 6));
  741. CORE_TEST_STRING(U"1üää3", s.substring(1, 5));
  742. CORE_TEST_STRING(U"üää", s.substring(2, 4));
  743. CORE_TEST_STRING(U"ä", s.substring(3, 3));
  744. CORE_TEST_STRING(U"", s.substring(4, 2));
  745. CORE_TEST_STRING(U"ä3ä", s.substring(4, 23));
  746. }
  747. static void testReplace32() {
  748. String32 s;
  749. CORE_TEST_ERROR(s.append(U"0äääää1üää3ä"));
  750. String32 search;
  751. CORE_TEST_ERROR(search.append(U"ää"));
  752. String32 replace;
  753. CORE_TEST_ERROR(replace.append(U"ABCD"));
  754. CORE_TEST_ERROR(s.replace(search, replace));
  755. CORE_TEST_STRING(U"0ABCDABCDä1üABCD3ä", s);
  756. String32 s2;
  757. CORE_TEST_ERROR(s2.append(U"0ABCDABCDä1üABCD3ä"));
  758. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  759. }
  760. static void testReplaceChar32() {
  761. String32 s;
  762. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  763. s.replace(U'0', U'A');
  764. CORE_TEST_STRING(U"A1üää3ä", s);
  765. s.replace(U'1', U'B');
  766. CORE_TEST_STRING(U"ABüää3ä", s);
  767. s.replace(U'ü', U'C');
  768. CORE_TEST_STRING(U"ABCää3ä", s);
  769. s.replace(U'ä', U'D');
  770. CORE_TEST_STRING(U"ABCDD3D", s);
  771. s.replace(U'3', U'E');
  772. CORE_TEST_STRING(U"ABCDDED", s);
  773. String32 s2;
  774. CORE_TEST_ERROR(s2.append(U"ABCDDED"));
  775. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  776. }
  777. static void testCastAppendSelf32() {
  778. String32 s;
  779. CORE_TEST_ERROR(s.append("abc"));
  780. CORE_TEST_ERROR(s.append(s));
  781. CORE_TEST_ERROR(s.append(static_cast<const c32*>(s)));
  782. CORE_TEST_STRING("abcabcabcabc", s);
  783. }
  784. static void testCompareWithShorter32() {
  785. String32 s;
  786. CORE_TEST_ERROR(s.append("abc"));
  787. CORE_TEST_FALSE(s == U"ab");
  788. }
  789. static void testAppendSignedChar32() {
  790. const signed char buffer[] = {'a', 'b', 'c', '\0'};
  791. String32 s;
  792. CORE_TEST_ERROR(s.append(buffer));
  793. CORE_TEST_TRUE(s == U"abc");
  794. }
  795. static void testAppendUnsignedChar32() {
  796. const unsigned char buffer[] = {'a', 'b', 'c', '\0'};
  797. String32 s;
  798. CORE_TEST_ERROR(s.append(buffer));
  799. CORE_TEST_TRUE(s == U"abc");
  800. }
  801. static void testAppendError32() {
  802. String32 s;
  803. CORE_TEST_ERROR(s.append(Core::Error::NONE));
  804. CORE_TEST_TRUE(s == U"NONE");
  805. }
  806. static void testPrint32() {
  807. String32 s;
  808. CORE_TEST_ERROR(s.append('\u0040'));
  809. CORE_TEST_ERROR(s.append(L'\u0400'));
  810. CORE_TEST_ERROR(s.append(L'\u8000'));
  811. CORE_TEST_ERROR(s.append(U'\U00100000'));
  812. CORE_TEST_EQUAL(build(U"\u0040\u0400\u8000\U00100000"), s);
  813. CORE_TEST_ERROR(s.print());
  814. }
  815. static void testVariousUnicode32() {
  816. const unsigned char buffer[] = {0xC0, 0};
  817. const unsigned char buffer2[] = {0xE0, 0};
  818. const unsigned char buffer3[] = {0xE0, 1, 2, 0};
  819. const unsigned char buffer4[] = {0xF0, 0};
  820. const unsigned char buffer5[] = {0xF0, 1, 2, 3, 0};
  821. const unsigned char buffer6[] = {0xFF, 0};
  822. String32 s;
  823. CORE_TEST_EQUAL(Core::Error::INVALID_CHAR, s.append(buffer));
  824. CORE_TEST_EQUAL(Core::Error::INVALID_CHAR, s.append(buffer2));
  825. CORE_TEST_EQUAL(Core::Error::NONE, s.append(buffer3));
  826. CORE_TEST_EQUAL(Core::Error::INVALID_CHAR, s.append(buffer4));
  827. CORE_TEST_EQUAL(Core::Error::NONE, s.append(buffer5));
  828. CORE_TEST_EQUAL(Core::Error::INVALID_CHAR, s.append(buffer6));
  829. }
  830. static void testKeepHash32() {
  831. String32 s;
  832. CORE_TEST_ERROR(s.append("a ## test #### #####"));
  833. CORE_TEST_ERROR(s.format(1, 2, 3, 4, 5, 6, 7, 8, 9));
  834. CORE_TEST_STRING("a # test ## ##123456789", s);
  835. }
  836. static void testConversion() {
  837. const c32* a = U"öüewfde_§$§%$ädsf";
  838. const char* b = "öüewfde_§$§%$ädsf";
  839. String32 sa;
  840. CORE_TEST_ERROR(sa.append(a));
  841. String8 sb;
  842. CORE_TEST_ERROR(sb.append(b));
  843. String8 sa2;
  844. CORE_TEST_ERROR(sa2.append(sa));
  845. String32 sb2;
  846. CORE_TEST_ERROR(sb2.append(sb));
  847. CORE_TEST_STRING(a, sa2);
  848. CORE_TEST_STRING(b, sb2);
  849. }
  850. void Core::testArrayString() {
  851. testEquality8();
  852. testUnicodeEquality8();
  853. testInequality8();
  854. testStringAppend8();
  855. testStringAppendOverflow8();
  856. testCharacters8();
  857. testLength8();
  858. testChar8();
  859. testSignedChar8();
  860. testUnsignedChar8();
  861. testSignedShort8();
  862. testUnsignedShort8();
  863. testSignedInt8();
  864. testUnsignedInt8();
  865. testSignedLong8();
  866. testUnsignedLong8();
  867. testSignedLongLong8();
  868. testUnsignedLongLong8();
  869. testFloat8();
  870. testDouble8();
  871. testLongDouble8();
  872. testBool8();
  873. testIntOverflow8();
  874. testUnicode8();
  875. testClear8();
  876. testHashCode8();
  877. testAddSelf8();
  878. testAsHashMapKey8();
  879. testStartsWith8();
  880. testSearch8();
  881. testContains8();
  882. testSearchChar8();
  883. testContainsChar8();
  884. testSubString8();
  885. testReplace8();
  886. testReplaceChar8();
  887. testCastAppendSelf8();
  888. testCompareWithShorter8();
  889. testAppendSignedChar8();
  890. testAppendUnsignedChar8();
  891. testAppendError8();
  892. testPrint8();
  893. testKeepHash8();
  894. testEquality32();
  895. testUnicodeEquality32();
  896. testInequality32();
  897. testStringAppend32();
  898. testStringAppendOverflow32();
  899. testCharacters32();
  900. testLength32();
  901. testChar32();
  902. testSignedChar32();
  903. testUnsignedChar32();
  904. testSignedShort32();
  905. testUnsignedShort32();
  906. testSignedInt32();
  907. testUnsignedInt32();
  908. testSignedLong32();
  909. testUnsignedLong32();
  910. testSignedLongLong32();
  911. testUnsignedLongLong32();
  912. testFloat32();
  913. testDouble32();
  914. testLongDouble32();
  915. testBool32();
  916. testIntOverflow32();
  917. testUnicode32();
  918. testClear32();
  919. testHashCode32();
  920. testAddSelf32();
  921. testAsHashMapKey32();
  922. testStartsWith32();
  923. testSearch32();
  924. testContains32();
  925. testSearchChar32();
  926. testContainsChar32();
  927. testSubString32();
  928. testReplace32();
  929. testReplaceChar32();
  930. testCastAppendSelf32();
  931. testCompareWithShorter32();
  932. testAppendSignedChar32();
  933. testAppendUnsignedChar32();
  934. testAppendError32();
  935. testPrint32();
  936. testVariousUnicode32();
  937. testKeepHash32();
  938. testConversion();
  939. }