awtk/tests/ubjson_writer_test.cc

305 lines
8.1 KiB
C++
Raw Normal View History

2019-09-14 18:16:47 +08:00
#include "gtest/gtest.h"
#include "tkc/buffer.h"
#include "ubjson/ubjson_writer.h"
TEST(UBJsonWriter, null) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_null(&ub), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_NULL);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 1u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, noop) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_noop(&ub), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_NOOP);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 1u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, true) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_true(&ub), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_TRUE);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 1u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, false) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_false(&ub), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_FALSE);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 1u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, array) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_array_begin(&ub), RET_OK);
ASSERT_EQ(ubjson_writer_write_array_end(&ub), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_ARRAY_BEGIN);
ASSERT_EQ(buff[1], UBJSON_MARKER_ARRAY_END);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 2u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, object) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_object_begin(&ub), RET_OK);
ASSERT_EQ(ubjson_writer_write_object_end(&ub), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_OBJECT_BEGIN);
ASSERT_EQ(buff[1], UBJSON_MARKER_OBJECT_END);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 2u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, int8) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_int8(&ub, 0x12), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[1], 0x12);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 2u);
2019-09-14 18:16:47 +08:00
}
2022-03-23 18:02:12 +08:00
TEST(UBJsonWriter, char) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_char(&ub, 'a'), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_CHAR);
ASSERT_EQ(buff[1], 'a');
ASSERT_EQ(wb.cursor, 2u);
}
2019-09-14 18:16:47 +08:00
TEST(UBJsonWriter, int16) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_int16(&ub, 0x1122), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT16);
ASSERT_EQ(buff[1], 0x11);
ASSERT_EQ(buff[2], 0x22);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 3u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, int32) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_int32(&ub, 0x11223344), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT32);
ASSERT_EQ(buff[1], 0x11);
ASSERT_EQ(buff[2], 0x22);
ASSERT_EQ(buff[3], 0x33);
ASSERT_EQ(buff[4], 0x44);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 5u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, int64) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_int64(&ub, 0x1122334455667788), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT64);
ASSERT_EQ(buff[1], 0x11);
ASSERT_EQ(buff[2], 0x22);
ASSERT_EQ(buff[3], 0x33);
ASSERT_EQ(buff[4], 0x44);
ASSERT_EQ(buff[5], 0x55);
ASSERT_EQ(buff[6], 0x66);
ASSERT_EQ(buff[7], 0x77);
ASSERT_EQ(buff[8], 0x88);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 9u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, float) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_float32(&ub, 1.2), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_FLOAT32);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 5u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, double) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_float64(&ub, 1.2), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_FLOAT64);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 9u);
2019-09-14 18:16:47 +08:00
}
TEST(UBJsonWriter, string) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
2019-09-15 17:57:19 +08:00
ASSERT_EQ(ubjson_writer_write_str(&ub, "a"), RET_OK);
2019-09-14 18:16:47 +08:00
ASSERT_EQ(buff[0], UBJSON_MARKER_STRING);
ASSERT_EQ(buff[1], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[2], 0x1);
ASSERT_EQ(buff[3], 'a');
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 4u);
2019-09-14 18:16:47 +08:00
}
2019-09-22 11:58:01 +08:00
TEST(UBJsonWriter, binary) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_str_len(&ub, "aasdf", 1), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_STRING);
ASSERT_EQ(buff[1], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[2], 0x1);
ASSERT_EQ(buff[3], 'a');
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 4u);
2019-09-22 11:58:01 +08:00
}
TEST(UBJsonWriter, kv_string) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_kv_str(&ub, "a", "b"), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[1], 0x1);
ASSERT_EQ(buff[2], 'a');
ASSERT_EQ(buff[3], UBJSON_MARKER_STRING);
ASSERT_EQ(buff[4], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[5], 0x1);
ASSERT_EQ(buff[6], 'b');
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 7u);
2019-09-22 11:58:01 +08:00
}
2023-06-07 09:48:55 +08:00
TEST(UBJsonWriter, kv_wstring) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_kv_wstr(&ub, "a", L"b"), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[1], 0x1);
ASSERT_EQ(buff[2], 'a');
ASSERT_EQ(buff[3], UBJSON_MARKER_WSTRING);
ASSERT_EQ(buff[4], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[5], 0x1);
ASSERT_EQ(buff[6], 'b');
ASSERT_EQ(wb.cursor, 7u);
}
2019-09-22 11:58:01 +08:00
TEST(UBJsonWriter, kv_string_len) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_kv_str_len(&ub, "a", "b1231", 1), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[1], 0x1);
ASSERT_EQ(buff[2], 'a');
ASSERT_EQ(buff[3], UBJSON_MARKER_STRING);
ASSERT_EQ(buff[4], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[5], 0x1);
ASSERT_EQ(buff[6], 'b');
2021-09-16 16:06:55 +08:00
ASSERT_EQ(wb.cursor, 7u);
2019-09-22 11:58:01 +08:00
}
2023-06-07 09:48:55 +08:00
TEST(UBJsonWriter, kv_wstring_len) {
uint8_t buff[256];
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);
ASSERT_EQ(ubjson_writer_write_kv_wstr_len(&ub, "a", L"b1231", 1), RET_OK);
ASSERT_EQ(buff[0], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[1], 0x1);
ASSERT_EQ(buff[2], 'a');
ASSERT_EQ(buff[3], UBJSON_MARKER_WSTRING);
ASSERT_EQ(buff[4], UBJSON_MARKER_INT8);
ASSERT_EQ(buff[5], 0x1);
ASSERT_EQ(buff[6], 'b');
ASSERT_EQ(wb.cursor, 7u);
}