tunsignedconv.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. # Tests unsigned literals and implicit conversion between uints and ints
  5. var h8: uint8 = 128
  6. var h16: uint16 = 32768
  7. var h32: uint32 = 2147483648'u32
  8. var h64: uint64 = 9223372036854775808'u64
  9. var foobar: uint64 = 9223372036854775813'u64 # Issue 728
  10. var v8: uint8 = 10
  11. var v16: uint16 = 10
  12. var v32: uint32 = 10
  13. var v64: uint64 = 10
  14. # u8 + literal produces u8:
  15. var a8: uint8 = v8 + 10
  16. var a16: uint16 = v16 + 10
  17. when false:
  18. var d8 = v8 + 10'i8
  19. var d16 = v8 + 10'i16
  20. var d32 = v8 + 10'i32
  21. when false:
  22. # these don't work yet because unsigned.nim is stupid. XXX We need to fix this.
  23. var f8 = v16 + 10'u8
  24. var f16 = v16 + 10'u16
  25. var f32 = v16 + 10'u32
  26. var g8 = v32 + 10'u8
  27. var g16 = v32 + 10'u16
  28. var g32 = v32 + 10'u32
  29. var ar: array[0..20, int]
  30. var n8 = ar[v8]
  31. var n16 = ar[v16]
  32. var n32 = ar[v32]
  33. var n64 = ar[v64]
  34. block t4176:
  35. var yyy: uint8 = 0
  36. yyy = yyy - 127
  37. doAssert type(yyy) is uint8
  38. # bug #13661
  39. proc fun(): uint = cast[uint](-1)
  40. const x0 = fun()
  41. doAssert typeof(x0) is uint
  42. discard $x0
  43. # bug #13671
  44. const x1 = cast[uint](-1)
  45. discard $(x1,)
  46. # bug #13698
  47. let n2: csize_t = 1
  48. doAssert $n2.int32 == "1"
  49. # bug #14616
  50. let limit = 1'u64
  51. let rangeVar = 0'u64 ..< limit
  52. when not defined(gcRefc):
  53. doAssert repr(rangeVar) == """0 .. 0""", repr(rangeVar)
  54. # bug #15210
  55. let a3 = not 0'u64
  56. var success = false
  57. try:
  58. discard a3.int64
  59. except RangeDefect:
  60. success = true
  61. doAssert success, "conversion should fail at runtime"
  62. template main() =
  63. # xxx move all tests under here so it gets tested in CT and RT
  64. block: # bug #17572
  65. type T = distinct uint64
  66. func f(x: uint64): auto =
  67. let a = T(x)
  68. (x, a.uint64)
  69. const x = 1'u64 shl 63 or 7
  70. const b = T(x)
  71. doAssert b.uint64 == 9223372036854775815'u64
  72. doAssert $b.uint64 == "9223372036854775815"
  73. doAssert f(x) == (9223372036854775815'u64, 9223372036854775815'u64)
  74. static: main()
  75. main()
  76. block:
  77. let a = uint64.high
  78. let b = uint32.high
  79. doAssert a.uint64 == a
  80. doAssert a.uint32 == uint32.high
  81. doAssert a.uint16 == uint16.high
  82. doAssert a.uint8 == uint8.high
  83. doAssert b.uint64 == b
  84. doAssert b.uint32 == b
  85. doAssert b.uint16 == uint16.high
  86. doAssert b.uint8 == uint8.high