tokenizecmd.cpp 1.6 KB

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