diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-20 17:43:21 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-21 16:14:31 +0200 |
commit | 12727806af641970a651b8f969cba33677ae7395 (patch) | |
tree | f02cb947ac2379cdd9bc15e093ec732dfd5bc45f /lib/util/test | |
parent | ca9b6ba17257f88b8d575f18cab0b1e23660cfa5 (diff) |
Add a helper to libutil for splitting token separated lines
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util/test')
-rw-r--r-- | lib/util/test/split_line.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/lib/util/test/split_line.c b/lib/util/test/split_line.c new file mode 100644 index 0000000..a0c32b8 --- /dev/null +++ b/lib/util/test/split_line.c @@ -0,0 +1,120 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * split_line.c + * + * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" +#include "util/parse.h" +#include "util/test.h" +#include "compat.h" + +#include <stdlib.h> +#include <string.h> + +static const struct { + const char *in; + size_t count; + const char **out; +} split[] = { + { "", 0, NULL }, + { " \t ", 0, NULL }, + { "foo", 1, (const char *[]){ "foo" } }, + { " foo ", 1, (const char *[]){ "foo" } }, + { "foo bar", 2, (const char *[]){ "foo", "bar" } }, + { " foo \t bar ", 2, (const char *[]){ "foo", "bar" } }, + { " foo \t bar baz ", 3, (const char *[]){ "foo", "bar", "baz" } }, + { " foo \t \" bar \" baz ", 3, + (const char *[]){ "foo", " bar ", "baz" } }, + { " foo \t \" \\\"bar \" baz ", 3, + (const char *[]){ "foo", " \"bar ", "baz" } }, + { " foo \t \" bar \\\\\" baz ", 3, + (const char *[]){ "foo", " bar \\", "baz" } }, +}; + +static const struct { + const char *in; + size_t orig; + size_t count; + size_t remain; + const char **out; +} drop_test[] = { + { "foo,bar,baz", 3, 0, 3, (const char *[]){ "foo", "bar", "baz" } }, + { "foo,bar,baz", 3, 1, 2, (const char *[]){ "bar", "baz" } }, + { "foo,bar,baz", 3, 2, 1, (const char *[]){ "baz" } }, + { "foo,bar,baz", 3, 3, 0, NULL }, + { "foo,bar,baz", 3, 4, 0, NULL }, + { "foo,bar,baz", 3, 100, 0, NULL }, +}; + +static void dump_components(split_line_t *sep) +{ + for (size_t i = 0; i < sep->count; ++i) + fprintf(stderr, "\t`%s`\n", sep->args[i]); +} + +int main(int argc, char **argv) +{ + (void)argc; (void)argv; + + for (size_t i = 0; i < sizeof(split) / sizeof(split[0]); ++i) { + split_line_t *sep; + char *copy; + int ret; + + copy = strdup(split[i].in); + TEST_NOT_NULL(copy); + + ret = split_line(copy, strlen(copy), " \t", &sep); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(sep); + + fprintf(stderr, "splitting `%s`\n", split[i].in); + dump_components(sep); + + TEST_EQUAL_UI(sep->count, split[i].count); + + for (size_t j = 0; j < sep->count; ++j) { + TEST_STR_EQUAL(sep->args[j], split[i].out[j]); + } + + free(sep); + free(copy); + } + + for (size_t i = 0; i < sizeof(drop_test) / sizeof(drop_test[0]); ++i) { + split_line_t *sep; + char *copy; + int ret; + + copy = strdup(drop_test[i].in); + TEST_NOT_NULL(copy); + + fprintf(stderr, "splitting `%s`\n", drop_test[i].in); + + ret = split_line(copy, strlen(copy), ",", &sep); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(sep); + + dump_components(sep); + + TEST_EQUAL_UI(sep->count, drop_test[i].orig); + + fprintf(stderr, "removing first %u components\n", + (unsigned int)drop_test[i].count); + + split_line_remove_front(sep, drop_test[i].count); + dump_components(sep); + + TEST_EQUAL_UI(sep->count, drop_test[i].remain); + + for (size_t j = 0; j < sep->count; ++j) { + TEST_STR_EQUAL(sep->args[j], drop_test[i].out[j]); + } + + free(sep); + free(copy); + } + + return EXIT_SUCCESS; +} |