ArrayStringTests.cpp 29 KB

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