tokenizecmd_test.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2012-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/commandline.h>
  7. #include <bx/string.h>
  8. TEST_CASE("commandLine", "")
  9. {
  10. const char* args[] =
  11. {
  12. "-s",
  13. "--long",
  14. "--platform",
  15. "x",
  16. "--num", "1389",
  17. "--foo",
  18. "--", // it should not parse arguments after argument terminator
  19. "--bar",
  20. };
  21. bx::CommandLine cmdLine(BX_COUNTOF(args), args);
  22. REQUIRE( cmdLine.hasArg("long") );
  23. REQUIRE( cmdLine.hasArg('s') );
  24. int32_t num;
  25. REQUIRE(cmdLine.hasArg(num, '\0', "num") );
  26. REQUIRE(1389 == num);
  27. // test argument terminator
  28. REQUIRE( cmdLine.hasArg("foo") );
  29. REQUIRE(!cmdLine.hasArg("bar") );
  30. // non-existing argument
  31. REQUIRE(!cmdLine.hasArg('x') );
  32. REQUIRE(!cmdLine.hasArg("preprocess") );
  33. }
  34. static bool test(const char* _input, int32_t _argc, ...)
  35. {
  36. char buffer[1024];
  37. uint32_t len = sizeof(buffer);
  38. char* argv[32];
  39. int32_t argc;
  40. bx::tokenizeCommandLine(_input, buffer, len, argc, argv, BX_COUNTOF(argv) );
  41. if (_argc != argc)
  42. {
  43. return false;
  44. }
  45. va_list argList;
  46. va_start(argList, _argc);
  47. for (int32_t ii = 0; ii < _argc; ++ii)
  48. {
  49. const char* arg = va_arg(argList, const char*);
  50. if (0 != bx::strCmp(argv[ii], arg) )
  51. {
  52. return false;
  53. }
  54. }
  55. va_end(argList);
  56. return true;
  57. }
  58. TEST_CASE("tokenizeCommandLine", "")
  59. {
  60. REQUIRE(test(" ", 0, NULL) );
  61. REQUIRE(test("\\", 0, NULL) );
  62. REQUIRE(test("a b v g d", 5, "a", "b", "v", "g", "d") );
  63. REQUIRE(test("\"ab\\\"v\" \"\\\\\" g", 3, "ab\"v", "\\", "g") );
  64. REQUIRE(test("a\\\\\\\"b v g", 3, "a\\\"b", "v", "g") );
  65. REQUIRE(test("a\\\\\\\\\"b v\" g d", 3, "a\\\\b v", "g", "d") );
  66. }