187 lines
4.2 KiB
C
187 lines
4.2 KiB
C
#include <check.h>
|
|
#include <ctype.h>
|
|
#include <ucore/dstr.h>
|
|
#include <ucore/utils.h>
|
|
|
|
|
|
START_TEST (test_dstr_basics)
|
|
struct DStr str;
|
|
int rc;
|
|
|
|
char *s;
|
|
|
|
uc_dstr_init(&str);
|
|
rc = uc_dstr_put_str(&str, "Hello");
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_int_eq(str.len, 5);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "Hello");
|
|
|
|
rc = uc_dstr_put_char(&str, '!');
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_int_eq(str.len, 6);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "Hello!");
|
|
|
|
rc = uc_dstr_put_str_sz(&str, " World 12345678", 13);
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_int_eq(str.len, 19);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "Hello! World 123456");
|
|
|
|
uc_dstr_reset(&str);
|
|
ck_assert_int_eq(str.len, 0);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "");
|
|
|
|
uc_dstr_reset(&str);
|
|
ck_assert_int_gt(uc_dstr_available(&str), 0);
|
|
|
|
s = uc_dstr_put(&str, 2);
|
|
fail_if(s == NULL);
|
|
s[0] = '1';
|
|
s[1] = '2';
|
|
|
|
ck_assert_str_eq(uc_dstr_str(&str), "12");
|
|
|
|
uc_dstr_destroy(&str);
|
|
|
|
//just test that this doesn't crash:
|
|
uc_dstr_init(&str);
|
|
uc_dstr_destroy(&str);
|
|
END_TEST
|
|
|
|
START_TEST (test_dstr_own_steal)
|
|
struct DStr str;
|
|
char *s;
|
|
|
|
uc_dstr_init(&str);
|
|
uc_dstr_own(&str, strdup("Hello !"));
|
|
ck_assert_int_eq(str.len, 7);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "Hello !");
|
|
|
|
s = uc_dstr_steal(&str);
|
|
ck_assert_str_eq(s, "Hello !");
|
|
ck_assert_int_eq(str.len, 0);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "");
|
|
|
|
free(s);
|
|
uc_dstr_destroy(&str);
|
|
END_TEST
|
|
|
|
START_TEST (test_dstr_trim)
|
|
struct DStr str = UC_DSTR_STATIC_INIT;
|
|
|
|
uc_dstr_put_str(&str, " \t \nHello !\r\n");
|
|
uc_dstr_trim(&str);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "Hello !");
|
|
|
|
uc_dstr_destroy(&str);
|
|
uc_dstr_init(&str);
|
|
|
|
uc_dstr_put_str(&str, " ");
|
|
|
|
uc_dstr_trim(&str);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "");
|
|
|
|
uc_dstr_destroy(&str);
|
|
uc_dstr_init(&str);
|
|
|
|
uc_dstr_trim(&str);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "");
|
|
|
|
uc_dstr_destroy(&str);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_dstr_replace)
|
|
struct DStr str;
|
|
size_t rc;
|
|
|
|
uc_dstr_init(&str);
|
|
uc_dstr_put_str(&str, "-1-2-3-4-5-");
|
|
rc = uc_dstr_replace(&str, '-',' ');
|
|
ck_assert_int_eq(rc, 6);
|
|
|
|
ck_assert_str_eq(uc_dstr_str(&str), " 1 2 3 4 5 ");
|
|
rc = uc_dstr_replace(&str, 'X','2');
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_str_eq(uc_dstr_str(&str), " 1 2 3 4 5 ");
|
|
|
|
uc_dstr_destroy(&str);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_dstr_filter)
|
|
struct DStr str;
|
|
size_t rc;
|
|
|
|
uc_dstr_init(&str);
|
|
uc_dstr_put_str(&str, "AB:");
|
|
rc = uc_dstr_put_str_filter(&str, "1:2,3 4 ", isdigit);
|
|
ck_assert_int_eq(rc, 4);
|
|
|
|
ck_assert_str_eq(uc_dstr_str(&str), "AB:1234");
|
|
|
|
uc_dstr_destroy(&str);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_dstr_sprintf)
|
|
struct DStr str;
|
|
size_t rc;
|
|
|
|
uc_dstr_init(&str);
|
|
rc = uc_dstr_sprintf(&str, "%d-%d-%d", 1, 2, 3);
|
|
ck_assert_int_gt(rc, 0);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "1-2-3");
|
|
|
|
rc = uc_dstr_sprintf(&str, " Another, bigger %s.", "string");
|
|
ck_assert_int_gt(rc, 0);
|
|
ck_assert_str_eq(uc_dstr_str(&str), "1-2-3 Another, bigger string.");
|
|
|
|
uc_dstr_destroy(&str);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_dstr_copy)
|
|
struct DStr str;
|
|
struct DStr copy;
|
|
size_t rc;
|
|
|
|
uc_dstr_init(&str);
|
|
rc = uc_dstr_put_str(&str, "123");
|
|
ck_assert_int_eq(rc, 0);
|
|
|
|
rc = uc_dstr_copy(©, &str);
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_str_eq(uc_dstr_str(©), uc_dstr_str(&str));
|
|
|
|
uc_dstr_destroy(&str);
|
|
uc_dstr_destroy(©);
|
|
uc_dstr_init(&str);
|
|
uc_dstr_init(©);
|
|
|
|
rc = uc_dstr_copy(©, &str);
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_str_eq(uc_dstr_str(©), "");
|
|
|
|
uc_dstr_destroy(&str);
|
|
uc_dstr_destroy(©);
|
|
|
|
END_TEST
|
|
|
|
Suite *dstr_suite(void)
|
|
{
|
|
Suite *s = suite_create("dstr");
|
|
TCase *tc = tcase_create("dstr tests");
|
|
tcase_add_test(tc, test_dstr_basics);
|
|
tcase_add_test(tc, test_dstr_own_steal);
|
|
tcase_add_test(tc, test_dstr_trim);
|
|
tcase_add_test(tc, test_dstr_replace);
|
|
tcase_add_test(tc, test_dstr_filter);
|
|
tcase_add_test(tc, test_dstr_sprintf);
|
|
tcase_add_test(tc, test_dstr_copy);
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
}
|
|
|