tokenizecmd_test.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2012-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/commandline.h>
  7. #include <string.h>
  8. TEST(commandLine)
  9. {
  10. const char* args[] =
  11. {
  12. "-s",
  13. "--long",
  14. };
  15. bx::CommandLine cmdLine(BX_COUNTOF(args), args);
  16. CHECK(cmdLine.hasArg("long") );
  17. CHECK(cmdLine.hasArg('s') );
  18. // non-existing argument
  19. CHECK(!cmdLine.hasArg('x') );
  20. }
  21. TEST(tokenizeCommandLine)
  22. {
  23. #if 0
  24. const char* input[] =
  25. {
  26. " ",
  27. "\\",
  28. // "\"a b c\" d e",
  29. "\"ab\\\"c\" \"\\\\\" d",
  30. "a\\\\\\b d\"e f\"g h",
  31. "a\\\\\\\"b c d",
  32. "a\\\\\\\\\"b c\" d e",
  33. };
  34. const int expected_argc[] =
  35. {
  36. 0,
  37. 0,
  38. // 3,
  39. 3,
  40. 3,
  41. 3,
  42. 3
  43. };
  44. const char* expected_results[] =
  45. {
  46. "a b c", "d", "e",
  47. "ab\"c", "\\", "d",
  48. "a\\\\\\b", "de fg", "h",
  49. "a\\\"b", "c", "d",
  50. "a\\\\b c", "d", "e",
  51. };
  52. const char** expected_argv[] =
  53. {
  54. NULL,
  55. NULL,
  56. // &expected_results[0],
  57. &expected_results[3],
  58. &expected_results[6],
  59. &expected_results[9],
  60. &expected_results[12],
  61. };
  62. for (uint32_t ii = 0; ii < BX_COUNTOF(exptected_argv); ++ii)
  63. {
  64. printf("x\n");
  65. char commandLine[1024];
  66. uint32_t size = BX_COUNTOF(commandLine);
  67. char* argv[50];
  68. int32_t argc;
  69. bx::tokenizeCommandLine(input[ii], commandLine, size, argc, argv, BX_COUNTOF(argv) );
  70. printf("\n%d (%d): %s %s\n", ii, argc, input[ii], expected_argc[ii] == argc ? "" : "FAILED!");
  71. for (uint32_t jj = 0; jj < argc; ++jj)
  72. {
  73. printf("\t%d: {%s} %s\n"
  74. , jj
  75. , argv[jj]
  76. , jj < argc ? (0==strcmp(argv[jj], expected_argv[ii][jj]) ? "" : "FAILED!") : "FAILED!"
  77. );
  78. }
  79. }
  80. #endif // 0
  81. }