[svn:parrot] r47714 - in trunk: src/pmc t/pmc
NotFound at svn.parrot.org
NotFound at svn.parrot.org
Sat Jun 19 16:04:40 UTC 2010
Author: NotFound
Date: Sat Jun 19 16:04:40 2010
New Revision: 47714
URL: https://trac.parrot.org/parrot/changeset/47714
Log:
implement ByteBuffer set_integer_native vtable to do explicit resize and add some tests for it
Modified:
trunk/src/pmc/bytebuffer.pmc
trunk/t/pmc/bytebuffer.t
Modified: trunk/src/pmc/bytebuffer.pmc
==============================================================================
--- trunk/src/pmc/bytebuffer.pmc Sat Jun 19 15:16:52 2010 (r47713)
+++ trunk/src/pmc/bytebuffer.pmc Sat Jun 19 16:04:40 2010 (r47714)
@@ -116,6 +116,63 @@
/*
+=item C<void set_integer_native()>
+
+Resize the buffer to the given value.
+
+=cut
+
+*/
+
+ VTABLE void set_integer_native(INTVAL set_size) {
+ INTVAL size, allocated_size;
+ unsigned char *content;
+ if (set_size < 0)
+ Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
+ "Negative size in ByteBuffer");
+
+ GET_ATTR_allocated_size(INTERP, SELF, allocated_size);
+ if (set_size == 0) {
+ if (allocated_size == 0)
+ SET_ATTR_source(INTERP, SELF, STRINGNULL);
+ else {
+ GET_ATTR_content(INTERP, SELF, content);
+ Parrot_gc_free_memory_chunk(INTERP, content);
+ }
+ SET_ATTR_allocated_size(INTERP, SELF, 0);
+ SET_ATTR_size(INTERP, SELF, 0);
+ SET_ATTR_content(INTERP, SELF, NULL);
+ }
+ else {
+ GET_ATTR_size(INTERP, SELF, size);
+ /* If reducing size, just change the size value */
+ if (set_size > size) {
+ INTVAL copysize = set_size > size ? set_size : size;
+ if (allocated_size == 0) {
+ content = (unsigned char *)Parrot_gc_allocate_memory_chunk(INTERP, set_size);
+ if (size > 0) {
+ STRING * source;
+ GET_ATTR_source(INTERP, SELF, source);
+ memcpy(content, source->strstart, copysize);
+ }
+ SET_ATTR_source(INTERP, SELF, STRINGNULL);
+ }
+ else {
+ GET_ATTR_content(INTERP, SELF, content);
+ content = (unsigned char *)
+ Parrot_gc_reallocate_memory_chunk(INTERP, content, set_size);
+ }
+ if (copysize < set_size)
+ memset(content + copysize, '\0', set_size - copysize);
+ SET_ATTR_allocated_size(INTERP, SELF, set_size);
+ SET_ATTR_content(INTERP, SELF, content);
+ }
+ SET_ATTR_size(INTERP, SELF, set_size);
+ }
+ }
+
+/*
+
=item C<void set_string_native()>
Reset the buffer with the content of the string.
Modified: trunk/t/pmc/bytebuffer.t
==============================================================================
--- trunk/t/pmc/bytebuffer.t Sat Jun 19 15:16:52 2010 (r47713)
+++ trunk/t/pmc/bytebuffer.t Sat Jun 19 16:04:40 2010 (r47714)
@@ -18,16 +18,18 @@
.include 'iglobals.pasm'
.include 'iterator.pasm'
+.include 'except_types.pasm'
.sub 'main' :main
.include 'test_more.pir'
- plan(26)
+ plan(33)
test_init()
test_set_string()
test_set_byte()
test_get_string()
test_push()
+ test_resize()
test_alloc()
test_iterate()
test_invalid()
@@ -194,6 +196,53 @@
is(s, 'hello', "push gives expected string result")
.end
+.sub test_resize
+ .local pmc bb
+ .local int n
+ .local string s
+ bb = new ['ByteBuffer']
+
+ bb = 723
+ n = elements bb
+ is(n, 723, 'resize from empty')
+
+ bb = 42
+ n = elements bb
+ is(n, 42, 'reduce size')
+
+ bb = 0
+ n = elements bb
+ is(n, 0, 'resize to 0')
+
+ bb = 'foobar'
+ bb = 3
+ n = elements bb
+ is(n, 3, 'reduce size from string content')
+
+ s = bb.'get_string_as'(ascii:"")
+ is(s, 'foo', 'resized string content has correct value')
+
+ bb = 'foobar'
+ bb = 24
+ n = elements bb
+ is(n, 24, 'increase size from string content')
+
+ .local pmc eh
+ eh = new ['ExceptionHandler']
+ eh.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
+ set_addr eh, catch_negative
+ n = 1
+ push_eh eh
+ bb = -1
+ n = 0
+ goto test_negative
+catch_negative:
+ finalize eh
+test_negative:
+ pop_eh
+ ok(n, 'negative size throws')
+.end
+
.sub test_alloc
# Exercise buffer reallocation building a utf16 string with the
# codepoints 32-8192
More information about the parrot-commits
mailing list