tarithm.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "Got ", ct
  24. echo "Expected ", rt
  25. template add1(x: uint8): untyped = x + 1
  26. template add1(x: uint16): untyped = x + 1
  27. template add1(x: uint32): untyped = x + 1
  28. template sub1(x: uint8): untyped = x - 1
  29. template sub1(x: uint16): untyped = x - 1
  30. template sub1(x: uint32): untyped = x - 1
  31. crossCheck(int8, 0'i16 - 5'i16)
  32. crossCheck(int16, 0'i32 - 5'i32)
  33. crossCheck(int32, 0'i64 - 5'i64)
  34. crossCheck(uint8, 0'u8 - 5'u8)
  35. crossCheck(uint16, 0'u16 - 5'u16)
  36. crossCheck(uint32, 0'u32 - 5'u32)
  37. crossCheck(uint64, 0'u64 - 5'u64)
  38. crossCheck(uint8, uint8.high + 5'u8)
  39. crossCheck(uint16, uint16.high + 5'u16)
  40. crossCheck(uint32, uint32.high + 5'u32)
  41. crossCheck(uint64, (-1).uint64 + 5'u64)
  42. doAssert $sub1(0'u8) == "255"
  43. doAssert $sub1(0'u16) == "65535"
  44. doAssert $sub1(0'u32) == "4294967295"
  45. doAssert $add1(255'u8) == "0"
  46. doAssert $add1(65535'u16) == "0"
  47. doAssert $add1(4294967295'u32) == "0"
  48. crossCheck(int32, high(int32))
  49. crossCheck(int32, high(int32).int32)
  50. crossCheck(int32, low(int32))
  51. crossCheck(int32, low(int32).int32)
  52. crossCheck(int64, high(int8).int16.int32.int64)
  53. crossCheck(int64, low(int8).int16.int32.int64)
  54. crossCheck(int64, 0xFFFFFFFFFFFFFFFF'u64)
  55. crossCheck(int32, 0xFFFFFFFFFFFFFFFF'u64)
  56. crossCheck(int16, 0xFFFFFFFFFFFFFFFF'u64)
  57. crossCheck(int8 , 0xFFFFFFFFFFFFFFFF'u64)
  58. block tnot:
  59. # Signed types
  60. block:
  61. const t0: int8 = not 4
  62. const t1: int16 = not 4
  63. const t2: int32 = not 4
  64. const t3: int64 = not 4
  65. const t4: int8 = not -5
  66. const t5: int16 = not -5
  67. const t6: int32 = not -5
  68. const t7: int64 = not -5
  69. doAssert t0 == -5
  70. doAssert t1 == -5
  71. doAssert t2 == -5
  72. doAssert t3 == -5
  73. doAssert t4 == 4
  74. doAssert t5 == 4
  75. doAssert t6 == 4
  76. doAssert t7 == 4
  77. # Unsigned types
  78. block:
  79. const t0: uint8 = not 4'u8
  80. const t1: uint16 = not 4'u16
  81. const t2: uint32 = not 4'u32
  82. const t3: uint64 = not 4'u64
  83. const t4: uint8 = not 251'u8
  84. const t5: uint16 = not 65531'u16
  85. const t6: uint32 = not 4294967291'u32
  86. const t7: uint64 = not 18446744073709551611'u64
  87. doAssert t0 == 251
  88. doAssert t1 == 65531
  89. doAssert t2 == 4294967291'u32
  90. doAssert t3 == 18446744073709551611'u64
  91. doAssert t4 == 4
  92. doAssert t5 == 4
  93. doAssert t6 == 4
  94. doAssert t7 == 4
  95. block tshl:
  96. # Signed types
  97. block:
  98. const t0: int8 = 1'i8 shl 8
  99. const t1: int16 = 1'i16 shl 16
  100. const t2: int32 = 1'i32 shl 32
  101. const t3: int64 = 1'i64 shl 64
  102. doAssert t0 == 0
  103. doAssert t1 == 0
  104. doAssert t2 == 1
  105. doAssert t3 == 1
  106. # Unsigned types
  107. block:
  108. const t0: uint8 = 1'u8 shl 8
  109. const t1: uint16 = 1'u16 shl 16
  110. const t2: uint32 = 1'u32 shl 32
  111. const t3: uint64 = 1'u64 shl 64
  112. doAssert t0 == 0
  113. doAssert t1 == 0
  114. doAssert t2 == 0
  115. doAssert t3 == 1
  116. block tshr:
  117. proc T() =
  118. # let VI = -8
  119. let VI64 = -8'i64
  120. let VI32 = -8'i32
  121. let VI16 = -8'i16
  122. let VI8 = -8'i8
  123. # doAssert( (VI shr 1) == 9_223_372_036_854_775_804, "Actual: " & $(VI shr 1))
  124. doAssert( (VI64 shr 1) == 9_223_372_036_854_775_804, "Actual: " & $(VI64 shr 1))
  125. doAssert( (VI32 shr 1) == 2_147_483_644, "Actual: " & $(VI32 shr 1))
  126. doAssert( (VI16 shr 1) == 32_764, "Actual: " & $(VI16 shr 1))
  127. doAssert( (VI8 shr 1) == 124, "Actual: " & $(VI8 shr 1))
  128. T()
  129. static:
  130. T()
  131. block tsubrange:
  132. # bug #5854
  133. type
  134. n16 = range[0'i16..high(int16)]
  135. var level: n16 = 1
  136. let maxLevel: n16 = 1
  137. level = min(level + 2, maxLevel)
  138. doAssert level == 1