summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorClyybber <darkmine956@gmail.com>2020-07-14 14:15:39 +0200
committerGitHub <noreply@github.com>2020-07-14 14:15:39 +0200
commit2b0e336c971401d06797240686722ce669d902b9 (patch)
tree51ce03a1310b5477a16a20f8234621ffa5d5d51f /tests
parentf999f916f31946be3cf8abc485fc07e0d89de9ae (diff)
downloadNim-2b0e336c971401d06797240686722ce669d902b9.tar.gz
injectdestructors fixes and refactor (#14964)
* injectdestructors fixes and refactor

* Tiny cleanup

* Refactor and expand testcase

* Closes #14902 by adding testcase

* Better naming

* Fix test failures

* Misc cleanup

* Add testcase for #14968

* Better approach; expand testcases

* Optimizations and fixes

* Add testcase

* typo

* Tiny cleanup
Diffstat (limited to 'tests')
-rw-r--r--tests/arc/tarcmisc.nim38
-rw-r--r--tests/arc/tcontrolflow.nim4
-rw-r--r--tests/arc/tmovebug.nim169
-rw-r--r--tests/destructor/tmove_objconstr.nim1
4 files changed, 207 insertions, 5 deletions
diff --git a/tests/arc/tarcmisc.nim b/tests/arc/tarcmisc.nim
index b6d9d781d..2d7e6b455 100644
--- a/tests/arc/tarcmisc.nim
+++ b/tests/arc/tarcmisc.nim
@@ -3,6 +3,11 @@ discard """
 123xyzabc
 destroyed: false
 destroyed: false
+destroyed2: false
+destroyed2: false
+destroying variable: 2
+destroying variable: 1
+whiley ends :(
 1
 (x: "0")
 (x: "1")
@@ -17,7 +22,8 @@ destroyed: false
 (x: "10")
 0
 closed
-destroying variable
+destroying variable: 20
+destroying variable: 10
 '''
   cmd: "nim c --gc:arc $file"
 """
@@ -40,11 +46,12 @@ type Variable = ref object
   value: int
 
 proc `=destroy`(self: var typeof(Variable()[])) =
-  echo "destroying variable"
+  echo "destroying variable: ",self.value
 
 proc newVariable(value: int): Variable =
   result = Variable()
   result.value = value
+  #echo "creating variable: ",result.value
 
 proc test(count: int) =
   var v {.global.} = newVariable(10)
@@ -57,6 +64,28 @@ proc test(count: int) =
 
 test(3)
 
+proc test2(count: int) =
+  #block: #XXX: Fails with block currently
+    var v {.global.} = newVariable(20)
+
+    var count = count - 1
+    if count == 0: return
+
+    test2(count)
+    echo "destroyed2: ", v.isNil
+
+test2(3)
+
+proc whiley =
+  var a = newVariable(1)
+  while true:
+    var b = newVariable(2)
+    if true: raise newException(CatchableError, "test")
+
+try:
+  whiley()
+except CatchableError:
+  echo "whiley ends :("
 
 #------------------------------------------------------------------------------
 # issue #13810
@@ -209,3 +238,8 @@ proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
 
 var l = SimpleLoopB()
 l.setParent(l)
+
+
+# bug #14968
+import times
+let currentTime = now().utc
diff --git a/tests/arc/tcontrolflow.nim b/tests/arc/tcontrolflow.nim
index 41f614cb2..80cc2b187 100644
--- a/tests/arc/tcontrolflow.nim
+++ b/tests/arc/tcontrolflow.nim
@@ -1,12 +1,12 @@
 discard """
   output: '''begin A
 elif
-destroyed
 end A
+destroyed
 begin false
 if
-destroyed
 end false
+destroyed
 begin true
 if
 end true
diff --git a/tests/arc/tmovebug.nim b/tests/arc/tmovebug.nim
index ec0fce9a8..424785ed7 100644
--- a/tests/arc/tmovebug.nim
+++ b/tests/arc/tmovebug.nim
@@ -40,6 +40,33 @@ sink me (not sink)
 sinked and not optimized to a bitcopy
 sinked and not optimized to a bitcopy
 sinked and not optimized to a bitcopy
+(data: @[0, 0])
+(data: @[0, 0])
+(data: @[0, 0])
+(data: @[0, 0])
+(data: @[0, 0])
+(data: @[0, 0])
+(data: @[0, 0])
+100
+hey
+hey
+(a: "a", b: 2)
+ho
+(a: "b", b: 3)
+(b: "b", a: 2)
+ho
+(b: "a", a: 3)
+hey
+break
+break
+hey
+ho
+hey
+ho
+ho
+king
+live long; long live
+king
 '''
 """
 
@@ -325,3 +352,145 @@ proc update() =
 
 for i in 1..3:
   update()
+
+
+# bug #14961
+type
+  Foo = object
+    data: seq[int]
+
+proc initFoo(len: int): Foo =
+  result = (let s = newSeq[int](len); Foo(data: s) )
+
+var f = initFoo(2)
+echo initFoo(2)
+
+proc initFoo2(len: int) =
+  echo   if true:
+             let s = newSeq[int](len); Foo(data: s)
+         else:
+             let s = newSeq[int](len); Foo(data: s)
+
+initFoo2(2)
+
+proc initFoo3(len: int) =
+  echo (block:
+         let s = newSeq[int](len); Foo(data: s))
+
+initFoo3(2)
+
+proc initFoo4(len: int) =
+  echo (let s = newSeq[int](len); Foo(data: s))
+
+initFoo4(2)
+
+proc initFoo5(len: int) =
+  echo (case true
+        of true:
+          let s = newSeq[int](len); Foo(data: s)
+        of false:
+          let s = newSeq[int](len); Foo(data: s))
+
+initFoo5(2)
+
+proc initFoo6(len: int) =
+  echo (block:
+          try:
+            let s = newSeq[int](len); Foo(data: s)
+          finally: discard)
+
+initFoo6(2)
+
+proc initFoo7(len: int) =
+  echo (block:
+          try:
+            raise newException(CatchableError, "sup")
+            let s = newSeq[int](len); Foo(data: s)
+          except CatchableError:
+            let s = newSeq[int](len); Foo(data: s) )
+
+initFoo7(2)
+
+
+# bug #14902
+iterator zip[T](s: openarray[T]): (T, T) =
+  var i = 0
+  while i < 10:
+    yield (s[i mod 2], s[i mod 2 + 1])
+    inc i
+
+var lastMem = int.high
+
+proc leak =
+  const len = 10
+  var x = @[newString(len), newString(len), newString(len)]
+
+  var c = 0
+  for (a, b) in zip(x):
+    let newMem = getOccupiedMem()
+    assert newMem <= lastMem
+    lastMem = newMem
+    c += a.len
+  echo c
+
+leak()
+
+
+proc consume(a: sink string) = echo a
+
+proc weirdScopes =
+  if (let a = "hey"; a.len > 0):
+    echo a
+
+  while (let a = "hey"; a.len > 0):
+    echo a
+    break
+
+  var a = block: (a: "a", b: 2)
+  echo a
+  (discard; a) = (echo "ho"; (a: "b", b: 3))
+  echo a
+
+  var b = try: (b: "b", a: 2)
+          except: raise
+  echo b
+  (discard; b) = (echo "ho"; (b: "a", a: 3))
+  echo b
+
+  var s = "break"
+  consume((echo "hey"; s))
+  echo s
+
+  echo (block:
+          var a = "hey"
+          (echo "hey"; "ho"))
+
+  var b2 = "ho"
+  echo (block:
+          var a = "hey"
+          (echo "hey"; b2))
+  echo b2
+
+  type status = enum
+    alive
+
+  var king = "king"
+  echo (block:
+          var a = "a"
+          when true:
+            var b = "b"
+            case alive
+            of alive:
+              try:
+                var c = "c"
+                if true:
+                  king
+                else:
+                  "the abyss"
+              except:
+                echo "he ded"
+                "dead king")
+  echo "live long; long live"
+  echo king
+
+weirdScopes()
diff --git a/tests/destructor/tmove_objconstr.nim b/tests/destructor/tmove_objconstr.nim
index 0740bd46b..16d0daa22 100644
--- a/tests/destructor/tmove_objconstr.nim
+++ b/tests/destructor/tmove_objconstr.nim
@@ -2,7 +2,6 @@
 discard """
 output:  '''test created
 test destroyed 0
-Pony is dying!
 1
 2
 3