StringBufferTests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "tests/StringBufferTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/StringBuffer.h"
  4. #include "utils/HashMap.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, "inverse equality with another string");
  12. test.checkEqual(true, s == s, "equality with itself");
  13. }
  14. static void testInequality(Test& test) {
  15. String s("test");
  16. test.checkEqual(false, s != "test", "inequality with c-string");
  17. test.checkEqual(false, s != String("test"), "inequality with another string");
  18. test.checkEqual(false, "test" != s, "inverse inequality with c-string");
  19. test.checkEqual(false, String("test") != s, "inverse inequality with another string");
  20. test.checkEqual(false, s != s, "inequality with itself");
  21. }
  22. static void testStringAppend(Test& test) {
  23. String s("test");
  24. s.append("22").append("333").append("4444");
  25. test.checkEqual(String("test223334444"), s, "multiple appends");
  26. }
  27. static void testStringAppendOverflow(Test& test) {
  28. StringBuffer<5> s("te");
  29. s.append("2").append("333").append("4444");
  30. test.checkEqual(StringBuffer<5>("te23"), s, "multiple appends with overflow");
  31. test.checkEqual(4, s.getLength(), "length after multiple appends with overflow");
  32. }
  33. static void testCharacters(Test& test) {
  34. String s("test");
  35. test.checkEqual('t', s[0], "character read 1");
  36. test.checkEqual('e', s[1], "character read 2");
  37. test.checkEqual('s', s[2], "character read 3");
  38. test.checkEqual('t', s[3], "character read 4");
  39. }
  40. static void testLength(Test& test) {
  41. String s("test");
  42. test.checkEqual(4, s.getLength(), "length 1");
  43. s.append("aaa");
  44. test.checkEqual(7, s.getLength(), "length 2");
  45. }
  46. static void testInt(Test& test) {
  47. String s("test");
  48. s.append(524).append(-37);
  49. test.checkEqual(String("test524-37"), s, "int append");
  50. }
  51. static void testOverflowConstructor(Test& test) {
  52. StringBuffer<5> s("12345678");
  53. test.checkEqual(StringBuffer<5>("1234"), s, "overflow constructor");
  54. test.checkEqual(4, s.getLength(), "length after overflow constructor");
  55. }
  56. static void testIntOverflow(Test& test) {
  57. StringBuffer<5> s("te");
  58. s.append(123456);
  59. test.checkEqual(StringBuffer<5>("te12"), s, "int append with overflow");
  60. test.checkEqual(4, s.getLength(), "length after int append with overflow");
  61. }
  62. static void testFloat(Test& test) {
  63. String s("test");
  64. s.append(5.2378f).append(-500.4321f);
  65. test.checkEqual(String("test5.24-500.43"), s, "float append");
  66. }
  67. static void testBool(Test& test) {
  68. String s("test");
  69. s.append(true).append(false).append(true);
  70. test.checkEqual(String("testtruefalsetrue"), s, "bool append");
  71. }
  72. static void testClear(Test& test) {
  73. String s("test");
  74. s.append(1234).clear().append("wusi").append("1234");
  75. test.checkEqual(String("wusi1234"), s, "clear");
  76. }
  77. static void testAppendChar(Test& test) {
  78. StringBuffer<5> s("");
  79. for(int i = 0; i < 20; i++) {
  80. s.append('a');
  81. }
  82. test.checkEqual(StringBuffer<5>("aaaa"), s, "char append with overflow");
  83. test.checkEqual(4, s.getLength(), "length after char append with overflow");
  84. }
  85. static void testHashCode(Test& test) {
  86. String s;
  87. s.append("a").append("bc").append(20).append(25.5f).append(true);
  88. test.checkEqual(String("abc2025.50true").hashCode(), s.hashCode(), "string modification recalculates hash 1");
  89. s.clear();
  90. test.checkEqual(String().hashCode(), s.hashCode(), "string modification recalculates hash 2");
  91. }
  92. static void testAsHashMapKey(Test& test) {
  93. HashMap<String, int, 5> map;
  94. map.add(String("wusi"), 3).add(String("hiThere"), 7).add(String("baum123"), 5);
  95. test.checkEqual(3, map.search(String("wusi"), 0), "strings works as hash key 1");
  96. test.checkEqual(7, map.search(String("hiThere"), 0), "strings works as hash key 2");
  97. test.checkEqual(5, map.search(String("baum123"), 0), "strings works as hash key 3");
  98. test.checkEqual(0, map.search(String("423hifd"), 0), "strings works as hash key 4");
  99. }
  100. void StringBufferTests::test() {
  101. Test test("StringBuffer");
  102. testEquality(test);
  103. testInequality(test);
  104. testStringAppend(test);
  105. testStringAppendOverflow(test);
  106. testCharacters(test);
  107. testLength(test);
  108. testInt(test);
  109. testOverflowConstructor(test);
  110. testIntOverflow(test);
  111. testFloat(test);
  112. testBool(test);
  113. testClear(test);
  114. testAppendChar(test);
  115. testHashCode(test);
  116. testAsHashMapKey(test);
  117. test.finalize();
  118. }