tarithm.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. discard """
  2. output: '''
  3. int32
  4. int32
  5. 1280
  6. 1280
  7. '''
  8. """
  9. import typetraits
  10. block tand:
  11. # bug #5216
  12. echo(name type((0x0A'i8 and 0x7F'i32) shl 7'i32))
  13. let i8 = 0x0A'i8
  14. echo(name type((i8 and 0x7F'i32) shl 7'i32))
  15. echo((0x0A'i8 and 0x7F'i32) shl 7'i32)
  16. let ii8 = 0x0A'i8
  17. echo((ii8 and 0x7F'i32) shl 7'i32)
  18. block tcast:
  19. template crossCheck(ty: untyped, exp: untyped) =
  20. let rt = ty(exp)
  21. const ct = ty(exp)
  22. if $rt != $ct:
  23. echo astToStr(exp)
  24. echo "Got ", ct
  25. echo "Expected ", rt
  26. template add1(x: uint8): untyped = x + 1
  27. template add1(x: uint16): untyped = x + 1
  28. template add1(x: uint32): untyped = x + 1
  29. template sub1(x: uint8): untyped = x - 1
  30. template sub1(x: uint16): untyped = x - 1
  31. template sub1(x: uint32): untyped = x - 1
  32. crossCheck(int8, 0'i16 - 5'i16)
  33. crossCheck(int16, 0'i32 - 5'i32)
  34. crossCheck(int32, 0'i64 - 5'i64)
  35. crossCheck(uint8, 0'u8 - 5'u8)
  36. crossCheck(uint16, 0'u16 - 5'u16)
  37. crossCheck(uint32, 0'u32 - 5'u32)
  38. crossCheck(uint64, 0'u64 - 5'u64)
  39. crossCheck(uint8, uint8.high + 5'u8)
  40. crossCheck(uint16, uint16.high + 5'u16)
  41. crossCheck(uint32, uint32.high + 5'u32)
  42. crossCheck(uint64, 0xFFFFFFFFFFFFFFFF'u64 + 5'u64)
  43. crossCheck(uint64, uint64.high + 5'u64)
  44. doAssert $sub1(0'u8) == "255"
  45. doAssert $sub1(0'u16) == "65535"
  46. doAssert $sub1(0'u32) == "4294967295"
  47. doAssert $add1(255'u8) == "0"
  48. doAssert $add1(65535'u16) == "0"
  49. doAssert $add1(4294967295'u32) == "0"
  50. crossCheck(int32, high(int32))
  51. crossCheck(int32, high(int32).int32)
  52. crossCheck(int32, low(int32))
  53. crossCheck(int32, low(int32).int32)
  54. crossCheck(int64, high(int8).int16.int32.int64)
  55. crossCheck(int64, low(int8).int16.int32.int64)
  56. doAssert not compiles(echo int64(0xFFFFFFFFFFFFFFFF'u64))
  57. doAssert not compiles(echo int32(0xFFFFFFFFFFFFFFFF'u64))
  58. doAssert not compiles(echo int16(0xFFFFFFFFFFFFFFFF'u64))
  59. doAssert not compiles(echo int8(0xFFFFFFFFFFFFFFFF'u64))
  60. block tnot:
  61. # Signed types
  62. block:
  63. const t0: int8 = not 4
  64. const t1: int16 = not 4
  65. const t2: int32 = not 4
  66. const t3: int64 = not 4
  67. const t4: int8 = not -5
  68. const t5: int16 = not -5
  69. const t6: int32 = not -5
  70. const t7: int64 = not -5
  71. doAssert t0 == -5
  72. doAssert t1 == -5
  73. doAssert t2 == -5
  74. doAssert t3 == -5
  75. doAssert t4 == 4
  76. doAssert t5 == 4
  77. doAssert t6 == 4
  78. doAssert t7 == 4
  79. # Unsigned types
  80. block:
  81. const t0: uint8 = not 4'u8
  82. const t1: uint16 = not 4'u16
  83. const t2: uint32 = not 4'u32
  84. const t3: uint64 = not 4'u64
  85. const t4: uint8 = not 251'u8
  86. const t5: uint16 = not 65531'u16
  87. const t6: uint32 = not 4294967291'u32
  88. const t7: uint64 = not 18446744073709551611'u64
  89. doAssert t0 == 251
  90. doAssert t1 == 65531
  91. doAssert t2 == 4294967291'u32
  92. doAssert t3 == 18446744073709551611'u64
  93. doAssert t4 == 4
  94. doAssert t5 == 4
  95. doAssert t6 == 4
  96. doAssert t7 == 4
  97. block tshr:
  98. proc T() =
  99. # let VI = -8
  100. let VI64 = -8'i64
  101. let VI32 = -8'i32
  102. let VI16 = -8'i16
  103. let VI8 = -8'i8
  104. # doAssert( (VI shr 1) == 9_223_372_036_854_775_804, "Actual: " & $(VI shr 1))
  105. doAssert( (VI64 shr 1) == -4, "Actual: " & $(VI64 shr 1))
  106. doAssert( (VI32 shr 1) == -4, "Actual: " & $(VI32 shr 1))
  107. doAssert( (VI16 shr 1) == -4, "Actual: " & $(VI16 shr 1))
  108. doAssert( (VI8 shr 1) == -4, "Actual: " & $(VI8 shr 1))
  109. T()
  110. static:
  111. T()
  112. block tsubrange:
  113. # bug #5854
  114. type
  115. n16 = range[0'i16..high(int16)]
  116. var level: n16 = 1
  117. let maxLevel: n16 = 1
  118. level = min(level + 2, maxLevel).n16
  119. doAssert level == 1