summary refs log tree commit diff stats
path: root/tests/destructor/tcustomstrings.nim
diff options
context:
space:
mode:
authorDaniil Yarancev <21169548+Yardanico@users.noreply.github.com>2018-01-07 21:02:00 +0300
committerGitHub <noreply@github.com>2018-01-07 21:02:00 +0300
commitfb44c522e6173528efa8035ecc459c84887d0167 (patch)
treea2f5e98606be265981a5f72748896967033e23d7 /tests/destructor/tcustomstrings.nim
parentccf99fa5ce4fe992fb80dc89271faa51456c3fa5 (diff)
parente23ea64c41e101d4e1d933f0b015f51cc6c2f7de (diff)
downloadNim-fb44c522e6173528efa8035ecc459c84887d0167.tar.gz
Merge pull request #1 from nim-lang/devel
upstream
Diffstat (limited to 'tests/destructor/tcustomstrings.nim')
-rw-r--r--tests/destructor/tcustomstrings.nim29
1 files changed, 16 insertions, 13 deletions
diff --git a/tests/destructor/tcustomstrings.nim b/tests/destructor/tcustomstrings.nim
index 2250d4772..1a78df20b 100644
--- a/tests/destructor/tcustomstrings.nim
+++ b/tests/destructor/tcustomstrings.nim
@@ -4,27 +4,27 @@ foo bar to appendmore here
 foo bar to appendmore here
 foo bar to appendmore here
 foo bar to appendmore here
-after 16 16'''
+after 20 20'''
   cmd: '''nim c --newruntime $file'''
 """
 
+{.this: self.}
+
 type
   mystring = object
     len, cap: int
     data: ptr UncheckedArray[char]
 
-{.this: self.}
-
 var
   allocCount, deallocCount: int
 
-proc `=destroy`*(self: var mystring) =
-  if data != nil:
-    dealloc(data)
+proc `=destroy`*(s: var mystring) =
+  if s.data != nil:
+    dealloc(s.data)
     inc deallocCount
-    data = nil
-    len = 0
-    cap = 0
+    s.data = nil
+    s.len = 0
+    s.cap = 0
 
 proc `=sink`*(a: var mystring, b: mystring) =
   # we hope this is optimized away for not yet alive objects:
@@ -48,10 +48,10 @@ proc `=`*(a: var mystring; b: mystring) =
     copyMem(a.data, b.data, a.cap+1)
 
 proc resize(self: var mystring) =
-  if cap == 0: cap = 8
-  else: cap = (cap * 3) shr 1
-  if data == nil: inc allocCount
-  data = cast[type(data)](realloc(data, cap + 1))
+  if self.cap == 0: self.cap = 8
+  else: self.cap = (self.cap * 3) shr 1
+  if self.data == nil: inc allocCount
+  self.data = cast[type(data)](realloc(self.data, self.cap + 1))
 
 proc add*(self: var mystring; c: char) =
   if self.len >= self.cap: resize(self)
@@ -92,5 +92,8 @@ proc main(n: int) =
     a.add c
     echo cstring(a.data)
 
+  var x: array[4, mystring]
+  for i in 0..high(x): x[i] = create"added to array"
+
 main(1000)
 echo "after ", allocCount, " ", deallocCount