awtk/tests/buffer_test.cc

176 lines
4.6 KiB
C++
Raw Normal View History

#include "tkc/buffer.h"
#include "tkc/object_rbuffer.h"
#include "tkc/object_wbuffer.h"
2018-02-21 19:36:38 +08:00
#include "gtest/gtest.h"
2018-05-04 11:27:12 +08:00
#include <string>
using std::string;
2018-02-21 19:36:38 +08:00
2018-12-27 15:28:20 +08:00
TEST(Buffer, demo1) {
wbuffer_t wbuffer;
wbuffer_init_extendable(&wbuffer);
wbuffer_write_string(&wbuffer, "hello awtk");
wbuffer_deinit(&wbuffer);
}
TEST(Buffer, demo2) {
uint8_t buff[128];
wbuffer_t wbuffer;
rbuffer_t rbuffer;
const char* str = NULL;
wbuffer_init(&wbuffer, buff, sizeof(buff));
wbuffer_write_string(&wbuffer, "hello awtk");
rbuffer_init(&rbuffer, wbuffer.data, wbuffer.cursor);
rbuffer_read_string(&rbuffer, &str);
ASSERT_EQ(string(str), "hello awtk");
}
static void testWriteRead(wbuffer_t* wb) {
2018-02-21 19:36:38 +08:00
uint8_t v8;
uint16_t v16;
uint32_t v32;
rbuffer_t rbuffer;
2018-12-27 15:28:20 +08:00
rbuffer_t* rb = &rbuffer;
2018-02-21 19:36:38 +08:00
for (uint32_t i = 0; i < 10; i++) {
2018-12-27 15:28:20 +08:00
ASSERT_EQ(wbuffer_write_uint8(wb, i), RET_OK);
ASSERT_EQ(wb->cursor, 11 * i + 1);
rbuffer_init(rb, wb->data, wb->cursor);
rb->cursor = wb->cursor - 1;
ASSERT_EQ(rbuffer_read_uint8(rb, &v8), RET_OK);
2018-02-21 19:36:38 +08:00
ASSERT_EQ((uint32_t)v8, i);
2018-12-27 15:28:20 +08:00
ASSERT_EQ(wbuffer_write_uint16(wb, i), RET_OK);
ASSERT_EQ(wb->cursor, 11 * i + 3);
rbuffer_init(rb, wb->data, wb->cursor);
rb->cursor = wb->cursor - 2;
ASSERT_EQ(rbuffer_read_uint16(rb, &v16), RET_OK);
2018-02-21 19:36:38 +08:00
ASSERT_EQ((uint32_t)v16, i);
2018-12-27 15:28:20 +08:00
ASSERT_EQ(wbuffer_write_uint32(wb, i), RET_OK);
ASSERT_EQ(wb->cursor, 11 * i + 7);
rbuffer_init(rb, wb->data, wb->cursor);
rb->cursor = wb->cursor - 4;
ASSERT_EQ(rbuffer_read_uint32(rb, &v32), RET_OK);
2018-02-21 19:36:38 +08:00
ASSERT_EQ((uint32_t)v32, i);
2018-12-27 15:28:20 +08:00
ASSERT_EQ(wbuffer_write_binary(wb, &i, sizeof(i)), RET_OK);
ASSERT_EQ(wb->cursor, 11 * i + 11);
rbuffer_init(rb, wb->data, wb->cursor);
rb->cursor = wb->cursor - sizeof(i);
ASSERT_EQ(rbuffer_read_binary(rb, &v32, sizeof(v32)), RET_OK);
2018-02-21 19:36:38 +08:00
ASSERT_EQ((uint32_t)v32, i);
}
}
2018-05-04 11:27:12 +08:00
2018-12-27 15:28:20 +08:00
TEST(Buffer, basic) {
uint8_t data[128];
wbuffer_t wbuffer;
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
testWriteRead(&wbuffer);
}
TEST(Buffer, extendable) {
wbuffer_t wbuffer;
ASSERT_EQ(wbuffer_init_extendable(&wbuffer), &wbuffer);
testWriteRead(&wbuffer);
wbuffer_deinit(&wbuffer);
}
TEST(Buffer, WbufferSkipExtendable) {
wbuffer_t wbuffer;
ASSERT_EQ(wbuffer_init_extendable(&wbuffer), &wbuffer);
ASSERT_EQ(wbuffer_skip(&wbuffer, 10), RET_OK);
ASSERT_EQ(wbuffer.cursor, 10);
ASSERT_EQ(wbuffer_skip(&wbuffer, -10), RET_OK);
ASSERT_EQ(wbuffer.cursor, 0);
ASSERT_EQ(wbuffer_skip(&wbuffer, -10), RET_BAD_PARAMS);
ASSERT_EQ(wbuffer.cursor, 0);
wbuffer_deinit(&wbuffer);
}
2018-05-04 11:27:12 +08:00
TEST(Buffer, WbufferSkip) {
uint8_t data[128];
wbuffer_t wbuffer;
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
ASSERT_EQ(wbuffer_skip(&wbuffer, 10), RET_OK);
ASSERT_EQ(wbuffer.cursor, 10);
ASSERT_EQ(wbuffer_skip(&wbuffer, -10), RET_OK);
ASSERT_EQ(wbuffer.cursor, 0);
ASSERT_EQ(wbuffer_skip(&wbuffer, -10), RET_BAD_PARAMS);
ASSERT_EQ(wbuffer.cursor, 0);
}
TEST(Buffer, RbufferSkip) {
uint8_t data[128];
rbuffer_t rbuffer;
ASSERT_EQ(rbuffer_init(&rbuffer, data, sizeof(data)), &rbuffer);
ASSERT_EQ(rbuffer_skip(&rbuffer, 10), RET_OK);
ASSERT_EQ(rbuffer.cursor, 10);
ASSERT_EQ(rbuffer_skip(&rbuffer, -10), RET_OK);
ASSERT_EQ(rbuffer.cursor, 0);
ASSERT_EQ(rbuffer_skip(&rbuffer, -10), RET_BAD_PARAMS);
ASSERT_EQ(rbuffer.cursor, 0);
}
TEST(Buffer, string) {
const char* str = NULL;
const char* cstr = "hello world";
uint8_t data[128];
wbuffer_t wbuffer;
rbuffer_t rbuffer;
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
ASSERT_EQ(rbuffer_init(&rbuffer, data, sizeof(data)), &rbuffer);
ASSERT_EQ(wbuffer_write_string(&wbuffer, cstr), RET_OK);
ASSERT_EQ(rbuffer_read_string(&rbuffer, &str), RET_OK);
ASSERT_EQ(string(str), string(cstr));
}
TEST(Buffer, obj) {
const char* str = NULL;
object_t* wobj = object_wbuffer_create_extendable();
wbuffer_t* wbuffer = OBJECT_WBUFFER(wobj)->wbuffer;
wbuffer_write_string(wbuffer, "hello");
wbuffer_write_string(wbuffer, " world");
object_t* robj = object_rbuffer_create(wbuffer->data, wbuffer->cursor);
rbuffer_t* rbuffer = OBJECT_RBUFFER(robj)->rbuffer;
ASSERT_EQ(object_get_prop_int(wobj, "cursor", 0), 13);
ASSERT_EQ(object_get_prop_int(wobj, "capacity", 0), 14);
ASSERT_EQ(object_get_prop_pointer(wobj, "data"), (void*)(wbuffer->data));
rbuffer_read_string(rbuffer, &str);
ASSERT_STREQ(str, "hello");
ASSERT_EQ(object_get_prop_int(robj, "cursor", 0), 6);
ASSERT_EQ(object_get_prop_int(robj, "capacity", 0), 13);
ASSERT_EQ(object_get_prop_pointer(robj, "data"), (void*)(rbuffer->data));
OBJECT_UNREF(wobj);
OBJECT_UNREF(robj);
}