ArrayStringTests.cpp 12 KB

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