Author: particle
Date: Thu Mar  9 17:47:38 2006
New Revision: 11844

Modified:
   trunk/src/pmc/resizablepmcarray.pmc
   trunk/t/pmc/iterator.t
   trunk/t/pmc/resizablepmcarray.t

Log:
PMC: add 'exists' and 'defined' methods to ResizablePMCArray
~ add tests
~ modify iterator tests to use ResizablePMCArray instead of PerlArray

Modified: trunk/src/pmc/resizablepmcarray.pmc
==============================================================================
--- trunk/src/pmc/resizablepmcarray.pmc (original)
+++ trunk/src/pmc/resizablepmcarray.pmc Thu Mar  9 17:47:38 2006
@@ -259,6 +259,58 @@
 
 /*
 
+=item C<INTVAL exists_keyed_int(INTVAL key)>
+
+=item C<INTVAL exists_keyed_int(PMC* key)>
+
+Returns TRUE is the element at C<key> exists; otherwise returns false.
+
+=cut
+
+*/
+    INTVAL exists_keyed_int (INTVAL key) {
+        PMC **data;
+
+        if (key < 0)
+            key += PMC_int_val(SELF);
+        if (key < 0 || key >= PMC_int_val(SELF))
+            return 0;
+
+        data = (PMC**)PMC_data(SELF);
+        return !PMC_IS_NULL(data[key]);
+    }
+
+    INTVAL exists_keyed (PMC* key) {
+        INTVAL ix = key_integer(INTERP, key);
+        return SELF.exists_keyed_int(ix);
+    }
+
+/*
+
+=item C<INTVAL defined_keyed_int(INTVAL key)>
+
+Returns TRUE is the element at C<key> is defined; otherwise returns false.
+
+=cut
+
+*/
+
+    INTVAL defined_keyed_int (INTVAL key) {
+        PMC* val;
+
+        if (key < 0)
+            key += PMC_int_val(SELF);
+        if (key < 0 || key >= PMC_int_val(SELF))
+            return 0;
+        val = DYNSELF.get_pmc_keyed_int(key);
+        if(PMC_IS_NULL(val))
+            return 0;
+
+        return VTABLE_defined(INTERP, val);
+    }
+
+/*
+
 =item C<void push_float(FLOATVAL value)>
 
 =item C<void push_integer(INTVAL value)>

Modified: trunk/t/pmc/iterator.t
==============================================================================
--- trunk/t/pmc/iterator.t      (original)
+++ trunk/t/pmc/iterator.t      Thu Mar  9 17:47:38 2006
@@ -23,7 +23,7 @@
 =cut
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "new iter");
-       new P2, .PerlArray
+       new P2, .ResizablePMCArray
        new P1, .Iterator, P2
        print "ok 1\n"
        end
@@ -33,8 +33,8 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "int test");
     .include "iterator.pasm"
-       new P0, .PerlArray      # empty array
-       new P2, .PerlArray      # array with 2 elements
+       new P0, .ResizablePMCArray      # empty array
+       new P2, .ResizablePMCArray      # array with 2 elements
        push P2, 10
        push P2, 20
        set I0, P2
@@ -478,12 +478,12 @@
 Iterator get_integer_keyed_int -1: 97
 OUTPUT
 
-pir_output_is(<< 'CODE', << 'OUTPUT', "Index access for Iterator on 
PerlArray");
+pir_output_is(<< 'CODE', << 'OUTPUT', "Index access for Iterator on 
ResizablePMCArray");
 
 .include "iterator.pasm"
 .sub _main
     .local pmc array_1
-    array_1 = new PerlArray
+    array_1 = new ResizablePMCArray
     push array_1, 'a'
     push array_1, 'b'
     push array_1, 'c'
@@ -493,7 +493,7 @@
     push array_1, '7'
     push array_1, '-8.8'
 
-    print 'PerlArray get_string: '
+    print 'ResizablePMCArray get_string: '
     print array_1
     print "\n"
 
@@ -571,7 +571,7 @@
     end
 .end
 CODE
-PerlArray get_string: 8
+ResizablePMCArray get_string: 8
 Iterator shift_string: a
 Iterator get_string_keyed_int 2: d
 Iterator get_string_keyed_int -1: a
@@ -591,7 +591,7 @@
 pasm_output_is(<<'CODE', <<'OUTPUT', "shift + index access");
     .include "iterator.pasm"
 
-       new P2, .PerlArray      # array with 4 elements
+       new P2, .ResizablePMCArray      # array with 4 elements
        push P2, 10
        push P2, 20
        push P2, 30
@@ -633,7 +633,7 @@
 }
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice syntax");
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    slice P2, P0[2 .. 3, 4, 5 ..6]
    slice P2, P0[10 ..]
    slice P2, P0[.. 11]
@@ -647,7 +647,7 @@
 OUTPUT
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice creates an iterator");
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    slice P2, P0[2 .. 3, 4, 5 ..6]
    typeof S0, P2
    print S0
@@ -659,7 +659,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter simple array elements");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -686,7 +686,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter simple array elements - 
repeat");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -744,7 +744,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter start range");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -770,7 +770,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter end range");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -796,7 +796,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter start range, value");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -824,7 +824,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter range, value");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -853,7 +853,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "slice iter range, range");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300
@@ -1163,7 +1163,7 @@
 .sub main :main
     .include "iterator.pasm"
     .local pmc ar
-    ar = new PerlArray
+    ar = new ResizablePMCArray
     push ar, "a"
     push ar, "b"
     push ar, "c"
@@ -1186,7 +1186,7 @@
 
 pasm_output_is(<<'CODE', <<'OUTPUT', "iter vtable");
    .include "iterator.pasm"
-   new P0, .PerlArray
+   new P0, .ResizablePMCArray
    push P0, 100
    push P0, 200
    push P0, 300

Modified: trunk/t/pmc/resizablepmcarray.t
==============================================================================
--- trunk/t/pmc/resizablepmcarray.t     (original)
+++ trunk/t/pmc/resizablepmcarray.t     Thu Mar  9 17:47:38 2006
@@ -6,7 +6,7 @@
 use warnings;
 use lib qw( . lib ../lib ../../lib );
 use Test::More;
-use Parrot::Test tests => 23;
+use Parrot::Test tests => 24;
 
 =head1 NAME
 
@@ -806,4 +806,102 @@
 int in ResizableIntegerArray: 43
 OUTPUT
 
+pir_output_is(<< 'CODE', << 'OUTPUT', "exists and defined");
+.sub test :main
+    .local pmc array
+    array = new ResizablePMCArray
+    push array, 'a'
+    push array, 'b'
+    push array, 'c'
+    $P0 = new .Null
+    push array, $P0
+    push array, 'e'
+    $P0 = new .Undef
+    push array, $P0
+    push array, '7'
+    push array, '-8.8'
+
+    .local int flag, index
+
+    ## bounds checking: lower
+    index = 0
+    bsr EXISTS
+    bsr DEFINED
+
+    ## bounds checking: upper
+    index = 7
+    bsr EXISTS
+    bsr DEFINED
+
+    ## bounds checking: negative lower
+    index = -1
+    bsr EXISTS
+    bsr DEFINED
+
+    ## bounds checking: negative upper
+    index = -8
+    bsr EXISTS
+    bsr DEFINED
+
+    ## bounds checking: out-of-bounds
+    index = 8
+    bsr EXISTS
+    bsr DEFINED
+
+    ## bounds checking: negative out-of-bounds
+    index = -9
+    bsr EXISTS
+    bsr DEFINED
+
+    ## null value
+    index = 3
+    bsr EXISTS
+    bsr DEFINED
+
+    ## undefined value
+    index = 5
+    bsr EXISTS
+    bsr DEFINED
+
+    goto END
+EXISTS:
+    print 'exists_keyed_int '
+    print index
+    print ': '
+    flag = exists array[index]
+    print flag
+    print "\n"
+    ret
+
+DEFINED:
+    print 'defined_keyed_int '
+    print index
+    print ': '
+    flag = defined array[index]
+    print flag
+    print "\n"
+    ret
+
+END:
+.end
+CODE
+exists_keyed_int 0: 1
+defined_keyed_int 0: 1
+exists_keyed_int 7: 1
+defined_keyed_int 7: 1
+exists_keyed_int -1: 1
+defined_keyed_int -1: 1
+exists_keyed_int -8: 1
+defined_keyed_int -8: 1
+exists_keyed_int 8: 0
+defined_keyed_int 8: 0
+exists_keyed_int -9: 0
+defined_keyed_int -9: 0
+exists_keyed_int 3: 0
+defined_keyed_int 3: 0
+exists_keyed_int 5: 1
+defined_keyed_int 5: 0
+OUTPUT
+
+
 # don't forget to change the number of tests

Reply via email to