summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bx/tests/tokenizecmd_test.cpp
blob: fece7ea2f257d9dd1046f6b9627f7fdd4c13764a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright 2012-2017 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
 */

#include "test.h"
#include <bx/commandline.h>
#include <bx/string.h>

TEST_CASE("commandLine", "")
{
	const char* args[] =
	{
		"-s",
		"--long",
		"--platform",
		"x",
		"--num", "1389",
		"--foo",
		"--", // it should not parse arguments after argument terminator
		"--bar",
	};

	bx::CommandLine cmdLine(BX_COUNTOF(args), args);

	REQUIRE( cmdLine.hasArg("long") );
	REQUIRE( cmdLine.hasArg('s') );

	int32_t num;
	REQUIRE(cmdLine.hasArg(num, '\0', "num") );
	REQUIRE(1389 == num);

	// test argument terminator
	REQUIRE( cmdLine.hasArg("foo") );
	REQUIRE(!cmdLine.hasArg("bar") );

	// non-existing argument
	REQUIRE(!cmdLine.hasArg('x') );
	REQUIRE(!cmdLine.hasArg("preprocess") );
}

static bool test(const char* _input, int32_t _argc, ...)
{
	char buffer[1024];
	uint32_t len = sizeof(buffer);
	char* argv[32];
	int32_t argc;
	bx::tokenizeCommandLine(_input, buffer, len, argc, argv, BX_COUNTOF(argv) );

	if (_argc != argc)
	{
		return false;
	}

	va_list argList;
	va_start(argList, _argc);

	for (int32_t ii = 0; ii < _argc; ++ii)
	{
		const char* arg = va_arg(argList, const char*);
		if (0 != bx::strCmp(argv[ii], arg) )
		{
			return false;
		}
	}

	va_end(argList);

	return true;
}

TEST_CASE("tokenizeCommandLine", "")
{
	REQUIRE(test("      ", 0, NULL) );
	REQUIRE(test("\\",     0, NULL) );

	REQUIRE(test("a b v g d", 5, "a", "b", "v", "g", "d") );

	REQUIRE(test("\"ab\\\"v\" \"\\\\\" g", 3, "ab\"v",    "\\",   "g") );
	REQUIRE(test("a\\\\\\\"b v g",         3, "a\\\"b",   "v",  "g") );
	REQUIRE(test("a\\\\\\\\\"b v\" g d",   3, "a\\\\b v", "g",  "d") );
}