StringTests.cpp 7.5 KB

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