diff options
Diffstat (limited to 'unit')
-rw-r--r-- | unit/test-argconfig-parse.c | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/unit/test-argconfig-parse.c b/unit/test-argconfig-parse.c index 23c8d4f..d861d69 100644 --- a/unit/test-argconfig-parse.c +++ b/unit/test-argconfig-parse.c @@ -7,7 +7,7 @@ #include <locale.h> #include "../util/argconfig.h" -#include "nvme/types.h" +#include "../util/cleanup.h" #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -28,6 +28,7 @@ union val { char *file; char *list; char *str; + __u8 val; }; struct toval_test { @@ -73,6 +74,7 @@ struct cfg { char *file; char *list; char *str; + __u8 val; }; static struct cfg cfg; @@ -105,6 +107,17 @@ static struct toval_test toval_tests[] = { VAL_TEST("--file=file", file, "file", false, 0), VAL_TEST("--list=list", list, "list", false, 0), VAL_TEST("--str=str", str, "str", false, 0), + VAL_TEST("--val=", val, 0, true, -EINVAL), + VAL_TEST("--val=o", val, 1, true, 0), + VAL_TEST("--val=t", val, 0, true, -EINVAL), + VAL_TEST("--val=tw", val, 2, true, 0), + VAL_TEST("--val=two", val, 2, true, 0), + VAL_TEST("--val=twoo", val, 0, true, -EINVAL), + VAL_TEST("--val=th", val, 3, true, 0), + VAL_TEST("--val=three", val, 3, true, 0), + VAL_TEST("--val=threed", val, 0, true, -EINVAL), + VAL_TEST("--val=123", val, 123, true, 0), + VAL_TEST("--val=1234", val, 0, true, -EINVAL), }; void toval_test(struct toval_test *test) @@ -113,6 +126,13 @@ void toval_test(struct toval_test *test) int ret; char *argv[] = { "test-argconfig", test->arg }; + OPT_VALS(opt_vals) = { + VAL_BYTE("one", 1), + VAL_BYTE("two", 2), + VAL_BYTE("three", 3), + VAL_END() + }; + OPT_ARGS(opts) = { OPT_FLAG("flag",'f', &cfg.flag, "flag"), OPT_SUFFIX("suffix", 's', &cfg.suffix, "suffix"), @@ -128,6 +148,7 @@ void toval_test(struct toval_test *test) OPT_FILE("file", 'L', &cfg.file, "file"), OPT_LIST("list", 'T', &cfg.list, "list"), OPT_STR("str", 'r', &cfg.str, "str"), + OPT_BYTE("val", 'v', &cfg.val, "val", opt_vals), OPT_END() }; @@ -143,6 +164,52 @@ void toval_test(struct toval_test *test) check_val(test->arg, &test->exp, test->val, test->size); } +#define COMMA_SEP_ARRAY_MAX_VALUES 4 + +struct comma_sep_array_test { + const char *input; + int ret; + __u32 values[COMMA_SEP_ARRAY_MAX_VALUES]; +}; + +const struct comma_sep_array_test comma_sep_array_tests[] = { + {"", 0}, + {",,,", 0}, + {" ", -1}, + {"abc", -1}, + {"0xFFFFFFFF", 1, {0xFFFFFFFF}}, + {"0x100000000", -1}, + {"123,0x456", 2, {123, 0x456}}, + {",1,,2,", 2, {1, 2}}, + {"1,22,333,4444", 4, {1, 22, 333, 4444}}, + {"1,2,3,4,5", -1}, +}; + +void comma_sep_array_test(const struct comma_sep_array_test *test) +{ + _cleanup_free_ char *input = strdup(test->input); + __u32 values[COMMA_SEP_ARRAY_MAX_VALUES] = {}; + int ret = argconfig_parse_comma_sep_array_u32( + input, values, COMMA_SEP_ARRAY_MAX_VALUES); + int i; + + if (ret != test->ret) { + printf("ERROR: input '%s' return value %d != %d\n", + test->input, ret, test->ret); + test_rc = 1; + return; + } + + for (i = 0; i < ret; i++) { + if (values[i] != test->values[i]) { + printf("ERROR: input '%s' values[%d] = %u != %u\n", + test->input, i, values[i], test->values[i]); + test_rc = 1; + return; + } + } +} + int main(void) { unsigned int i; @@ -157,6 +224,9 @@ int main(void) for (i = 0; i < ARRAY_SIZE(toval_tests); i++) toval_test(&toval_tests[i]); + for (i = 0; i < ARRAY_SIZE(comma_sep_array_tests); i++) + comma_sep_array_test(&comma_sep_array_tests[i]); + if (f) fclose(f); |