lib/varint: Fix fromVar64 function

The problem was that I let the compiler assume data type widths.
`1 << 63` is a 32 bit integer on x86_64-pc-linux-gnu.
windows
BodgeMaster 2022-11-26 14:47:15 +01:00
parent fe7c763d06
commit 44716a55bb
2 changed files with 28 additions and 21 deletions

View File

@ -34,7 +34,7 @@ namespace VarInt {
uint64_t currentPosition = initialPosition;
uint8_t bits = 0;
while (data[currentPosition] & 0b10000000 && currentPosition < initialPosition+4) {
returnValue = returnValue + ((0b01111111 & data[currentPosition]) << bits);
returnValue = returnValue + (((int32_t) 0b01111111 & data[currentPosition]) << bits);
(*processedBytes)++;
bits += 7;
@ -48,7 +48,7 @@ namespace VarInt {
if (data[currentPosition] & 0b10000000) {
return ErrorOr<int32_t>(true, ErrorCodes::OVERFLOW);
}
returnValue = returnValue + ((0b01111111 & data[currentPosition]) << bits);
returnValue = returnValue + (((int32_t) 0b01111111 & data[currentPosition]) << bits);
(*processedBytes)++;
return ErrorOr<int32_t>(returnValue);
@ -65,7 +65,7 @@ namespace VarInt {
uint64_t currentPosition = initialPosition;
uint8_t bits = 0;
while (data[currentPosition] & 0b10000000 && currentPosition < initialPosition+9) {
returnValue = returnValue + ((0b01111111 & data[currentPosition]) << bits);
returnValue = returnValue + (((int64_t) 0b01111111 & data[currentPosition]) << bits);
(*processedBytes)++;
bits += 7;
@ -78,7 +78,7 @@ namespace VarInt {
if (data[currentPosition] & 0b10000000) {
return ErrorOr<int64_t>(true, ErrorCodes::OVERFLOW);
}
returnValue = returnValue + ((0b00000001 & data[currentPosition]) << bits);
returnValue = returnValue + (((int64_t) 0b01111111 & data[currentPosition]) << bits);
(*processedBytes)++;
return ErrorOr<int64_t>(returnValue);

View File

@ -39,7 +39,7 @@ int main() {
//
// 64 Bit
// 0010 0000 0001 0000 0000 0000 1010 0010 1010 1000 0010 0000 1101 0000 1001 0011 = 2310347307446489235 -> 1101 0011 1010 0001 1000 0011 1100 0001 1010 1010 1001 0100 1000 0000 1000 1000 0010 0000
// 1000 0000 0100 0000 0010 0000 0001 0000 0000 1000 0000 0100 0000 0010 0000 0001 = 9241421688590303745 -> 1000 0001 1000 0100 1001 0000 1100 0000 1000 0000 1000 0010 1000 1000 1010 0000 1000 0000 0000 0001
// 1000 0000 0100 0000 0010 0000 0001 0000 0000 1000 0000 0100 0000 0010 0000 0001 = -9205322385119247871 -> 1000 0001 1000 0100 1001 0000 1100 0000 1000 0000 1000 0010 1000 1000 1010 0000 1000 0000 0000 0001
// 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 = -1 (unsigned int64 max) -> 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0000 0001
uint8_t zeroProcessedBytes = 0;
@ -112,9 +112,7 @@ int main() {
minusOne64Data.push_back(1);
ErrorOr<int64_t> minusOne64 = VarInt::fromVar64(minusOne64Data, 0, &minusOne64ProcessedBytes);
ASSERT(!minusOne64.isError);
//FIXME: We get -9 here, WTF?
//std::cout << minusOne64.value << std::endl;
//ASSERT(minusOne64.value == -1);
ASSERT(minusOne64.value == -1);
ASSERT(minusOne64ProcessedBytes == 10);
// 0010 0000 0001 0000 0000 0000 1010 0010 1010 1000 0010 0000 1101 0000 1001 0011 = 2310347307446489235 -> 1101 0011 1010 0001 1000 0011 1100 0001 1010 1010 1001 0100 1000 0000 1000 1000 0010 0000
@ -124,7 +122,7 @@ int main() {
small64Data.push_back(0b11010011);
small64Data.push_back(0b11010011);
small64Data.push_back(0b11010011);
small64Data.push_back(0b11010011);
small64Data.push_back(0b10010011);
small64Data.push_back(0b10100001);
small64Data.push_back(0b10000011);
small64Data.push_back(0b11000001);
@ -133,23 +131,32 @@ int main() {
small64Data.push_back(0b10000000);
small64Data.push_back(0b10001000);
small64Data.push_back(0b00100000);
//ErrorOr<int64_t> small64 = VarInt::fromVar64(small64Data, 3, &small64ProcessedBytes);
//ASSERT(!small64.isError);
//std::cout << small64.value << std::endl;
//ASSERT(small64.value == 2310347307446489235);
//ASSERT(small64ProcessedBytes == 9);
ErrorOr<int64_t> small64 = VarInt::fromVar64(small64Data, 3, &small64ProcessedBytes);
ASSERT(!small64.isError);
ASSERT(small64.value == 2310347307446489235);
ASSERT(small64ProcessedBytes == 9);
// 1000 0000 0100 0000 0010 0000 0001 0000 0000 1000 0000 0100 0000 0010 0000 0001 = 9241421688590303745 -> 1000 0001 1000 0100 1001 0000 1100 0000 1000 0000 1000 0010 1000 1000 1010 0000 1000 0000 0000 0001
// 1000 0000 0100 0000 0010 0000 0001 0000 0000 1000 0000 0100 0000 0010 0000 0001 = -9205322385119247871 -> 1000 0001 1000 0100 1001 0000 1100 0000 1000 0000 1000 0010 1000 1000 1010 0000 1000 0000 0000 0001
uint8_t big64ProcessedBytes = 0;
std::vector<uint8_t> big64Data;
big64Data.push_back(0b10111101);
big64Data.push_back(0b11111011);
big64Data.push_back(0b10000001);
big64Data.push_back(0b10000100);
big64Data.push_back(0b10010000);
big64Data.push_back(0b11000000);
big64Data.push_back(0b10000000);
big64Data.push_back(0b10000010);
big64Data.push_back(0b10001000);
big64Data.push_back(0b10100000);
big64Data.push_back(0b10000000);
big64Data.push_back(0b00000001);
//ErrorOr<int64_t> big64 = VarInt::fromVar64(big64Data, 0, &big64ProcessedBytes);
//ASSERT(!big64.isError);
//ASSERT(big64.value == 2375101);
//ASSERT(big64ProcessedBytes == 4);
ErrorOr<int64_t> big64 = VarInt::fromVar64(big64Data, 0, &big64ProcessedBytes);
ASSERT(!big64.isError);
ASSERT(big64.value == -9205322385119247871);
ASSERT(big64ProcessedBytes == 10);
//TODO: Test error conditions
std::cout << "Passed fromVar64 test." << std::endl;
return 0;