tints.nim 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. discard """
  2. output: '''
  3. 0 0
  4. 0 0
  5. Success'''
  6. """
  7. # Test the different integer operations
  8. var testNumber = 0
  9. template test(opr, a, b, c: untyped): untyped =
  10. # test the expression at compile and runtime
  11. block:
  12. const constExpr = opr(a, b)
  13. when constExpr != c:
  14. {.error: "Test failed " & $constExpr & " " & $c.}
  15. inc(testNumber)
  16. #Echo("Test: " & $testNumber)
  17. var aa = a
  18. var bb = b
  19. var varExpr = opr(aa, bb)
  20. assert(varExpr == c)
  21. test(`+`, 12'i8, -13'i16, -1'i16)
  22. test(`shl`, 0b11, 0b100, 0b110000)
  23. when not defined(js):
  24. test(`shl`, 0b11'i32, 0b100'i64, 0b110000'i64)
  25. test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32)
  26. test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16)
  27. test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16)
  28. when not defined(js):
  29. test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0xffffffffffffffff'i64)
  30. test(`shr`, 0xffff'i16, 0x4'i16, 0xffff'i16)
  31. test(`shr`, 0xff'i8, 0x4'i8, 0xff'i8)
  32. when not defined(js):
  33. test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64)
  34. test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32)
  35. when not defined(js):
  36. test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64)
  37. test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16)
  38. test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8)
  39. when not defined(js):
  40. test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64)
  41. test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
  42. # bug #916
  43. proc unc(a: float): float =
  44. return a
  45. echo int(unc(0.5)), " ", int(unc(-0.5))
  46. echo int(0.5), " ", int(-0.5)
  47. block: # Casts to uint
  48. template testCast(fromValue: typed, toType: typed, expectedResult: typed) =
  49. let src = fromValue
  50. let dst = cast[toType](src)
  51. if dst != expectedResult:
  52. echo "Casting ", astToStr(fromValue), " to ", astToStr(toType), " = ", dst.int, " instead of ", astToStr(expectedResult)
  53. doAssert(dst == expectedResult)
  54. testCast(-1'i16, uint16, 0xffff'u16)
  55. testCast(0xffff'u16, int16, -1'i16)
  56. testCast(0xff'u16, uint8, 0xff'u8)
  57. testCast(0xffff'u16, uint8, 0xff'u8)
  58. testCast(-1'i16, uint32, 0xffffffff'u32)
  59. testCast(0xffffffff'u32, int32, -1)
  60. testCast(0xfffffffe'u32, int32, -2'i32)
  61. testCast(0xffffff'u32, int16, -1'i32)
  62. testCast(-5'i32, uint8, 251'u8)
  63. # issue #7174
  64. let c = 1'u
  65. let val = c > 0
  66. doAssert val
  67. echo("Success") #OUT Success