Main.cpp 3.2 KB

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