tarithm.nim 4.1 KB

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