StringBufferTests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "tests/StringBufferTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/StringBuffer.h"
  4. typedef StringBuffer<20> String;
  5. static void testEquality(Test& test) {
  6. String s("test");
  7. test.checkEqual(true, s == "test", "equality with c-string");
  8. test.checkEqual(true, s == String("test"), "equality with another string");
  9. test.checkEqual(true, "test" == s, "inverse equality with c-string");
  10. test.checkEqual(true, String("test") == s, "inverse equality with another string");
  11. test.checkEqual(true, s == s, "equality with itself");
  12. }
  13. static void testInequality(Test& test) {
  14. String s("test");
  15. test.checkEqual(false, s != "test", "inequality with c-string");
  16. test.checkEqual(false, s != String("test"), "inequality with another string");
  17. test.checkEqual(false, "test" != s, "inverse inequality with c-string");
  18. test.checkEqual(false, String("test") != s, "inverse inequality with another string");
  19. test.checkEqual(false, s != s, "inequality with itself");
  20. }
  21. static void testStringAppend(Test& test) {
  22. String s("test");
  23. s.append("22").append("333").append("4444");
  24. test.checkEqual(String("test223334444"), s, "multiple appends");
  25. }
  26. static void testStringAppendOverflow(Test& test) {
  27. StringBuffer<5> s("te");
  28. s.append("2").append("333").append("4444");
  29. test.checkEqual(StringBuffer<5>("te23"), s, "multiple appends with overflow");
  30. test.checkEqual(4, s.getLength(), "length after multiple appends with overflow");
  31. }
  32. static void testCharacters(Test& test) {
  33. String s("test");
  34. test.checkEqual('t', s[0], "character read 1");
  35. test.checkEqual('e', s[1], "character read 2");
  36. test.checkEqual('s', s[2], "character read 3");
  37. test.checkEqual('t', s[3], "character read 4");
  38. }
  39. static void testLength(Test& test) {
  40. String s("test");
  41. test.checkEqual(4, s.getLength(), "length 1");
  42. s.append("aaa");
  43. test.checkEqual(7, s.getLength(), "length 2");
  44. }
  45. static void testInt(Test& test) {
  46. String s("test");
  47. s.append(524).append(-37);
  48. test.checkEqual(String("test524-37"), s, "int append");
  49. }
  50. static void testOverflowConstructor(Test& test) {
  51. StringBuffer<5> s("12345678");
  52. test.checkEqual(StringBuffer<5>("1234"), s, "overflow constructor");
  53. test.checkEqual(4, s.getLength(), "length after overflow constructor");
  54. }
  55. static void testIntOverflow(Test& test) {
  56. StringBuffer<5> s("te");
  57. s.append(123456);
  58. test.checkEqual(StringBuffer<5>("te12"), s, "int append with overflow");
  59. test.checkEqual(4, s.getLength(), "length after int append with overflow");
  60. }
  61. static void testFloat(Test& test) {
  62. String s("test");
  63. s.append(5.2378f).append(-500.4321f);
  64. test.checkEqual(String("test5.24-500.43"), s, "float append");
  65. }
  66. static void testBool(Test& test) {
  67. String s("test");
  68. s.append(true).append(false).append(true);
  69. test.checkEqual(String("testtruefalsetrue"), s, "bool append");
  70. }
  71. static void testClear(Test& test) {
  72. String s("test");
  73. s.append(1234).clear().append("wusi").append("1234");
  74. test.checkEqual(String("wusi1234"), s, "clear");
  75. }
  76. void StringBufferTests::test() {
  77. Test test("StringBuffer");
  78. testEquality(test);
  79. testInequality(test);
  80. testStringAppend(test);
  81. testStringAppendOverflow(test);
  82. testCharacters(test);
  83. testLength(test);
  84. testInt(test);
  85. testOverflowConstructor(test);
  86. testIntOverflow(test);
  87. testFloat(test);
  88. testBool(test);
  89. testClear(test);
  90. test.finalize();
  91. }