tints.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. discard """
  2. matrix: "; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
  3. output: '''
  4. 0 0
  5. 0 0
  6. Success'''
  7. """
  8. # Test the different integer operations
  9. import std/private/jsutils
  10. var testNumber = 0
  11. template test(opr, a, b, c: untyped): untyped =
  12. # test the expression at compile and runtime
  13. block:
  14. const constExpr = opr(a, b)
  15. when constExpr != c:
  16. {.error: "Test failed " & $constExpr & " " & $c.}
  17. inc(testNumber)
  18. #Echo("Test: " & $testNumber)
  19. var aa = a
  20. var bb = b
  21. var varExpr = opr(aa, bb)
  22. assert(varExpr == c)
  23. test(`+`, 12'i8, -13'i16, -1'i16)
  24. test(`shl`, 0b11, 0b100, 0b110000)
  25. when hasWorkingInt64:
  26. test(`shl`, 0b11'i64, 0b100'i64, 0b110000'i64)
  27. when not defined(js):
  28. # mixed type shr needlessly complicates codegen with bigint
  29. # and thus is not yet supported in JS for 64 bit ints
  30. test(`shl`, 0b11'i32, 0b100'i64, 0b110000'i64)
  31. test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32)
  32. test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16)
  33. test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16)
  34. when hasWorkingInt64:
  35. test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0xffffffffffffffff'i64)
  36. test(`shr`, 0xffff'i16, 0x4'i16, 0xffff'i16)
  37. test(`shr`, 0xff'i8, 0x4'i8, 0xff'i8)
  38. when hasWorkingInt64:
  39. test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64)
  40. test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32)
  41. when hasWorkingInt64:
  42. test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64)
  43. test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16)
  44. test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8)
  45. when hasWorkingInt64:
  46. test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64)
  47. test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
  48. # bug #916
  49. proc unc(a: float): float =
  50. return a
  51. echo int(unc(0.5)), " ", int(unc(-0.5))
  52. echo int(0.5), " ", int(-0.5)
  53. block: # Casts to uint
  54. template testCast(fromValue: typed, toType: typed, expectedResult: typed) =
  55. let src = fromValue
  56. let dst = cast[toType](src)
  57. if dst != expectedResult:
  58. echo "Casting ", astToStr(fromValue), " to ", astToStr(toType), " = ", dst.int, " instead of ", astToStr(expectedResult)
  59. doAssert(dst == expectedResult)
  60. testCast(-1'i16, uint16, 0xffff'u16)
  61. testCast(0xffff'u16, int16, -1'i16)
  62. testCast(0xff'u16, uint8, 0xff'u8)
  63. testCast(0xffff'u16, uint8, 0xff'u8)
  64. testCast(-1'i16, uint32, 0xffffffff'u32)
  65. testCast(0xffffffff'u32, int32, -1)
  66. testCast(0xfffffffe'u32, int32, -2'i32)
  67. testCast(0xffffff'u32, int16, -1'i32)
  68. testCast(-5'i32, uint8, 251'u8)
  69. # issue #7174
  70. let c = 1'u
  71. let val = c > 0
  72. doAssert val
  73. block: # bug #6752
  74. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  75. let x = 711127'i64
  76. doAssert x * 86400'i64 == 61441372800'i64
  77. block: # bug #17604
  78. let a = 2147483648'u
  79. doAssert (a and a) == a
  80. doAssert (a or 0) == a
  81. block: # bitwise not
  82. let
  83. z8 = 0'u8
  84. z16 = 0'u16
  85. z32 = 0'u32
  86. z64 = 0'u64
  87. doAssert (not z8) == uint8.high
  88. doAssert (not z16) == uint16.high
  89. doAssert (not z32) == uint32.high
  90. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  91. doAssert (not z64) == uint64.high
  92. block: # shl
  93. let i8 = int8.high
  94. let i16 = int16.high
  95. let i32 = int32.high
  96. let i64 = int64.high
  97. doAssert i8 shl 1 == -2
  98. doAssert i8 shl 2 == -4
  99. doAssert i16 shl 1 == -2
  100. doAssert i16 shl 2 == -4
  101. doAssert i32 shl 1 == -2
  102. doAssert i32 shl 2 == -4
  103. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  104. doAssert i64 shl 1 == -2
  105. doAssert i64 shl 2 == -4
  106. let u8 = uint8.high
  107. let u16 = uint16.high
  108. let u32 = uint32.high
  109. let u64 = uint64.high
  110. doAssert u8 shl 1 == u8 - 1
  111. doAssert u16 shl 1 == u16 - 1
  112. doAssert u32 shl 1 == u32 - 1
  113. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  114. doAssert u64 shl 1 == u64 - 1
  115. block: # bug #23378
  116. var neg = -1 # prevent compile-time evaluation
  117. let n = abs BiggestInt neg
  118. doAssert n == 1
  119. echo("Success") #OUT Success