ArrayStringTests.cpp 29 KB

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