ArrayStringTests.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. void Core::ArrayStringTests::test() {
  230. testEquality();
  231. testUnicodeEquality();
  232. testInequality();
  233. testStringAppend();
  234. testStringAppendOverflow();
  235. testCharacters();
  236. testLength();
  237. testChar();
  238. testSignedChar();
  239. testUnsignedChar();
  240. testSignedShort();
  241. testUnsignedShort();
  242. testSignedInt();
  243. testUnsignedInt();
  244. testSignedLong();
  245. testUnsignedLong();
  246. testSignedLongLong();
  247. testUnsignedLongLong();
  248. testFloat();
  249. testDouble();
  250. testLongDouble();
  251. testBool();
  252. testIntOverflow();
  253. testUnicode();
  254. testClear();
  255. testHashCode();
  256. testAddSelf();
  257. testAsHashMapKey();
  258. }