Files
2025-07-18 22:45:08 +02:00

142 lines
2.7 KiB
C

#include <check.h>
#include <string.h>
#include "ucore/buffer.h"
START_TEST (test_new_gbuf)
{
GBuf *buf = uc_new_gbuf(11);
fail_if(buf == NULL);
fail_if(buf->len != 11);
fail_if(buf->ref_cnt != 1);
fail_if(buf->buf == NULL);
fail_if(buf->used != 0);
uc_gbuf_unref(buf);
}
END_TEST
START_TEST (test_gbuf_grow)
{
GBuf *buf = uc_new_gbuf(33);
int rc;
fail_if(buf == NULL);
fail_if(buf->len != 33);
rc = uc_gbuf_grow(buf, 128);
fail_if(rc != 0);
fail_if(buf->len < 128 + 33);
fail_if(buf->used != 0);
uc_gbuf_unref(buf);
}
END_TEST
START_TEST (test_gbuf_append1)
{
const char *data = "lorum_ ipson 1234567890";
int rc;
GBuf *buf = uc_new_gbuf(11);
fail_if(buf == NULL);
rc = uc_gbuf_append(buf, data, strlen(data) + 1);
fail_if(rc != 0);
fail_if(buf->used != strlen(data) + 1);
fail_if(strcmp(data, buf->buf) != 0);
uc_gbuf_unref(buf);
}
END_TEST
START_TEST (test_gbuf_printf1)
{
int rc;
GBuf *buf = uc_new_gbuf(10);
fail_if(buf == NULL);
rc = uc_gbuf_printf(buf, "test %d", 1);
fail_if(rc != 6);
fail_if(buf->used != 6);
fail_if(strcmp("test 1", buf->buf) != 0);
uc_gbuf_unref(buf);
}
END_TEST
START_TEST (test_gbuf_printf2)
{
int rc;
GBuf *buf = uc_new_gbuf(8);
fail_if(buf == NULL);
rc = uc_gbuf_printf(buf, "test %d test %d test %d", 1, 2, 3);
fail_if(rc != 20);
fail_if(buf->used != 20);
fail_if(strcmp("test 1 test 2 test 3", buf->buf) != 0);
uc_gbuf_unref(buf);
}
END_TEST
START_TEST (test_gbuf_printf3)
{
int rc;
GBuf *buf = uc_new_gbuf(8);
fail_if(buf == NULL);
rc = uc_gbuf_printf(buf, "test %d test %d test %d", 1, 2, 3);
fail_if(rc != 20);
fail_if(buf->used != 20);
fail_if(strcmp("test 1 test 2 test 3", buf->buf) != 0);
rc = uc_gbuf_printf(buf, " test %d test %d", 4, 5);
fail_if(rc != 14);
fail_if(buf->used != 34);
fail_if(strcmp("test 1 test 2 test 3 test 4 test 5", buf->buf) != 0);
uc_gbuf_unref(buf);
}
END_TEST
START_TEST (test_gbuf_printf_empty_string)
{
int rc;
GBuf *buf = uc_new_gbuf(1);
fail_if(buf == NULL);
rc = uc_gbuf_printf(buf, "");
fail_if(rc != 0);
fail_if(buf->used != 0);
uc_gbuf_unref(buf);
}
END_TEST
Suite *buffer_suite(void)
{
Suite *s = suite_create("buffer");
TCase *tc = tcase_create("buffer (GBuf) tests");
tcase_add_test(tc, test_new_gbuf);
tcase_add_test(tc, test_gbuf_append1);
tcase_add_test(tc, test_gbuf_grow);
tcase_add_test(tc, test_gbuf_printf1);
tcase_add_test(tc, test_gbuf_printf2);
tcase_add_test(tc, test_gbuf_printf3);
tcase_add_test(tc, test_gbuf_printf_empty_string);
suite_add_tcase(s, tc);
return s;
}