cvsuser 02/07/13 10:07:56
Modified: classes array.pmc
t/pmc array.t
Log:
Fix off by errors in array PMC and add tests for out of bounds array access.
Patch from Simon Glover <[EMAIL PROTECTED]>
Revision Changes Path
1.27 +18 -18 parrot/classes/array.pmc
Index: array.pmc
===================================================================
RCS file: /cvs/public/parrot/classes/array.pmc,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -w -r1.26 -r1.27
--- array.pmc 13 Jul 2002 16:35:07 -0000 1.26
+++ array.pmc 13 Jul 2002 17:07:25 -0000 1.27
@@ -1,7 +1,7 @@
/* array.pmc
* Copyright: (When this is determined...it will go here)
* CVS Info
- * $Id: array.pmc,v 1.26 2002/07/13 16:35:07 tom Exp $
+ * $Id: array.pmc,v 1.27 2002/07/13 17:07:25 tom Exp $
* Overview:
* These are the vtable functions for the Array base class
* Data Structure and Algorithms:
@@ -71,7 +71,7 @@
}
INTVAL type () {
- return 0;
+ return enum_class_Array;
}
INTVAL type_keyed (KEY* key) {
@@ -157,7 +157,7 @@
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");
}
@@ -175,7 +175,7 @@
ix = *key;
- 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");
}
@@ -199,7 +199,7 @@
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");
}
@@ -217,7 +217,7 @@
ix = *key;
- 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");
}
@@ -242,7 +242,7 @@
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");
}
@@ -260,7 +260,7 @@
ix = *key;
- 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");
}
@@ -284,7 +284,7 @@
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");
}
@@ -302,7 +302,7 @@
ix = *key;
- 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");
}
@@ -326,7 +326,7 @@
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");
}
@@ -344,7 +344,7 @@
ix = *key;
- 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");
}
@@ -369,7 +369,7 @@
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");
}
@@ -387,7 +387,7 @@
ix = *key;
- 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");
}
@@ -407,7 +407,7 @@
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");
}
@@ -426,7 +426,7 @@
ix = *key;
- 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");
}
@@ -511,11 +511,11 @@
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) {
1.7 +87 -10 parrot/t/pmc/array.t
Index: array.t
===================================================================
RCS file: /cvs/public/parrot/t/pmc/array.t,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -w -r1.6 -r1.7
--- array.t 3 Jun 2002 20:17:05 -0000 1.6
+++ array.t 13 Jul 2002 17:07:56 -0000 1.7
@@ -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 @@
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;