Main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <iostream>
  2. #include "common/utils/SplitString.h"
  3. #include "common/utils/Array.h"
  4. #include "common/utils/List.h"
  5. #include "common/utils/HashedString.h"
  6. #include "common/utils/Random.h"
  7. typedef List<const char*, 16> StringList;
  8. const char* RED = "\033[0;31m";
  9. const char* GREEN = "\033[0;32m";
  10. uint tests = 0;
  11. uint successTests = 0;
  12. void finalizeTest() {
  13. std::cout << ((successTests == tests) ? GREEN : RED);
  14. std::cout << successTests << " / " << tests << " succeeded\n";
  15. tests = 0;
  16. successTests = 0;
  17. }
  18. template<typename T>
  19. void checkEqual(const T& wanted, const T& actual, const char* text) {
  20. if(wanted == actual) {
  21. tests++;
  22. successTests++;
  23. std::cout << GREEN << tests << ": " << text << "\n";
  24. } else {
  25. tests++;
  26. std::cout << RED << tests << ": " << text << " - ";
  27. std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n";
  28. }
  29. }
  30. void checkCommandParser(const char* rawCommand, const StringList& result, const char* text) {
  31. SplitString split(rawCommand);
  32. checkEqual(result.getLength(), split.getLength(), text);
  33. for(uint i = 0; i < result.getLength() && i < split.getLength(); i++) {
  34. checkEqual(String(result[i]), String(split[i]), text);
  35. }
  36. }
  37. void testCommandParser() {
  38. StringList list;
  39. list.clear().add("test");
  40. checkCommandParser("test", list, "command without arguments");
  41. list.clear().add("test").add("aaa");
  42. checkCommandParser("test aaa", list, "command with one argument");
  43. list.clear().add("test").add("aaa").add("bbbb");
  44. checkCommandParser("test aaa bbbb", list, "command with two arguments");
  45. list.clear().add("test").add("aaa").add("bbbb").add("ccccc");
  46. checkCommandParser("test aaa bbbb ccccc", list, "command with three arguments");
  47. list.clear().add("test");
  48. checkCommandParser("test ", list, "command with spaces");
  49. list.clear().add("test").add("aaa").add("bbbb");
  50. checkCommandParser("test aaa bbbb", list, "command with arguments and spaces");
  51. list.clear().add("test").add("aaa bbbb");
  52. checkCommandParser("test \"aaa bbbb\"", list, "command with one argument and quotation marks");
  53. list.clear().add("test").add("aaa bbbb").add("ccc");
  54. checkCommandParser("test \"aaa bbbb\" ccc", list, "command with two arguments and quotation marks");
  55. list.clear().add("test").add("ddd").add("aaa bbbb").add("ccc");
  56. checkCommandParser("test ddd \"aaa bbbb\" ccc", list, "command with tree arguments and quotation marks");
  57. list.clear().add("test").add("");
  58. checkCommandParser("test \"\"", list, "command with empty argument");
  59. list.clear();
  60. checkCommandParser("test \"", list, "command syntax exception 1");
  61. checkCommandParser("test aaa\"", list, "command syntax exception 2");
  62. checkCommandParser("test aaa\"bbb\"", list, "command syntax exception 3");
  63. checkCommandParser("test aaa \"bbb\"ccc", list, "command syntax exception 4");
  64. finalizeTest();
  65. }
  66. long getNanos() {
  67. struct timespec time;
  68. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
  69. return time.tv_nsec + time.tv_sec * 1000000000;
  70. }
  71. int main() {
  72. //testCommandParser();
  73. return 0;
  74. }