]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
test: add test cases for argconfig comma-separated array parsing
authorCaleb Sander Mateos <csander@purestorage.com>
Tue, 16 Jul 2024 22:46:04 +0000 (16:46 -0600)
committerDaniel Wagner <wagi@monom.org>
Wed, 17 Jul 2024 14:57:45 +0000 (16:57 +0200)
There are no unit tests currently covering
argconfig's comma-separated integer array parsing.
Add test cases to test-argconfig-parse.c
that use argconfig_parse_comma_sep_array_u32(),
verifying its functionality and edge cases.

Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
unit/test-argconfig-parse.c

index 24756163ff5c8aae74ea9e73f352e19da6c197ea..d861d69279f18fb5dcfa6f9a9e6be0cc3b008ed9 100644 (file)
@@ -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]))
 
@@ -164,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;
@@ -178,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);