SplitStringTests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "tests/SplitStringTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/List.h"
  4. #include "utils/SplitString.h"
  5. typedef StringBuffer<60> String;
  6. typedef SplitString<60> Split;
  7. typedef List<String> StringList;
  8. static void checkList(Test& test, const char* str, const StringList& list,
  9. const char* message) {
  10. Split split(str);
  11. test.checkEqual(list.getLength(), split.getLength(),
  12. String(message).append(" - split amount"));
  13. for(int i = 0; i < list.getLength() && i < split.getLength(); i++) {
  14. test.checkEqual(list[i], String(split[i]),
  15. String(message).append(" ").append(i));
  16. }
  17. }
  18. static void testOneArgument(Test& test) {
  19. StringList list;
  20. list.add("test");
  21. checkList(test, "test", list, "one argument");
  22. }
  23. static void testThreeArguments(Test& test) {
  24. StringList list;
  25. list.add("test");
  26. list.add("aaa");
  27. list.add("bbbb");
  28. checkList(test, "test aaa bbbb", list, "three arguments");
  29. }
  30. static void testFourArguments(Test& test) {
  31. StringList list;
  32. list.add("test");
  33. list.add("aaa");
  34. list.add("bbbb");
  35. list.add("ccccc");
  36. checkList(test, "test aaa bbbb ccccc", list, "four arguments");
  37. }
  38. static void testOneArgumentWithSpaces(Test& test) {
  39. StringList list;
  40. list.add("test");
  41. checkList(test, "test ", list, "spaces");
  42. }
  43. static void testThreeArgumentsWithSpaces(Test& test) {
  44. StringList list;
  45. list.add("test");
  46. list.add("aaa");
  47. list.add("bbbb");
  48. checkList(test, "test aaa bbbb", list, "three arguments and spaces");
  49. }
  50. static void testTwoArgumentsWithQuotationMarks(Test& test) {
  51. StringList list;
  52. list.add("test");
  53. list.add("aaa bbbb");
  54. checkList(test, "test \"aaa bbbb\"", list,
  55. "two arguments and quotation marks");
  56. }
  57. static void testThreeArgumentsWithQuotationMarks(Test& test) {
  58. StringList list;
  59. list.add("test");
  60. list.add("aaa bbbb");
  61. list.add("ccc");
  62. checkList(test, "test \"aaa bbbb\" ccc", list,
  63. "three arguments and quotation marks");
  64. }
  65. static void testFourArgumentsWithQuotationMarks(Test& test) {
  66. StringList list;
  67. list.add("test");
  68. list.add("ddd");
  69. list.add("aaa bbbb");
  70. list.add("ccc");
  71. checkList(test, "test ddd \"aaa bbbb\" ccc", list,
  72. "four arguments and quotation marks");
  73. }
  74. static void testEmptyArgument(Test& test) {
  75. StringList list;
  76. list.add("test");
  77. list.add("");
  78. checkList(test, "test \"\"", list, "empty argument");
  79. }
  80. static void testSyntaxException(Test& test) {
  81. StringList list;
  82. checkList(test, "test \"", list, "syntax exception 1");
  83. checkList(test, "test aaa\"", list, "syntax exception 2");
  84. checkList(test, "test aaa\"bbb\"", list, "syntax exception 3");
  85. checkList(test, "test aaa \"bbb\"ccc", list, "syntax exception 4");
  86. checkList(test, "test aaa \"bbb ccc", list, "syntax exception 5");
  87. }
  88. void SplitStringTests::test() {
  89. Test test("SplitString");
  90. testOneArgument(test);
  91. testThreeArguments(test);
  92. testFourArguments(test);
  93. testOneArgumentWithSpaces(test);
  94. testThreeArgumentsWithSpaces(test);
  95. testTwoArgumentsWithQuotationMarks(test);
  96. testThreeArgumentsWithQuotationMarks(test);
  97. testFourArgumentsWithQuotationMarks(test);
  98. testEmptyArgument(test);
  99. testSyntaxException(test);
  100. test.finalize();
  101. }