]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-strescape - Refactor the unit test to use an array of tests
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Sun, 5 Nov 2017 19:14:41 +0000 (21:14 +0200)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 6 Nov 2017 07:34:31 +0000 (09:34 +0200)
src/lib/test-strescape.c

index 5de54f5c0f6727abed50e51beec4c11b87a431e0..2314956e43217675aee2fa04486f1ab541dcd854 100644 (file)
@@ -9,12 +9,31 @@ struct strinput {
        const char *output;
 };
 
-static const char *tabescaped_input = "\0011\001t\001r\001nplip\001n";
-static const char *tabunescaped_input = "\001\t\r\nplip\n";
+static const char tabescaped_input[] = "\0011\001t\001r\001nplip\001n";
+static const char tabunescaped_input[] = "\001\t\r\nplip\n";
 
 static const char *wrong_tabescaped_input = "a\001\001b\001\nc\0011\001t\001r\001nplip\001n";
 static const char *wrong_tabescaped_output = "a\001b\nc\001\t\r\nplip\n";
 
+static struct {
+       const char *input;
+       const char *const *output;
+} strsplit_tests[] = {
+       { /*tabescaped_input3*/NULL, (const char *const []) {
+               tabunescaped_input,
+               tabunescaped_input,
+               tabunescaped_input,
+               "",
+               NULL
+       } },
+       { "", (const char *const []) { NULL } },
+       { "\t", (const char *const []) { "", "", NULL } },
+       { tabescaped_input, (const char *const []) {
+               tabunescaped_input,
+               NULL
+       } },
+};
+
 static void test_str_escape(void)
 {
        static const struct strinput unesc[] = {
@@ -125,37 +144,21 @@ static void test_tabescape(void)
 
 static void test_strsplit_tabescaped(void)
 {
-       const char *input = t_strdup_printf("%s\t%s\t%s\t",
-               tabescaped_input, tabescaped_input, tabescaped_input);
-       const char *const output[] = {
-               tabunescaped_input,
-               tabunescaped_input,
-               tabunescaped_input,
-               "",
-               NULL
-       };
        const char *const *args;
 
        test_begin("*_strsplit_tabescaped()");
-
-       args = t_strsplit_tabescaped("");
-       test_assert(args[0] == NULL);
-
-       args = t_strsplit_tabescaped("\t");
-       test_assert(args[0][0] == '\0' && args[1][0] == '\0' && args[2] == NULL);
-
-       args = t_strsplit_tabescaped(tabescaped_input);
-       test_assert(strcmp(args[0], tabunescaped_input) == 0 && args[1] == NULL);
-
-       args = t_strsplit_tabescaped(input);
-       for (unsigned int i = 0; i < N_ELEMENTS(output); i++)
-               test_assert_idx(null_strcmp(output[i], args[i]) == 0, i);
-
+       for (unsigned int i = 0; i < N_ELEMENTS(strsplit_tests); i++) {
+               args = t_strsplit_tabescaped(strsplit_tests[i].input);
+               for (unsigned int j = 0; strsplit_tests[i].output[j] != NULL; j++)
+                       test_assert_idx(null_strcmp(strsplit_tests[i].output[j], args[j]) == 0, i);
+       }
        test_end();
 }
 
 void test_strescape(void)
 {
+       strsplit_tests[0].input = t_strdup_printf("%s\t%s\t%s\t",
+               tabescaped_input, tabescaped_input, tabescaped_input);
        test_str_escape();
        test_tabescape();
        test_strsplit_tabescaped();