|
| 1 | +#include "unit-test.h" |
| 2 | +#include "string-list.h" |
| 3 | + |
| 4 | +static void t_vcreate_string_list_dup(struct string_list *list, |
| 5 | + int free_util, va_list ap) |
| 6 | +{ |
| 7 | + const char *arg; |
| 8 | + |
| 9 | + cl_assert(list->strdup_strings); |
| 10 | + |
| 11 | + string_list_clear(list, free_util); |
| 12 | + while ((arg = va_arg(ap, const char *))) |
| 13 | + string_list_append(list, arg); |
| 14 | +} |
| 15 | + |
| 16 | +static void t_string_list_equal(struct string_list *list, |
| 17 | + struct string_list *expected_strings) |
| 18 | +{ |
| 19 | + cl_assert_equal_i(list->nr, expected_strings->nr); |
| 20 | + cl_assert(list->nr <= list->alloc); |
| 21 | + for (size_t i = 0; i < expected_strings->nr; i++) |
| 22 | + cl_assert_equal_s(list->items[i].string, |
| 23 | + expected_strings->items[i].string); |
| 24 | +} |
| 25 | + |
| 26 | +static void t_string_list_split(const char *data, int delim, int maxsplit, ...) |
| 27 | +{ |
| 28 | + struct string_list expected_strings = STRING_LIST_INIT_DUP; |
| 29 | + struct string_list list = STRING_LIST_INIT_DUP; |
| 30 | + va_list ap; |
| 31 | + int len; |
| 32 | + |
| 33 | + va_start(ap, maxsplit); |
| 34 | + t_vcreate_string_list_dup(&expected_strings, 0, ap); |
| 35 | + va_end(ap); |
| 36 | + |
| 37 | + string_list_clear(&list, 0); |
| 38 | + len = string_list_split(&list, data, delim, maxsplit); |
| 39 | + cl_assert_equal_i(len, expected_strings.nr); |
| 40 | + t_string_list_equal(&list, &expected_strings); |
| 41 | + |
| 42 | + string_list_clear(&expected_strings, 0); |
| 43 | + string_list_clear(&list, 0); |
| 44 | +} |
| 45 | + |
| 46 | +void test_string_list__split(void) |
| 47 | +{ |
| 48 | + t_string_list_split("foo:bar:baz", ':', -1, "foo", "bar", "baz", NULL); |
| 49 | + t_string_list_split("foo:bar:baz", ':', 0, "foo:bar:baz", NULL); |
| 50 | + t_string_list_split("foo:bar:baz", ':', 1, "foo", "bar:baz", NULL); |
| 51 | + t_string_list_split("foo:bar:baz", ':', 2, "foo", "bar", "baz", NULL); |
| 52 | + t_string_list_split("foo:bar:", ':', -1, "foo", "bar", "", NULL); |
| 53 | + t_string_list_split("", ':', -1, "", NULL); |
| 54 | + t_string_list_split(":", ':', -1, "", "", NULL); |
| 55 | +} |
0 commit comments