diff options
author | Juan Carlos <juancarlospaco@gmail.com> | 2020-04-11 17:16:26 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-11 22:16:26 +0200 |
commit | 7238b968f3820331eb342b3e14283f03c71df035 (patch) | |
tree | 0ef2d4a011435eaa292762b668c7719d1bea8bf9 | |
parent | 5f6962337de94b524afbb4f41b79eae362af67c8 (diff) | |
download | Nim-7238b968f3820331eb342b3e14283f03c71df035.tar.gz |
Make unused code into actual test, replace echo with doassert (#13952)
-rw-r--r-- | lib/std/varints.nim | 33 | ||||
-rw-r--r-- | tests/stdlib/tvarints.nim | 84 |
2 files changed, 84 insertions, 33 deletions
diff --git a/lib/std/varints.nim b/lib/std/varints.nim index fe06b478a..0d18b9069 100644 --- a/lib/std/varints.nim +++ b/lib/std/varints.nim @@ -119,36 +119,3 @@ proc encodeZigzag*(x: int64): uint64 {.inline.} = proc decodeZigzag*(x: uint64): int64 {.inline.} = let casted = cast[int64](x) result = (`shr`(casted, 1)) xor (-(casted and 1)) - -when isMainModule: - #import random - - 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(low(int32)),uint64(low(int64))]: - let wrLen = writeVu64(dest, test) - let rdLen = readVu64(dest, got) - assert wrLen == rdLen - echo(if got == test: "YES" else: "NO") - echo "number is ", got - - if encodeZigzag(decodeZigzag(test)) != test: - echo "Failure for ", test, " ", encodeZigzag(decodeZigzag(test)), " ", decodeZigzag(test) - - for test in 0u64..300u64: - let wrLen = writeVu64(dest, test) - let rdLen = readVu64(dest, got) - assert wrLen == rdLen - if got != test: - echo "BUG! expected: ", test, " got: ", got, " z0: ", dest[0] - - # check this also works for floats: - for test in [0.0, 0.1, 2.0, +Inf, Nan, NegInf]: - let t = cast[uint64](test) - let wrLenB = writeVu64(dest, t) - let rdLenB = readVu64(dest, got) - assert wrLenB == rdLenB - echo rdLenB - echo(if cast[float64](got) == test: "YES" else: "NO") diff --git a/tests/stdlib/tvarints.nim b/tests/stdlib/tvarints.nim new file mode 100644 index 000000000..dcdb756ce --- /dev/null +++ b/tests/stdlib/tvarints.nim @@ -0,0 +1,84 @@ +discard """ + cmd: "nim c -r --styleCheck:hint --panics:on $options $file" + matrix: "-d:danger; -d:release" + targets: "c cpp" + nimout: "" + action: "run" + exitcode: 0 + timeout: 60.0 +""" + +import std/varints + + +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 + doAssert writeVu64(hugeIntArray, 0.uint64) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == 0.uint64 + doAssert writeVu64(hugeIntArray, uint64.high) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == uint64.high + doAssert writeVu64(hugeIntArray, uint64(int64.high)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == uint64(int64.high) + doAssert writeVu64(hugeIntArray, uint64(int32.high)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == uint64(int32.high) + doAssert writeVu64(hugeIntArray, uint64(int16.high)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == uint64(int16.high) + doAssert writeVu64(hugeIntArray, uint64(int8.high)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == uint64(int8.high) + doAssert writeVu64(hugeIntArray, cast[uint64](0.0)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](0.0) + doAssert writeVu64(hugeIntArray, cast[uint64](-0.0)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](-0.0) + doAssert writeVu64(hugeIntArray, cast[uint64](0.1)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](0.1) + doAssert writeVu64(hugeIntArray, cast[uint64](0.9555555555555555555555501)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](0.9555555555555555555555501) + doAssert writeVu64(hugeIntArray, cast[uint64](+Inf)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](+Inf) + doAssert writeVu64(hugeIntArray, cast[uint64](NegInf)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](NegInf) + doAssert writeVu64(hugeIntArray, cast[uint64](Nan)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](Nan) + doAssert writeVu64(hugeIntArray, cast[uint64](3.1415926535897932384626433)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](3.1415926535897932384626433) + doAssert writeVu64(hugeIntArray, cast[uint64](2.71828182845904523536028747)) == readVu64(hugeIntArray, readedInt) + doAssert readedInt == cast[uint64](2.71828182845904523536028747) + +block: + doAssert encodeZigzag(decodeZigzag(0.uint64)) == 0.uint64 + doAssert encodeZigzag(decodeZigzag(uint64(uint32.high))) == uint64(uint32.high) + doAssert encodeZigzag(decodeZigzag(uint64(int32.high))) == uint64(int32.high) + doAssert encodeZigzag(decodeZigzag(uint64(int16.high))) == uint64(int16.high) + doAssert encodeZigzag(decodeZigzag(uint64(int8.high))) == uint64(int8.high) + doAssert encodeZigzag(decodeZigzag(cast[uint64](0.0))) == cast[uint64](0.0) + doAssert encodeZigzag(decodeZigzag(cast[uint64](0.1))) == cast[uint64](0.1) + doAssert encodeZigzag(decodeZigzag(cast[uint64](0.9555555555555555555555501))) == cast[uint64](0.9555555555555555555555501) + doAssert encodeZigzag(decodeZigzag(cast[uint64](+Inf))) == cast[uint64](+Inf) + doAssert encodeZigzag(decodeZigzag(cast[uint64](3.1415926535897932384626433))) == cast[uint64](3.1415926535897932384626433) + doAssert encodeZigzag(decodeZigzag(cast[uint64](2.71828182845904523536028747))) == cast[uint64](2.71828182845904523536028747) |