tsystem.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. import stdtest/testutils
  6. import std/[assertions, formatfloat]
  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. c: int
  73. d: float
  74. e: int64
  75. var x = X(b: {'a'}, e: 10)
  76. var y = move x
  77. doAssert x.a == ""
  78. doAssert x.b == {}
  79. doAssert x.c == 0
  80. doAssert x.d == 0.0
  81. doAssert x.e == 0
  82. reset(y)
  83. doAssert y.a == ""
  84. doAssert y.b == {}
  85. doAssert y.c == 0
  86. doAssert y.d == 0.0
  87. doAssert y.e == 0
  88. block:
  89. var x = 2
  90. var y = move x
  91. doAssert y == 2
  92. doAssert x == 0
  93. reset y
  94. doAssert y == 0
  95. block:
  96. type
  97. X = object
  98. a: string
  99. b: float
  100. var y = X(b: 1314.521)
  101. reset(y)
  102. doAssert y.b == 0.0
  103. block:
  104. type
  105. X = object
  106. a: string
  107. b: string
  108. var y = X(b: "1314")
  109. reset(y)
  110. doAssert y.b == ""
  111. block:
  112. type
  113. X = object
  114. a: string
  115. b: seq[int]
  116. var y = X(b: @[1, 3])
  117. reset(y)
  118. doAssert y.b == @[]
  119. block:
  120. type
  121. X = object
  122. a: string
  123. b: tuple[a: int, b: string]
  124. var y = X(b: (1, "cc"))
  125. reset(y)
  126. doAssert y.b == (0, "")
  127. block:
  128. type
  129. Color = enum
  130. Red, Blue, Yellow
  131. X = object
  132. a: string
  133. b: set[Color]
  134. var y = X(b: {Red, Blue})
  135. reset(y)
  136. doAssert y.b == {}
  137. block: # bug #20516
  138. type Foo = object
  139. x {.bitsize:4.}: uint
  140. y {.bitsize:4.}: uint
  141. when not defined(js):
  142. let a = create(Foo)
  143. block: # bug #6549
  144. when not defined(js):
  145. block:
  146. const v = 18446744073709551615'u64
  147. doAssert $v == "18446744073709551615"
  148. doAssert $float32(v) == "1.8446744e+19", $float32(v)
  149. doAssert $float64(v) == "1.8446744073709552e+19", $float64(v)
  150. block:
  151. let v = 18446744073709551615'u64
  152. doAssert $v == "18446744073709551615"
  153. doAssert $float32(v) == "1.8446744e+19"
  154. doAssert $float64(v) == "1.8446744073709552e+19"