tvmmisc.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # bug #4462
  2. import macros
  3. import os
  4. import ospaths
  5. import strutils
  6. block:
  7. proc foo(t: typedesc) {.compileTime.} =
  8. assert sameType(getType(t), getType(int))
  9. static:
  10. foo(int)
  11. # #4412
  12. block:
  13. proc default[T](t: typedesc[T]): T {.inline.} = discard
  14. static:
  15. var x = default(type(0))
  16. # #6379
  17. import algorithm
  18. static:
  19. var numArray = [1, 2, 3, 4, -1]
  20. numArray.sort(cmp)
  21. assert numArray == [-1, 1, 2, 3, 4]
  22. var str = "cba"
  23. str.sort(cmp)
  24. assert str == "abc"
  25. # #6086
  26. import math, sequtils, sugar
  27. block:
  28. proc f: int =
  29. toSeq(10..<10_000).filter(
  30. a => a == ($a).map(
  31. d => (d.ord-'0'.ord).int^4
  32. ).sum
  33. ).sum
  34. var a = f()
  35. const b = f()
  36. assert a == b
  37. block:
  38. proc f(): seq[char] =
  39. result = "hello".map(proc(x: char): char = x)
  40. var runTime = f()
  41. const compTime = f()
  42. assert runTime == compTime
  43. # #6083
  44. block:
  45. proc abc(): seq[int] =
  46. result = @[0]
  47. result.setLen(2)
  48. var tmp: int
  49. for i in 0 ..< 2:
  50. inc tmp
  51. result[i] = tmp
  52. const fact1000 = abc()
  53. assert fact1000 == @[1, 2]
  54. # Tests for VM ops
  55. block:
  56. static:
  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