sb: add some more tests

This commit is contained in:
Boris Faure 2020-05-24 00:35:27 +02:00
parent c2c324c497
commit 4703e374b9
Signed by: borisfaure
GPG Key ID: 35C0410516166BE8
1 changed files with 33 additions and 0 deletions

View File

@ -247,6 +247,9 @@ tytest_sb_trim(void)
assert(strncmp(sb.buf, "sb_trim_spaces", sb.len) == 0);
ty_sb_free(&sb);
/* on empty */
ty_sb_spaces_rtrim(&sb);
ty_sb_spaces_ltrim(&sb);
return 0;
}
@ -272,17 +275,47 @@ tytest_sb_gap(void)
assert(ty_sb_prepend(&sb, ">>>>>>", strlen(">>>>>>")) == 0);
assert(sb.len == strlen(data) + 6);
assert(strncmp(sb.buf,">>>>>>alpha", 6+5) == 0);
ty_sb_free(&sb);
/** add that realloc, with gap */
assert(ty_sb_add(&sb, "foobar", strlen("foobar")) == 0);
ty_sb_lskip(&sb, 3);
assert(ty_sb_add(&sb, data, strlen(data)) == 0);
ty_sb_free(&sb);
return 0;
}
static int
tytest_sb_steal(void)
{
struct ty_sb sb = {};
const char *data = "foobar foobar";
char *buf;
assert(ty_sb_steal_buf(&sb) == NULL);
/* with gap */
assert(ty_sb_add(&sb, data, strlen(data)) == 0);
ty_sb_lskip(&sb, 7);
buf = ty_sb_steal_buf(&sb);
assert(strlen(buf) == strlen("foobar"));
assert(sb.buf == NULL);
assert(sb.len == 0);
assert(sb.alloc == 0);
assert(sb.gap == 0);
free(buf);
ty_sb_free(&sb);
return 0;
}
int
tytest_sb(void)
{
assert(tytest_sb_skip() == 0);
assert(tytest_sb_trim() == 0);
assert(tytest_sb_gap() == 0);
assert(tytest_sb_steal() == 0);
return 0;
}
#endif