tsystem_misc.nim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. # bug #6710
  58. var s = @[1]
  59. s.delete(0)
  60. proc foo(a: openArray[int]) =
  61. for x in a: echo x
  62. foo(toOpenArray([1, 2, 3], 0, 0))
  63. foo(toOpenArray([1, 2, 3], 0, 2))
  64. var arr: array[8..12, int] = [11, 12, 13, 14, 15]
  65. foo(toOpenArray(arr, 8, 12))
  66. var seqq = @[1, 2, 3, 4, 5]
  67. foo(toOpenArray(seqq, 1, 3))
  68. # empty openArray issue #7904
  69. foo(toOpenArray(seqq, 0, -1))
  70. foo(toOpenArray(seqq, 1, 0))
  71. doAssertRaises(IndexDefect):
  72. foo(toOpenArray(seqq, 0, -2))
  73. foo(toOpenArray(arr, 9, 8))
  74. foo(toOpenArray(arr, 0, -1))
  75. foo(toOpenArray(arr, 1, 0))
  76. doAssertRaises(IndexDefect):
  77. foo(toOpenArray(arr, 10, 8))
  78. # test openArray of openArray
  79. proc oaEmpty(a: openArray[int]) =
  80. foo(toOpenArray(a, 0, -1))
  81. proc oaFirstElm(a: openArray[int]) =
  82. foo(toOpenArray(a, 0, 0))
  83. oaEmpty(toOpenArray(seqq, 0, -1))
  84. oaEmpty(toOpenArray(seqq, 1, 0))
  85. oaEmpty(toOpenArray(seqq, 1, 2))
  86. oaFirstElm(toOpenArray(seqq, 1, seqq.len-1))
  87. var arrNeg: array[-3 .. -1, int] = [1, 2, 3]
  88. foo(toOpenArray(arrNeg, -3, -1))
  89. foo(toOpenArray(arrNeg, 0, -1))
  90. foo(toOpenArray(arrNeg, -3, -4))
  91. doAssertRaises(IndexDefect):
  92. foo(toOpenArray(arrNeg, -4, -1))
  93. doAssertRaises(IndexDefect):
  94. foo(toOpenArray(arrNeg, -1, 0))
  95. doAssertRaises(IndexDefect):
  96. foo(toOpenArray(arrNeg, -1, -3))
  97. doAssertRaises(Exception):
  98. raise newException(Exception, "foo")
  99. block:
  100. var didThrow = false
  101. try:
  102. doAssertRaises(IndexDefect): # should fail since it's wrong exception
  103. raise newException(FieldDefect, "foo")
  104. except AssertionDefect:
  105. # ok, throwing was correct behavior
  106. didThrow = true
  107. doAssert didThrow
  108. type seqqType = ptr UncheckedArray[int]
  109. let qData = cast[seqqType](addr seqq[0])
  110. oaFirstElm(toOpenArray(qData, 1, 3))
  111. proc foo(a: openArray[byte]) =
  112. for x in a: echo x
  113. let str = "0123456789"
  114. foo(toOpenArrayByte(str, 0, str.high))
  115. template boundedOpenArray[T](x: seq[T], first, last: int): openarray[T] =
  116. toOpenarray(x, max(0, first), min(x.high, last))
  117. # bug #9281
  118. proc foo[T](x: openarray[T]) =
  119. echo x.len
  120. let a = @[1, 2, 3]
  121. # a.boundedOpenArray(1, 2).foo() # Works
  122. echo a.boundedOpenArray(1, 2).len # Internal compiler error
  123. block: # `$`*[T: tuple|object](x: T)
  124. doAssert $(foo1:0, bar1:"a") == """(foo1: 0, bar1: "a")"""
  125. doAssert $(foo1:0, ) == """(foo1: 0)"""
  126. doAssert $(0, "a") == """(0, "a")"""
  127. doAssert $(0, ) == "(0,)"
  128. type Foo = object
  129. x:int
  130. x2:float
  131. doAssert $Foo(x:2) == "(x: 2, x2: 0.0)"
  132. doAssert $() == "()"
  133. # this is a call indirection to prevent `toInt` to be resolved at compile time.
  134. proc testToInt(arg: float64, a: int, b: BiggestInt) =
  135. doAssert toInt(arg) == a
  136. doAssert toBiggestInt(arg) == b
  137. testToInt(0.45, 0, 0) # should round towards 0
  138. testToInt(-0.45, 0, 0) # should round towards 0
  139. testToInt(0.5, 1, 1) # should round away from 0
  140. testToInt(-0.5, -1, -1) # should round away from 0
  141. testToInt(13.37, 13, 13) # should round towards 0
  142. testToInt(-13.37, -13, -13) # should round towards 0
  143. testToInt(7.8, 8, 8) # should round away from 0
  144. testToInt(-7.8, -8, -8) # should round away from 0
  145. # test min/max for correct NaN handling
  146. proc testMinMax(a,b: float32) =
  147. doAssert max(float32(a),float32(b)) == 0'f32
  148. doAssert min(float32(a),float32(b)) == 0'f32
  149. doAssert max(float64(a),float64(b)) == 0'f64
  150. doAssert min(float64(a),float64(b)) == 0'f64
  151. testMinMax(0.0, NaN)
  152. testMinMax(NaN, 0.0)
  153. block:
  154. type Foo = enum
  155. k1, k2
  156. var
  157. a = {k1}
  158. b = {k1,k2}
  159. doAssert a < b
  160. block: # Ordinal
  161. doAssert int is Ordinal
  162. doAssert uint is Ordinal
  163. doAssert int64 is Ordinal
  164. doAssert uint64 is Ordinal
  165. doAssert char is Ordinal
  166. type Foo = enum k1, k2
  167. doAssert Foo is Ordinal
  168. doAssert Foo is SomeOrdinal
  169. doAssert enum is SomeOrdinal
  170. # these fail:
  171. # doAssert enum is Ordinal # fails
  172. # doAssert Ordinal is SomeOrdinal
  173. # doAssert SomeOrdinal is Ordinal