Initial version of struct DStr
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
#include <check.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_ge(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_init(&str);
|
||||
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
|
||||
|
||||
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);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user