fix TDB encode and decode bug

This commit is contained in:
Hongze Cheng 2022-04-06 10:21:36 +00:00
parent e707047d6c
commit f136211bad
3 changed files with 30 additions and 3 deletions

View File

@ -84,15 +84,18 @@ static inline int tdbPutVarInt(u8 *p, int v) {
static inline int tdbGetVarInt(const u8 *p, int *v) { static inline int tdbGetVarInt(const u8 *p, int *v) {
int n = 0; int n = 0;
int tv = 0; int tv = 0;
int t;
for (;;) { for (;;) {
if (p[n] <= 0x7f) { if (p[n] <= 0x7f) {
tv = (tv << 7) | p[n]; t = p[n];
tv |= (t << (7 * n));
n++; n++;
break; break;
} }
tv = (tv << 7) | (p[n] & 0x7f); t = p[n] & 0x7f;
tv |= (t << (7 * n));
n++; n++;
} }

View File

@ -1,3 +1,7 @@
# tdbTest # tdbTest
add_executable(tdbTest "tdbTest.cpp") add_executable(tdbTest "tdbTest.cpp")
target_link_libraries(tdbTest tdb gtest gtest_main) target_link_libraries(tdbTest tdb gtest gtest_main)
# tdbUtilTest
add_executable(tdbUtilTest "tdbUtilTest.cpp")
target_link_libraries(tdbUtilTest tdb gtest gtest_main)

View File

@ -0,0 +1,20 @@
#include <gtest/gtest.h>
#include "tdbInt.h"
#include <string>
TEST(tdb_util_test, simple_test) {
int vEncode = 5000;
int vDecode;
int nEncode;
int nDecode;
u8 buffer[128];
nEncode = tdbPutVarInt(buffer, vEncode);
nDecode = tdbGetVarInt(buffer, &vDecode);
GTEST_ASSERT_EQ(nEncode, nDecode);
GTEST_ASSERT_EQ(vEncode, vDecode);
}