2019-08-01 14:21:14 +00:00
|
|
|
import
|
|
|
|
unittest, random,
|
|
|
|
../stew/[varints, byteutils]
|
|
|
|
|
|
|
|
const edgeValues = {
|
|
|
|
0'u64 : "00",
|
|
|
|
(1'u64 shl 7) - 1'u64 : "7f",
|
|
|
|
(1'u64 shl 7) : "8001",
|
|
|
|
(1'u64 shl 14) - 1'u64 : "ff7f",
|
|
|
|
(1'u64 shl 14) : "808001",
|
|
|
|
(1'u64 shl 21) - 1'u64 : "ffff7f",
|
|
|
|
(1'u64 shl 21) : "80808001",
|
|
|
|
(1'u64 shl 28) - 1'u64 : "ffffff7f",
|
|
|
|
(1'u64 shl 28) : "8080808001",
|
|
|
|
(1'u64 shl 35) - 1'u64 : "ffffffff7f",
|
|
|
|
(1'u64 shl 35) : "808080808001",
|
|
|
|
(1'u64 shl 42) - 1'u64 : "ffffffffff7f",
|
|
|
|
(1'u64 shl 42) : "80808080808001",
|
|
|
|
(1'u64 shl 49) - 1'u64 : "ffffffffffff7f",
|
|
|
|
(1'u64 shl 49) : "8080808080808001",
|
|
|
|
(1'u64 shl 56) - 1'u64 : "ffffffffffffff7f",
|
|
|
|
(1'u64 shl 56) : "808080808080808001",
|
|
|
|
(1'u64 shl 63) - 1'u64 : "ffffffffffffffff7f",
|
|
|
|
uint64(1'u64 shl 63) : "80808080808080808001",
|
|
|
|
0xFFFF_FFFF_FFFF_FFFF'u64 : "ffffffffffffffffff01"
|
|
|
|
}
|
|
|
|
|
|
|
|
suite "varints":
|
|
|
|
template roundtipTest(val) =
|
2019-09-09 02:33:03 +00:00
|
|
|
var s {.inject.}: VarintBuffer
|
2020-05-06 12:46:32 +00:00
|
|
|
s.writeVarint val
|
2019-08-01 14:21:14 +00:00
|
|
|
|
2020-05-12 19:59:46 +00:00
|
|
|
var roundtripVal: type(val)
|
2019-08-01 14:21:14 +00:00
|
|
|
let bytesRead = readVarint(s.bytes, roundtripVal)
|
|
|
|
|
|
|
|
check:
|
|
|
|
val == roundtripVal
|
2019-09-09 02:33:03 +00:00
|
|
|
bytesRead == s.totalBytesWritten
|
2019-08-01 14:21:14 +00:00
|
|
|
bytesRead == vsizeof(val)
|
|
|
|
|
|
|
|
test "[ProtoBuf] Success edge cases test":
|
|
|
|
for pair in edgeValues:
|
|
|
|
let (val, hex) = pair
|
|
|
|
roundtipTest val
|
|
|
|
check:
|
2019-09-09 02:33:03 +00:00
|
|
|
s.totalBytesWritten == hex.len div 2
|
|
|
|
toHex(s.writtenBytes) == hex
|
|
|
|
toHex(val.varintBytes) == hex
|
2019-08-01 14:21:14 +00:00
|
|
|
|
2020-05-12 19:59:46 +00:00
|
|
|
test "[ProtoBuf] random 64-bit values":
|
2019-09-09 02:33:03 +00:00
|
|
|
for i in 0..10000:
|
2019-12-10 14:50:02 +00:00
|
|
|
# TODO nim 1.0 random casts limits to int, so anything bigger will crash
|
|
|
|
# * sigh *
|
2020-05-12 19:59:46 +00:00
|
|
|
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
|
2019-08-01 14:21:14 +00:00
|
|
|
|
|
|
|
# TODO Migrate the rest of the LibP2P test cases
|