tints.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. whenJsNoBigInt64: discard
  26. do:
  27. test(`shl`, 0b11'i64, 0b100'i64, 0b110000'i64)
  28. when not defined(js):
  29. # mixed type shr needlessly complicates codegen with bigint
  30. # and thus is not yet supported in JS for 64 bit ints
  31. test(`shl`, 0b11'i32, 0b100'i64, 0b110000'i64)
  32. test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32)
  33. test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16)
  34. test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16)
  35. whenJsNoBigInt64: discard
  36. do:
  37. test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0xffffffffffffffff'i64)
  38. test(`shr`, 0xffff'i16, 0x4'i16, 0xffff'i16)
  39. test(`shr`, 0xff'i8, 0x4'i8, 0xff'i8)
  40. whenJsNoBigInt64: discard
  41. do:
  42. test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64)
  43. test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32)
  44. whenJsNoBigInt64: discard
  45. do:
  46. test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64)
  47. test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16)
  48. test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8)
  49. whenJsNoBigInt64: discard
  50. do:
  51. test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64)
  52. test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
  53. # bug #916
  54. proc unc(a: float): float =
  55. return a
  56. echo int(unc(0.5)), " ", int(unc(-0.5))
  57. echo int(0.5), " ", int(-0.5)
  58. block: # Casts to uint
  59. template testCast(fromValue: typed, toType: typed, expectedResult: typed) =
  60. let src = fromValue
  61. let dst = cast[toType](src)
  62. if dst != expectedResult:
  63. echo "Casting ", astToStr(fromValue), " to ", astToStr(toType), " = ", dst.int, " instead of ", astToStr(expectedResult)
  64. doAssert(dst == expectedResult)
  65. testCast(-1'i16, uint16, 0xffff'u16)
  66. testCast(0xffff'u16, int16, -1'i16)
  67. testCast(0xff'u16, uint8, 0xff'u8)
  68. testCast(0xffff'u16, uint8, 0xff'u8)
  69. testCast(-1'i16, uint32, 0xffffffff'u32)
  70. testCast(0xffffffff'u32, int32, -1)
  71. testCast(0xfffffffe'u32, int32, -2'i32)
  72. testCast(0xffffff'u32, int16, -1'i32)
  73. testCast(-5'i32, uint8, 251'u8)
  74. # issue #7174
  75. let c = 1'u
  76. let val = c > 0
  77. doAssert val
  78. block: # bug #6752
  79. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  80. let x = 711127'i64
  81. doAssert x * 86400'i64 == 61441372800'i64
  82. block: # bug #17604
  83. let a = 2147483648'u
  84. doAssert (a and a) == a
  85. doAssert (a or 0) == a
  86. block: # bitwise not
  87. let
  88. z8 = 0'u8
  89. z16 = 0'u16
  90. z32 = 0'u32
  91. z64 = 0'u64
  92. doAssert (not z8) == uint8.high
  93. doAssert (not z16) == uint16.high
  94. doAssert (not z32) == uint32.high
  95. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  96. doAssert (not z64) == uint64.high
  97. block: # shl
  98. let i8 = int8.high
  99. let i16 = int16.high
  100. let i32 = int32.high
  101. let i64 = int64.high
  102. doAssert i8 shl 1 == -2
  103. doAssert i8 shl 2 == -4
  104. doAssert i16 shl 1 == -2
  105. doAssert i16 shl 2 == -4
  106. doAssert i32 shl 1 == -2
  107. doAssert i32 shl 2 == -4
  108. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  109. doAssert i64 shl 1 == -2
  110. doAssert i64 shl 2 == -4
  111. let u8 = uint8.high
  112. let u16 = uint16.high
  113. let u32 = uint32.high
  114. let u64 = uint64.high
  115. doAssert u8 shl 1 == u8 - 1
  116. doAssert u16 shl 1 == u16 - 1
  117. doAssert u32 shl 1 == u32 - 1
  118. when not defined(js) or (defined(js) and compileOption("jsbigint64")):
  119. doAssert u64 shl 1 == u64 - 1
  120. block: # bug #23378
  121. var neg = -1 # prevent compile-time evaluation
  122. let n = abs BiggestInt neg
  123. doAssert n == 1
  124. echo("Success") #OUT Success