Files
libucore/test/test_strv.c
T
2025-06-28 00:56:58 +02:00

156 lines
3.0 KiB
C

#include <check.h>
#include <string.h>
#include <stdlib.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.cnt != 1); //shouldn't increase .cnt
fail_if(strv.strings[strv.cnt] != NULL);
uc_strv_destroy(&strv);
}
END_TEST
START_TEST (test_strv_append_all_copy)
{
struct UCStrv src;
struct UCStrv dst;
int rc;
uc_strv_init(&src);
uc_strv_init(&dst);
uc_strv_append(&dst, "string1");
uc_strv_append(&src, "string2");
uc_strv_append(&src, "string3");
rc = uc_strv_append_all(&dst, &src, 1);
fail_if(rc != 0);
fail_if(dst.cnt != 3);
ck_assert_str_eq("string1", dst.strings[0]);
ck_assert_str_eq("string2", dst.strings[1]);
ck_assert_str_eq("string3", dst.strings[2]);
uc_strv_destroy(&src);
uc_strv_destroy(&dst);
}
END_TEST
START_TEST (test_strv_append_all_nocopy)
{
struct UCStrv src;
struct UCStrv dst;
int rc;
uc_strv_init(&src);
uc_strv_init(&dst);
uc_strv_append(&dst, "string1");
uc_strv_append(&src, "string2");
uc_strv_append(&src, "string3");
rc = uc_strv_append_all(&dst, &src, 0);
fail_if(rc != 0);
fail_if(dst.cnt != 3);
ck_assert_str_eq("string1", dst.strings[0]);
ck_assert_str_eq("string2", dst.strings[1]);
ck_assert_str_eq("string3", dst.strings[2]);
uc_strv_destroy(&dst);
free(src.strings);
}
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);
tcase_add_test(tc, test_strv_append_all_copy);
tcase_add_test(tc, test_strv_append_all_nocopy);
suite_add_tcase(s, tc);
return s;
}