tvarints.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import std/varints
  2. import std/assertions
  3. # xxx doesn't work with js: tvarints.nim(18, 14) `wrLen == rdLen` [AssertionDefect]
  4. block:
  5. var dest: array[50, byte]
  6. var got: uint64
  7. for test in [0xFFFF_FFFF_FFFFF_FFFFu64, 77u64, 0u64, 10_000_000u64, uint64(high(int64)),
  8. uint64(high(int32)), uint64(high(int32)), uint64(high(int64))]:
  9. let wrLen = writeVu64(dest, test)
  10. let rdLen = readVu64(dest, got)
  11. doAssert wrLen == rdLen
  12. doAssert got == test
  13. for test in 0u64..300u64:
  14. let wrLen = writeVu64(dest, test)
  15. let rdLen = readVu64(dest, got)
  16. doAssert wrLen == rdLen
  17. doAssert got == test
  18. # check this also works for floats:
  19. for test in [0.0, 0.1, 2.0, +Inf, NegInf]:
  20. let t = cast[uint64](test)
  21. let wrLenB = writeVu64(dest, t)
  22. let rdLenB = readVu64(dest, got)
  23. doAssert wrLenB == rdLenB
  24. doAssert cast[float64](got) == test
  25. block:
  26. var hugeIntArray: array[50, byte]
  27. var readedInt: uint64
  28. template chk(a) =
  29. let b = cast[uint64](a)
  30. doAssert writeVu64(hugeIntArray, b) == readVu64(hugeIntArray, readedInt)
  31. doAssert readedInt == b
  32. chk 0
  33. chk uint64.high
  34. chk int64.high
  35. chk int32.high
  36. chk int16.high
  37. chk int16.high
  38. chk int8.high
  39. chk 0.0
  40. chk -0.0
  41. chk 0.1
  42. chk Inf
  43. chk NegInf
  44. chk NaN
  45. chk 3.1415926535897932384626433
  46. block:
  47. template chk(a) =
  48. let b = cast[uint64](a)
  49. doAssert encodeZigzag(decodeZigzag(b)) == b
  50. chk 0
  51. chk uint32.high
  52. chk int32.high
  53. chk int16.high
  54. chk int8.high
  55. chk 0.0
  56. chk 0.1
  57. chk 0.9555555555555555555555501
  58. chk Inf
  59. chk 3.1415926535897932384626433
  60. chk 2.71828182845904523536028747