ArrayStringTests.cpp 27 KB

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