tvarints.nim 1.6 KB

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