tints.nim 4.0 KB

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