diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 042026d0a4dee7fda2eb6247adefe3b0d66a6f28..7085e71567bfb976ed12c42132e5c98a26e5aa16 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -84,15 +84,18 @@ static inline int tdbPutVarInt(u8 *p, int v) { static inline int tdbGetVarInt(const u8 *p, int *v) { int n = 0; int tv = 0; + int t; for (;;) { if (p[n] <= 0x7f) { - tv = (tv << 7) | p[n]; + t = p[n]; + tv |= (t << (7 * n)); n++; break; } - tv = (tv << 7) | (p[n] & 0x7f); + t = p[n] & 0x7f; + tv |= (t << (7 * n)); n++; } diff --git a/source/libs/tdb/test/CMakeLists.txt b/source/libs/tdb/test/CMakeLists.txt index 2d77c1f4e9124bd07d8ecb3e27bf9db9103f33e7..b2c8aaf9bc9a8fa7cff472623d894c42340f0a0a 100644 --- a/source/libs/tdb/test/CMakeLists.txt +++ b/source/libs/tdb/test/CMakeLists.txt @@ -1,3 +1,7 @@ # tdbTest add_executable(tdbTest "tdbTest.cpp") -target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file +target_link_libraries(tdbTest tdb gtest gtest_main) + +# tdbUtilTest +add_executable(tdbUtilTest "tdbUtilTest.cpp") +target_link_libraries(tdbUtilTest tdb gtest gtest_main) \ No newline at end of file diff --git a/source/libs/tdb/test/tdbUtilTest.cpp b/source/libs/tdb/test/tdbUtilTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fd45daa3e59780927616b2155310cf339d527ca --- /dev/null +++ b/source/libs/tdb/test/tdbUtilTest.cpp @@ -0,0 +1,20 @@ +#include + +#include "tdbInt.h" + +#include + +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); +} \ No newline at end of file