tvmmisc.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # bug #4462
  2. import macros
  3. import os
  4. block:
  5. proc foo(t: typedesc) {.compileTime.} =
  6. assert sameType(getType(t), getType(int))
  7. static:
  8. foo(int)
  9. # #4412
  10. block:
  11. proc default[T](t: typedesc[T]): T {.inline.} = discard
  12. static:
  13. var x = default(type(0))
  14. # #6379
  15. import algorithm
  16. static:
  17. var numArray = [1, 2, 3, 4, -1]
  18. numArray.sort(cmp)
  19. assert numArray == [-1, 1, 2, 3, 4]
  20. var str = "cba"
  21. str.sort(cmp)
  22. assert str == "abc"
  23. # #6086
  24. import math, sequtils, sugar
  25. block:
  26. proc f: int =
  27. toSeq(10..<10_000).filter(
  28. a => a == ($a).map(
  29. d => (d.ord-'0'.ord).int^4
  30. ).sum
  31. ).sum
  32. var a = f()
  33. const b = f()
  34. assert a == b
  35. block:
  36. proc f(): seq[char] =
  37. result = "hello".map(proc(x: char): char = x)
  38. var runTime = f()
  39. const compTime = f()
  40. assert runTime == compTime
  41. # #6083
  42. block:
  43. proc abc(): seq[int] =
  44. result = @[0]
  45. result.setLen(2)
  46. var tmp: int
  47. for i in 0 ..< 2:
  48. inc tmp
  49. result[i] = tmp
  50. const fact1000 = abc()
  51. assert fact1000 == @[1, 2]
  52. # Tests for VM ops
  53. block:
  54. static:
  55. # for joint test, the project path is different, so I disabled it:
  56. when false:
  57. assert "vm" in getProjectPath()
  58. let b = getEnv("UNSETENVVAR")
  59. assert b == ""
  60. assert existsEnv("UNSERENVVAR") == false
  61. putEnv("UNSETENVVAR", "VALUE")
  62. assert getEnv("UNSETENVVAR") == "VALUE"
  63. assert existsEnv("UNSETENVVAR") == true
  64. assert fileExists("MISSINGFILE") == false
  65. assert dirExists("MISSINGDIR") == false
  66. # #7210
  67. block:
  68. static:
  69. proc f(size: int): int =
  70. var some = newStringOfCap(size)
  71. result = size
  72. doAssert f(4) == 4
  73. # #6689
  74. block:
  75. static:
  76. proc foo(): int = 0
  77. var f: proc(): int
  78. doAssert f.isNil
  79. f = foo
  80. doAssert(not f.isNil)
  81. block:
  82. static:
  83. var x: ref ref int
  84. new(x)
  85. doAssert(not x.isNil)
  86. # #7871
  87. static:
  88. type Obj = object
  89. field: int
  90. var s = newSeq[Obj](1)
  91. var o = Obj()
  92. s[0] = o
  93. o.field = 2
  94. doAssert s[0].field == 0
  95. # #8125
  96. static:
  97. let def_iter_var = ident("it")
  98. # #8142
  99. static:
  100. type Obj = object
  101. names: string
  102. proc pushName(o: var Obj) =
  103. var s = ""
  104. s.add("FOOBAR")
  105. o.names.add(s)
  106. var o = Obj()
  107. o.names = ""
  108. o.pushName()
  109. o.pushName()
  110. doAssert o.names == "FOOBARFOOBAR"
  111. # #8154
  112. import parseutils
  113. static:
  114. type Obj = object
  115. i: int
  116. proc foo(): Obj =
  117. discard parseInt("1", result.i, 0)
  118. static:
  119. doAssert foo().i == 1
  120. # #10333
  121. block:
  122. const
  123. encoding: auto = [
  124. ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"],
  125. ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"],
  126. ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"],
  127. ["", "M", "MM", "MMM", "--", "-", "--", "---", "----", "--"],
  128. ]
  129. doAssert encoding.len == 4
  130. # #10886
  131. proc tor(): bool =
  132. result = true
  133. result = false or result
  134. proc tand(): bool =
  135. result = false
  136. result = true and result
  137. const
  138. ctor = tor()
  139. ctand = not tand()
  140. static:
  141. doAssert ctor
  142. doAssert ctand
  143. block: # bug #13081
  144. type Kind = enum
  145. k0, k1, k2, k3
  146. type Foo = object
  147. x0: float
  148. case kind: Kind
  149. of k0: discard
  150. of k1: x1: int
  151. of k2: x2: string
  152. of k3: x3: string
  153. const j1 = Foo(x0: 1.2, kind: k1, x1: 12)
  154. const j2 = Foo(x0: 1.3, kind: k2, x2: "abc")
  155. const j3 = Foo(x0: 1.3, kind: k3, x3: "abc2")
  156. static:
  157. doAssert $j1 == "(x0: 1.2, kind: k1, x1: 12)"
  158. doAssert $j2 == """(x0: 1.3, kind: k2, x2: "abc")"""
  159. doAssert $j3 == """(x0: 1.3, kind: k3, x3: "abc2")"""
  160. doAssert $j1 == "(x0: 1.2, kind: k1, x1: 12)"
  161. doAssert $j2 == """(x0: 1.3, kind: k2, x2: "abc")"""
  162. doAssert $j3 == """(x0: 1.3, kind: k3, x3: "abc2")"""
  163. doAssert j1.x1 == 12
  164. static:
  165. doAssert j1.x1 == 12
  166. # bug #15363
  167. import sequtils
  168. block:
  169. func identity(a: bool): bool = a
  170. var a: seq[bool] = static:
  171. newSeq[bool](0).mapIt(it) # segfaults
  172. var b: seq[bool] = static:
  173. newSeq[bool](0).filterIt(it) # does not segfault
  174. var c: seq[bool] = static:
  175. newSeq[bool](0).map(identity) # does not segfault
  176. var d: seq[bool] = static:
  177. newSeq[bool](0).map(proc (a: bool): bool = false) # segfaults
  178. var e: seq[bool] = static:
  179. newSeq[bool](0).filter(identity) # does not segfault
  180. var f: seq[bool] = static:
  181. newSeq[bool](0).filter(proc (a: bool): bool = false) # segfaults
  182. doAssert a == @[]
  183. doAssert b == @[]
  184. doAssert c == @[]
  185. doAssert d == @[]
  186. doAssert e == @[]
  187. doAssert f == @[]