This is an automated email from the ASF dual-hosted git repository.
jiangtian pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/tsfile.git
The following commit(s) were added to refs/heads/develop by this push:
new a71b8d4e fix minus encode and decode. (#511)
a71b8d4e is described below
commit a71b8d4e8c2c216db5e8df053645157a974e1c80
Author: Colin Lee <[email protected]>
AuthorDate: Thu Jun 12 11:46:42 2025 +0800
fix minus encode and decode. (#511)
---
cpp/src/common/allocator/byte_stream.h | 10 ++++++++--
cpp/test/encoding/plain_codec_test.cc | 22 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/cpp/src/common/allocator/byte_stream.h
b/cpp/src/common/allocator/byte_stream.h
index f5534d7a..78366345 100644
--- a/cpp/src/common/allocator/byte_stream.h
+++ b/cpp/src/common/allocator/byte_stream.h
@@ -1005,8 +1005,11 @@ class SerializationUtil {
FORCE_INLINE static int write_var_int(int32_t i32, ByteStream &out) {
// TODO 8byte to 4byte.
// but in IoTDB java, it has only write_var_uint(i32)
- i32 = i32 << 1;
- return do_write_var_uint((uint32_t)i32, out);
+ int ui32 = i32 << 1;
+ if (i32 < 0) {
+ ui32 = ~ui32;
+ }
+ return do_write_var_uint(static_cast<uint32_t>(ui32), out);
}
FORCE_INLINE static int read_var_int(int32_t &i32, ByteStream &in) {
int ret = common::E_OK;
@@ -1014,6 +1017,9 @@ class SerializationUtil {
if (RET_FAIL(do_read_var_uint(ui32, in))) {
} else {
i32 = (int32_t)(ui32 >> 1);
+ if ((ui32 & 1) != 0) {
+ i32 = ~i32;
+ }
}
return ret;
}
diff --git a/cpp/test/encoding/plain_codec_test.cc
b/cpp/test/encoding/plain_codec_test.cc
index 0c46d944..a51fa926 100644
--- a/cpp/test/encoding/plain_codec_test.cc
+++ b/cpp/test/encoding/plain_codec_test.cc
@@ -49,6 +49,28 @@ TEST(PlainEncoderDecoderTest, EncodeDecodeInt32) {
EXPECT_EQ(original, decoded);
}
+TEST(PlainEncoderDecoderTest, EncodeDecodeMinusInt32) {
+ PlainEncoder encoder;
+ PlainDecoder decoder;
+ common::ByteStream stream(1024, common::MOD_DEFAULT);
+ int32_t original = -12345333;
+ int32_t decoded = 0;
+ encoder.encode(original, stream);
+ decoder.read_int32(decoded, stream);
+ EXPECT_EQ(original, decoded);
+}
+
+TEST(PlainEncoderDecoderTest, EncodeDecodeMinusInt64) {
+ PlainEncoder encoder;
+ PlainDecoder decoder;
+ common::ByteStream stream(1024, common::MOD_DEFAULT);
+ int64_t original = -12345333;
+ int64_t decoded = 0;
+ encoder.encode(original, stream);
+ decoder.read_int64(decoded, stream);
+ EXPECT_EQ(original, decoded);
+}
+
TEST(PlainEncoderDecoderTest, EncodeDecodeInt64) {
PlainEncoder encoder;
PlainDecoder decoder;