StringBufferTests.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "tests/StringBufferTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/HashMap.h"
  4. #include "utils/StringBuffer.h"
  5. typedef StringBuffer<20> String;
  6. static void testEquality(Test& test) {
  7. String s("test");
  8. test.checkEqual(true, s == "test", "equality with c-string");
  9. test.checkEqual(true, s == String("test"), "equality with another string");
  10. test.checkEqual(true, "test" == s, "inverse equality with c-string");
  11. test.checkEqual(true, String("test") == s,
  12. "inverse equality with another string");
  13. test.checkEqual(true, s == s, "equality with itself");
  14. }
  15. static void testInequality(Test& test) {
  16. String s("test");
  17. test.checkEqual(false, s != "test", "inequality with c-string");
  18. test.checkEqual(false, s != String("test"),
  19. "inequality with another string");
  20. test.checkEqual(false, "test" != s, "inverse inequality with c-string");
  21. test.checkEqual(false, String("test") != s,
  22. "inverse inequality with another string");
  23. test.checkEqual(false, s != s, "inequality with itself");
  24. }
  25. static void testStringAppend(Test& test) {
  26. String s("test");
  27. s.append("22").append("333").append("4444");
  28. test.checkEqual(String("test223334444"), s, "multiple appends");
  29. }
  30. static void testStringAppendOverflow(Test& test) {
  31. StringBuffer<5> s("te");
  32. s.append("2").append("333").append("4444");
  33. test.checkEqual(StringBuffer<5>("te23"), s,
  34. "multiple appends with overflow");
  35. test.checkEqual(4, s.getLength(),
  36. "length after multiple appends with overflow");
  37. }
  38. static void testCharacters(Test& test) {
  39. String s("test");
  40. test.checkEqual('t', s[0], "character read 1");
  41. test.checkEqual('e', s[1], "character read 2");
  42. test.checkEqual('s', s[2], "character read 3");
  43. test.checkEqual('t', s[3], "character read 4");
  44. }
  45. static void testLength(Test& test) {
  46. String s("test");
  47. test.checkEqual(4, s.getLength(), "length 1");
  48. s.append("aaa");
  49. test.checkEqual(7, s.getLength(), "length 2");
  50. }
  51. static void testInt(Test& test) {
  52. String s("test");
  53. s.append(524).append(-37);
  54. test.checkEqual(String("test524-37"), s, "int append");
  55. }
  56. static void testUnsignedInt(Test& test) {
  57. String s("test");
  58. s.append(524u).append(4294967295u);
  59. test.checkEqual(String("test5244294967295"), s, "unsigned int append");
  60. }
  61. static void testOverflowConstructor(Test& test) {
  62. StringBuffer<5> s("12345678");
  63. test.checkEqual(StringBuffer<5>("1234"), s, "overflow constructor");
  64. test.checkEqual(4, s.getLength(), "length after overflow constructor");
  65. }
  66. static void testIntOverflow(Test& test) {
  67. StringBuffer<5> s("te");
  68. s.append(123456);
  69. test.checkEqual(StringBuffer<5>("te12"), s, "int append with overflow");
  70. test.checkEqual(4, s.getLength(), "length after int append with overflow");
  71. }
  72. static void testFloat(Test& test) {
  73. String s("test");
  74. s.append(5.2378f).append(-500.4321f);
  75. test.checkEqual(String("test5.24-500.43"), s, "float append");
  76. }
  77. static void testBool(Test& test) {
  78. String s("test");
  79. s.append(true).append(false).append(true);
  80. test.checkEqual(String("testtruefalsetrue"), s, "bool append");
  81. }
  82. static void testUnicode(Test& test) {
  83. String s("");
  84. s.appendUnicode('\u0040')
  85. .appendUnicode(L'\u0400')
  86. .appendUnicode(L'\u8000')
  87. .appendUnicode(U'\U00100000');
  88. test.checkEqual(String("\u0040\u0400\u8000\U00100000"), s,
  89. "unicode append");
  90. }
  91. static void testClear(Test& test) {
  92. String s("test");
  93. s.append(1234).clear().append("wusi").append("1234");
  94. test.checkEqual(String("wusi1234"), s, "clear");
  95. }
  96. static void testAppendChar(Test& test) {
  97. StringBuffer<5> s("");
  98. for(int i = 0; i < 20; i++) {
  99. s.append('a');
  100. }
  101. test.checkEqual(StringBuffer<5>("aaaa"), s, "char append with overflow");
  102. test.checkEqual(4, s.getLength(), "length after char append with overflow");
  103. }
  104. static void testHashCode(Test& test) {
  105. String s;
  106. s.append("a").append("bc").append(20).append(25.5f).append(true);
  107. test.checkEqual(String("abc2025.50true").hashCode(), s.hashCode(),
  108. "string modification recalculates hash 1");
  109. s.clear();
  110. test.checkEqual(String().hashCode(), s.hashCode(),
  111. "string modification recalculates hash 2");
  112. }
  113. static void testAsHashMapKey(Test& test) {
  114. HashMap<String, int> map;
  115. map.add(String("wusi"), 3)
  116. .add(String("hiThere"), 7)
  117. .add(String("baum123"), 5);
  118. int* a = map.search(String("wusi"));
  119. int* b = map.search(String("hiThere"));
  120. int* c = map.search(String("baum123"));
  121. int* d = map.search(String("423hifd"));
  122. test.checkEqual(true, a != nullptr, "strings works as hash key 1");
  123. test.checkEqual(true, b != nullptr, "strings works as hash key 2");
  124. test.checkEqual(true, c != nullptr, "strings works as hash key 3");
  125. test.checkEqual(true, d == nullptr, "strings works as hash key 4");
  126. if(a != nullptr && b != nullptr && c != nullptr) {
  127. test.checkEqual(3, *a, "strings works as hash key 1");
  128. test.checkEqual(7, *b, "strings works as hash key 2");
  129. test.checkEqual(5, *c, "strings works as hash key 3");
  130. }
  131. }
  132. void StringBufferTests::test() {
  133. Test test("StringBuffer");
  134. testEquality(test);
  135. testInequality(test);
  136. testStringAppend(test);
  137. testStringAppendOverflow(test);
  138. testCharacters(test);
  139. testLength(test);
  140. testInt(test);
  141. testUnsignedInt(test);
  142. testOverflowConstructor(test);
  143. testIntOverflow(test);
  144. testFloat(test);
  145. testBool(test);
  146. testUnicode(test);
  147. testClear(test);
  148. testAppendChar(test);
  149. testHashCode(test);
  150. testAsHashMapKey(test);
  151. test.finalize();
  152. }