tokenizecmd_test.cpp 1.6 KB

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