tsystem.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. import stdtest/testutils
  6. import std/assertions
  7. # TODO: in future work move existing `system` tests here, where they belong
  8. template main =
  9. block: # closure
  10. proc outer() =
  11. var a = 0
  12. proc inner1 = a.inc
  13. proc inner2 = discard
  14. doAssert inner1 is "closure"
  15. doAssert inner2 isnot "closure"
  16. doAssert inner1 is (proc)
  17. doAssert inner2 is (proc)
  18. let inner1b = inner1
  19. doAssert inner1b is "closure"
  20. doAssert inner1b == inner1
  21. outer()
  22. block: # rawProc, rawProc, bug #17911
  23. proc outer() =
  24. var a = 0
  25. var b = 0
  26. proc inner1() = a.inc
  27. proc inner2() = a += 2
  28. proc inner3() = b.inc
  29. let inner1b = inner1
  30. doAssert inner2 != inner1
  31. doAssert inner3 != inner1
  32. whenVMorJs: discard
  33. do:
  34. doAssert rawProc(inner1b) == rawProc(inner1)
  35. doAssert rawProc(inner2) != rawProc(inner1)
  36. doAssert rawProc(inner3) != rawProc(inner1)
  37. doAssert rawEnv(inner1b) == rawEnv(inner1)
  38. doAssert rawEnv(inner2) == rawEnv(inner1) # because both use `a`
  39. # doAssert rawEnv(inner3) != rawEnv(inner1) # because `a` vs `b` # this doesn't hold
  40. outer()
  41. block: # system.delete
  42. block:
  43. var s = @[1]
  44. s.delete(0)
  45. doAssert s == @[]
  46. block:
  47. var s = @["foo", "bar"]
  48. s.delete(1)
  49. doAssert s == @["foo"]
  50. when false:
  51. var s: seq[string]
  52. doAssertRaises(IndexDefect):
  53. s.delete(0)
  54. block:
  55. doAssert not compiles(@["foo"].delete(-1))
  56. block: # bug #6710
  57. var s = @["foo"]
  58. s.delete(0)
  59. doAssert s == @[]
  60. when false: # bug #16544: deleting out of bounds index should raise
  61. var s = @["foo"]
  62. doAssertRaises(IndexDefect):
  63. s.delete(1)
  64. static: main()
  65. main()
  66. # bug #19967
  67. block:
  68. type
  69. X = object
  70. a: string
  71. b: set[char]
  72. var y = X(b: {'a'})
  73. reset(y)
  74. doAssert y.b == {}
  75. block:
  76. type
  77. X = object
  78. a: string
  79. b: int
  80. var y = X(b: 1314)
  81. reset(y)
  82. doAssert y.b == 0
  83. block:
  84. type
  85. X = object
  86. a: string
  87. b: float
  88. var y = X(b: 1314.521)
  89. reset(y)
  90. doAssert y.b == 0.0
  91. block:
  92. type
  93. X = object
  94. a: string
  95. b: string
  96. var y = X(b: "1314")
  97. reset(y)
  98. doAssert y.b == ""
  99. block:
  100. type
  101. X = object
  102. a: string
  103. b: seq[int]
  104. var y = X(b: @[1, 3])
  105. reset(y)
  106. doAssert y.b == @[]
  107. block:
  108. type
  109. X = object
  110. a: string
  111. b: tuple[a: int, b: string]
  112. var y = X(b: (1, "cc"))
  113. reset(y)
  114. doAssert y.b == (0, "")
  115. block:
  116. type
  117. Color = enum
  118. Red, Blue, Yellow
  119. X = object
  120. a: string
  121. b: set[Color]
  122. var y = X(b: {Red, Blue})
  123. reset(y)
  124. doAssert y.b == {}
  125. block: # bug #20516
  126. type Foo = object
  127. x {.bitsize:4.}: uint
  128. y {.bitsize:4.}: uint
  129. when not defined(js):
  130. let a = create(Foo)