Add function to split string into list.

- With working unit tests.
This commit is contained in:
Nexarian
2023-02-15 00:50:28 -05:00
parent 39abe6fbfe
commit 89e178e7c9
4 changed files with 141 additions and 13 deletions
+44 -2
View File
@@ -1,4 +1,3 @@
#if defined(HAVE_CONFIG_H)
#include "config_ac.h"
#endif
@@ -112,8 +111,49 @@ START_TEST(test_list__simple_append_list)
}
END_TEST
/******************************************************************************/
int
split_string_append_fragment(const char **start, const char *end,
struct list *list);
START_TEST(test_list__append_fragment)
{
struct list *l = list_create();
l->auto_free = 1;
const char *test_string = "split this";
int fragment_ret = split_string_append_fragment(&test_string,
test_string + 5, l);
ck_assert_int_eq(fragment_ret, 1);
ck_assert_str_eq((const char *)l->items[0], "split");
fragment_ret = split_string_append_fragment(&test_string,
test_string + 1000, l);
ck_assert_int_eq(fragment_ret, 1);
ck_assert_str_eq((const char *)l->items[1], "this");
list_delete(l);
}
END_TEST
START_TEST(test_list__split_string_into_list)
{
struct list *l = split_string_into_list("The fat cat sat on my hat.", ' ');
ck_assert_int_eq(l->count, 7);
ck_assert_str_eq((const char *)l->items[0], "The");
ck_assert_str_eq((const char *)l->items[1], "fat");
ck_assert_str_eq((const char *)l->items[2], "cat");
ck_assert_str_eq((const char *)l->items[3], "sat");
ck_assert_str_eq((const char *)l->items[4], "on");
ck_assert_str_eq((const char *)l->items[5], "my");
ck_assert_str_eq((const char *)l->items[6], "hat.");
list_delete(l);
}
END_TEST
/******************************************************************************/
Suite *
make_suite_test_list(void)
{
@@ -127,6 +167,8 @@ make_suite_test_list(void)
tcase_add_test(tc_simple, test_list__simple);
tcase_add_test(tc_simple, test_list__simple_auto_free);
tcase_add_test(tc_simple, test_list__simple_append_list);
tcase_add_test(tc_simple, test_list__append_fragment);
tcase_add_test(tc_simple, test_list__split_string_into_list);
return s;
}