[svn:parrot] r46337 - in trunk: src/pmc t/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Thu May 6 12:03:52 UTC 2010


Author: bacek
Date: Thu May  6 12:03:51 2010
New Revision: 46337
URL: https://trac.parrot.org/parrot/changeset/46337

Log:
Add test for sting capacity and fixed bug in capacilty calculation

Modified:
   trunk/src/pmc/stringbuilder.pmc
   trunk/t/pmc/stringbuilder.t

Modified: trunk/src/pmc/stringbuilder.pmc
==============================================================================
--- trunk/src/pmc/stringbuilder.pmc	Thu May  6 12:03:36 2010	(r46336)
+++ trunk/src/pmc/stringbuilder.pmc	Thu May  6 12:03:51 2010	(r46337)
@@ -123,8 +123,7 @@
         /* TODO If strings are incompatible - convert them */
 
         /* Calculate (possibly new) total size */
-        total_size = calculate_capacity(INTERP, Buffer_buflen(buffer),
-                s->bufused);
+        total_size = calculate_capacity(INTERP, buffer->bufused, s->bufused);
 
         /* Reallocate if necessary */
         if (total_size > Buffer_buflen(buffer))
@@ -137,8 +136,26 @@
         /* Update buffer */
         buffer->bufused += s->bufused;
         buffer->strlen  += Parrot_str_length(INTERP, s);
+
+        PARROT_ASSERT(buffer->bufused <= Buffer_buflen(buffer));
     }
 
+/*
+
+=item C<VTABLE get_integer()>
+
+Returns current capacity of allocated buffer.
+
+For testing purpose only?
+
+=cut
+
+*/
+    INTVAL get_integer() {
+        STRING *buffer;
+        GET_ATTR_buffer(INTERP, SELF, buffer);
+        return Buffer_buflen(buffer);
+    }
 
 /*
 
@@ -182,11 +199,11 @@
     ASSERT_ARGS(calculate_capacity)
     size_t total_size = current + additional;
     if (total_size < 1024)
-        total_size = (total_size + 128) & ~128;
+        total_size = (total_size / 128 + 1) * 128;
     else if (total_size < 4096)
-        total_size = (total_size + 1024) & ~1024;
+        total_size = (total_size / 1024 + 1) * 1024;
     else
-        total_size = (total_size + 4096) & ~4096;
+        total_size = (total_size / 4096 + 1) * 4096;
 
     return total_size;
 }

Modified: trunk/t/pmc/stringbuilder.t
==============================================================================
--- trunk/t/pmc/stringbuilder.t	Thu May  6 12:03:36 2010	(r46336)
+++ trunk/t/pmc/stringbuilder.t	Thu May  6 12:03:51 2010	(r46337)
@@ -20,7 +20,7 @@
 .sub 'main' :main
     .include 'test_more.pir'
 
-    plan(5)
+    plan(12)
     test_create()       # 2 tests
     test_push_string()
 
@@ -42,19 +42,51 @@
 
 .sub 'test_push_string'
     .local pmc sb
-    sb = new ['StringBuilder']
+    sb = new ["StringBuilder"]
 
-    push sb, 'foo'
+    push sb, "foo"
     $S0 = sb
-    is( $S0, 'foo', 'First string pushed')
+    is( $S0, "foo", "First string pushed")
 
-    push sb, 'bar'
+    push sb, "bar"
     $S1 = sb
-    is( $S1, 'foobar', 'Second string pushed')
+    is( $S1, "foobar", "Second string pushed")
+
+    is( $S0, "foo", "... without clobbering first string")
 
-    is( $S0, 'foo', '... without clobbering first string')
+    $I0 = sb
+    is( $I0, 128, "... and capacity still 128" )
 
     # Push large string which will cause reallocate
+    $S99 = repeat "x", 128
+    push sb, $S99
+
+    $S0 = concat "foobar", $S99
+    $S1 = sb
+    is( $S0, $S1, "Push 128 chars string works")
+
+    $I0 = sb
+    is( $I0, 256, "... and capacity increased" )
+
+    $S99 = repeat "x", 1000
+    push sb, $S99
+
+    $S0 = concat $S0, $S99
+    $S1 = sb
+    is( $S0, $S1, "Push 1000 chars string works")
+
+    $I0 = sb
+    is( $I0, 2048, "... and capacity increased" )
+
+    $S99 = repeat "x", 12000
+    push sb, $S99
+
+    $S0 = concat $S0, $S99
+    $S1 = sb
+    is( $S0, $S1, "Push 10000 chars string works")
+
+    $I0 = sb
+    is( $I0, 16384, "... and capacity increased" )
 
 .end
 


More information about the parrot-commits mailing list