Added string vector(argv like) functions

This commit is contained in:
Nils O. Selåsdal
2013-09-04 18:10:26 +02:00
parent a5b8d53177
commit 216dfe879d
4 changed files with 291 additions and 1 deletions
+91
View File
@@ -0,0 +1,91 @@
#include <check.h>
#include <string.h>
#include <ucore/strvec.h>
START_TEST (test_strv_append)
struct UCStrv strv;
int rc;
uc_strv_init(&strv);
rc = uc_strv_append(&strv, "string");
fail_if(rc != 0);
fail_if(strv.cnt != 1);
ck_assert_str_eq("string", strv.strings[0]);
rc = uc_strv_append(&strv, "string2");
fail_if(rc != 0);
fail_if(strv.cnt != 2);
ck_assert_str_eq("string2", strv.strings[1]);
uc_strv_destroy(&strv);
END_TEST
START_TEST (test_strv_clear)
struct UCStrv strv;
int rc;
uc_strv_init(&strv);
rc = uc_strv_append(&strv, "string");
fail_if(rc != 0);
fail_if(strv.cnt != 1);
uc_strv_clear(&strv);
fail_if(strv.cnt != 0);
uc_strv_destroy(&strv);
END_TEST
START_TEST (test_strv_nocopy)
struct UCStrv strv;
char *str = strdup("string");
int rc;
uc_strv_init(&strv);
rc = uc_strv_append_nocopy(&strv, str);
fail_if(rc != 0);
fail_if(strv.cnt != 1);
fail_if(strv.strings[0] != str);
uc_strv_destroy(&strv);
END_TEST
START_TEST (test_strv_null_terminate)
struct UCStrv strv;
int rc;
uc_strv_init(&strv);
rc = uc_strv_append(&strv, "string");
fail_if(rc != 0);
fail_if(strv.cnt != 1);
rc = uc_strv_null_terminate(&strv);
fail_if(rc != 0);
fail_if(strv.strings[strv.cnt] != NULL);
uc_strv_destroy(&strv);
END_TEST
Suite *strv_suite(void)
{
Suite *s = suite_create("strvec");
TCase *tc = tcase_create("strvec");
tcase_add_test(tc, test_strv_append);
tcase_add_test(tc, test_strv_clear);
tcase_add_test(tc, test_strv_nocopy);
tcase_add_test(tc, test_strv_null_terminate);
suite_add_tcase(s, tc);
return s;
}