ArrayStringTests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include "tests/ArrayStringTests.h"
  2. #include "data/HashMap.h"
  3. #include "test/Test.h"
  4. #include "utils/ArrayString.h"
  5. template<typename String>
  6. static String build(const char* cs) {
  7. String s;
  8. CORE_TEST_ERROR(s.append(cs));
  9. return s;
  10. }
  11. template<typename String>
  12. static void testEquality() {
  13. String s = build<String>("test");
  14. CORE_TEST_TRUE(s == "test");
  15. CORE_TEST_TRUE(s == build<String>("test"));
  16. CORE_TEST_TRUE("test" == s);
  17. CORE_TEST_TRUE(build<String>("test") == s);
  18. CORE_TEST_TRUE(s == s);
  19. }
  20. template<typename String>
  21. static void testUnicodeEquality() {
  22. const char* cs = "\u0040\u0400\u8000\U00100000";
  23. String s = build<String>(cs);
  24. CORE_TEST_TRUE(s == cs);
  25. CORE_TEST_TRUE(s == build<String>(cs));
  26. CORE_TEST_TRUE(cs == s);
  27. CORE_TEST_TRUE(build<String>(cs) == s);
  28. CORE_TEST_TRUE(s == s);
  29. }
  30. template<typename String>
  31. static void testInequality() {
  32. String s = build<String>("test");
  33. CORE_TEST_FALSE(s != "test");
  34. CORE_TEST_FALSE(s != build<String>("test"));
  35. CORE_TEST_FALSE("test" != s);
  36. CORE_TEST_FALSE(build<String>("test") != s);
  37. CORE_TEST_FALSE(s != s);
  38. }
  39. template<typename String>
  40. static void testStringAppend() {
  41. String s = build<String>("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<String>("test223334444"), s);
  46. }
  47. template<typename String, typename CharType>
  48. static void testStringAppendOverflow() {
  49. Core::ArrayString<6, CharType> s;
  50. CORE_TEST_ERROR(s.append("te"));
  51. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append("23334444"));
  52. CORE_TEST_TRUE(build<String>("te23334444") != s);
  53. }
  54. template<typename String>
  55. static void testCharacters() {
  56. String s = build<String>("test");
  57. CORE_TEST_EQUAL('t', s[0]);
  58. CORE_TEST_EQUAL('e', s[1]);
  59. CORE_TEST_EQUAL('s', s[2]);
  60. CORE_TEST_EQUAL('t', s[3]);
  61. }
  62. template<typename String>
  63. static void testLength() {
  64. String s = build<String>("test");
  65. CORE_TEST_EQUAL(4, s.getLength());
  66. CORE_TEST_ERROR(s.append("aaa"));
  67. CORE_TEST_EQUAL(7, s.getLength());
  68. }
  69. template<typename String>
  70. static void testChar() {
  71. String s = build<String>("test");
  72. for(char i = 'a'; i < 'd'; i++) {
  73. CORE_TEST_ERROR(s.append(i));
  74. }
  75. CORE_TEST_EQUAL(build<String>("testabc"), s);
  76. }
  77. template<typename String>
  78. static void testSignedChar() {
  79. String s = build<String>("test");
  80. for(signed char i = 'b'; i < 'e'; i++) {
  81. CORE_TEST_ERROR(s.append(i));
  82. }
  83. CORE_TEST_EQUAL(build<String>("testbcd"), s);
  84. }
  85. template<typename String>
  86. static void testUnsignedChar() {
  87. String s = build<String>("test");
  88. for(unsigned char i = 'c'; i < 'f'; i++) {
  89. CORE_TEST_ERROR(s.append(i));
  90. }
  91. CORE_TEST_EQUAL(build<String>("testcde"), s);
  92. }
  93. template<typename String>
  94. static void testSignedShort() {
  95. String s = build<String>("test");
  96. for(signed short i = 100; i < 103; i++) {
  97. CORE_TEST_ERROR(s.append(i));
  98. }
  99. CORE_TEST_EQUAL(build<String>("test100101102"), s);
  100. }
  101. template<typename String>
  102. static void testUnsignedShort() {
  103. String s = build<String>("test");
  104. for(unsigned short i = 101; i < 104; i++) {
  105. CORE_TEST_ERROR(s.append(i));
  106. }
  107. CORE_TEST_EQUAL(build<String>("test101102103"), s);
  108. }
  109. template<typename String>
  110. static void testSignedInt() {
  111. String s = build<String>("test");
  112. for(signed int i = 102; i < 105; i++) {
  113. CORE_TEST_ERROR(s.append(i));
  114. }
  115. CORE_TEST_EQUAL(build<String>("test102103104"), s);
  116. }
  117. template<typename String>
  118. static void testUnsignedInt() {
  119. String s = build<String>("test");
  120. for(unsigned int i = 103; i < 106; i++) {
  121. CORE_TEST_ERROR(s.append(i));
  122. }
  123. CORE_TEST_EQUAL(build<String>("test103104105"), s);
  124. }
  125. template<typename String>
  126. static void testSignedLong() {
  127. String s = build<String>("test");
  128. for(signed long i = 104; i < 107; i++) {
  129. CORE_TEST_ERROR(s.append(i));
  130. }
  131. CORE_TEST_EQUAL(build<String>("test104105106"), s);
  132. }
  133. template<typename String>
  134. static void testUnsignedLong() {
  135. String s = build<String>("test");
  136. for(unsigned long i = 105; i < 108; i++) {
  137. CORE_TEST_ERROR(s.append(i));
  138. }
  139. CORE_TEST_EQUAL(build<String>("test105106107"), s);
  140. }
  141. template<typename String>
  142. static void testSignedLongLong() {
  143. String s = build<String>("test");
  144. for(signed long long i = 106; i < 109; i++) {
  145. CORE_TEST_ERROR(s.append(i));
  146. }
  147. CORE_TEST_EQUAL(build<String>("test106107108"), s);
  148. }
  149. template<typename String>
  150. static void testUnsignedLongLong() {
  151. String s = build<String>("test");
  152. for(unsigned long long i = 107; i < 110; i++) {
  153. CORE_TEST_ERROR(s.append(i));
  154. }
  155. CORE_TEST_EQUAL(build<String>("test107108109"), s);
  156. }
  157. template<typename String>
  158. static void testFloat() {
  159. String s = build<String>("test");
  160. for(float i = 108; i < 111; i++) {
  161. CORE_TEST_ERROR(s.append(i));
  162. }
  163. CORE_TEST_EQUAL(build<String>("test108.00109.00110.00"), s);
  164. }
  165. template<typename String>
  166. static void testDouble() {
  167. String s = build<String>("test");
  168. for(double i = 109; i < 112; i++) {
  169. CORE_TEST_ERROR(s.append(i));
  170. }
  171. CORE_TEST_EQUAL(build<String>("test109.00110.00111.00"), s);
  172. }
  173. template<typename String>
  174. static void testLongDouble() {
  175. String s = build<String>("test");
  176. for(long double i = 110; i < 113; i++) {
  177. CORE_TEST_ERROR(s.append(i));
  178. }
  179. CORE_TEST_EQUAL(build<String>("test110.00111.00112.00"), s);
  180. }
  181. template<typename String>
  182. static void testBool() {
  183. String s = build<String>("test");
  184. CORE_TEST_ERROR(s.append(true));
  185. CORE_TEST_ERROR(s.append(false));
  186. CORE_TEST_ERROR(s.append(true));
  187. CORE_TEST_EQUAL(build<String>("testtruefalsetrue"), s);
  188. }
  189. template<typename String, typename CharType>
  190. static void testIntOverflow() {
  191. Core::ArrayString<4, CharType> s;
  192. CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
  193. String o;
  194. for(int i = 0; i < s.getCapacity(); i++) {
  195. CORE_TEST_ERROR(o.append(i + 1));
  196. }
  197. CORE_TEST_TRUE(o == s);
  198. }
  199. template<typename String>
  200. static void testUnicode() {
  201. String s;
  202. CORE_TEST_ERROR(s.append('\u0040'));
  203. CORE_TEST_ERROR(s.append(L'\u0400'));
  204. CORE_TEST_ERROR(s.append(L'\u8000'));
  205. CORE_TEST_ERROR(s.append(U'\U00100000'));
  206. CORE_TEST_EQUAL(build<String>("\u0040\u0400\u8000\U00100000"), s);
  207. }
  208. template<typename String>
  209. static void testClear() {
  210. String s = build<String>("test");
  211. CORE_TEST_ERROR(s.append(1234));
  212. s.clear();
  213. CORE_TEST_ERROR(s.append("wusi"));
  214. CORE_TEST_ERROR(s.append("1234"));
  215. CORE_TEST_EQUAL(build<String>("wusi1234"), s);
  216. }
  217. template<typename String>
  218. static void testHashCode() {
  219. String s;
  220. CORE_TEST_ERROR(s.append("a"));
  221. CORE_TEST_ERROR(s.append("bc"));
  222. CORE_TEST_ERROR(s.append(20));
  223. CORE_TEST_ERROR(s.append(25.5f));
  224. CORE_TEST_ERROR(s.append(true));
  225. CORE_TEST_EQUAL(build<String>("abc2025.50true").hashCode(), s.hashCode());
  226. s.clear();
  227. CORE_TEST_EQUAL(String().hashCode(), s.hashCode());
  228. }
  229. template<typename String>
  230. static void testAddSelf() {
  231. String s;
  232. CORE_TEST_ERROR(s.append("test1"));
  233. CORE_TEST_ERROR(s.append(s));
  234. CORE_TEST_ERROR(s.append(s));
  235. CORE_TEST_EQUAL(build<String>("test1test1test1test1"), s);
  236. }
  237. template<typename String>
  238. static void testAsHashMapKey() {
  239. Core::HashMap<String, int> map;
  240. CORE_TEST_ERROR(map.add(build<String>("wusi"), 3));
  241. CORE_TEST_ERROR(map.add(build<String>("hiThere"), 7));
  242. CORE_TEST_ERROR(map.add(build<String>("baum123"), 5));
  243. int* a = map.search(build<String>("wusi"));
  244. int* b = map.search(build<String>("hiThere"));
  245. int* c = map.search(build<String>("baum123"));
  246. int* d = map.search(build<String>("423hifd"));
  247. CORE_TEST_NOT_NULL(a);
  248. CORE_TEST_NOT_NULL(b);
  249. CORE_TEST_NOT_NULL(c);
  250. CORE_TEST_NULL(d);
  251. if(a != nullptr && b != nullptr && c != nullptr) {
  252. CORE_TEST_EQUAL(3, *a);
  253. CORE_TEST_EQUAL(7, *b);
  254. CORE_TEST_EQUAL(5, *c);
  255. }
  256. }
  257. template<typename String>
  258. static void testStartsWith() {
  259. String s;
  260. CORE_TEST_ERROR(s.append("0123456789"));
  261. String s2;
  262. CORE_TEST_ERROR(s2.append("123"));
  263. String s3;
  264. CORE_TEST_ERROR(s3.append("234"));
  265. String s4;
  266. CORE_TEST_ERROR(s4.append("789"));
  267. String s5;
  268. CORE_TEST_ERROR(s5.append("124"));
  269. String s6;
  270. String s7;
  271. CORE_TEST_ERROR(s7.append("7891"));
  272. CORE_TEST_FALSE(s.startsWidth(s2));
  273. CORE_TEST_TRUE(s.startsWidth(s2, 1));
  274. CORE_TEST_FALSE(s.startsWidth(s3));
  275. CORE_TEST_TRUE(s.startsWidth(s3, 2));
  276. CORE_TEST_FALSE(s.startsWidth(s4));
  277. CORE_TEST_TRUE(s.startsWidth(s4, 7));
  278. CORE_TEST_FALSE(s.startsWidth(s5));
  279. CORE_TEST_FALSE(s.startsWidth(s5, 3));
  280. CORE_TEST_TRUE(s.startsWidth(s6));
  281. CORE_TEST_TRUE(s.startsWidth(s6, 3));
  282. CORE_TEST_FALSE(s.startsWidth(s7));
  283. CORE_TEST_FALSE(s.startsWidth(s7, 7));
  284. }
  285. template<typename String>
  286. static void testSearch() {
  287. String s;
  288. CORE_TEST_ERROR(s.append("0123456789"));
  289. String s2;
  290. CORE_TEST_ERROR(s2.append("123"));
  291. String s3;
  292. CORE_TEST_ERROR(s3.append("234"));
  293. String s4;
  294. CORE_TEST_ERROR(s4.append("789"));
  295. String s5;
  296. CORE_TEST_ERROR(s5.append("124"));
  297. String s6;
  298. String s7;
  299. CORE_TEST_ERROR(s7.append("7891"));
  300. CORE_TEST_EQUAL(1, s.search(s2));
  301. CORE_TEST_EQUAL(2, s.search(s3));
  302. CORE_TEST_EQUAL(7, s.search(s4));
  303. CORE_TEST_EQUAL(-1, s.search(s5));
  304. CORE_TEST_EQUAL(0, s.search(s6));
  305. CORE_TEST_EQUAL(-1, s.search(s7));
  306. CORE_TEST_EQUAL(-1, s.search(s2, 3));
  307. CORE_TEST_EQUAL(-1, s.search(s3, 3));
  308. CORE_TEST_EQUAL(7, s.search(s4, 3));
  309. CORE_TEST_EQUAL(-1, s.search(s5, 3));
  310. CORE_TEST_EQUAL(3, s.search(s6, 3));
  311. CORE_TEST_EQUAL(-1, s.search(s7, 3));
  312. }
  313. template<typename String>
  314. static void testContains() {
  315. String s;
  316. CORE_TEST_ERROR(s.append("0123456789"));
  317. String s2;
  318. CORE_TEST_ERROR(s2.append("123"));
  319. String s3;
  320. CORE_TEST_ERROR(s3.append("234"));
  321. String s4;
  322. CORE_TEST_ERROR(s4.append("789"));
  323. String s5;
  324. CORE_TEST_ERROR(s5.append("124"));
  325. String s6;
  326. String s7;
  327. CORE_TEST_ERROR(s7.append("7891"));
  328. CORE_TEST_TRUE(s.contains(s2));
  329. CORE_TEST_TRUE(s.contains(s3));
  330. CORE_TEST_TRUE(s.contains(s4));
  331. CORE_TEST_FALSE(s.contains(s5));
  332. CORE_TEST_TRUE(s.contains(s6));
  333. CORE_TEST_FALSE(s.contains(s7));
  334. }
  335. template<typename String, typename CharType>
  336. static void testSearchChar() {
  337. String s;
  338. CORE_TEST_ERROR(s.append("01üää3ä"));
  339. CORE_TEST_EQUAL(0, s.search('0'));
  340. CORE_TEST_EQUAL(1, s.search('1'));
  341. CORE_TEST_EQUAL((Core::IsSame<CharType, c32> ? 5 : 8), s.search('3'));
  342. if constexpr(Core::IsSame<CharType, c32>) {
  343. CORE_TEST_EQUAL(2, s.search(U'ü'));
  344. CORE_TEST_EQUAL(3, s.search(U'ä'));
  345. CORE_TEST_EQUAL(4, s.search(U'ä', 4));
  346. CORE_TEST_EQUAL(6, s.search(U'ä', 5));
  347. }
  348. }
  349. template<typename String, typename CharType>
  350. static void testContainsChar() {
  351. String s;
  352. CORE_TEST_ERROR(s.append("01üää3ä"));
  353. if constexpr(Core::IsSame<CharType, c32>) {
  354. CORE_TEST_TRUE(s.contains(U'0'));
  355. CORE_TEST_TRUE(s.contains(U'1'));
  356. CORE_TEST_TRUE(s.contains(U'ü'));
  357. CORE_TEST_TRUE(s.contains(U'ä'));
  358. CORE_TEST_FALSE(s.contains(U'ö'));
  359. } else {
  360. CORE_TEST_TRUE(s.contains('0'));
  361. CORE_TEST_TRUE(s.contains('1'));
  362. }
  363. }
  364. template<typename String, typename CharType>
  365. static void testSubString() {
  366. String s;
  367. CORE_TEST_ERROR(s.append("01üää3ä"));
  368. if constexpr(Core::IsSame<CharType, c32>) {
  369. CORE_TEST_STRING("01üää3ä", s.substring(-2));
  370. CORE_TEST_STRING("1üää3ä", s.substring(1));
  371. CORE_TEST_STRING("üää3ä", s.substring(2));
  372. CORE_TEST_STRING("ää3ä", s.substring(3));
  373. CORE_TEST_STRING("ä3ä", s.substring(4));
  374. CORE_TEST_STRING("01üää3ä", s.substring(0, 6));
  375. CORE_TEST_STRING("1üää3", s.substring(1, 5));
  376. CORE_TEST_STRING("üää", s.substring(2, 4));
  377. CORE_TEST_STRING("ä", s.substring(3, 3));
  378. CORE_TEST_STRING("", s.substring(4, 2));
  379. CORE_TEST_STRING("ä3ä", s.substring(4, 23));
  380. } else {
  381. CORE_TEST_STRING("01üää3ä", s.substring(-2));
  382. CORE_TEST_STRING("1üää3ä", s.substring(1));
  383. CORE_TEST_STRING("üää3ä", s.substring(2));
  384. CORE_TEST_STRING("ää3ä", s.substring(4));
  385. CORE_TEST_STRING("ä3ä", s.substring(6));
  386. CORE_TEST_STRING("01üää3ä", s.substring(0, 10));
  387. CORE_TEST_STRING("1üää3", s.substring(1, 8));
  388. CORE_TEST_STRING("üää", s.substring(2, 7));
  389. CORE_TEST_STRING("ä", s.substring(4, 5));
  390. CORE_TEST_STRING("", s.substring(4, 2));
  391. CORE_TEST_STRING("ä3ä", s.substring(6, 23));
  392. }
  393. }
  394. template<typename String>
  395. static void testReplace() {
  396. String s;
  397. CORE_TEST_ERROR(s.append("0äääää1üää3ä"));
  398. String search;
  399. CORE_TEST_ERROR(search.append("ää"));
  400. String replace;
  401. CORE_TEST_ERROR(replace.append("ABCD"));
  402. CORE_TEST_ERROR(s.replace(search, replace));
  403. CORE_TEST_STRING("0ABCDABCDä1üABCD3ä", s);
  404. String s2;
  405. CORE_TEST_ERROR(s2.append("0ABCDABCDä1üABCD3ä"));
  406. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  407. }
  408. template<typename String, typename CharType>
  409. static void testReplaceChar() {
  410. String s;
  411. if constexpr(Core::IsSame<CharType, c32>) {
  412. CORE_TEST_ERROR(s.append("01üää3ä"));
  413. s.replace(U'0', U'A');
  414. CORE_TEST_STRING("A1üää3ä", s);
  415. s.replace(U'1', U'B');
  416. CORE_TEST_STRING("ABüää3ä", s);
  417. s.replace(U'ü', U'C');
  418. CORE_TEST_STRING("ABCää3ä", s);
  419. s.replace(U'ä', U'D');
  420. CORE_TEST_STRING("ABCDD3D", s);
  421. s.replace(U'3', U'E');
  422. CORE_TEST_STRING("ABCDDED", s);
  423. } else {
  424. CORE_TEST_ERROR(s.append("01YXX3X"));
  425. s.replace('0', 'A');
  426. CORE_TEST_STRING("A1YXX3X", s);
  427. s.replace('1', 'B');
  428. CORE_TEST_STRING("ABYXX3X", s);
  429. s.replace('Y', 'C');
  430. CORE_TEST_STRING("ABCXX3X", s);
  431. s.replace('X', 'D');
  432. CORE_TEST_STRING("ABCDD3D", s);
  433. s.replace('3', 'E');
  434. CORE_TEST_STRING("ABCDDED", s);
  435. }
  436. String s2;
  437. CORE_TEST_ERROR(s2.append("ABCDDED"));
  438. CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
  439. }
  440. template<int N, typename CharType>
  441. static void typedTest() {
  442. using String = Core::ArrayString<N, CharType>;
  443. testEquality<String>();
  444. testUnicodeEquality<String>();
  445. testInequality<String>();
  446. testStringAppend<String>();
  447. testStringAppendOverflow<String, CharType>();
  448. testCharacters<String>();
  449. testLength<String>();
  450. testChar<String>();
  451. testSignedChar<String>();
  452. testUnsignedChar<String>();
  453. testSignedShort<String>();
  454. testUnsignedShort<String>();
  455. testSignedInt<String>();
  456. testUnsignedInt<String>();
  457. testSignedLong<String>();
  458. testUnsignedLong<String>();
  459. testSignedLongLong<String>();
  460. testUnsignedLongLong<String>();
  461. testFloat<String>();
  462. testDouble<String>();
  463. testLongDouble<String>();
  464. testBool<String>();
  465. testIntOverflow<String, CharType>();
  466. testUnicode<String>();
  467. testClear<String>();
  468. testHashCode<String>();
  469. testAddSelf<String>();
  470. testAsHashMapKey<String>();
  471. testStartsWith<String>();
  472. testSearch<String>();
  473. testContains<String>();
  474. testSearchChar<String, CharType>();
  475. testContainsChar<String, CharType>();
  476. testSubString<String, CharType>();
  477. testReplace<String>();
  478. testReplaceChar<String, CharType>();
  479. }
  480. void Core::ArrayStringTests::test() {
  481. typedTest<128, c32>();
  482. typedTest<128, char>();
  483. }