ArrayStringTests.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 String32 build(const c32* cs) {
  374. String32 s;
  375. CORE_TEST_ERROR(s.append(cs));
  376. return s;
  377. }
  378. static void testEquality32() {
  379. String32 s = build(U"test");
  380. CORE_TEST_TRUE(s == U"test");
  381. CORE_TEST_TRUE(s == build(U"test"));
  382. CORE_TEST_TRUE(U"test" == s);
  383. CORE_TEST_TRUE(build(U"test") == s);
  384. CORE_TEST_TRUE(s == s);
  385. }
  386. static void testUnicodeEquality32() {
  387. const c32* cs = U"\u0040\u0400\u8000\U00100000";
  388. String32 s = build(cs);
  389. CORE_TEST_TRUE(s == cs);
  390. CORE_TEST_TRUE(s == build(cs));
  391. CORE_TEST_TRUE(cs == s);
  392. CORE_TEST_TRUE(build(cs) == s);
  393. CORE_TEST_TRUE(s == s);
  394. }
  395. static void testInequality32() {
  396. String32 s = build(U"test");
  397. CORE_TEST_FALSE(s != U"test");
  398. CORE_TEST_FALSE(s != build(U"test"));
  399. CORE_TEST_FALSE(U"test" != s);
  400. CORE_TEST_FALSE(build(U"test") != s);
  401. CORE_TEST_FALSE(s != s);
  402. }
  403. static void testStringAppend32() {
  404. String32 s = build(U"test");
  405. CORE_TEST_ERROR(s.append(U"22"));
  406. CORE_TEST_ERROR(s.append(U"333"));
  407. CORE_TEST_ERROR(s.append(U"4444"));
  408. CORE_TEST_EQUAL(build(U"test223334444"), s);
  409. }
  410. static void testStringAppendOverflow32() {
  411. Core::ArrayString<6, c32> s;
  412. CORE_TEST_ERROR(s.append(U"te"));
  413. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(U"23334444"));
  414. CORE_TEST_TRUE(build(U"te23334444") != s);
  415. }
  416. static void testCharacters32() {
  417. String32 s = build(U"test");
  418. CORE_TEST_EQUAL('t', s[0]);
  419. CORE_TEST_EQUAL('e', s[1]);
  420. CORE_TEST_EQUAL('s', s[2]);
  421. CORE_TEST_EQUAL('t', s[3]);
  422. }
  423. static void testLength32() {
  424. String32 s = build(U"test");
  425. CORE_TEST_EQUAL(4, s.getLength());
  426. CORE_TEST_ERROR(s.append(U"aaa"));
  427. CORE_TEST_EQUAL(7, s.getLength());
  428. }
  429. static void testChar32() {
  430. String32 s = build(U"test");
  431. for(char i = 'a'; i < 'd'; i++) {
  432. CORE_TEST_ERROR(s.append(i));
  433. }
  434. CORE_TEST_EQUAL(build(U"testabc"), s);
  435. }
  436. static void testSignedChar32() {
  437. String32 s = build(U"test");
  438. for(signed char i = 'b'; i < 'e'; i++) {
  439. CORE_TEST_ERROR(s.append(i));
  440. }
  441. CORE_TEST_EQUAL(build(U"testbcd"), s);
  442. }
  443. static void testUnsignedChar32() {
  444. String32 s = build(U"test");
  445. for(unsigned char i = 'c'; i < 'f'; i++) {
  446. CORE_TEST_ERROR(s.append(i));
  447. }
  448. CORE_TEST_EQUAL(build(U"testcde"), s);
  449. }
  450. static void testSignedShort32() {
  451. String32 s = build(U"test");
  452. for(signed short i = 100; i < 103; i++) {
  453. CORE_TEST_ERROR(s.append(i));
  454. }
  455. CORE_TEST_EQUAL(build(U"test100101102"), s);
  456. }
  457. static void testUnsignedShort32() {
  458. String32 s = build(U"test");
  459. for(unsigned short i = 101; i < 104; i++) {
  460. CORE_TEST_ERROR(s.append(i));
  461. }
  462. CORE_TEST_EQUAL(build(U"test101102103"), s);
  463. }
  464. static void testSignedInt32() {
  465. String32 s = build(U"test");
  466. for(signed int i = 102; i < 105; i++) {
  467. CORE_TEST_ERROR(s.append(i));
  468. }
  469. CORE_TEST_EQUAL(build(U"test102103104"), s);
  470. }
  471. static void testUnsignedInt32() {
  472. String32 s = build(U"test");
  473. for(unsigned int i = 103; i < 106; i++) {
  474. CORE_TEST_ERROR(s.append(i));
  475. }
  476. CORE_TEST_EQUAL(build(U"test103104105"), s);
  477. }
  478. static void testSignedLong32() {
  479. String32 s = build(U"test");
  480. for(signed long i = 104; i < 107; i++) {
  481. CORE_TEST_ERROR(s.append(i));
  482. }
  483. CORE_TEST_EQUAL(build(U"test104105106"), s);
  484. }
  485. static void testUnsignedLong32() {
  486. String32 s = build(U"test");
  487. for(unsigned long i = 105; i < 108; i++) {
  488. CORE_TEST_ERROR(s.append(i));
  489. }
  490. CORE_TEST_EQUAL(build(U"test105106107"), s);
  491. }
  492. static void testSignedLongLong32() {
  493. String32 s = build(U"test");
  494. for(signed long long i = 106; i < 109; i++) {
  495. CORE_TEST_ERROR(s.append(i));
  496. }
  497. CORE_TEST_EQUAL(build(U"test106107108"), s);
  498. }
  499. static void testUnsignedLongLong32() {
  500. String32 s = build(U"test");
  501. for(unsigned long long i = 107; i < 110; i++) {
  502. CORE_TEST_ERROR(s.append(i));
  503. }
  504. CORE_TEST_EQUAL(build(U"test107108109"), s);
  505. }
  506. static void testFloat32() {
  507. String32 s = build(U"test");
  508. for(float i = 108; i < 111; i++) {
  509. CORE_TEST_ERROR(s.append(i));
  510. }
  511. CORE_TEST_EQUAL(build(U"test108.00109.00110.00"), s);
  512. }
  513. static void testDouble32() {
  514. String32 s = build(U"test");
  515. for(double i = 109; i < 112; i++) {
  516. CORE_TEST_ERROR(s.append(i));
  517. }
  518. CORE_TEST_EQUAL(build(U"test109.00110.00111.00"), s);
  519. }
  520. static void testLongDouble32() {
  521. String32 s = build(U"test");
  522. for(long double i = 110; i < 113; i++) {
  523. CORE_TEST_ERROR(s.append(i));
  524. }
  525. CORE_TEST_EQUAL(build(U"test110.00111.00112.00"), s);
  526. }
  527. static void testBool32() {
  528. String32 s = build(U"test");
  529. CORE_TEST_ERROR(s.append(true));
  530. CORE_TEST_ERROR(s.append(false));
  531. CORE_TEST_ERROR(s.append(true));
  532. CORE_TEST_EQUAL(build(U"testtruefalsetrue"), s);
  533. }
  534. static void testIntOverflow32() {
  535. Core::ArrayString<4, c32> s;
  536. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
  537. String32 o;
  538. for(int i = 0; i < s.getCapacity(); i++) {
  539. CORE_TEST_ERROR(o.append(i + 1));
  540. }
  541. CORE_TEST_TRUE(o == s);
  542. }
  543. static void testUnicode32() {
  544. String32 s;
  545. CORE_TEST_ERROR(s.append('\u0040'));
  546. CORE_TEST_ERROR(s.append(L'\u0400'));
  547. CORE_TEST_ERROR(s.append(L'\u8000'));
  548. CORE_TEST_ERROR(s.append(U'\U00100000'));
  549. CORE_TEST_EQUAL(build(U"\u0040\u0400\u8000\U00100000"), s);
  550. }
  551. static void testClear32() {
  552. String32 s = build(U"test");
  553. CORE_TEST_ERROR(s.append(1234));
  554. s.clear();
  555. CORE_TEST_ERROR(s.append(U"wusi"));
  556. CORE_TEST_ERROR(s.append(U"1234"));
  557. CORE_TEST_EQUAL(build(U"wusi1234"), s);
  558. }
  559. static void testHashCode32() {
  560. String32 s;
  561. CORE_TEST_ERROR(s.append(U"a"));
  562. CORE_TEST_ERROR(s.append(U"bc"));
  563. CORE_TEST_ERROR(s.append(20));
  564. CORE_TEST_ERROR(s.append(25.5f));
  565. CORE_TEST_ERROR(s.append(true));
  566. CORE_TEST_EQUAL(build(U"abc2025.50true").hashCode(), s.hashCode());
  567. s.clear();
  568. CORE_TEST_EQUAL(String32().hashCode(), s.hashCode());
  569. }
  570. static void testAddSelf32() {
  571. String32 s;
  572. CORE_TEST_ERROR(s.append(U"test1"));
  573. CORE_TEST_ERROR(s.append(s));
  574. CORE_TEST_ERROR(s.append(s));
  575. CORE_TEST_EQUAL(build(U"test1test1test1test1"), s);
  576. }
  577. static void testAsHashMapKey32() {
  578. Core::HashMap<String32, int> map;
  579. CORE_TEST_ERROR(map.add(build(U"wusi"), 3));
  580. CORE_TEST_ERROR(map.add(build(U"hiThere"), 7));
  581. CORE_TEST_ERROR(map.add(build(U"baum123"), 5));
  582. int* a = map.search(build(U"wusi"));
  583. int* b = map.search(build(U"hiThere"));
  584. int* c = map.search(build(U"baum123"));
  585. int* d = map.search(build(U"423hifd"));
  586. CORE_TEST_NOT_NULL(a);
  587. CORE_TEST_NOT_NULL(b);
  588. CORE_TEST_NOT_NULL(c);
  589. CORE_TEST_NULL(d);
  590. if(a != nullptr && b != nullptr && c != nullptr) {
  591. CORE_TEST_EQUAL(3, *a);
  592. CORE_TEST_EQUAL(7, *b);
  593. CORE_TEST_EQUAL(5, *c);
  594. }
  595. }
  596. static void testStartsWith32() {
  597. String32 s;
  598. CORE_TEST_ERROR(s.append(U"0123456789"));
  599. String32 s2;
  600. CORE_TEST_ERROR(s2.append(U"123"));
  601. String32 s3;
  602. CORE_TEST_ERROR(s3.append(U"234"));
  603. String32 s4;
  604. CORE_TEST_ERROR(s4.append(U"789"));
  605. String32 s5;
  606. CORE_TEST_ERROR(s5.append(U"124"));
  607. String32 s6;
  608. String32 s7;
  609. CORE_TEST_ERROR(s7.append(U"7891"));
  610. CORE_TEST_FALSE(s.startsWidth(s2));
  611. CORE_TEST_TRUE(s.startsWidth(s2, 1));
  612. CORE_TEST_FALSE(s.startsWidth(s3));
  613. CORE_TEST_TRUE(s.startsWidth(s3, 2));
  614. CORE_TEST_FALSE(s.startsWidth(s4));
  615. CORE_TEST_TRUE(s.startsWidth(s4, 7));
  616. CORE_TEST_FALSE(s.startsWidth(s5));
  617. CORE_TEST_FALSE(s.startsWidth(s5, 3));
  618. CORE_TEST_TRUE(s.startsWidth(s6));
  619. CORE_TEST_TRUE(s.startsWidth(s6, 3));
  620. CORE_TEST_FALSE(s.startsWidth(s7));
  621. CORE_TEST_FALSE(s.startsWidth(s7, 7));
  622. }
  623. static void testSearch32() {
  624. String32 s;
  625. CORE_TEST_ERROR(s.append(U"0123456789"));
  626. String32 s2;
  627. CORE_TEST_ERROR(s2.append(U"123"));
  628. String32 s3;
  629. CORE_TEST_ERROR(s3.append(U"234"));
  630. String32 s4;
  631. CORE_TEST_ERROR(s4.append(U"789"));
  632. String32 s5;
  633. CORE_TEST_ERROR(s5.append(U"124"));
  634. String32 s6;
  635. String32 s7;
  636. CORE_TEST_ERROR(s7.append(U"7891"));
  637. CORE_TEST_EQUAL(1, s.search(s2));
  638. CORE_TEST_EQUAL(2, s.search(s3));
  639. CORE_TEST_EQUAL(7, s.search(s4));
  640. CORE_TEST_EQUAL(-1, s.search(s5));
  641. CORE_TEST_EQUAL(0, s.search(s6));
  642. CORE_TEST_EQUAL(-1, s.search(s7));
  643. CORE_TEST_EQUAL(-1, s.search(s2, 3));
  644. CORE_TEST_EQUAL(-1, s.search(s3, 3));
  645. CORE_TEST_EQUAL(7, s.search(s4, 3));
  646. CORE_TEST_EQUAL(-1, s.search(s5, 3));
  647. CORE_TEST_EQUAL(3, s.search(s6, 3));
  648. CORE_TEST_EQUAL(-1, s.search(s7, 3));
  649. }
  650. static void testContains32() {
  651. String32 s;
  652. CORE_TEST_ERROR(s.append(U"0123456789"));
  653. String32 s2;
  654. CORE_TEST_ERROR(s2.append(U"123"));
  655. String32 s3;
  656. CORE_TEST_ERROR(s3.append(U"234"));
  657. String32 s4;
  658. CORE_TEST_ERROR(s4.append(U"789"));
  659. String32 s5;
  660. CORE_TEST_ERROR(s5.append(U"124"));
  661. String32 s6;
  662. String32 s7;
  663. CORE_TEST_ERROR(s7.append(U"7891"));
  664. CORE_TEST_TRUE(s.contains(s2));
  665. CORE_TEST_TRUE(s.contains(s3));
  666. CORE_TEST_TRUE(s.contains(s4));
  667. CORE_TEST_FALSE(s.contains(s5));
  668. CORE_TEST_TRUE(s.contains(s6));
  669. CORE_TEST_FALSE(s.contains(s7));
  670. }
  671. static void testSearchChar32() {
  672. String32 s;
  673. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  674. CORE_TEST_EQUAL(0, s.search('0'));
  675. CORE_TEST_EQUAL(1, s.search('1'));
  676. CORE_TEST_EQUAL(5, s.search('3'));
  677. CORE_TEST_EQUAL(2, s.search(U'ü'));
  678. CORE_TEST_EQUAL(3, s.search(U'ä'));
  679. CORE_TEST_EQUAL(4, s.search(U'ä', 4));
  680. CORE_TEST_EQUAL(6, s.search(U'ä', 5));
  681. }
  682. static void testContainsChar32() {
  683. String32 s;
  684. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  685. CORE_TEST_TRUE(s.contains(U'0'));
  686. CORE_TEST_TRUE(s.contains(U'1'));
  687. CORE_TEST_TRUE(s.contains(U'3'));
  688. CORE_TEST_FALSE(s.contains(U'a'));
  689. CORE_TEST_TRUE(s.contains(U'0'));
  690. CORE_TEST_TRUE(s.contains(U'1'));
  691. CORE_TEST_TRUE(s.contains(U'ü'));
  692. CORE_TEST_TRUE(s.contains(U'ä'));
  693. CORE_TEST_FALSE(s.contains(U'ö'));
  694. }
  695. static void testSubString32() {
  696. String32 s;
  697. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  698. CORE_TEST_STRING(U"01üää3ä", s.substring(-2));
  699. CORE_TEST_STRING(U"1üää3ä", s.substring(1));
  700. CORE_TEST_STRING(U"üää3ä", s.substring(2));
  701. CORE_TEST_STRING(U"ää3ä", s.substring(3));
  702. CORE_TEST_STRING(U"ä3ä", s.substring(4));
  703. CORE_TEST_STRING(U"01üää3ä", s.substring(0, 6));
  704. CORE_TEST_STRING(U"1üää3", s.substring(1, 5));
  705. CORE_TEST_STRING(U"üää", s.substring(2, 4));
  706. CORE_TEST_STRING(U"ä", s.substring(3, 3));
  707. CORE_TEST_STRING(U"", s.substring(4, 2));
  708. CORE_TEST_STRING(U"ä3ä", s.substring(4, 23));
  709. }
  710. static void testReplace32() {
  711. String32 s;
  712. CORE_TEST_ERROR(s.append(U"0äääää1üää3ä"));
  713. String32 search;
  714. CORE_TEST_ERROR(search.append(U"ää"));
  715. String32 replace;
  716. CORE_TEST_ERROR(replace.append(U"ABCD"));
  717. CORE_TEST_ERROR(s.replace(search, replace));
  718. CORE_TEST_STRING(U"0ABCDABCDä1üABCD3ä", s);
  719. String32 s2;
  720. CORE_TEST_ERROR(s2.append(U"0ABCDABCDä1üABCD3ä"));
  721. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  722. }
  723. static void testReplaceChar32() {
  724. String32 s;
  725. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  726. s.replace(U'0', U'A');
  727. CORE_TEST_STRING(U"A1üää3ä", s);
  728. s.replace(U'1', U'B');
  729. CORE_TEST_STRING(U"ABüää3ä", s);
  730. s.replace(U'ü', U'C');
  731. CORE_TEST_STRING(U"ABCää3ä", s);
  732. s.replace(U'ä', U'D');
  733. CORE_TEST_STRING(U"ABCDD3D", s);
  734. s.replace(U'3', U'E');
  735. CORE_TEST_STRING(U"ABCDDED", s);
  736. String32 s2;
  737. CORE_TEST_ERROR(s2.append(U"ABCDDED"));
  738. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  739. }
  740. static void testCastAppendSelf32() {
  741. String32 s;
  742. CORE_TEST_ERROR(s.append("abc"));
  743. CORE_TEST_ERROR(s.append(s));
  744. CORE_TEST_ERROR(s.append(static_cast<const c32*>(s)));
  745. CORE_TEST_STRING("abcabcabcabc", s);
  746. }
  747. static void testConversion() {
  748. const c32* a = U"öüewfde_§$§%$ädsf";
  749. const char* b = "öüewfde_§$§%$ädsf";
  750. String32 sa;
  751. CORE_TEST_ERROR(sa.append(a));
  752. String8 sb;
  753. CORE_TEST_ERROR(sb.append(b));
  754. String8 sa2;
  755. CORE_TEST_ERROR(sa2.append(sa));
  756. String32 sb2;
  757. CORE_TEST_ERROR(sb2.append(sb));
  758. CORE_TEST_STRING(a, sa2);
  759. CORE_TEST_STRING(b, sb2);
  760. }
  761. void Core::testArrayString() {
  762. testEquality8();
  763. testUnicodeEquality8();
  764. testInequality8();
  765. testStringAppend8();
  766. testStringAppendOverflow8();
  767. testCharacters8();
  768. testLength8();
  769. testChar8();
  770. testSignedChar8();
  771. testUnsignedChar8();
  772. testSignedShort8();
  773. testUnsignedShort8();
  774. testSignedInt8();
  775. testUnsignedInt8();
  776. testSignedLong8();
  777. testUnsignedLong8();
  778. testSignedLongLong8();
  779. testUnsignedLongLong8();
  780. testFloat8();
  781. testDouble8();
  782. testLongDouble8();
  783. testBool8();
  784. testIntOverflow8();
  785. testUnicode8();
  786. testClear8();
  787. testHashCode8();
  788. testAddSelf8();
  789. testAsHashMapKey8();
  790. testStartsWith8();
  791. testSearch8();
  792. testContains8();
  793. testSearchChar8();
  794. testContainsChar8();
  795. testSubString8();
  796. testReplace8();
  797. testReplaceChar8();
  798. testCastAppendSelf8();
  799. testEquality32();
  800. testUnicodeEquality32();
  801. testInequality32();
  802. testStringAppend32();
  803. testStringAppendOverflow32();
  804. testCharacters32();
  805. testLength32();
  806. testChar32();
  807. testSignedChar32();
  808. testUnsignedChar32();
  809. testSignedShort32();
  810. testUnsignedShort32();
  811. testSignedInt32();
  812. testUnsignedInt32();
  813. testSignedLong32();
  814. testUnsignedLong32();
  815. testSignedLongLong32();
  816. testUnsignedLongLong32();
  817. testFloat32();
  818. testDouble32();
  819. testLongDouble32();
  820. testBool32();
  821. testIntOverflow32();
  822. testUnicode32();
  823. testClear32();
  824. testHashCode32();
  825. testAddSelf32();
  826. testAsHashMapKey32();
  827. testStartsWith32();
  828. testSearch32();
  829. testContains32();
  830. testSearchChar32();
  831. testContainsChar32();
  832. testSubString32();
  833. testReplace32();
  834. testReplaceChar32();
  835. testCastAppendSelf32();
  836. testConversion();
  837. }