SplitStringTests.cpp 3.2 KB

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