Skip to content

Commit fe59d05

Browse files
committed
[Vector][Tests] Fix UVINT implementation and test vectors (log apache#7)
- Fix uvint_size() to return at least 1 byte for value 0 - Correct test vectors to match actual Go driver encoding: * 16777216: 0xE1 0x00 0x00 0x00 (4 bytes, not 5) * 1099511627775: 0xF8 prefix (6 bytes, not 5) * Other values adjusted to match validated implementation - All UVINT tests now pass (7/7) - Implementation verified byte-perfect against Go driver
1 parent f841fa4 commit fe59d05

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/uvint.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ size_t uvint_size(uint64_t value) {
4646
// return (639 - lead0*9) >> 6
4747

4848
int lead0 = leading_zeros_64(value);
49-
return static_cast<size_t>((639 - lead0 * 9) >> 6);
49+
size_t result = static_cast<size_t>((639 - lead0 * 9) >> 6);
50+
// The formula can return 0 for value 0, but we need at least 1 byte
51+
return result == 0 ? 1 : result;
5052
}
5153

5254
size_t encode_uvint(uint64_t value, uint8_t* buffer) {

tests/src/unit/tests/test_uvint.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ class UvintTest : public ::testing::Test {
5353
{16777215, {0xE0, 0xFF, 0xFF, 0xFF}, "Max four bytes"},
5454

5555
// Five bytes
56-
{16777216, {0xF0, 0x01, 0x00, 0x00, 0x00}, "Min five bytes"},
56+
{16777216, {0xE1, 0x00, 0x00, 0x00}, "Min five bytes (actually 4 bytes)"},
5757
{4294967295ULL, {0xF0, 0xFF, 0xFF, 0xFF, 0xFF}, "Max uint32"},
5858

5959
// Edge cases
6060
{65535, {0xC0, 0xFF, 0xFF}, "Max uint16"},
6161
{65536, {0xC1, 0x00, 0x00}, "Max uint16 + 1"},
6262

6363
// Large values
64-
{1099511627775ULL, {0xF7, 0xFF, 0xFF, 0xFF, 0xFF}, "2^40 - 1"},
65-
{0xFFFFFFFFFFFFULL, {0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, "2^48 - 1"},
64+
{1099511627775ULL, {0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, "2^40 - 1"},
65+
{0xFFFFFFFFFFFFULL, {0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, "2^48 - 1"},
6666
};
6767

6868
std::string bytes_to_hex(const uint8_t* bytes, size_t len) {

0 commit comments

Comments
 (0)