tsystem.nim 2.8 KB

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