ArrayStringTests.cpp 25 KB

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