Fix a compilation error when using a 32-bit varint parser

This commit is contained in:
Zahary Karadjov 2020-05-12 22:59:46 +03:00
parent c500d3dda1
commit d0f5be4971
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
3 changed files with 16 additions and 18 deletions

View File

@ -14,17 +14,9 @@ template shift*(p: pointer, delta: int): pointer {.deprecated: "use ptrops".} =
template shift*[T](p: ptr T, delta: int): ptr T {.deprecated: "use ptrops".} =
p.offset(delta)
when (NimMajor,NimMinor,NimPatch) >= (0,19,9):
template makeOpenArray*[T](p: ptr T, len: int): auto =
toOpenArray(cast[ptr UncheckedArray[T]](p), 0, len - 1)
template makeOpenArray*[T](p: ptr T, len: Natural): auto =
toOpenArray(cast[ptr UncheckedArray[T]](p), 0, len - 1)
template makeOpenArray*(p: pointer, T: type, len: int): auto =
toOpenArray(cast[ptr UncheckedArray[T]](p), 0, len - 1)
template makeOpenArray*(p: pointer, T: type, len: Natural): auto =
toOpenArray(cast[ptr UncheckedArray[T]](p), 0, len - 1)
elif (NimMajor,NimMinor,NimPatch) >= (0,19,0):
# TODO: These are fallbacks until we upgrade to 0.19.9
template makeOpenArray*(p: pointer, T: type, len: int): auto =
toOpenArray(cast[ptr array[0, T]](p)[], 0, len - 1)
template makeOpenArray*[T](p: ptr T, len: int): auto =
toOpenArray(cast[ptr array[0, T]](p)[], 0, len - 1)

View File

@ -97,7 +97,7 @@ func feedByte*(p: var VarintParser, b: byte): VarintState =
if p.shift >= maxShift:
return Overflow
p.res = p.res or (uint64(b and 0x7F'u8) shl p.shift)
p.res = p.res or (p.IntType(b and 0x7F'u8) shl p.shift)
p.shift += 7
if (b and 0x80'u8) == 0'u8:

View File

@ -30,7 +30,7 @@ suite "varints":
var s {.inject.}: VarintBuffer
s.writeVarint val
var roundtripVal: uint64
var roundtripVal: type(val)
let bytesRead = readVarint(s.bytes, roundtripVal)
check:
@ -47,12 +47,18 @@ suite "varints":
toHex(s.writtenBytes) == hex
toHex(val.varintBytes) == hex
test "[ProtoBuf] random values":
test "[ProtoBuf] random 64-bit values":
for i in 0..10000:
# TODO nim 1.0 random casts limits to int, so anything bigger will crash
# * sigh *
let
v1 = rand(0'u64 .. cast[uint64](int.high))
roundtipTest v1
let v = rand(0'u64 .. cast[uint64](int.high))
roundtipTest v
test "[ProtoBuf] random 32-bit values":
for i in 0..10000:
# TODO nim 1.0 random casts limits to int, so anything bigger will crash
# * sigh *
let v = rand(0'u32 .. cast[uint32](int.high))
roundtipTest v
# TODO Migrate the rest of the LibP2P test cases