ArrayStringTests.cpp 25 KB

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