Files
Nim/tests/stdlib/tvarints.nim
Timothee Cour a36efb59b5 fix #16256: nimout: <empty> should give error (vacuously true); improve a few tests (#18089)
* fix #16256: nimout: <empty> should give error (vacuously true); improve some tests

* renamed:    tests/stdlib/t9710.nim -> tests/misc/t9710.nim

* improve tests

* fix non-DRY tests

* improve $nim_prs_D/tests/stdlib/t9091.nim

* renamed:    tests/stdlib/t9091.nim -> tests/misc/t9091.nim

* fixup

* address comment: doAssert => result.parseErrors
2021-05-31 13:39:19 +02:00

69 lines
1.6 KiB
Nim

import std/varints
# xxx doesn't work with js: tvarints.nim(18, 14) `wrLen == rdLen` [AssertionDefect]
block:
var dest: array[50, byte]
var got: uint64
for test in [0xFFFF_FFFF_FFFFF_FFFFu64, 77u64, 0u64, 10_000_000u64, uint64(high(int64)),
uint64(high(int32)), uint64(high(int32)), uint64(high(int64))]:
let wrLen = writeVu64(dest, test)
let rdLen = readVu64(dest, got)
doAssert wrLen == rdLen
doAssert got == test
for test in 0u64..300u64:
let wrLen = writeVu64(dest, test)
let rdLen = readVu64(dest, got)
doAssert wrLen == rdLen
doAssert got == test
# check this also works for floats:
for test in [0.0, 0.1, 2.0, +Inf, NegInf]:
let t = cast[uint64](test)
let wrLenB = writeVu64(dest, t)
let rdLenB = readVu64(dest, got)
doAssert wrLenB == rdLenB
doAssert cast[float64](got) == test
block:
var hugeIntArray: array[50, byte]
var readedInt: uint64
template chk(a) =
let b = cast[uint64](a)
doAssert writeVu64(hugeIntArray, b) == readVu64(hugeIntArray, readedInt)
doAssert readedInt == b
chk 0
chk uint64.high
chk int64.high
chk int32.high
chk int16.high
chk int16.high
chk int8.high
chk 0.0
chk -0.0
chk 0.1
chk Inf
chk NegInf
chk Nan
chk 3.1415926535897932384626433
block:
template chk(a) =
let b = cast[uint64](a)
doAssert encodeZigzag(decodeZigzag(b)) == b
chk 0
chk uint32.high
chk int32.high
chk int16.high
chk int8.high
chk 0.0
chk 0.1
chk 0.9555555555555555555555501
chk Inf
chk 3.1415926535897932384626433
chk 2.71828182845904523536028747