ArrayStringTests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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(Core::Test& test, const char* cs) {
  7. String s;
  8. test.checkFalse(s.append(cs), "append works");
  9. return s;
  10. }
  11. static void testEquality(Core::Test& test) {
  12. String s = build(test, "test");
  13. test.checkTrue(s == "test", "equality with c-string");
  14. test.checkTrue(s == build(test, "test"), "equality with another string");
  15. test.checkTrue("test" == s, "inverse equality with c-string");
  16. test.checkTrue(build(test, "test") == s,
  17. "inverse equality with another string");
  18. test.checkTrue(s == s, "equality with itself");
  19. }
  20. static void testUnicodeEquality(Core::Test& test) {
  21. const char* cs = "\u0040\u0400\u8000\U00100000";
  22. String s = build(test, cs);
  23. test.checkTrue(s == cs, "unicode equality with c-string");
  24. test.checkTrue(s == build(test, cs),
  25. "unicode equality with another string");
  26. test.checkTrue(cs == s, "unicode inverse equality with c-string");
  27. test.checkTrue(build(test, cs) == s,
  28. "unicode inverse equality with another string");
  29. test.checkTrue(s == s, "unicode equality with itself");
  30. }
  31. static void testInequality(Core::Test& test) {
  32. String s = build(test, "test");
  33. test.checkFalse(s != "test", "inequality with c-string");
  34. test.checkFalse(s != build(test, "test"), "inequality with another string");
  35. test.checkFalse("test" != s, "inverse inequality with c-string");
  36. test.checkFalse(build(test, "test") != s,
  37. "inverse inequality with another string");
  38. test.checkFalse(s != s, "inequality with itself");
  39. }
  40. static void testStringAppend(Core::Test& test) {
  41. String s = build(test, "test");
  42. test.checkFalse(s.append("22"), "multiple append works");
  43. test.checkFalse(s.append("333"), "multiple append works");
  44. test.checkFalse(s.append("4444"), "multiple append works");
  45. test.checkEqual(build(test, "test223334444"), s, "multiple appends");
  46. }
  47. static void testStringAppendOverflow(Core::Test& test) {
  48. Core::ArrayString<20> s;
  49. test.checkFalse(s.append("te"), "overflow test 1");
  50. test.checkTrue(s.append("23334444"), "overflow test 2");
  51. test.checkTrue(build(test, "te23334444") != s, "overflow test 3");
  52. }
  53. static void testCharacters(Core::Test& test) {
  54. String s = build(test, "test");
  55. test.checkEqual<u32>('t', s[0], "character read 1");
  56. test.checkEqual<u32>('e', s[1], "character read 2");
  57. test.checkEqual<u32>('s', s[2], "character read 3");
  58. test.checkEqual<u32>('t', s[3], "character read 4");
  59. }
  60. static void testLength(Core::Test& test) {
  61. String s = build(test, "test");
  62. test.checkEqual(4, s.getLength(), "length 1");
  63. test.checkFalse(s.append("aaa"), "length append works");
  64. test.checkEqual(7, s.getLength(), "length 2");
  65. }
  66. static void testChar(Core::Test& test) {
  67. String s = build(test, "test");
  68. for(char i = 'a'; i < 'd'; i++) {
  69. test.checkFalse(s.append(i), "char append");
  70. }
  71. test.checkEqual(build(test, "testabc"), s, "char append");
  72. }
  73. static void testSignedChar(Core::Test& test) {
  74. String s = build(test, "test");
  75. for(signed char i = 'b'; i < 'e'; i++) {
  76. test.checkFalse(s.append(i), "signed char append");
  77. }
  78. test.checkEqual(build(test, "testbcd"), s, "signed char append");
  79. }
  80. static void testUnsignedChar(Core::Test& test) {
  81. String s = build(test, "test");
  82. for(unsigned char i = 'c'; i < 'f'; i++) {
  83. test.checkFalse(s.append(i), "unsigned char append");
  84. }
  85. test.checkEqual(build(test, "testcde"), s, "unsigned char append");
  86. }
  87. static void testSignedShort(Core::Test& test) {
  88. String s = build(test, "test");
  89. for(signed short i = 100; i < 103; i++) {
  90. test.checkFalse(s.append(i), "signed short append");
  91. }
  92. test.checkEqual(build(test, "test100101102"), s, "signed short append");
  93. }
  94. static void testUnsignedShort(Core::Test& test) {
  95. String s = build(test, "test");
  96. for(unsigned short i = 101; i < 104; i++) {
  97. test.checkFalse(s.append(i), "unsigned short append");
  98. }
  99. test.checkEqual(build(test, "test101102103"), s, "unsigned short append");
  100. }
  101. static void testSignedInt(Core::Test& test) {
  102. String s = build(test, "test");
  103. for(signed int i = 102; i < 105; i++) {
  104. test.checkFalse(s.append(i), "signed int append");
  105. }
  106. test.checkEqual(build(test, "test102103104"), s, "signed int append");
  107. }
  108. static void testUnsignedInt(Core::Test& test) {
  109. String s = build(test, "test");
  110. for(unsigned int i = 103; i < 106; i++) {
  111. test.checkFalse(s.append(i), "unsigned int append");
  112. }
  113. test.checkEqual(build(test, "test103104105"), s, "unsigned int append");
  114. }
  115. static void testSignedLong(Core::Test& test) {
  116. String s = build(test, "test");
  117. for(signed long i = 104; i < 107; i++) {
  118. test.checkFalse(s.append(i), "signed long append");
  119. }
  120. test.checkEqual(build(test, "test104105106"), s, "signed long append");
  121. }
  122. static void testUnsignedLong(Core::Test& test) {
  123. String s = build(test, "test");
  124. for(unsigned long i = 105; i < 108; i++) {
  125. test.checkFalse(s.append(i), "unsigned long append");
  126. }
  127. test.checkEqual(build(test, "test105106107"), s, "unsigned long append");
  128. }
  129. static void testSignedLongLong(Core::Test& test) {
  130. String s = build(test, "test");
  131. for(signed long long i = 106; i < 109; i++) {
  132. test.checkFalse(s.append(i), "signed long long append");
  133. }
  134. test.checkEqual(build(test, "test106107108"), s, "signed long long append");
  135. }
  136. static void testUnsignedLongLong(Core::Test& test) {
  137. String s = build(test, "test");
  138. for(unsigned long long i = 107; i < 110; i++) {
  139. test.checkFalse(s.append(i), "unsigned long long append");
  140. }
  141. test.checkEqual(build(test, "test107108109"), s,
  142. "unsigned long long append");
  143. }
  144. static void testFloat(Core::Test& test) {
  145. String s = build(test, "test");
  146. for(float i = 108; i < 111; i++) {
  147. test.checkFalse(s.append(i), "float append");
  148. }
  149. test.checkEqual(build(test, "test108.00109.00110.00"), s, "float append");
  150. }
  151. static void testDouble(Core::Test& test) {
  152. String s = build(test, "test");
  153. for(double i = 109; i < 112; i++) {
  154. test.checkFalse(s.append(i), "double append");
  155. }
  156. test.checkEqual(build(test, "test109.00110.00111.00"), s, "double append");
  157. }
  158. static void testLongDouble(Core::Test& test) {
  159. String s = build(test, "test");
  160. for(long double i = 110; i < 113; i++) {
  161. test.checkFalse(s.append(i), "long double append");
  162. }
  163. test.checkEqual(build(test, "test110.00111.00112.00"), s,
  164. "long double append");
  165. }
  166. static void testBool(Core::Test& test) {
  167. String s = build(test, "test");
  168. test.checkFalse(s.append(true), "bool append 1");
  169. test.checkFalse(s.append(false), "bool append 2");
  170. test.checkFalse(s.append(true), "bool append 3");
  171. test.checkEqual(build(test, "testtruefalsetrue"), s, "bool append 4");
  172. }
  173. static void testIntOverflow(Core::Test& test) {
  174. Core::ArrayString<20> s;
  175. test.checkTrue(s.append(123456), "int overflow");
  176. String o;
  177. for(int i = 0; i < s.getCapacity(); i++) {
  178. test.checkFalse(o.append(i + 1), "int overflow compare build");
  179. }
  180. test.checkTrue(o == s, "int overflow compare");
  181. }
  182. static void testUnicode(Core::Test& test) {
  183. String s;
  184. test.checkFalse(s.appendUnicode('\u0040'), "unicode 1");
  185. test.checkFalse(s.appendUnicode(L'\u0400'), "unicode 2");
  186. test.checkFalse(s.appendUnicode(L'\u8000'), "unicode 3");
  187. test.checkFalse(s.appendUnicode(U'\U00100000'), "unicode 4");
  188. test.checkEqual(build(test, "\u0040\u0400\u8000\U00100000"), s,
  189. "unicode append");
  190. }
  191. static void testClear(Core::Test& test) {
  192. String s = build(test, "test");
  193. test.checkFalse(s.append(1234), "clear 1");
  194. s.clear();
  195. test.checkFalse(s.append("wusi"), "clear 2");
  196. test.checkFalse(s.append("1234"), "clear 3");
  197. test.checkEqual(build(test, "wusi1234"), s, "clear 4");
  198. }
  199. static void testHashCode(Core::Test& test) {
  200. String s;
  201. test.checkFalse(s.append("a"), "hash code 1");
  202. test.checkFalse(s.append("bc"), "hash code 2");
  203. test.checkFalse(s.append(20), "hash code 3");
  204. test.checkFalse(s.append(25.5f), "hash code 4");
  205. test.checkFalse(s.append(true), "hash code 5");
  206. test.checkEqual(build(test, "abc2025.50true").hashCode(), s.hashCode(),
  207. "string modification recalculates hash 1");
  208. s.clear();
  209. test.checkEqual(String().hashCode(), s.hashCode(),
  210. "string modification recalculates hash 2");
  211. }
  212. static void testAddSelf(Core::Test& test) {
  213. String s;
  214. test.checkFalse(s.append("test1"), "add self 1");
  215. test.checkFalse(s.append(s), "add self 2");
  216. test.checkFalse(s.append(s), "add self 3");
  217. test.checkEqual(build(test, "test1test1test1test1"), s, "add self 4");
  218. }
  219. static void testAsHashMapKey(Core::Test& test) {
  220. Core::HashMap<String, int> map;
  221. test.checkFalse(map.add(build(test, "wusi"), 3), "as hashmap key 1");
  222. test.checkFalse(map.add(build(test, "hiThere"), 7), "as hashmap key 2");
  223. test.checkFalse(map.add(build(test, "baum123"), 5), "as hashmap key 3");
  224. int* a = map.search(build(test, "wusi"));
  225. int* b = map.search(build(test, "hiThere"));
  226. int* c = map.search(build(test, "baum123"));
  227. int* d = map.search(build(test, "423hifd"));
  228. test.checkEqual(true, a != nullptr, "strings works as hash key 1");
  229. test.checkEqual(true, b != nullptr, "strings works as hash key 2");
  230. test.checkEqual(true, c != nullptr, "strings works as hash key 3");
  231. test.checkEqual(true, d == nullptr, "strings works as hash key 4");
  232. if(a != nullptr && b != nullptr && c != nullptr) {
  233. test.checkEqual(3, *a, "strings works as hash key 1");
  234. test.checkEqual(7, *b, "strings works as hash key 2");
  235. test.checkEqual(5, *c, "strings works as hash key 3");
  236. }
  237. }
  238. void Core::ArrayStringTests::test() {
  239. Core::Test test("String");
  240. testEquality(test);
  241. testUnicodeEquality(test);
  242. testInequality(test);
  243. testStringAppend(test);
  244. testStringAppendOverflow(test);
  245. testCharacters(test);
  246. testLength(test);
  247. testChar(test);
  248. testSignedChar(test);
  249. testUnsignedChar(test);
  250. testSignedShort(test);
  251. testUnsignedShort(test);
  252. testSignedInt(test);
  253. testUnsignedInt(test);
  254. testSignedLong(test);
  255. testUnsignedLong(test);
  256. testSignedLongLong(test);
  257. testUnsignedLongLong(test);
  258. testFloat(test);
  259. testDouble(test);
  260. testLongDouble(test);
  261. testBool(test);
  262. testIntOverflow(test);
  263. testUnicode(test);
  264. testClear(test);
  265. testHashCode(test);
  266. testAddSelf(test);
  267. testAsHashMapKey(test);
  268. test.finalize();
  269. }