tvarints.nim 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. discard """
  2. cmd: "nim c -r --styleCheck:hint --panics:on $options $file"
  3. matrix: "-d:danger; -d:release"
  4. targets: "c cpp"
  5. nimout: ""
  6. action: "run"
  7. exitcode: 0
  8. timeout: 60.0
  9. """
  10. import std/varints
  11. block:
  12. var dest: array[50, byte]
  13. var got: uint64
  14. for test in [0xFFFF_FFFF_FFFFF_FFFFu64, 77u64, 0u64, 10_000_000u64, uint64(high(int64)),
  15. uint64(high(int32)), uint64(high(int32)), uint64(high(int64))]:
  16. let wrLen = writeVu64(dest, test)
  17. let rdLen = readVu64(dest, got)
  18. doAssert wrLen == rdLen
  19. doAssert got == test
  20. for test in 0u64..300u64:
  21. let wrLen = writeVu64(dest, test)
  22. let rdLen = readVu64(dest, got)
  23. doAssert wrLen == rdLen
  24. doAssert got == test
  25. # check this also works for floats:
  26. for test in [0.0, 0.1, 2.0, +Inf, NegInf]:
  27. let t = cast[uint64](test)
  28. let wrLenB = writeVu64(dest, t)
  29. let rdLenB = readVu64(dest, got)
  30. doAssert wrLenB == rdLenB
  31. doAssert cast[float64](got) == test
  32. block:
  33. var hugeIntArray: array[50, byte]
  34. var readedInt: uint64
  35. doAssert writeVu64(hugeIntArray, 0.uint64) == readVu64(hugeIntArray, readedInt)
  36. doAssert readedInt == 0.uint64
  37. doAssert writeVu64(hugeIntArray, uint64.high) == readVu64(hugeIntArray, readedInt)
  38. doAssert readedInt == uint64.high
  39. doAssert writeVu64(hugeIntArray, uint64(int64.high)) == readVu64(hugeIntArray, readedInt)
  40. doAssert readedInt == uint64(int64.high)
  41. doAssert writeVu64(hugeIntArray, uint64(int32.high)) == readVu64(hugeIntArray, readedInt)
  42. doAssert readedInt == uint64(int32.high)
  43. doAssert writeVu64(hugeIntArray, uint64(int16.high)) == readVu64(hugeIntArray, readedInt)
  44. doAssert readedInt == uint64(int16.high)
  45. doAssert writeVu64(hugeIntArray, uint64(int8.high)) == readVu64(hugeIntArray, readedInt)
  46. doAssert readedInt == uint64(int8.high)
  47. doAssert writeVu64(hugeIntArray, cast[uint64](0.0)) == readVu64(hugeIntArray, readedInt)
  48. doAssert readedInt == cast[uint64](0.0)
  49. doAssert writeVu64(hugeIntArray, cast[uint64](-0.0)) == readVu64(hugeIntArray, readedInt)
  50. doAssert readedInt == cast[uint64](-0.0)
  51. doAssert writeVu64(hugeIntArray, cast[uint64](0.1)) == readVu64(hugeIntArray, readedInt)
  52. doAssert readedInt == cast[uint64](0.1)
  53. doAssert writeVu64(hugeIntArray, cast[uint64](0.9555555555555555555555501)) == readVu64(hugeIntArray, readedInt)
  54. doAssert readedInt == cast[uint64](0.9555555555555555555555501)
  55. doAssert writeVu64(hugeIntArray, cast[uint64](+Inf)) == readVu64(hugeIntArray, readedInt)
  56. doAssert readedInt == cast[uint64](+Inf)
  57. doAssert writeVu64(hugeIntArray, cast[uint64](NegInf)) == readVu64(hugeIntArray, readedInt)
  58. doAssert readedInt == cast[uint64](NegInf)
  59. doAssert writeVu64(hugeIntArray, cast[uint64](Nan)) == readVu64(hugeIntArray, readedInt)
  60. doAssert readedInt == cast[uint64](Nan)
  61. doAssert writeVu64(hugeIntArray, cast[uint64](3.1415926535897932384626433)) == readVu64(hugeIntArray, readedInt)
  62. doAssert readedInt == cast[uint64](3.1415926535897932384626433)
  63. doAssert writeVu64(hugeIntArray, cast[uint64](2.71828182845904523536028747)) == readVu64(hugeIntArray, readedInt)
  64. doAssert readedInt == cast[uint64](2.71828182845904523536028747)
  65. block:
  66. doAssert encodeZigzag(decodeZigzag(0.uint64)) == 0.uint64
  67. doAssert encodeZigzag(decodeZigzag(uint64(uint32.high))) == uint64(uint32.high)
  68. doAssert encodeZigzag(decodeZigzag(uint64(int32.high))) == uint64(int32.high)
  69. doAssert encodeZigzag(decodeZigzag(uint64(int16.high))) == uint64(int16.high)
  70. doAssert encodeZigzag(decodeZigzag(uint64(int8.high))) == uint64(int8.high)
  71. doAssert encodeZigzag(decodeZigzag(cast[uint64](0.0))) == cast[uint64](0.0)
  72. doAssert encodeZigzag(decodeZigzag(cast[uint64](0.1))) == cast[uint64](0.1)
  73. doAssert encodeZigzag(decodeZigzag(cast[uint64](0.9555555555555555555555501))) == cast[uint64](0.9555555555555555555555501)
  74. doAssert encodeZigzag(decodeZigzag(cast[uint64](+Inf))) == cast[uint64](+Inf)
  75. doAssert encodeZigzag(decodeZigzag(cast[uint64](3.1415926535897932384626433))) == cast[uint64](3.1415926535897932384626433)
  76. doAssert encodeZigzag(decodeZigzag(cast[uint64](2.71828182845904523536028747))) == cast[uint64](2.71828182845904523536028747)