develooper Front page | perl.perl6.internals | Postings from July 2002

[netlabs #788] [PATCH] Array fixes (and tests)

Thread Next
From:
Simon Glover
Date:
July 10, 2002 13:20
Subject:
[netlabs #788] [PATCH] Array fixes (and tests)
Message ID:
rt-788-3710.18.5426444465243@netlabs
# New Ticket Created by  Simon Glover 
# Please include the string:  [netlabs #788]
# in the subject line of all future correspondence about this issue. 
# <URL: http://bugs6.perl.org/rt2/Ticket/Display.html?id=788 >



 This patch fixes a number of off-by-one errors in array.pmc, and adds a
 few more tests.

 Simon

--- classes/array.pmc.old	Wed Jul 10 15:33:45 2002
+++ classes/array.pmc	Wed Jul 10 15:50:03 2002
@@ -71,7 +71,7 @@ pmclass Array {
     }

     INTVAL type () {
-        return 0;
+        return enum_class_Array;
     }

     INTVAL type_keyed (KEY* key) {
@@ -132,7 +132,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -156,7 +156,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -181,7 +181,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -205,7 +205,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -229,7 +229,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -254,7 +254,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -274,7 +274,7 @@ pmclass Array {
         kp = &key->atom;
         ix = atom2int(INTERP, kp);

-        if (ix > SELF->cache.int_val || ix < 0) {
+        if (ix >= SELF->cache.int_val || ix < 0) {
             internal_exception(OUT_OF_BOUNDS, "Array element out of bounds!\n");
         }

@@ -336,11 +336,11 @@ pmclass Array {

     void set_number (PMC * value) {
         INTVAL idx = (INTVAL)value->cache.num_val;
-        resize_array(interpreter, SELF, idx+1);
+        resize_array(interpreter, SELF, idx);
     }

     void set_number_native (FLOATVAL idx) {
-        resize_array(interpreter, SELF, (INTVAL)idx + 1);
+        resize_array(interpreter, SELF, (INTVAL)idx);
     }

     void set_number_bignum (BIGNUM* value) {


--- t/pmc/array.t.old	Wed Jul 10 15:24:00 2002
+++ t/pmc/array.t	Wed Jul 10 16:01:47 2002
@@ -1,9 +1,9 @@
 #! perl -w

-use Parrot::Test tests => 1;
+use Parrot::Test tests => 5;
 use Test::More;

-output_is(<<'CODE', <<'OUTPUT', "Basic array tests");
+output_is(<<'CODE', <<'OUTPUT', "Setting array size");
 	new P0,.Array

 	set I0,P0
@@ -17,31 +17,108 @@ OK_1:	print "ok 1\n"
 	print "not "
 OK_2:	print "ok 2\n"

+	set P0,2.0
+	set I0,P0
+	eq I0,2,OK_3
+	print "not "
+OK_3:	print "ok 3\n"
+
+        new P1, .PerlInt
+        set P1, 3
+	set P0,P1
+	set I0,P0
+	eq I0,3,OK_4
+	print "not "
+OK_4:	print "ok 4\n"
+
+
+        end
+CODE
+ok 1
+ok 2
+ok 3
+ok 4
+OUTPUT
+
+output_is(<<'CODE', <<'OUTPUT', "Setting first element");
+        new P0, .Array
+        set P0, 1
+
 	set P0,0,-7 # set P0[0], -7
 	set I0,P0,0 # set I0, P0[0]
-	eq I0,-7,OK_3
+	eq I0,-7,OK_1
 	print "not "
-OK_3:	print "ok 3\n"
+OK_1:	print "ok 1\n"

 	set P0,0,3.7 # set P0[0], 3.7
 	set N0,P0,0 # set N0, P0[0]
-	eq N0,3.7,OK_4
+	eq N0,3.7,OK_2
 	print "not "
-OK_4:	print "ok 4\n"
+OK_2:	print "ok 2\n"

 	set P0,0,"Buckaroo" # set P0[0], "Buckaroo"
 	set S0,P0,0 # set S0, P0[0]
-	eq S0,"Buckaroo",OK_5
+	eq S0,"Buckaroo",OK_3
 	print "not "
-OK_5:	print "ok 5\n"
+OK_3:	print "ok 3\n"

 	end
 CODE
 ok 1
 ok 2
 ok 3
-ok 4
-ok 5
+OUTPUT
+
+output_is(<<'CODE', <<'OUTPUT', "Setting second element");
+        new P0, .Array
+        set P0, 2
+
+	set P0[1], -7
+	set I0, P0[1]
+	eq I0,-7,OK_1
+	print "not "
+OK_1:	print "ok 1\n"
+
+	set P0[1], 3.7
+	set N0, P0[1]
+	eq N0,3.7,OK_2
+	print "not "
+OK_2:	print "ok 2\n"
+
+	set P0[1],"Buckaroo"
+	set S0, P0[1]
+	eq S0,"Buckaroo",OK_3
+	print "not "
+OK_3:	print "ok 3\n"
+
+	end
+CODE
+ok 1
+ok 2
+ok 3
+OUTPUT
+
+# TODO: Rewrite these properly when we have exceptions
+
+output_is(<<'CODE', <<'OUTPUT', "Setting out-of-bounds elements");
+        new P0, .Array
+        set P0, 1
+
+	set P0[1], -7
+
+	end
+CODE
+Array element out of bounds!
+OUTPUT
+
+output_is(<<'CODE', <<'OUTPUT', "Getting out-of-bounds elements");
+        new P0, .Array
+        set P0, 1
+
+	set I0, P0[1]
+	end
+CODE
+Array element out of bounds!
 OUTPUT

 1;




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About