tsystem_misc.nim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. discard """
  2. output:'''1
  3. 1
  4. 2
  5. 3
  6. 11
  7. 12
  8. 13
  9. 14
  10. 15
  11. 2
  12. 3
  13. 4
  14. 2
  15. 1
  16. 2
  17. 3
  18. 2
  19. 48
  20. 49
  21. 50
  22. 51
  23. 52
  24. 53
  25. 54
  26. 55
  27. 56
  28. 57
  29. 2
  30. '''
  31. """
  32. block:
  33. const a2 = $(int)
  34. const a3 = $int
  35. doAssert a2 == "int"
  36. doAssert a3 == "int"
  37. proc fun[T: typedesc](t: T) =
  38. const a2 = $(t)
  39. const a3 = $t
  40. doAssert a2 == "int"
  41. doAssert a3 == "int"
  42. fun(int)
  43. # check high/low implementations
  44. doAssert high(int) > low(int)
  45. doAssert high(int8) > low(int8)
  46. doAssert high(int16) > low(int16)
  47. doAssert high(int32) > low(int32)
  48. doAssert high(int64) > low(int64)
  49. # doAssert high(uint) > low(uint) # reconsider depending on issue #6620
  50. doAssert high(uint8) > low(uint8)
  51. doAssert high(uint16) > low(uint16)
  52. doAssert high(uint32) > low(uint32)
  53. # doAssert high(uint64) > low(uint64) # reconsider depending on issue #6620
  54. doAssert high(float) > low(float)
  55. doAssert high(float32) > low(float32)
  56. doAssert high(float64) > low(float64)
  57. proc foo(a: openArray[int]) =
  58. for x in a: echo x
  59. foo(toOpenArray([1, 2, 3], 0, 0))
  60. foo(toOpenArray([1, 2, 3], 0, 2))
  61. var arr: array[8..12, int] = [11, 12, 13, 14, 15]
  62. foo(toOpenArray(arr, 8, 12))
  63. var seqq = @[1, 2, 3, 4, 5]
  64. foo(toOpenArray(seqq, 1, 3))
  65. # empty openArray issue #7904
  66. foo(toOpenArray(seqq, 0, -1))
  67. foo(toOpenArray(seqq, 1, 0))
  68. doAssertRaises(IndexDefect):
  69. foo(toOpenArray(seqq, 0, -2))
  70. foo(toOpenArray(arr, 9, 8))
  71. foo(toOpenArray(arr, 0, -1))
  72. foo(toOpenArray(arr, 1, 0))
  73. doAssertRaises(IndexDefect):
  74. foo(toOpenArray(arr, 10, 8))
  75. # test openArray of openArray
  76. proc oaEmpty(a: openArray[int]) =
  77. foo(toOpenArray(a, 0, -1))
  78. proc oaFirstElm(a: openArray[int]) =
  79. foo(toOpenArray(a, 0, 0))
  80. oaEmpty(toOpenArray(seqq, 0, -1))
  81. oaEmpty(toOpenArray(seqq, 1, 0))
  82. oaEmpty(toOpenArray(seqq, 1, 2))
  83. oaFirstElm(toOpenArray(seqq, 1, seqq.len-1))
  84. var arrNeg: array[-3 .. -1, int] = [1, 2, 3]
  85. foo(toOpenArray(arrNeg, -3, -1))
  86. foo(toOpenArray(arrNeg, 0, -1))
  87. foo(toOpenArray(arrNeg, -3, -4))
  88. doAssertRaises(IndexDefect):
  89. foo(toOpenArray(arrNeg, -4, -1))
  90. doAssertRaises(IndexDefect):
  91. foo(toOpenArray(arrNeg, -1, 0))
  92. doAssertRaises(IndexDefect):
  93. foo(toOpenArray(arrNeg, -1, -3))
  94. doAssertRaises(Exception):
  95. raise newException(Exception, "foo")
  96. block:
  97. var didThrow = false
  98. try:
  99. doAssertRaises(IndexDefect): # should fail since it's wrong exception
  100. raise newException(FieldDefect, "foo")
  101. except AssertionDefect:
  102. # ok, throwing was correct behavior
  103. didThrow = true
  104. doAssert didThrow
  105. type seqqType = ptr UncheckedArray[int]
  106. let qData = cast[seqqType](addr seqq[0])
  107. oaFirstElm(toOpenArray(qData, 1, 3))
  108. proc foo(a: openArray[byte]) =
  109. for x in a: echo x
  110. let str = "0123456789"
  111. foo(toOpenArrayByte(str, 0, str.high))
  112. template boundedOpenArray[T](x: seq[T], first, last: int): openArray[T] =
  113. toOpenarray(x, max(0, first), min(x.high, last))
  114. # bug #9281
  115. proc foo[T](x: openArray[T]) =
  116. echo x.len
  117. let a = @[1, 2, 3]
  118. # a.boundedOpenArray(1, 2).foo() # Works
  119. echo a.boundedOpenArray(1, 2).len # Internal compiler error
  120. block: # `$`*[T: tuple|object](x: T)
  121. doAssert $(foo1:0, bar1:"a") == """(foo1: 0, bar1: "a")"""
  122. doAssert $(foo1:0, ) == """(foo1: 0)"""
  123. doAssert $(0, "a") == """(0, "a")"""
  124. doAssert $(0, ) == "(0,)"
  125. type Foo = object
  126. x:int
  127. x2:float
  128. doAssert $Foo(x:2) == "(x: 2, x2: 0.0)"
  129. doAssert $() == "()"
  130. # this is a call indirection to prevent `toInt` to be resolved at compile time.
  131. proc testToInt(arg: float64, a: int, b: BiggestInt) =
  132. doAssert toInt(arg) == a
  133. doAssert toBiggestInt(arg) == b
  134. testToInt(0.45, 0, 0) # should round towards 0
  135. testToInt(-0.45, 0, 0) # should round towards 0
  136. testToInt(0.5, 1, 1) # should round away from 0
  137. testToInt(-0.5, -1, -1) # should round away from 0
  138. testToInt(13.37, 13, 13) # should round towards 0
  139. testToInt(-13.37, -13, -13) # should round towards 0
  140. testToInt(7.8, 8, 8) # should round away from 0
  141. testToInt(-7.8, -8, -8) # should round away from 0
  142. # test min/max for correct NaN handling
  143. proc testMinMax(a,b: float32) =
  144. doAssert max(float32(a),float32(b)) == 0'f32
  145. doAssert min(float32(a),float32(b)) == 0'f32
  146. doAssert max(float64(a),float64(b)) == 0'f64
  147. doAssert min(float64(a),float64(b)) == 0'f64
  148. testMinMax(0.0, NaN)
  149. testMinMax(NaN, 0.0)
  150. block:
  151. type Foo = enum
  152. k1, k2
  153. var
  154. a = {k1}
  155. b = {k1,k2}
  156. doAssert a < b
  157. block: # Ordinal
  158. doAssert int is Ordinal
  159. doAssert uint is Ordinal
  160. doAssert int64 is Ordinal
  161. doAssert uint64 is Ordinal
  162. doAssert char is Ordinal
  163. type Foo = enum k1, k2
  164. doAssert Foo is Ordinal
  165. doAssert Foo is SomeOrdinal
  166. doAssert enum is SomeOrdinal
  167. # these fail:
  168. # doAssert enum is Ordinal # fails
  169. # doAssert Ordinal is SomeOrdinal
  170. # doAssert SomeOrdinal is Ordinal
  171. block:
  172. proc p() = discard
  173. doAssert not compiles(echo p.rawProc.repr)
  174. doAssert not compiles(echo p.rawEnv.repr)
  175. doAssert not compiles(echo p.finished)
  176. proc bug23223 = # bug #23223
  177. var stuff = "hello"
  178. stuff.insert ""
  179. doAssert stuff == "hello"
  180. bug23223()
  181. block: # bug #23894
  182. let v = high(uint) div 2
  183. let s = v + 1 # 9223372036854775808
  184. let m = succ v
  185. doAssert s == m