ArrayStringTests.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. #include "../Tests.hpp"
  2. #include "core/data/HashMap.hpp"
  3. #include "core/utils/ArrayString.hpp"
  4. template class Core::ArrayString<128, char, Core::CharString>;
  5. template class Core::ArrayString<128, c32, Core::Char32String>;
  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::String8<6> s;
  47. CORE_TEST_ERROR(s.append("te"));
  48. CORE_TEST_EQUAL(Core::ErrorCode::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::String8<4> s;
  171. CORE_TEST_EQUAL(Core::ErrorCode::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 testAddSelf8() {
  195. String8 s;
  196. CORE_TEST_ERROR(s.append("test1"));
  197. CORE_TEST_ERROR(s.append(s));
  198. CORE_TEST_ERROR(s.append(s));
  199. CORE_TEST_EQUAL(build("test1test1test1test1"), s);
  200. }
  201. static void testStartsWith8() {
  202. String8 s;
  203. CORE_TEST_ERROR(s.append("0123456789"));
  204. String8 s2;
  205. CORE_TEST_ERROR(s2.append("123"));
  206. String8 s3;
  207. CORE_TEST_ERROR(s3.append("234"));
  208. String8 s4;
  209. CORE_TEST_ERROR(s4.append("789"));
  210. String8 s5;
  211. CORE_TEST_ERROR(s5.append("124"));
  212. String8 s6;
  213. String8 s7;
  214. CORE_TEST_ERROR(s7.append("7891"));
  215. CORE_TEST_FALSE(s.startsWidth(s2));
  216. CORE_TEST_TRUE(s.startsWidth(s2, 1));
  217. CORE_TEST_FALSE(s.startsWidth(s3));
  218. CORE_TEST_TRUE(s.startsWidth(s3, 2));
  219. CORE_TEST_FALSE(s.startsWidth(s4));
  220. CORE_TEST_TRUE(s.startsWidth(s4, 7));
  221. CORE_TEST_FALSE(s.startsWidth(s5));
  222. CORE_TEST_FALSE(s.startsWidth(s5, 3));
  223. CORE_TEST_TRUE(s.startsWidth(s6));
  224. CORE_TEST_TRUE(s.startsWidth(s6, 3));
  225. CORE_TEST_FALSE(s.startsWidth(s7));
  226. CORE_TEST_FALSE(s.startsWidth(s7, 7));
  227. }
  228. static void testSearch8() {
  229. String8 s;
  230. CORE_TEST_ERROR(s.append("0123456789"));
  231. String8 s2;
  232. CORE_TEST_ERROR(s2.append("123"));
  233. String8 s3;
  234. CORE_TEST_ERROR(s3.append("234"));
  235. String8 s4;
  236. CORE_TEST_ERROR(s4.append("789"));
  237. String8 s5;
  238. CORE_TEST_ERROR(s5.append("124"));
  239. String8 s6;
  240. String8 s7;
  241. CORE_TEST_ERROR(s7.append("7891"));
  242. CORE_TEST_EQUAL(1, s.search(s2));
  243. CORE_TEST_EQUAL(2, s.search(s3));
  244. CORE_TEST_EQUAL(7, s.search(s4));
  245. CORE_TEST_EQUAL(-1, s.search(s5));
  246. CORE_TEST_EQUAL(0, s.search(s6));
  247. CORE_TEST_EQUAL(-1, s.search(s7));
  248. CORE_TEST_EQUAL(-1, s.search(s2, 3));
  249. CORE_TEST_EQUAL(-1, s.search(s3, 3));
  250. CORE_TEST_EQUAL(7, s.search(s4, 3));
  251. CORE_TEST_EQUAL(-1, s.search(s5, 3));
  252. CORE_TEST_EQUAL(3, s.search(s6, 3));
  253. CORE_TEST_EQUAL(-1, s.search(s7, 3));
  254. }
  255. static void testContains8() {
  256. String8 s;
  257. CORE_TEST_ERROR(s.append("0123456789"));
  258. String8 s2;
  259. CORE_TEST_ERROR(s2.append("123"));
  260. String8 s3;
  261. CORE_TEST_ERROR(s3.append("234"));
  262. String8 s4;
  263. CORE_TEST_ERROR(s4.append("789"));
  264. String8 s5;
  265. CORE_TEST_ERROR(s5.append("124"));
  266. String8 s6;
  267. String8 s7;
  268. CORE_TEST_ERROR(s7.append("7891"));
  269. CORE_TEST_TRUE(s.contains(s2));
  270. CORE_TEST_TRUE(s.contains(s3));
  271. CORE_TEST_TRUE(s.contains(s4));
  272. CORE_TEST_FALSE(s.contains(s5));
  273. CORE_TEST_TRUE(s.contains(s6));
  274. CORE_TEST_FALSE(s.contains(s7));
  275. }
  276. static void testSearchChar8() {
  277. String8 s;
  278. CORE_TEST_ERROR(s.append("01üää3ä"));
  279. CORE_TEST_EQUAL(0, s.search('0'));
  280. CORE_TEST_EQUAL(1, s.search('1'));
  281. CORE_TEST_EQUAL(8, s.search('3'));
  282. }
  283. static void testContainsChar8() {
  284. String8 s;
  285. CORE_TEST_ERROR(s.append("01üää3ä"));
  286. CORE_TEST_TRUE(s.contains('0'));
  287. CORE_TEST_TRUE(s.contains('1'));
  288. CORE_TEST_TRUE(s.contains('3'));
  289. CORE_TEST_FALSE(s.contains('a'));
  290. }
  291. static void testSubString8() {
  292. String8 s;
  293. CORE_TEST_ERROR(s.append("01üää3ä"));
  294. String8 sub;
  295. CORE_TEST_ERROR(s.substring(sub, -2));
  296. CORE_TEST_STRING("01üää3ä", sub);
  297. CORE_TEST_ERROR(s.substring(sub, 2));
  298. CORE_TEST_STRING("üää3ä", sub);
  299. CORE_TEST_ERROR(s.substring(sub, 4));
  300. CORE_TEST_STRING("ää3ä", sub);
  301. CORE_TEST_ERROR(s.substring(sub, 6));
  302. CORE_TEST_STRING("ä3ä", sub);
  303. CORE_TEST_ERROR(s.substring(sub, 0, 10));
  304. CORE_TEST_STRING("01üää3ä", sub);
  305. CORE_TEST_ERROR(s.substring(sub, 1, 8));
  306. CORE_TEST_STRING("1üää3", sub);
  307. CORE_TEST_ERROR(s.substring(sub, 2, 7));
  308. CORE_TEST_STRING("üää", sub);
  309. CORE_TEST_ERROR(s.substring(sub, 4, 5));
  310. CORE_TEST_STRING("ä", sub);
  311. CORE_TEST_ERROR(s.substring(sub, 4, 2));
  312. CORE_TEST_STRING("", sub);
  313. CORE_TEST_ERROR(s.substring(sub, 6, 23));
  314. CORE_TEST_STRING("ä3ä", sub);
  315. }
  316. static void testReplace8() {
  317. String8 s;
  318. CORE_TEST_ERROR(s.append("0äääää1üää3ä"));
  319. String8 search;
  320. CORE_TEST_ERROR(search.append("ää"));
  321. String8 replace;
  322. CORE_TEST_ERROR(replace.append("ABCD"));
  323. CORE_TEST_ERROR(s.replace(search, replace));
  324. CORE_TEST_STRING("0ABCDABCDä1üABCD3ä", s);
  325. }
  326. static void testReplaceChar8() {
  327. String8 s;
  328. CORE_TEST_ERROR(s.append("01YXX3X"));
  329. s.replace('0', 'A');
  330. CORE_TEST_STRING("A1YXX3X", s);
  331. s.replace('1', 'B');
  332. CORE_TEST_STRING("ABYXX3X", s);
  333. s.replace('Y', 'C');
  334. CORE_TEST_STRING("ABCXX3X", s);
  335. s.replace('X', 'D');
  336. CORE_TEST_STRING("ABCDD3D", s);
  337. s.replace('3', 'E');
  338. CORE_TEST_STRING("ABCDDED", s);
  339. }
  340. static void testCastAppendSelf8() {
  341. String8 s;
  342. CORE_TEST_ERROR(s.append("abc"));
  343. CORE_TEST_ERROR(s.append(s));
  344. CORE_TEST_ERROR(s.append(static_cast<const char*>(s)));
  345. CORE_TEST_STRING("abcabcabcabc", s);
  346. }
  347. static void testCompareWithShorter8() {
  348. String8 s;
  349. CORE_TEST_ERROR(s.append("abc"));
  350. CORE_TEST_FALSE(s == "ab");
  351. }
  352. static void testAppendSignedChar8() {
  353. const signed char buffer[] = {'a', 'b', 'c', '\0'};
  354. String8 s;
  355. CORE_TEST_ERROR(s.append(buffer));
  356. CORE_TEST_TRUE(s == "abc");
  357. }
  358. static void testAppendUnsignedChar8() {
  359. const unsigned char buffer[] = {'a', 'b', 'c', '\0'};
  360. String8 s;
  361. CORE_TEST_ERROR(s.append(buffer));
  362. CORE_TEST_TRUE(s == "abc");
  363. }
  364. static void testAppendError8() {
  365. String8 s;
  366. CORE_TEST_ERROR(s.append(Core::ErrorCode::NONE));
  367. CORE_TEST_STRING("0", s);
  368. }
  369. static void testPrint8() {
  370. String8 s;
  371. CORE_TEST_ERROR(s.append('\u0040'));
  372. CORE_TEST_ERROR(s.append(L'\u0400'));
  373. CORE_TEST_ERROR(s.append(L'\u8000'));
  374. CORE_TEST_ERROR(s.append(U'\U00100000'));
  375. CORE_TEST_EQUAL(build("\u0040\u0400\u8000\U00100000"), s);
  376. CORE_TEST_ERROR(s.print());
  377. }
  378. static void testKeepHash8() {
  379. String8 s;
  380. CORE_TEST_ERROR(s.append("a ## test #### #####"));
  381. CORE_TEST_ERROR(s.format(1, 2, 3, 4, 5, 6, 7, 8, 9));
  382. CORE_TEST_STRING("a # test ## ##123456789", s);
  383. }
  384. static String32 build(const c32* cs) {
  385. String32 s;
  386. CORE_TEST_ERROR(s.append(cs));
  387. return s;
  388. }
  389. static void testEquality32() {
  390. String32 s = build(U"test");
  391. CORE_TEST_TRUE(s == U"test");
  392. CORE_TEST_TRUE(s == build(U"test"));
  393. CORE_TEST_TRUE(U"test" == s);
  394. CORE_TEST_TRUE(build(U"test") == s);
  395. CORE_TEST_TRUE(s == s);
  396. }
  397. static void testUnicodeEquality32() {
  398. const c32* cs = U"\u0040\u0400\u8000\U00100000";
  399. String32 s = build(cs);
  400. CORE_TEST_TRUE(s == cs);
  401. CORE_TEST_TRUE(s == build(cs));
  402. CORE_TEST_TRUE(cs == s);
  403. CORE_TEST_TRUE(build(cs) == s);
  404. CORE_TEST_TRUE(s == s);
  405. }
  406. static void testInequality32() {
  407. String32 s = build(U"test");
  408. CORE_TEST_FALSE(s != U"test");
  409. CORE_TEST_FALSE(s != build(U"test"));
  410. CORE_TEST_FALSE(U"test" != s);
  411. CORE_TEST_FALSE(build(U"test") != s);
  412. CORE_TEST_FALSE(s != s);
  413. }
  414. static void testStringAppend32() {
  415. String32 s = build(U"test");
  416. CORE_TEST_ERROR(s.append(U"22"));
  417. CORE_TEST_ERROR(s.append(U"333"));
  418. CORE_TEST_ERROR(s.append(U"4444"));
  419. CORE_TEST_EQUAL(build(U"test223334444"), s);
  420. }
  421. static void testStringAppendOverflow32() {
  422. Core::String32<6> s;
  423. CORE_TEST_ERROR(s.append(U"te"));
  424. CORE_TEST_EQUAL(Core::ErrorCode::CAPACITY_REACHED, s.append(U"23334444"));
  425. CORE_TEST_TRUE(build(U"te23334444") != s);
  426. }
  427. static void testCharacters32() {
  428. String32 s = build(U"test");
  429. CORE_TEST_EQUAL('t', s[0]);
  430. CORE_TEST_EQUAL('e', s[1]);
  431. CORE_TEST_EQUAL('s', s[2]);
  432. CORE_TEST_EQUAL('t', s[3]);
  433. }
  434. static void testLength32() {
  435. String32 s = build(U"test");
  436. CORE_TEST_EQUAL(4, s.getLength());
  437. CORE_TEST_ERROR(s.append(U"aaa"));
  438. CORE_TEST_EQUAL(7, s.getLength());
  439. }
  440. static void testChar32() {
  441. String32 s = build(U"test");
  442. for(char i = 'a'; i < 'd'; i++) {
  443. CORE_TEST_ERROR(s.append(i));
  444. }
  445. CORE_TEST_EQUAL(build(U"testabc"), s);
  446. }
  447. static void testSignedChar32() {
  448. String32 s = build(U"test");
  449. for(signed char i = 'b'; i < 'e'; i++) {
  450. CORE_TEST_ERROR(s.append(i));
  451. }
  452. CORE_TEST_EQUAL(build(U"testbcd"), s);
  453. }
  454. static void testUnsignedChar32() {
  455. String32 s = build(U"test");
  456. for(unsigned char i = 'c'; i < 'f'; i++) {
  457. CORE_TEST_ERROR(s.append(i));
  458. }
  459. CORE_TEST_EQUAL(build(U"testcde"), s);
  460. }
  461. static void testSignedShort32() {
  462. String32 s = build(U"test");
  463. for(signed short i = 100; i < 103; i++) {
  464. CORE_TEST_ERROR(s.append(i));
  465. }
  466. CORE_TEST_EQUAL(build(U"test100101102"), s);
  467. }
  468. static void testUnsignedShort32() {
  469. String32 s = build(U"test");
  470. for(unsigned short i = 101; i < 104; i++) {
  471. CORE_TEST_ERROR(s.append(i));
  472. }
  473. CORE_TEST_EQUAL(build(U"test101102103"), s);
  474. }
  475. static void testSignedInt32() {
  476. String32 s = build(U"test");
  477. for(signed int i = 102; i < 105; i++) {
  478. CORE_TEST_ERROR(s.append(i));
  479. }
  480. CORE_TEST_EQUAL(build(U"test102103104"), s);
  481. }
  482. static void testUnsignedInt32() {
  483. String32 s = build(U"test");
  484. for(unsigned int i = 103; i < 106; i++) {
  485. CORE_TEST_ERROR(s.append(i));
  486. }
  487. CORE_TEST_EQUAL(build(U"test103104105"), s);
  488. }
  489. static void testSignedLong32() {
  490. String32 s = build(U"test");
  491. for(signed long i = 104; i < 107; i++) {
  492. CORE_TEST_ERROR(s.append(i));
  493. }
  494. CORE_TEST_EQUAL(build(U"test104105106"), s);
  495. }
  496. static void testUnsignedLong32() {
  497. String32 s = build(U"test");
  498. for(unsigned long i = 105; i < 108; i++) {
  499. CORE_TEST_ERROR(s.append(i));
  500. }
  501. CORE_TEST_EQUAL(build(U"test105106107"), s);
  502. }
  503. static void testSignedLongLong32() {
  504. String32 s = build(U"test");
  505. for(signed long long i = 106; i < 109; i++) {
  506. CORE_TEST_ERROR(s.append(i));
  507. }
  508. CORE_TEST_EQUAL(build(U"test106107108"), s);
  509. }
  510. static void testUnsignedLongLong32() {
  511. String32 s = build(U"test");
  512. for(unsigned long long i = 107; i < 110; i++) {
  513. CORE_TEST_ERROR(s.append(i));
  514. }
  515. CORE_TEST_EQUAL(build(U"test107108109"), s);
  516. }
  517. static void testFloat32() {
  518. String32 s = build(U"test");
  519. for(float i = 108; i < 111; i++) {
  520. CORE_TEST_ERROR(s.append(i));
  521. }
  522. CORE_TEST_EQUAL(build(U"test108.00109.00110.00"), s);
  523. }
  524. static void testDouble32() {
  525. String32 s = build(U"test");
  526. for(double i = 109; i < 112; i++) {
  527. CORE_TEST_ERROR(s.append(i));
  528. }
  529. CORE_TEST_EQUAL(build(U"test109.00110.00111.00"), s);
  530. }
  531. static void testLongDouble32() {
  532. String32 s = build(U"test");
  533. for(long double i = 110; i < 113; i++) {
  534. CORE_TEST_ERROR(s.append(i));
  535. }
  536. CORE_TEST_EQUAL(build(U"test110.00111.00112.00"), s);
  537. }
  538. static void testBool32() {
  539. String32 s = build(U"test");
  540. CORE_TEST_ERROR(s.append(true));
  541. CORE_TEST_ERROR(s.append(false));
  542. CORE_TEST_ERROR(s.append(true));
  543. CORE_TEST_EQUAL(build(U"testtruefalsetrue"), s);
  544. }
  545. static void testIntOverflow32() {
  546. Core::String32<4> s;
  547. CORE_TEST_EQUAL(Core::ErrorCode::CAPACITY_REACHED, s.append(123456));
  548. String32 o;
  549. for(int i = 0; i < s.getCapacity(); i++) {
  550. CORE_TEST_ERROR(o.append(i + 1));
  551. }
  552. CORE_TEST_TRUE(o == s);
  553. }
  554. static void testUnicode32() {
  555. String32 s;
  556. CORE_TEST_ERROR(s.append('\u0040'));
  557. CORE_TEST_ERROR(s.append(L'\u0400'));
  558. CORE_TEST_ERROR(s.append(L'\u8000'));
  559. CORE_TEST_ERROR(s.append(U'\U00100000'));
  560. CORE_TEST_EQUAL(build(U"\u0040\u0400\u8000\U00100000"), s);
  561. }
  562. static void testClear32() {
  563. String32 s = build(U"test");
  564. CORE_TEST_ERROR(s.append(1234));
  565. s.clear();
  566. CORE_TEST_ERROR(s.append(U"wusi"));
  567. CORE_TEST_ERROR(s.append(U"1234"));
  568. CORE_TEST_EQUAL(build(U"wusi1234"), s);
  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 testStartsWith32() {
  578. String32 s;
  579. CORE_TEST_ERROR(s.append(U"0123456789"));
  580. String32 s2;
  581. CORE_TEST_ERROR(s2.append(U"123"));
  582. String32 s3;
  583. CORE_TEST_ERROR(s3.append(U"234"));
  584. String32 s4;
  585. CORE_TEST_ERROR(s4.append(U"789"));
  586. String32 s5;
  587. CORE_TEST_ERROR(s5.append(U"124"));
  588. String32 s6;
  589. String32 s7;
  590. CORE_TEST_ERROR(s7.append(U"7891"));
  591. CORE_TEST_FALSE(s.startsWidth(s2));
  592. CORE_TEST_TRUE(s.startsWidth(s2, 1));
  593. CORE_TEST_FALSE(s.startsWidth(s3));
  594. CORE_TEST_TRUE(s.startsWidth(s3, 2));
  595. CORE_TEST_FALSE(s.startsWidth(s4));
  596. CORE_TEST_TRUE(s.startsWidth(s4, 7));
  597. CORE_TEST_FALSE(s.startsWidth(s5));
  598. CORE_TEST_FALSE(s.startsWidth(s5, 3));
  599. CORE_TEST_TRUE(s.startsWidth(s6));
  600. CORE_TEST_TRUE(s.startsWidth(s6, 3));
  601. CORE_TEST_FALSE(s.startsWidth(s7));
  602. CORE_TEST_FALSE(s.startsWidth(s7, 7));
  603. }
  604. static void testSearch32() {
  605. String32 s;
  606. CORE_TEST_ERROR(s.append(U"0123456789"));
  607. String32 s2;
  608. CORE_TEST_ERROR(s2.append(U"123"));
  609. String32 s3;
  610. CORE_TEST_ERROR(s3.append(U"234"));
  611. String32 s4;
  612. CORE_TEST_ERROR(s4.append(U"789"));
  613. String32 s5;
  614. CORE_TEST_ERROR(s5.append(U"124"));
  615. String32 s6;
  616. String32 s7;
  617. CORE_TEST_ERROR(s7.append(U"7891"));
  618. CORE_TEST_EQUAL(1, s.search(s2));
  619. CORE_TEST_EQUAL(2, s.search(s3));
  620. CORE_TEST_EQUAL(7, s.search(s4));
  621. CORE_TEST_EQUAL(-1, s.search(s5));
  622. CORE_TEST_EQUAL(0, s.search(s6));
  623. CORE_TEST_EQUAL(-1, s.search(s7));
  624. CORE_TEST_EQUAL(-1, s.search(s2, 3));
  625. CORE_TEST_EQUAL(-1, s.search(s3, 3));
  626. CORE_TEST_EQUAL(7, s.search(s4, 3));
  627. CORE_TEST_EQUAL(-1, s.search(s5, 3));
  628. CORE_TEST_EQUAL(3, s.search(s6, 3));
  629. CORE_TEST_EQUAL(-1, s.search(s7, 3));
  630. }
  631. static void testContains32() {
  632. String32 s;
  633. CORE_TEST_ERROR(s.append(U"0123456789"));
  634. String32 s2;
  635. CORE_TEST_ERROR(s2.append(U"123"));
  636. String32 s3;
  637. CORE_TEST_ERROR(s3.append(U"234"));
  638. String32 s4;
  639. CORE_TEST_ERROR(s4.append(U"789"));
  640. String32 s5;
  641. CORE_TEST_ERROR(s5.append(U"124"));
  642. String32 s6;
  643. String32 s7;
  644. CORE_TEST_ERROR(s7.append(U"7891"));
  645. CORE_TEST_TRUE(s.contains(s2));
  646. CORE_TEST_TRUE(s.contains(s3));
  647. CORE_TEST_TRUE(s.contains(s4));
  648. CORE_TEST_FALSE(s.contains(s5));
  649. CORE_TEST_TRUE(s.contains(s6));
  650. CORE_TEST_FALSE(s.contains(s7));
  651. }
  652. static void testSearchChar32() {
  653. String32 s;
  654. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  655. CORE_TEST_EQUAL(0, s.search('0'));
  656. CORE_TEST_EQUAL(1, s.search('1'));
  657. CORE_TEST_EQUAL(5, s.search('3'));
  658. CORE_TEST_EQUAL(2, s.search(U'ü'));
  659. CORE_TEST_EQUAL(3, s.search(U'ä'));
  660. CORE_TEST_EQUAL(4, s.search(U'ä', 4));
  661. CORE_TEST_EQUAL(6, s.search(U'ä', 5));
  662. }
  663. static void testContainsChar32() {
  664. String32 s;
  665. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  666. CORE_TEST_TRUE(s.contains(U'0'));
  667. CORE_TEST_TRUE(s.contains(U'1'));
  668. CORE_TEST_TRUE(s.contains(U'3'));
  669. CORE_TEST_FALSE(s.contains(U'a'));
  670. CORE_TEST_TRUE(s.contains(U'0'));
  671. CORE_TEST_TRUE(s.contains(U'1'));
  672. CORE_TEST_TRUE(s.contains(U'ü'));
  673. CORE_TEST_TRUE(s.contains(U'ä'));
  674. CORE_TEST_FALSE(s.contains(U'ö'));
  675. }
  676. static void testSubString32() {
  677. String32 s;
  678. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  679. String32 sub;
  680. CORE_TEST_ERROR(s.substring(sub, -2));
  681. CORE_TEST_STRING(U"01üää3ä", sub);
  682. CORE_TEST_ERROR(s.substring(sub, 1));
  683. CORE_TEST_STRING(U"1üää3ä", sub);
  684. CORE_TEST_ERROR(s.substring(sub, 2));
  685. CORE_TEST_STRING(U"üää3ä", sub);
  686. CORE_TEST_ERROR(s.substring(sub, 3));
  687. CORE_TEST_STRING(U"ää3ä", sub);
  688. CORE_TEST_ERROR(s.substring(sub, 4));
  689. CORE_TEST_STRING(U"ä3ä", sub);
  690. CORE_TEST_ERROR(s.substring(sub, 0, 6));
  691. CORE_TEST_STRING(U"01üää3ä", sub);
  692. CORE_TEST_ERROR(s.substring(sub, 1, 5));
  693. CORE_TEST_STRING(U"1üää3", sub);
  694. CORE_TEST_ERROR(s.substring(sub, 2, 4));
  695. CORE_TEST_STRING(U"üää", sub);
  696. CORE_TEST_ERROR(s.substring(sub, 3, 3));
  697. CORE_TEST_STRING(U"ä", sub);
  698. CORE_TEST_ERROR(s.substring(sub, 4, 2));
  699. CORE_TEST_STRING(U"", sub);
  700. CORE_TEST_ERROR(s.substring(sub, 4, 23));
  701. CORE_TEST_STRING(U"ä3ä", sub);
  702. }
  703. static void testReplace32() {
  704. String32 s;
  705. CORE_TEST_ERROR(s.append(U"0äääää1üää3ä"));
  706. String32 search;
  707. CORE_TEST_ERROR(search.append(U"ää"));
  708. String32 replace;
  709. CORE_TEST_ERROR(replace.append(U"ABCD"));
  710. CORE_TEST_ERROR(s.replace(search, replace));
  711. CORE_TEST_STRING(U"0ABCDABCDä1üABCD3ä", s);
  712. }
  713. static void testReplaceChar32() {
  714. String32 s;
  715. CORE_TEST_ERROR(s.append(U"01üää3ä"));
  716. s.replace(U'0', U'A');
  717. CORE_TEST_STRING(U"A1üää3ä", s);
  718. s.replace(U'1', U'B');
  719. CORE_TEST_STRING(U"ABüää3ä", s);
  720. s.replace(U'ü', U'C');
  721. CORE_TEST_STRING(U"ABCää3ä", s);
  722. s.replace(U'ä', U'D');
  723. CORE_TEST_STRING(U"ABCDD3D", s);
  724. s.replace(U'3', U'E');
  725. CORE_TEST_STRING(U"ABCDDED", s);
  726. }
  727. static void testCastAppendSelf32() {
  728. String32 s;
  729. CORE_TEST_ERROR(s.append("abc"));
  730. CORE_TEST_ERROR(s.append(s));
  731. CORE_TEST_ERROR(s.append(static_cast<const c32*>(s)));
  732. CORE_TEST_STRING("abcabcabcabc", s);
  733. }
  734. static void testCompareWithShorter32() {
  735. String32 s;
  736. CORE_TEST_ERROR(s.append("abc"));
  737. CORE_TEST_FALSE(s == U"ab");
  738. }
  739. static void testAppendSignedChar32() {
  740. const signed char buffer[] = {'a', 'b', 'c', '\0'};
  741. String32 s;
  742. CORE_TEST_ERROR(s.append(buffer));
  743. CORE_TEST_TRUE(s == U"abc");
  744. }
  745. static void testAppendUnsignedChar32() {
  746. const unsigned char buffer[] = {'a', 'b', 'c', '\0'};
  747. String32 s;
  748. CORE_TEST_ERROR(s.append(buffer));
  749. CORE_TEST_TRUE(s == U"abc");
  750. }
  751. static void testAppendError32() {
  752. String32 s;
  753. CORE_TEST_ERROR(s.append(Core::ErrorCode::NONE));
  754. CORE_TEST_STRING(U"0", s);
  755. }
  756. static void testPrint32() {
  757. String32 s;
  758. CORE_TEST_ERROR(s.append('\u0040'));
  759. CORE_TEST_ERROR(s.append(L'\u0400'));
  760. CORE_TEST_ERROR(s.append(L'\u8000'));
  761. CORE_TEST_ERROR(s.append(U'\U00100000'));
  762. CORE_TEST_EQUAL(build(U"\u0040\u0400\u8000\U00100000"), s);
  763. CORE_TEST_ERROR(s.print());
  764. }
  765. static void testVariousUnicode32() {
  766. const unsigned char buffer[] = {0xC0, 0};
  767. const unsigned char buffer2[] = {0xE0, 0};
  768. const unsigned char buffer3[] = {0xE0, 1, 2, 0};
  769. const unsigned char buffer4[] = {0xF0, 0};
  770. const unsigned char buffer5[] = {0xF0, 1, 2, 3, 0};
  771. const unsigned char buffer6[] = {0xFF, 0};
  772. String32 s;
  773. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_CHAR, s.append(buffer));
  774. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_CHAR, s.append(buffer2));
  775. CORE_TEST_EQUAL(Core::ErrorCode::NONE, s.append(buffer3));
  776. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_CHAR, s.append(buffer4));
  777. CORE_TEST_EQUAL(Core::ErrorCode::NONE, s.append(buffer5));
  778. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_CHAR, s.append(buffer6));
  779. }
  780. static void testKeepHash32() {
  781. String32 s;
  782. CORE_TEST_ERROR(s.append("a ## test #### #####"));
  783. CORE_TEST_ERROR(s.format(1, 2, 3, 4, 5, 6, 7, 8, 9));
  784. CORE_TEST_STRING("a # test ## ##123456789", s);
  785. }
  786. static void testConversion() {
  787. const c32* a = U"öüewfde_§$§%$ädsf";
  788. const char* b = "öüewfde_§$§%$ädsf";
  789. String32 sa;
  790. CORE_TEST_ERROR(sa.append(a));
  791. String8 sb;
  792. CORE_TEST_ERROR(sb.append(b));
  793. String8 sa2;
  794. CORE_TEST_ERROR(sa2.append(sa));
  795. String32 sb2;
  796. CORE_TEST_ERROR(sb2.append(sb));
  797. CORE_TEST_STRING(a, sa2);
  798. CORE_TEST_STRING(b, sb2);
  799. }
  800. void Core::testArrayString() {
  801. testEquality8();
  802. testUnicodeEquality8();
  803. testInequality8();
  804. testStringAppend8();
  805. testStringAppendOverflow8();
  806. testCharacters8();
  807. testLength8();
  808. testChar8();
  809. testSignedChar8();
  810. testUnsignedChar8();
  811. testSignedShort8();
  812. testUnsignedShort8();
  813. testSignedInt8();
  814. testUnsignedInt8();
  815. testSignedLong8();
  816. testUnsignedLong8();
  817. testSignedLongLong8();
  818. testUnsignedLongLong8();
  819. testFloat8();
  820. testDouble8();
  821. testLongDouble8();
  822. testBool8();
  823. testIntOverflow8();
  824. testUnicode8();
  825. testClear8();
  826. testAddSelf8();
  827. testStartsWith8();
  828. testSearch8();
  829. testContains8();
  830. testSearchChar8();
  831. testContainsChar8();
  832. testSubString8();
  833. testReplace8();
  834. testReplaceChar8();
  835. testCastAppendSelf8();
  836. testCompareWithShorter8();
  837. testAppendSignedChar8();
  838. testAppendUnsignedChar8();
  839. testAppendError8();
  840. testPrint8();
  841. testKeepHash8();
  842. testEquality32();
  843. testUnicodeEquality32();
  844. testInequality32();
  845. testStringAppend32();
  846. testStringAppendOverflow32();
  847. testCharacters32();
  848. testLength32();
  849. testChar32();
  850. testSignedChar32();
  851. testUnsignedChar32();
  852. testSignedShort32();
  853. testUnsignedShort32();
  854. testSignedInt32();
  855. testUnsignedInt32();
  856. testSignedLong32();
  857. testUnsignedLong32();
  858. testSignedLongLong32();
  859. testUnsignedLongLong32();
  860. testFloat32();
  861. testDouble32();
  862. testLongDouble32();
  863. testBool32();
  864. testIntOverflow32();
  865. testUnicode32();
  866. testClear32();
  867. testAddSelf32();
  868. testStartsWith32();
  869. testSearch32();
  870. testContains32();
  871. testSearchChar32();
  872. testContainsChar32();
  873. testSubString32();
  874. testReplace32();
  875. testReplaceChar32();
  876. testCastAppendSelf32();
  877. testCompareWithShorter32();
  878. testAppendSignedChar32();
  879. testAppendUnsignedChar32();
  880. testAppendError32();
  881. testPrint32();
  882. testVariousUnicode32();
  883. testKeepHash32();
  884. testConversion();
  885. }