tsystem_misc.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(IndexError):
  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(IndexError):
  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(IndexError):
  92. foo(toOpenArray(arrNeg, -4, -1))
  93. doAssertRaises(IndexError):
  94. foo(toOpenArray(arrNeg, -1, 0))
  95. doAssertRaises(IndexError):
  96. foo(toOpenArray(arrNeg, -1, -3))
  97. doAssertRaises(Exception):
  98. raise newException(Exception, "foo")
  99. block:
  100. var didThrow = false
  101. try:
  102. doAssertRaises(IndexError): # should fail since it's wrong exception
  103. raise newException(FieldError, "foo")
  104. except AssertionError:
  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 $() == "()"