SplitStringTests.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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").add("aaa").add("bbbb");
  23. checkList(test, "test aaa bbbb", list, "three arguments");
  24. }
  25. static void testFourArguments(Test& test) {
  26. StringList list;
  27. list.add("test").add("aaa").add("bbbb").add("ccccc");
  28. checkList(test, "test aaa bbbb ccccc", list, "four arguments");
  29. }
  30. static void testOneArgumentWithSpaces(Test& test) {
  31. StringList list;
  32. list.add("test");
  33. checkList(test, "test ", list, "spaces");
  34. }
  35. static void testThreeArgumentsWithSpaces(Test& test) {
  36. StringList list;
  37. list.add("test").add("aaa").add("bbbb");
  38. checkList(test, "test aaa bbbb", list, "three arguments and spaces");
  39. }
  40. static void testTwoArgumentsWithQuotationMarks(Test& test) {
  41. StringList list;
  42. list.add("test").add("aaa bbbb");
  43. checkList(test, "test \"aaa bbbb\"", list, "two arguments and quotation marks");
  44. }
  45. static void testThreeArgumentsWithQuotationMarks(Test& test) {
  46. StringList list;
  47. list.add("test").add("aaa bbbb").add("ccc");
  48. checkList(test, "test \"aaa bbbb\" ccc", list, "three arguments and quotation marks");
  49. }
  50. static void testFourArgumentsWithQuotationMarks(Test& test) {
  51. StringList list;
  52. list.add("test").add("ddd").add("aaa bbbb").add("ccc");
  53. checkList(test, "test ddd \"aaa bbbb\" ccc", list, "four arguments and quotation marks");
  54. }
  55. static void testEmptyArgument(Test& test) {
  56. StringList list;
  57. list.add("test").add("");
  58. checkList(test, "test \"\"", list, "empty argument");
  59. }
  60. static void testSyntaxException(Test& test) {
  61. StringList list;
  62. checkList(test, "test \"", list, "syntax exception 1");
  63. checkList(test, "test aaa\"", list, "syntax exception 2");
  64. checkList(test, "test aaa\"bbb\"", list, "syntax exception 3");
  65. checkList(test, "test aaa \"bbb\"ccc", list, "syntax exception 4");
  66. checkList(test, "test aaa \"bbb ccc", list, "syntax exception 5");
  67. }
  68. void SplitStringTests::test() {
  69. Test test("SplitString");
  70. testOneArgument(test);
  71. testThreeArguments(test);
  72. testFourArguments(test);
  73. testOneArgumentWithSpaces(test);
  74. testThreeArgumentsWithSpaces(test);
  75. testTwoArgumentsWithQuotationMarks(test);
  76. testThreeArgumentsWithQuotationMarks(test);
  77. testFourArgumentsWithQuotationMarks(test);
  78. testEmptyArgument(test);
  79. testSyntaxException(test);
  80. test.finalize();
  81. }