ArrayStringTests.cpp 28 KB

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