tokenizecmd_test.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "--platform",
  15. "x",
  16. };
  17. bx::CommandLine cmdLine(BX_COUNTOF(args), args);
  18. CHECK(cmdLine.hasArg("long") );
  19. CHECK(cmdLine.hasArg('s') );
  20. // non-existing argument
  21. CHECK(!cmdLine.hasArg('x') );
  22. CHECK(!cmdLine.hasArg("preprocess") );
  23. }
  24. TEST(tokenizeCommandLine)
  25. {
  26. #if 0
  27. const char* input[] =
  28. {
  29. " ",
  30. "\\",
  31. // "\"a b c\" d e",
  32. "\"ab\\\"c\" \"\\\\\" d",
  33. "a\\\\\\b d\"e f\"g h",
  34. "a\\\\\\\"b c d",
  35. "a\\\\\\\\\"b c\" d e",
  36. };
  37. const int expected_argc[] =
  38. {
  39. 0,
  40. 0,
  41. // 3,
  42. 3,
  43. 3,
  44. 3,
  45. 3
  46. };
  47. const char* expected_results[] =
  48. {
  49. "a b c", "d", "e",
  50. "ab\"c", "\\", "d",
  51. "a\\\\\\b", "de fg", "h",
  52. "a\\\"b", "c", "d",
  53. "a\\\\b c", "d", "e",
  54. };
  55. const char** expected_argv[] =
  56. {
  57. NULL,
  58. NULL,
  59. // &expected_results[0],
  60. &expected_results[3],
  61. &expected_results[6],
  62. &expected_results[9],
  63. &expected_results[12],
  64. };
  65. for (uint32_t ii = 0; ii < BX_COUNTOF(exptected_argv); ++ii)
  66. {
  67. printf("x\n");
  68. char commandLine[1024];
  69. uint32_t size = BX_COUNTOF(commandLine);
  70. char* argv[50];
  71. int32_t argc;
  72. bx::tokenizeCommandLine(input[ii], commandLine, size, argc, argv, BX_COUNTOF(argv) );
  73. printf("\n%d (%d): %s %s\n", ii, argc, input[ii], expected_argc[ii] == argc ? "" : "FAILED!");
  74. for (uint32_t jj = 0; jj < argc; ++jj)
  75. {
  76. printf("\t%d: {%s} %s\n"
  77. , jj
  78. , argv[jj]
  79. , jj < argc ? (0==strcmp(argv[jj], expected_argv[ii][jj]) ? "" : "FAILED!") : "FAILED!"
  80. );
  81. }
  82. }
  83. #endif // 0
  84. }