Hi,

The following patch adds parameter checking to _sort().
Wrong parameters are now reported (no exceptions are used yet).
I've also added 3 more tests in t/pmc/sort.t (included in patchfile)

jens
Index: library/sort.imc
===================================================================
RCS file: /cvs/public/parrot/library/sort.imc,v
retrieving revision 1.1
diff -u -w -r1.1 sort.imc
--- library/sort.imc	12 Feb 2004 07:36:02 -0000	1.1
+++ library/sort.imc	16 Feb 2004 17:52:47 -0000
@@ -45,7 +45,6 @@
 
 .sub _sort
     .param pmc array
-    .param pmc function
     .param int start
     .local int i
     .local int size
@@ -54,6 +53,22 @@
     .local pmc minval
     .local int v
     
+    # check number of INT args
+    if I1 == 0 goto NOINT
+    if I1 == 1 goto OK
+ERROR:
+    print "_sort Syntax:\n"
+    print "_sort( array )\n"
+    print "_sort( array, startoffset )\n"
+    branch END
+NOINT:
+    set start, 0
+OK:
+    # check number of STR args
+    if I2 != 0 goto ERROR
+    # check number of PMC args
+    if I3 != 1 goto ERROR
+        
     size = array
     # already sorted
     if size <= 1 goto END
@@ -73,7 +88,7 @@
 SKIP:
     inc i
     if i < size goto LOOP
-    if start > minpos goto SKIP2
+    if start >= minpos goto SKIP2
     tmp = array[start]
     array[start] = minval
     array[minpos] = tmp
Index: t/pmc/sort.t
===================================================================
RCS file: /cvs/public/parrot/t/pmc/sort.t,v
retrieving revision 1.1
diff -u -w -r1.1 sort.t
--- t/pmc/sort.t	12 Feb 2004 07:36:05 -0000	1.1
+++ t/pmc/sort.t	16 Feb 2004 17:52:47 -0000
@@ -1,7 +1,7 @@
 #! perl -w
 use strict;
 
-use Parrot::Test tests => 6;
+use Parrot::Test tests => 9;
 
 output_is(<<'CODE', <<'OUT', "sorting already sorted numbers");
 ##PIR##
@@ -283,4 +283,121 @@
 foxtrot
 golf
 hotel
+OUT
+
+output_is(<<'CODE', <<'OUT', "sorting letters");
+##PIR##
+.sub _main
+    .local pmc array
+    .local int i
+    .local int j
+    .local pmc tmp
+    
+    new array, .PerlArray
+    push array, "w"
+    push array, "x"
+    push array, "h"
+    push array, "y"
+
+    _sort( array )
+    i = 0
+    j = array
+LOOP:
+    set tmp, array[i]
+    print tmp
+    print "\n"
+    inc i
+    if i < j goto LOOP
+    end
+.end
+.include "library/sort.imc"
+CODE
+h
+w
+x
+y
+OUT
+
+output_is(<<'CODE', <<'OUT', "sorting PerlString letters");
+##PIR##
+.sub _main
+    .local pmc array
+    .local int i
+    .local int j
+    .local pmc tmp
+    
+    new array, .PerlArray
+    new tmp, .PerlString
+    set tmp, "w"
+    push array, tmp
+
+    new tmp, .PerlString
+    set tmp, "x"
+    push array, tmp
+
+    new tmp, .PerlString
+    set tmp, "h"
+    push array, tmp
+
+    new tmp, .PerlString
+    set tmp, "y"
+    push array, tmp
+
+    _sort( array )
+    i = 0
+    j = array
+LOOP:
+    set tmp, array[i]
+    print tmp
+    print "\n"
+    inc i
+    if i < j goto LOOP
+    end
+.end
+.include "library/sort.imc"
+CODE
+h
+w
+x
+y
+OUT
+
+output_is(<<'CODE', <<'OUT', "sorting strings");
+##PIR##
+.sub _main
+    .local pmc array
+    .local int i
+    .local int j
+    .local pmc tmp
+    
+    new array, .PerlArray
+    new tmp, .PerlString
+    push array, "hello"
+    push array, "hash2"
+    push array, "hello2"
+    push array, "aaaa2"
+    push array, "bbb"
+    push array, "bbbbbb"
+    push array, "aaaa1"
+
+    _sort( array )
+    i = 0
+    j = array
+LOOP:
+    set tmp, array[i]
+    print tmp
+    print "\n"
+    inc i
+    if i < j goto LOOP
+    end
+.end
+.include "library/sort.imc"
+CODE
+aaaa1
+aaaa2
+bbb
+bbbbbb
+hash2
+hello
+hello2
 OUT

Reply via email to