Add uc_mbuf_extend_headroom function, for growing an mbuf
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <check.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ucore/mbuf.h>
|
||||
|
||||
|
||||
@@ -240,6 +241,58 @@ START_TEST (test_mbuf_static)
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_mbuf_extend_tailroom)
|
||||
struct MBuf *mbuf;
|
||||
uint8_t *data;
|
||||
void *filler;
|
||||
int rc;
|
||||
|
||||
mbuf = uc_mbuf_new_hr(5,2);
|
||||
fail_if(mbuf == NULL);
|
||||
|
||||
data = uc_mbuf_put(mbuf, 5);
|
||||
fail_if(data == NULL);
|
||||
memcpy((char*)data, "hello", 5);
|
||||
mbuf->p1 = mbuf->tail;
|
||||
|
||||
data = uc_mbuf_put(mbuf, 6);
|
||||
fail_if(data != NULL);
|
||||
|
||||
filler = malloc(5); //just a hope, so realloc also moves..
|
||||
rc = uc_mbuf_extend_tailroom(mbuf, 1024);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
ck_assert_int_eq(uc_mbuf_tailroom(mbuf), 1024);
|
||||
ck_assert_int_eq(uc_mbuf_headroom(mbuf), 2);
|
||||
ck_assert_int_eq(uc_mbuf_len(mbuf), 5);
|
||||
|
||||
//should work now
|
||||
data = uc_mbuf_put(mbuf, 6);
|
||||
fail_if(data == NULL);
|
||||
strcpy((char*)data, "world");
|
||||
ck_assert_str_eq((const char*)mbuf->head, "helloworld");
|
||||
ck_assert_str_eq((const char*)mbuf->p1, "world");
|
||||
|
||||
uc_mbuf_free(mbuf);
|
||||
free(filler);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_mbuf_extend_tailroom_wrong_type)
|
||||
struct MBuf *mbuf;
|
||||
uint8_t *data;
|
||||
int rc;
|
||||
|
||||
mbuf = uc_mbuf_new_inline(10);
|
||||
fail_if(mbuf == NULL);
|
||||
|
||||
rc = uc_mbuf_extend_tailroom(mbuf, 1024);
|
||||
ck_assert_int_ne(rc, 0);
|
||||
|
||||
fail_if(data == NULL);
|
||||
|
||||
uc_mbuf_free(mbuf);
|
||||
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *mbuf_suite(void)
|
||||
@@ -258,6 +311,8 @@ Suite *mbuf_suite(void)
|
||||
tcase_add_test(tc, test_mbuf_copy);
|
||||
tcase_add_test(tc, test_mbuf_zero);
|
||||
tcase_add_test(tc, test_mbuf_static);
|
||||
tcase_add_test(tc, test_mbuf_extend_tailroom);
|
||||
tcase_add_test(tc, test_mbuf_extend_tailroom_wrong_type);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user