tdollars.nim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. #[
  5. if https://github.com/nim-lang/Nim/pull/14043 is merged (or at least its
  6. tests/system/tostring.nim diff subset), merge
  7. tests/system/tostring.nim into this file, named after dollars.nim
  8. The goal is to increase test coverage across backends while minimizing test code
  9. duplication (which always results in weaker test coverage in practice).
  10. ]#
  11. import std/unittest
  12. template test[T](a: T, expected: string) =
  13. check $a == expected
  14. var b = a
  15. check $b == expected
  16. static:
  17. doAssert $a == expected
  18. template testType(T: typedesc) =
  19. when T is bool:
  20. test true, "true"
  21. test false, "false"
  22. elif T is char:
  23. test char, "\0"
  24. test char.high, static($T.high)
  25. else:
  26. test T.default, "0"
  27. test 1.T, "1"
  28. test T.low, static($T.low)
  29. test T.high, static($T.high)
  30. block: # `$`(SomeInteger)
  31. # direct tests
  32. check $0'u8 == "0"
  33. check $255'u8 == "255"
  34. check $(-127'i8) == "-127"
  35. # known limitation: Error: number out of range: '128'i8',
  36. # see https://github.com/timotheecour/Nim/issues/125
  37. # check $(-128'i8) == "-128"
  38. check $int8.low == "-128"
  39. check $int8(-128) == "-128"
  40. when not defined js: # pending https://github.com/nim-lang/Nim/issues/14127
  41. check $cast[int8](-128) == "-128"
  42. var a = 12345'u16
  43. check $a == "12345"
  44. check $12345678'u64 == "12345678"
  45. check $12345678'i64 == "12345678"
  46. check $(-12345678'i64) == "-12345678"
  47. # systematic tests
  48. testType uint8
  49. testType uint16
  50. testType uint32
  51. testType uint
  52. testType int8
  53. testType int16
  54. testType int32
  55. testType int
  56. testType bool
  57. when not defined(js): # requires BigInt support
  58. testType uint64
  59. testType int64
  60. testType BiggestInt
  61. block: # #14350 for JS
  62. var cstr: cstring
  63. doAssert cstr == cstring(nil)
  64. doAssert cstr == nil
  65. doAssert cstr.isNil
  66. doAssert cstr != cstring("")