2020-12-01 14:55:16 -05:00
|
|
|
#include <kernaux/cmdline.h>
|
|
|
|
|
2020-12-01 15:22:38 -05:00
|
|
|
#include <assert.h>
|
2020-12-01 15:29:45 -05:00
|
|
|
#include <string.h>
|
2020-12-01 15:22:38 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
static const unsigned int ARGV_COUNT_MAX = 100;
|
2020-12-01 15:22:38 -05:00
|
|
|
static const unsigned int ARG_SIZE_MAX = 4096;
|
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
static void test(
|
|
|
|
const char *cmdline,
|
|
|
|
unsigned int argv_count_max,
|
|
|
|
unsigned int arg_size_max,
|
2020-12-01 15:22:38 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
kernaux_bool expected_result,
|
|
|
|
const char *expected_error_msg,
|
|
|
|
unsigned int expected_argc,
|
|
|
|
const char *const *const expected_argv
|
|
|
|
);
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
static const char *const argv0[] = {};
|
2020-12-01 15:22:38 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
static const char *const argv_foo[] = {"foo"};
|
2020-12-01 15:29:45 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
static const char *const argv_foo_bar[] = {"foo", "bar"};
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
static const char *const argv_foo_bar_car[] = {"foo", "bar", "car"};
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
test(
|
|
|
|
"",
|
2020-12-01 15:32:35 -05:00
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
0,
|
|
|
|
argv0
|
|
|
|
);
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
|
|
|
" ",
|
|
|
|
ARGV_COUNT_MAX,
|
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
0,
|
|
|
|
argv0
|
|
|
|
);
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:32:35 -05:00
|
|
|
"foo",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
1,
|
|
|
|
argv_foo
|
|
|
|
);
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:32:35 -05:00
|
|
|
"foo bar",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:32:35 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
" foo bar",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
"foo bar ",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
" foo bar ",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
"foo bar",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
" foo bar",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
"foo bar ",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
" foo bar ",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
2,
|
|
|
|
argv_foo_bar
|
|
|
|
);
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
test(
|
2020-12-01 15:59:53 -05:00
|
|
|
"foo bar car",
|
|
|
|
ARGV_COUNT_MAX,
|
2020-12-01 16:34:46 -05:00
|
|
|
ARG_SIZE_MAX,
|
2020-12-01 15:59:53 -05:00
|
|
|
|
2020-12-01 16:34:46 -05:00
|
|
|
KERNAUX_TRUE,
|
|
|
|
"",
|
|
|
|
3,
|
|
|
|
argv_foo_bar_car
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test(
|
|
|
|
const char *const cmdline,
|
|
|
|
const unsigned int argv_count_max,
|
|
|
|
const unsigned int arg_size_max,
|
|
|
|
|
|
|
|
const kernaux_bool expected_result,
|
|
|
|
const char *const expected_error_msg,
|
|
|
|
unsigned int expected_argc,
|
|
|
|
const char *const *const expected_argv
|
|
|
|
) {
|
|
|
|
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
|
|
|
|
unsigned int argc = 1234;
|
|
|
|
char *argv[argv_count_max];
|
|
|
|
char buffer[argv_count_max * arg_size_max];
|
|
|
|
|
|
|
|
assert(
|
|
|
|
kernaux_cmdline_parse(
|
|
|
|
cmdline,
|
|
|
|
error_msg,
|
|
|
|
&argc,
|
|
|
|
argv,
|
|
|
|
buffer,
|
|
|
|
argv_count_max,
|
|
|
|
arg_size_max
|
|
|
|
) == !!expected_result
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(strcmp(error_msg, expected_error_msg) == 0);
|
|
|
|
assert(argc == expected_argc);
|
|
|
|
|
|
|
|
for (unsigned int index = 0; index < argc; ++index) {
|
|
|
|
assert(strcmp(argv[index], expected_argv[index]) == 0);
|
|
|
|
}
|
2020-12-01 15:59:53 -05:00
|
|
|
|
|
|
|
for (unsigned int index = argc; index < ARGV_COUNT_MAX; ++index) {
|
|
|
|
assert(argv[index] == KERNAUX_NULL);
|
|
|
|
}
|
2020-12-01 14:55:16 -05:00
|
|
|
}
|