[Kaffe] StringBuffer patch.

Mo DeJong mdejong at cygnus.com
Thu Apr 6 01:46:03 PDT 2000


This patch fixes a bug that was added to StringBuffer recently.
It also adds some regressions tests for the bug and related calls.

Mo Dejong
Red Hat Inc.

Thu Apr  6 00:30:30 PDT 2000  Mo DeJong <mdejong at cygnus.com>

	* libraries/javalib/java/lang/StringBuffer.java: fixed
	special case where insert(0,...) is called on a
	StringBuffer of size zero.
	* test/regression/Str.java: Added regression tests for
	some calls to StringBuffer methods.


Index: libraries/javalib/java/lang/StringBuffer.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/lang/StringBuffer.java,v
retrieving revision 1.13
diff -u -r1.13 StringBuffer.java
--- libraries/javalib/java/lang/StringBuffer.java       2000/04/01 
18:04:32     
1.13
+++ libraries/javalib/java/lang/StringBuffer.java       2000/04/06 08:41:57
@@ -167,7 +167,9 @@
 
 public synchronized StringBuffer insert(int index, char[] str,
                int offset, int len) {
-       checkIndex(index);
+       if (index != 0) { // Special case for insert(0,"str") on zero length buffer!
+               checkIndex(index);
+       }
        if (offset < 0 || len < 0 || offset + len > str.length) {
                throw new StringIndexOutOfBoundsException();
        }
Index: test/regression/Str.java
===================================================================
RCS file: /cvs/kaffe/kaffe/test/regression/Str.java,v
retrieving revision 1.2
diff -u -r1.2 Str.java
--- test/regression/Str.java    1999/02/12 13:51:10     1.2
+++ test/regression/Str.java    2000/04/06 08:41:57
@@ -4,10 +4,60 @@
                StringBuffer a = new StringBuffer(str);
                a.insert(2, "abcd");
                System.out.println(a);
+
+               a = new StringBuffer();
+               a.insert(0, "Hello");
+               System.out.println(a);
+
+               a = new StringBuffer();
+               try {
+                   a.insert(-1, "Str");
+               } catch (StringIndexOutOfBoundsException si) {
+                   System.out.println("Caught -1");
+               }
+
+               a = new StringBuffer();
+               try {
+                   a.insert(1, "Str");
+               } catch (StringIndexOutOfBoundsException si) {
+                   System.out.println("Caught +1");
+               }
+
+               // Is this really an error? If I insert at the "end"
+               // of a StringBuffer should that throw and exception
+
+               //a = new StringBuffer(1);
+               //a.insert(1, "S");
+               //System.out.println(a);
+
+               a = new StringBuffer(1);
+               try {
+                   a.insert(1, "Str");
+               } catch (StringIndexOutOfBoundsException si) {
+                   System.out.println("Caught 1+1");
+               }
+
+
+               // Pass -1 as the size of a StringBuffer
+               try {
+                   new StringBuffer(-1);
+               } catch (NegativeArraySizeException e) {
+                   System.out.println("Caught -1");
+               }
+
+               // Pass 0 as the size of a StringBuffer
+               new StringBuffer(0);
+               System.out.println("Zero OK");
        }
 }
 
 
 /* Expected Output:
 12abcd34567890
+Hello
+Caught -1
+Caught +1
+Caught 1+1
+Caught -1
+Zero OK
 */



More information about the kaffe mailing list