tvmmisc.nim 3.0 KB

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