提交 7b3a04c4 编写于 作者: H Hongze Cheng

more

上级 8aaf22ae
......@@ -100,7 +100,7 @@ static int tEncodeI64v(SCoder* pEncoder, int64_t val);
static int tEncodeFloat(SCoder* pEncoder, float val);
static int tEncodeDouble(SCoder* pEncoder, double val);
static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len);
static int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len);
static int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len);
static int tEncodeCStr(SCoder* pEncoder, const char* val);
/* ------------------------ DECODE ------------------------ */
......@@ -141,7 +141,7 @@ static int tDecodeCStrTo(SCoder* pDecoder, char* val);
return 0;
#define TD_ENCODE_VARIANT_MACRO(CODER, VAL) \
while ((VAL) >= ENCODE_LIMIT) { \
while ((VAL) >= ENCODE_LIMIT) { \
if ((CODER)->data) { \
if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \
TD_CODER_CURRENT(CODER)[0] = ((VAL) | ENCODE_LIMIT) & 0xff; \
......@@ -261,12 +261,12 @@ static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_
return 0;
}
static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len) {
static FORCE_INLINE int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len) {
return tEncodeBinary(pEncoder, (void*)val, len + 1);
}
static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) {
return tEncodeCstrWithLen(pEncoder, val, (uint64_t)strlen(val));
return tEncodeCStrWithLen(pEncoder, val, (uint64_t)strlen(val));
}
/* ------------------------ FOR DECODER ------------------------ */
......
......@@ -166,4 +166,36 @@ TEST(td_encode_test, encode_decode_variant_len_integer) {
simple_encode_decode_func<uint32_t>(true);
simple_encode_decode_func<int64_t>(true);
simple_encode_decode_func<uint64_t>(true);
}
TEST(td_encode_test, encode_decode_cstr) {
uint8_t * buf = new uint8_t[1024 * 1024];
char * cstr = new char[1024 * 1024];
const char *dcstr;
SCoder encoder;
SCoder decoder;
for (size_t i = 0; i < 1024 * 2 - 1; i++) {
memset(cstr, 'a', i);
cstr[i] = '\0';
for (td_endian_t endian : endian_arr) {
// Encode
tCoderInit(&encoder, endian, buf, 1024 * 1024, TD_ENCODER);
GTEST_ASSERT_EQ(tEncodeCStr(&encoder, cstr), 0);
tCoderClear(&encoder);
// Decode
tCoderInit(&decoder, endian, buf, 1024 * 1024, TD_DECODER);
GTEST_ASSERT_EQ(tDecodeCStr(&decoder, &dcstr), 0);
GTEST_ASSERT_EQ(memcmp(dcstr, cstr, i + 1), 0);
tCoderClear(&decoder);
}
}
delete buf;
delete cstr;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册