StringBufferTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 testOverflowConstructor(Test& test) {
  57. StringBuffer<5> s("12345678");
  58. test.checkEqual(StringBuffer<5>("1234"), s, "overflow constructor");
  59. test.checkEqual(4, s.getLength(), "length after overflow constructor");
  60. }
  61. static void testIntOverflow(Test& test) {
  62. StringBuffer<5> s("te");
  63. s.append(123456);
  64. test.checkEqual(StringBuffer<5>("te12"), s, "int append with overflow");
  65. test.checkEqual(4, s.getLength(), "length after int append with overflow");
  66. }
  67. static void testFloat(Test& test) {
  68. String s("test");
  69. s.append(5.2378f).append(-500.4321f);
  70. test.checkEqual(String("test5.24-500.43"), s, "float append");
  71. }
  72. static void testBool(Test& test) {
  73. String s("test");
  74. s.append(true).append(false).append(true);
  75. test.checkEqual(String("testtruefalsetrue"), s, "bool append");
  76. }
  77. static void testClear(Test& test) {
  78. String s("test");
  79. s.append(1234).clear().append("wusi").append("1234");
  80. test.checkEqual(String("wusi1234"), s, "clear");
  81. }
  82. static void testAppendChar(Test& test) {
  83. StringBuffer<5> s("");
  84. for(int i = 0; i < 20; i++) {
  85. s.append('a');
  86. }
  87. test.checkEqual(StringBuffer<5>("aaaa"), s, "char append with overflow");
  88. test.checkEqual(4, s.getLength(), "length after char append with overflow");
  89. }
  90. static void testHashCode(Test& test) {
  91. String s;
  92. s.append("a").append("bc").append(20).append(25.5f).append(true);
  93. test.checkEqual(String("abc2025.50true").hashCode(), s.hashCode(),
  94. "string modification recalculates hash 1");
  95. s.clear();
  96. test.checkEqual(String().hashCode(), s.hashCode(),
  97. "string modification recalculates hash 2");
  98. }
  99. static void testAsHashMapKey(Test& test) {
  100. HashMap<String, int> map;
  101. map.add(String("wusi"), 3)
  102. .add(String("hiThere"), 7)
  103. .add(String("baum123"), 5);
  104. int* a = map.search(String("wusi"));
  105. int* b = map.search(String("hiThere"));
  106. int* c = map.search(String("baum123"));
  107. int* d = map.search(String("423hifd"));
  108. test.checkEqual(true, a != nullptr, "strings works as hash key 1");
  109. test.checkEqual(true, b != nullptr, "strings works as hash key 2");
  110. test.checkEqual(true, c != nullptr, "strings works as hash key 3");
  111. test.checkEqual(true, d == nullptr, "strings works as hash key 4");
  112. if(a != nullptr && b != nullptr && c != nullptr) {
  113. test.checkEqual(3, *a, "strings works as hash key 1");
  114. test.checkEqual(7, *b, "strings works as hash key 2");
  115. test.checkEqual(5, *c, "strings works as hash key 3");
  116. }
  117. }
  118. void StringBufferTests::test() {
  119. Test test("StringBuffer");
  120. testEquality(test);
  121. testInequality(test);
  122. testStringAppend(test);
  123. testStringAppendOverflow(test);
  124. testCharacters(test);
  125. testLength(test);
  126. testInt(test);
  127. testOverflowConstructor(test);
  128. testIntOverflow(test);
  129. testFloat(test);
  130. testBool(test);
  131. testClear(test);
  132. testAppendChar(test);
  133. testHashCode(test);
  134. testAsHashMapKey(test);
  135. test.finalize();
  136. }