Initial load
diff --git a/test/java/lang/StringBuffer/AppendCharSequence.java b/test/java/lang/StringBuffer/AppendCharSequence.java
new file mode 100644
index 0000000..05e3e62
--- /dev/null
+++ b/test/java/lang/StringBuffer/AppendCharSequence.java
@@ -0,0 +1,234 @@
+/*
+ * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 4812591 4705328 5019111
+ * @summary Test append and insert methods with CharSequence params
+ */
+
+import java.util.Random;
+
+public class AppendCharSequence {
+ private static Random generator = new Random();
+
+ public static void main(String[] args) throws Exception {
+ bash();
+ checkNulls();
+ checkOffsets();
+ checkConstructor();
+ }
+
+ // Sanity test of contents
+ private static void bash() throws Exception {
+ for (int i=0; i<1000; i++) {
+ StringBuffer sb1 = generateTestBuffer(0, 100);
+ StringBuffer sb2 = generateTestBuffer(0, 100);
+ StringBuffer sb3 = generateTestBuffer(0, 100);
+ StringBuffer sb4 = generateTestBuffer(0, 100);
+ StringBuffer sb5 = new StringBuffer();
+
+ String s1 = sb1.toString();
+ String s2 = sb2.toString();
+ String s3 = sb3.toString();
+ String s4 = sb4.toString();
+ String s5 = null;
+
+ // append(CharSequence cs)
+ sb5.append((CharSequence)sb1);
+ s5 = sb1.toString();
+
+ if (!sb5.toString().equals(s5))
+ throw new RuntimeException("StringBuffer.append failure 1");
+
+ // append (CharSequence cs, int start, int end)
+ int index = generator.nextInt(100);
+ int len = generator.nextInt(100);
+ while (index > sb2.length() - len) {
+ index = generator.nextInt(100);
+ len = generator.nextInt(100);
+ }
+ sb5.append((CharSequence)sb2, index, index + len);
+ s5 = s5 + sb2.toString().substring(index, index + len);
+
+ if (!sb5.toString().equals(s5))
+ throw new RuntimeException("StringBuffer.append failure 2");
+
+ // insert(int dstOffset, CharSequence cs)
+ index = generator.nextInt(100);
+ while (index > s5.length()) {
+ index = generator.nextInt(100);
+ }
+ sb5.insert(index, (CharSequence)sb3);
+ s5 = new StringBuffer(s5).insert(index, sb3).toString();
+
+ if (!sb5.toString().equals(s5))
+ throw new RuntimeException("StringBuffer.insert failure 1");
+
+ // insert(int dstOffset, CharSequence s, int start, int end)
+ int index1 = generator.nextInt(100);
+ while (index1 > s5.length()) {
+ index1 = generator.nextInt(100);
+ }
+ int index2 = generator.nextInt(100);
+ len = generator.nextInt(100);
+ while (index2 > sb4.length() - len) {
+ index2 = generator.nextInt(100);
+ len = generator.nextInt(100);
+ }
+ sb5.insert(index1, (CharSequence)sb4, index2, index2 + len);
+ s5 = new StringBuffer(s5).insert(index1, s4.toCharArray(),
+ index2, len).toString();
+
+ if (!sb5.toString().equals(s5))
+ throw new RuntimeException("StringBuffer.insert failure 2");
+ }
+ }
+
+ private static int getRandomIndex(int constraint1, int constraint2) {
+ int range = constraint2 - constraint1;
+ int x = generator.nextInt(range);
+ return constraint1 + x;
+ }
+
+ private static StringBuffer generateTestBuffer(int min, int max) {
+ StringBuffer aNewStringBuffer = new StringBuffer();
+ int aNewLength = getRandomIndex(min, max);
+ for(int y=0; y<aNewLength; y++) {
+ int achar = generator.nextInt(30)+30;
+ char test = (char)(achar);
+ aNewStringBuffer.append(test);
+ }
+ return aNewStringBuffer;
+ }
+
+ // Check handling of null as "null"
+ private static void checkNulls() throws Exception {
+ StringBuffer sb1 = new StringBuffer();
+ CharSequence cs = null;
+ sb1.append("test");
+ sb1.append(cs);
+ if (!sb1.toString().equals("testnull"))
+ throw new RuntimeException("StringBuffer.append failure 3");
+
+ sb1 = new StringBuffer();
+ sb1.append("test", 0, 2);
+ sb1.append(cs, 0, 2);
+ if (!sb1.toString().equals("tenu"))
+ throw new RuntimeException("StringBuffer.append failure 4");
+
+ sb1 = new StringBuffer("test");
+ sb1.insert(2, cs);
+ if (!sb1.toString().equals("tenullst"))
+ throw new RuntimeException("StringBuffer.insert failure 3");
+
+ sb1 = new StringBuffer("test");
+ sb1.insert(2, cs, 0, 2);
+ if (!sb1.toString().equals("tenust"))
+ throw new RuntimeException("StringBuffer.insert failure 4");
+ }
+
+ // Test the bounds checking
+ private static void checkOffsets() throws Exception {
+
+ // append (CharSeqeunce cs, int start, int end)
+ for (int i=0; i<100; i++) {
+ StringBuffer sb = generateTestBuffer(0, 80);
+ CharSequence cs = (CharSequence)generateTestBuffer(0, 80);
+ int index = 0;
+ int len = 0;
+ while (index <= cs.length() - len) {
+ index = generator.nextInt(100) - 50;
+ len = generator.nextInt(100) - 50;
+ if (index < 0)
+ break;
+ if (len < 0)
+ break;
+ }
+ try {
+ sb.append(cs, index, index + len);
+ throw new RuntimeException("Append bounds checking failure");
+ } catch (IndexOutOfBoundsException e) {
+ // Correct result
+ }
+ }
+
+ // insert(int dstOffset, CharSequence cs)
+ for (int i=0; i<100; i++) {
+ StringBuffer sb = new StringBuffer("test1");
+ CharSequence cs = (CharSequence)new StringBuffer("test2");
+ int index = 0;
+ while (index <= sb.length()) {
+ index = generator.nextInt(100) - 50;
+ if (index < 0)
+ break;
+ }
+ try {
+ sb.insert(index, cs);
+ throw new RuntimeException("Insert bounds checking failure");
+ } catch (IndexOutOfBoundsException e) {
+ // Correct result
+ }
+ }
+
+ // insert(int dstOffset, CharSequence s, int start, int end)
+ for (int i=0; i<100; i++) {
+ StringBuffer sb = new StringBuffer("test1");
+ CharSequence cs = (CharSequence)new StringBuffer("test2");
+ int index1 = 0;
+ while (index1 <= sb.length()) {
+ index1 = generator.nextInt(100) - 50;
+ if (index1 < 0)
+ break;
+ }
+ int index2 = 0;
+ int len = 0;
+ while (index2 < sb.length() - len) {
+ index2 = generator.nextInt(100) - 50;
+ len = generator.nextInt(100) - 50;
+ if (index2 < 0)
+ break;
+ if (len < 0)
+ break;
+ }
+ try {
+ sb.insert(index1, cs, index2, index2 + len);
+ throw new RuntimeException("Insert bounds checking failure");
+ } catch (IndexOutOfBoundsException e) {
+ // Correct result
+ }
+ }
+ }
+
+ // Test the CharSequence constructor
+ private static void checkConstructor() throws Exception {
+ for (int i=0; i<100; i++) {
+ StringBuffer sb = generateTestBuffer(0, 100);
+ CharSequence cs = (CharSequence)sb;
+ StringBuffer sb2 = new StringBuffer(cs);
+ if (!sb.toString().equals(sb2.toString())) {
+ throw new RuntimeException("CharSequence constructor failure");
+ }
+ }
+ }
+
+}
diff --git a/test/java/lang/StringBuffer/AppendSB.java b/test/java/lang/StringBuffer/AppendSB.java
new file mode 100644
index 0000000..32097d4
--- /dev/null
+++ b/test/java/lang/StringBuffer/AppendSB.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 4144267
+ * @summary Test StringBuffer.append(StringBuffer);
+ */
+
+import java.util.Random;
+
+public class AppendSB {
+ private static Random generator = new Random();
+
+ public static void main(String[] args) throws Exception {
+ for (int i=0; i<1000; i++) {
+ StringBuffer sb1 = generateTestBuffer(10, 100);
+ StringBuffer sb2 = generateTestBuffer(10, 100);
+ StringBuffer sb3 = generateTestBuffer(10, 100);
+ String s1 = sb1.toString();
+ String s2 = sb2.toString();
+ String s3 = sb3.toString();
+
+ String concatResult = new String(s1+s2+s3);
+
+ StringBuffer test = new StringBuffer();
+ test.append(sb1);
+ test.append(sb2);
+ test.append(sb3);
+
+ if (!test.toString().equals(concatResult))
+ throw new RuntimeException("StringBuffer.append failure");
+ }
+ }
+
+ private static int getRandomIndex(int constraint1, int constraint2) {
+ int range = constraint2 - constraint1;
+ int x = generator.nextInt(range);
+ return constraint1 + x;
+ }
+
+ private static StringBuffer generateTestBuffer(int min, int max) {
+ StringBuffer aNewStringBuffer = new StringBuffer(120);
+ int aNewLength = getRandomIndex(min, max);
+ for(int y=0; y<aNewLength; y++) {
+ int achar = generator.nextInt(30)+30;
+ char test = (char)(achar);
+ aNewStringBuffer.append(test);
+ }
+ return aNewStringBuffer;
+ }
+}
diff --git a/test/java/lang/StringBuffer/Exceptions.java b/test/java/lang/StringBuffer/Exceptions.java
new file mode 100644
index 0000000..40e08d5
--- /dev/null
+++ b/test/java/lang/StringBuffer/Exceptions.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4414306 6248507
+ * @summary Verify that exceptions are thrown as expected.
+ */
+
+public class Exceptions {
+ private static boolean ok = true;
+
+ private static void fail(Throwable ex, String s, Throwable got) {
+ ok = false;
+ System.err.println("expected "
+ + ex.getClass().getName() + ": " + ex.getMessage()
+ + " for " + s
+ + " got "
+ + got.getClass().getName() + ": " + got.getMessage()
+ + " - FAILED");
+ }
+
+ private static void pass(String s) {
+ System.out.println(s + " -- OK");
+ }
+
+ private static void tryCatch(String s, Throwable ex, Runnable thunk) {
+ Throwable t = null;
+ try {
+ thunk.run();
+ } catch (Throwable x) {
+// x.printStackTrace();
+ if (ex.getClass().isAssignableFrom(x.getClass()))
+ t = x;
+ else
+ x.printStackTrace();
+ }
+ if ((t == null) && (ex != null))
+ fail(ex, s, t);
+
+ String msg = (ex == null ? null : ex.getMessage());
+ if ((msg != null) && !msg.equals(t.getMessage()))
+ fail(ex, s, t);
+ else
+ pass(s);
+ }
+
+ public static void main(String [] args) {
+ System.out.println("StringBuffer()");
+ tryCatch(" no args", null, new Runnable() {
+ public void run() {
+ new StringBuffer();
+ }});
+
+ System.out.println("StringBuffer(int length)");
+ tryCatch(" 1", null, new Runnable() {
+ public void run() {
+ new StringBuffer(1);
+ }});
+ tryCatch(" -1", new NegativeArraySizeException(), new Runnable() {
+ public void run() {
+ new StringBuffer(-1);
+ }});
+
+ System.out.println("StringBuffer(String str)");
+ tryCatch(" null", new NullPointerException(), new Runnable() {
+ public void run() {
+ new StringBuffer(null);
+ }});
+ tryCatch(" foo", null, new Runnable() {
+ public void run() {
+ new StringBuffer("foo");
+ }});
+
+ System.out.println("StringBuffer.replace(int start, int end, String str)");
+ tryCatch(" -1, 2, \" \"",
+ new StringIndexOutOfBoundsException(-1),
+ new Runnable() {
+ public void run() {
+ StringBuffer sb = new StringBuffer("hilbert");
+ sb.replace(-1, 2, " ");
+ }});
+
+ tryCatch(" 7, 8, \" \"",
+ new StringIndexOutOfBoundsException("start > length()"),
+ new Runnable() {
+ public void run() {
+ StringBuffer sb = new StringBuffer("banach");
+ sb.replace(7, 8, " ");
+ }});
+ tryCatch(" 2, 1, \" \"",
+ new StringIndexOutOfBoundsException("start > end"),
+ new Runnable() {
+ public void run() {
+ StringBuffer sb = new StringBuffer("riemann");
+ sb.replace(2, 1, " ");
+ }});
+
+ if (!ok)
+ throw new RuntimeException("Some tests FAILED");
+ else
+ System.out.println("All tests PASSED");
+ }
+}
diff --git a/test/java/lang/StringBuffer/GetCharsOverLength.java b/test/java/lang/StringBuffer/GetCharsOverLength.java
new file mode 100644
index 0000000..76b4e38
--- /dev/null
+++ b/test/java/lang/StringBuffer/GetCharsOverLength.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4230290
+ * @summary Test GetChars method parameter checking
+ */
+
+public class GetCharsOverLength {
+
+ public static void main (String argv[]) {
+
+ StringBuffer sb = new StringBuffer("sample string buffer");
+ char dst[] = new char[30];
+ boolean failed = false;
+
+ int a[][] = {
+ {0, 0, dst.length + 1},
+ {0, 0, dst.length + 2},
+ {0, 0, dst.length + 20},
+ {5, 5, dst.length + 1},
+ {5, 5, dst.length + 2},
+ {5, 5, dst.length + 20}
+ };
+
+ for (int i = 0; i < a.length; i++) {
+ try {
+ sb.getChars(a[i][0], a[i][1], dst, a[i][2]);
+ throw new RuntimeException("Bounds test failed");
+ } catch (IndexOutOfBoundsException iobe) {
+ // Test passed
+ }
+ }
+ }
+}
diff --git a/test/java/lang/StringBuffer/GetCharsSrcEndLarger.java b/test/java/lang/StringBuffer/GetCharsSrcEndLarger.java
new file mode 100644
index 0000000..b4f0000
--- /dev/null
+++ b/test/java/lang/StringBuffer/GetCharsSrcEndLarger.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 4096341
+ @summary StringBuffer.getChars(): JLS requires exception if
+ srcBegin > srcEnd
+ @author Anand Palaniswamy
+ */
+public class GetCharsSrcEndLarger {
+ public static void main(String[] args) throws Exception {
+ boolean exceptionOccurred = false;
+ try {
+ new StringBuffer("abc").getChars(1, 0, new char[10], 0);
+ } catch (StringIndexOutOfBoundsException sioobe) {
+ exceptionOccurred = true;
+ }
+ if (!exceptionOccurred) {
+ throw new Exception("StringBuffer.getChars() must throw" +
+ " an exception if srcBegin > srcEnd");
+ }
+ }
+}
diff --git a/test/java/lang/StringBuffer/IndexOf.java b/test/java/lang/StringBuffer/IndexOf.java
new file mode 100644
index 0000000..c19497c
--- /dev/null
+++ b/test/java/lang/StringBuffer/IndexOf.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 4162796 4162796
+ * @summary Test indexOf and lastIndexOf
+ */
+
+import java.util.Random;
+
+public class IndexOf {
+
+ static Random generator = new Random();
+ private static boolean failure = false;
+
+ public static void main(String[] args) throws Exception {
+ simpleTest();
+ compareIndexOfLastIndexOf();
+ compareStringStringBuffer();
+
+ if (failure)
+ throw new RuntimeException("One or more BitSet failures.");
+ }
+
+ private static void report(String testName, int failCount) {
+ System.err.println(testName+": " +
+ (failCount==0 ? "Passed":"Failed("+failCount+")"));
+ if (failCount > 0)
+ failure = true;
+ }
+
+ private static String generateTestString(int min, int max) {
+ StringBuffer aNewString = new StringBuffer(120);
+ int aNewLength = getRandomIndex(min, max);
+ for(int y=0; y<aNewLength; y++) {
+ int achar = generator.nextInt(30)+30;
+ char test = (char)(achar);
+ aNewString.append(test);
+ }
+ return aNewString.toString();
+ }
+
+ private static int getRandomIndex(int constraint1, int constraint2) {
+ int range = constraint2 - constraint1;
+ int x = generator.nextInt(range);
+ return constraint1 + x;
+ }
+
+ private static void simpleTest() {
+ int failCount = 0;
+ String sourceString;
+ StringBuffer sourceBuffer;
+ String targetString;
+
+ for (int i=0; i<10000; i++) {
+ do {
+ sourceString = generateTestString(99, 100);
+ sourceBuffer = new StringBuffer(sourceString);
+ targetString = generateTestString(10, 11);
+ } while (sourceString.indexOf(targetString) != -1);
+
+ int index1 = generator.nextInt(90) + 5;
+ sourceBuffer = sourceBuffer.replace(index1, index1, targetString);
+
+ if (sourceBuffer.indexOf(targetString) != index1)
+ failCount++;
+ if (sourceBuffer.indexOf(targetString, 5) != index1)
+ failCount++;
+ if (sourceBuffer.indexOf(targetString, 99) == index1)
+ failCount++;
+ }
+
+ report("Basic Test ", failCount);
+ }
+
+ // Note: it is possible although highly improbable that failCount will
+ // be > 0 even if everthing is working ok
+ private static void compareIndexOfLastIndexOf() {
+ int failCount = 0;
+ String sourceString;
+ StringBuffer sourceBuffer;
+ String targetString;
+
+ for (int i=0; i<10000; i++) {
+ do {
+ sourceString = generateTestString(99, 100);
+ sourceBuffer = new StringBuffer(sourceString);
+ targetString = generateTestString(10, 11);
+ } while (sourceString.indexOf(targetString) != -1);
+
+ int index1 = generator.nextInt(100);
+ sourceBuffer = sourceBuffer.replace(index1, index1, targetString);
+
+ // extremely remote possibility of > 1 match
+ int matches = 0;
+ int index2 = -1;
+ while((index2 = sourceBuffer.indexOf(targetString,index2+1)) != -1)
+ matches++;
+ if (matches > 1)
+ continue;
+
+ if (sourceBuffer.indexOf(targetString) !=
+ sourceBuffer.lastIndexOf(targetString))
+ failCount++;
+ sourceString = sourceBuffer.toString();
+ if (sourceString.indexOf(targetString) !=
+ sourceString.lastIndexOf(targetString))
+ failCount++;
+ }
+
+ report("IndexOf vs LastIndexOf ", failCount);
+ }
+
+ private static void compareStringStringBuffer() {
+ int failCount = 0;
+
+ for (int x=0; x<10000; x++) {
+ String testString = generateTestString(1, 100);
+ int len = testString.length();
+
+ StringBuffer testBuffer = new StringBuffer(len);
+ testBuffer.append(testString);
+ if (!testString.equals(testBuffer.toString()))
+ throw new RuntimeException("Initial equality failure");
+
+ int x1 = 0;
+ int x2 = 1000;
+ while(x2 > testString.length()) {
+ x1 = generator.nextInt(len);
+ x2 = generator.nextInt(100);
+ x2 = x1 + x2;
+ }
+ String fragment = testString.substring(x1,x2);
+
+ int sAnswer = testString.indexOf(fragment);
+ int sbAnswer = testBuffer.indexOf(fragment);
+
+ if (sAnswer != sbAnswer)
+ failCount++;
+
+ int testIndex = getRandomIndex(-100, 100);
+
+ sAnswer = testString.indexOf(fragment, testIndex);
+ sbAnswer = testBuffer.indexOf(fragment, testIndex);
+
+ if (sAnswer != sbAnswer)
+ failCount++;
+
+ sAnswer = testString.lastIndexOf(fragment);
+ sbAnswer = testBuffer.lastIndexOf(fragment);
+
+ if (sAnswer != sbAnswer)
+ failCount++;
+
+ testIndex = getRandomIndex(-100, 100);
+
+ sAnswer = testString.lastIndexOf(fragment, testIndex);
+ sbAnswer = testBuffer.lastIndexOf(fragment, testIndex);
+
+ if (sAnswer != sbAnswer)
+ failCount++;
+ }
+
+ report("String vs StringBuffer ", failCount);
+ }
+
+}
diff --git a/test/java/lang/StringBuffer/InsertMaxValue.java b/test/java/lang/StringBuffer/InsertMaxValue.java
new file mode 100644
index 0000000..3466d18
--- /dev/null
+++ b/test/java/lang/StringBuffer/InsertMaxValue.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4231107
+ * @summary Test Insert method parameter checking
+ */
+
+public class InsertMaxValue {
+
+ public static void main (String argv[]) throws Exception {
+ StringBuffer sb = new StringBuffer("");
+ StringBuffer sb1 = new StringBuffer("Some test StringBuffer");
+
+ try {
+ sb.insert(0, new char[5], 1, Integer.MAX_VALUE);
+ throw new RuntimeException("Exception expected");
+ } catch (StringIndexOutOfBoundsException sobe) {
+ // Test passed
+ } catch (OutOfMemoryError oome) {
+ throw new RuntimeException("Wrong exception thrown.");
+ }
+
+ try {
+ sb1.insert(2, new char[25], 5, Integer.MAX_VALUE);
+ throw new RuntimeException("Exception expected");
+ } catch (StringIndexOutOfBoundsException sobe) {
+ // Test passed
+ } catch (ArrayIndexOutOfBoundsException aioe) {
+ throw new RuntimeException("Wrong exception thrown.");
+ }
+ }
+}
diff --git a/test/java/lang/StringBuffer/InsertNullString.java b/test/java/lang/StringBuffer/InsertNullString.java
new file mode 100644
index 0000000..d14847f
--- /dev/null
+++ b/test/java/lang/StringBuffer/InsertNullString.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 4085679
+ @summary JLS requires that if you insert a null string, the string
+ "null" must be inserted.
+ @author Anand Palaniswamy
+ */
+public class InsertNullString {
+ public static void main(String[] args) throws Exception {
+ StringBuffer s = new StringBuffer("FOOBAR");
+
+ try {
+ String nullstr = null;
+ s.insert(3, nullstr); /* this will throw null pointer exception
+ before the bug was fixed. */
+ if (!s.toString().equals("FOOnullBAR")) {
+ throw new Exception("StringBuffer.insert() did not insert!");
+ }
+ } catch (NullPointerException npe) {
+ throw new Exception("StringBuffer.insert() of null String reference threw a NullPointerException");
+ }
+ }
+}
diff --git a/test/java/lang/StringBuffer/Replace.java b/test/java/lang/StringBuffer/Replace.java
new file mode 100644
index 0000000..63f6df8
--- /dev/null
+++ b/test/java/lang/StringBuffer/Replace.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 4174396
+ @summary Use replace to append chars; No OutOfMemoryException should result
+*/
+
+public class Replace {
+ public static void main(String[] arg) throws Exception {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < 200; i++) {
+ sb.replace(i, i+1, "a");
+ }
+ }
+}
diff --git a/test/java/lang/StringBuffer/SBBasher.java b/test/java/lang/StringBuffer/SBBasher.java
new file mode 100644
index 0000000..6141128
--- /dev/null
+++ b/test/java/lang/StringBuffer/SBBasher.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4120694
+ * @summary Test new methods in StringBuffer
+ *
+ */
+
+import java.lang.*;
+import java.util.*;
+
+public class SBBasher {
+ public static void main(String[] args) throws Exception {
+ SBBasher basher = new SBBasher();
+
+ for (int iterations=0; iterations<100; iterations++) {
+ String testString = basher.generateTestString();
+ boolean result = basher.Test1(testString);
+ if (result == false)
+ throw new RuntimeException("Substring or replace failure.");
+ }
+
+ for (int iterations=0; iterations<100; iterations++) {
+ String testString = basher.generateTestString();
+ boolean result = basher.Test2(testString);
+ if (result == false)
+ throw new RuntimeException("Insert or delete failure.");
+ }
+
+ for (int iterations=0; iterations<100; iterations++) {
+ String testString = basher.generateTestString();
+ boolean result = basher.Test3(testString);
+ if (result == false)
+ throw new RuntimeException("Insert, delete or replace failure.");
+ }
+
+ }
+
+ private int getRandomIndex(int constraint1, int constraint2) {
+ int range = constraint2 - constraint1;
+ int x = generator.nextInt();
+ return constraint1 + Math.abs(x % range);
+ }
+
+ static Random generator = new Random();
+
+ private String generateTestString() {
+ StringBuffer aNewString = new StringBuffer(120);
+ int aNewLength = getRandomIndex(1,100);
+ for(int y=0; y<aNewLength; y++) {
+ int achar = generator.nextInt();
+ char test = (char)(achar);
+ aNewString.append(test);
+ }
+ return aNewString.toString();
+ }
+
+ /**
+ * first test, get substring of a random string
+ * and replace same spot with same substring
+ * then check for equality with original
+ * this tests both substrings and the replace method
+ */
+ private boolean Test1(String before) {
+ StringBuffer bashed = new StringBuffer(before);
+ String slice;
+ for (int i=0; i<100; i++) {
+ int startIndex = getRandomIndex(0, before.length());
+ int endIndex = getRandomIndex(startIndex, before.length());
+ if (endIndex < bashed.length()) {
+ slice = bashed.substring(startIndex, endIndex);
+ }
+ else {
+ slice = bashed.substring(startIndex);
+ }
+ bashed.replace(startIndex, endIndex, slice);
+ }
+ String after = bashed.toString();
+ if (!before.equals(after))
+ return false;
+ else
+ return true;
+ }
+
+ /**
+ * second test, delete random section of string
+ * then insert same section back again
+ * then check for equality with original
+ * this tests both substrings, both deletes and insert method
+ */
+ private boolean Test2(String before) {
+ StringBuffer bashed = new StringBuffer(before);
+ String slice;
+ for (int i=0; i<100; i++) {
+ int startIndex = getRandomIndex(0, before.length());
+ int endIndex = getRandomIndex(startIndex, before.length());
+ if (endIndex < bashed.length())
+ slice = bashed.substring(startIndex, endIndex);
+ else
+ slice = bashed.substring(startIndex);
+ if (slice.length() == 1)
+ bashed.deleteCharAt(startIndex);
+ else
+ bashed.delete(startIndex, endIndex);
+ bashed.insert(startIndex, slice.toCharArray(), 0, slice.length());
+ }
+ String after = bashed.toString();
+ if (!before.equals(after))
+ return false;
+ else
+ return true;
+ }
+
+ /**
+ * Third test, clone string and make sure that the
+ * replace operation is equivalent to a delete followed
+ * by an insert with the equivalent arguments
+ * this tests replace, delete and insert
+ */
+ private boolean Test3(String before) {
+ StringBuffer bashed1 = new StringBuffer(before);
+ StringBuffer bashed2 = new StringBuffer(before);
+ int startIndex = getRandomIndex(0, bashed1.length());
+ int endIndex = getRandomIndex(startIndex, bashed2.length());
+
+ String insertString = generateTestString();
+
+ bashed1.delete(startIndex, endIndex);
+ bashed1.insert(startIndex, insertString);
+ bashed2.replace(startIndex, endIndex, insertString);
+
+ String result1 = bashed1.toString();
+ String result2 = bashed2.toString();
+ if (!result1.equals(result2))
+ return false;
+ else
+ return true;
+ }
+
+}
diff --git a/test/java/lang/StringBuffer/SetLength.java b/test/java/lang/StringBuffer/SetLength.java
new file mode 100644
index 0000000..557e234
--- /dev/null
+++ b/test/java/lang/StringBuffer/SetLength.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 1997 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ @bug 4089062
+ @summary A String created from a StringBuffer can be overwritten
+ if setLength() to a value less than the buffer length is called
+ on the StringBuffer and then the StringBuffer is appended to.
+ @author Robert Field
+*/
+
+public class SetLength {
+ public static void main(String[] argv) throws Exception {
+ StringBuffer active = new StringBuffer();
+ active.append("first one");
+ String a = active.toString();
+ active.setLength(0);
+ active.append("second");
+ String b = active.toString();
+ active.setLength(0);
+ System.out.println("first: " + a);
+ System.out.println("second: " + b);
+ if (!a.equals("first one")) {
+ throw new Exception("StringBuffer.setLength() overwrote string");
+ }
+ }
+}
diff --git a/test/java/lang/StringBuffer/Substring.java b/test/java/lang/StringBuffer/Substring.java
new file mode 100644
index 0000000..849cfef
--- /dev/null
+++ b/test/java/lang/StringBuffer/Substring.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 4853816
+ * @summary Test StringBuffer.substring(int)
+ */
+
+public class Substring {
+ public static void main(String[] args) {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("Guten Morgen!");
+ if (buffer.substring(0).length() != 13)
+ throw new RuntimeException();
+ }
+}
diff --git a/test/java/lang/StringBuffer/Supplementary.java b/test/java/lang/StringBuffer/Supplementary.java
new file mode 100644
index 0000000..0dd9dd6
--- /dev/null
+++ b/test/java/lang/StringBuffer/Supplementary.java
@@ -0,0 +1,417 @@
+/*
+ * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ *
+ * @test
+ * @bug 4533872 4915683 4985217 5017280
+ * @summary Unit tests for supplementary character support (JSR-204)
+ */
+
+public class Supplementary {
+
+ public static void main(String[] args) {
+ test1(); // Test for codePointAt(int index)
+ test2(); // Test for codePointBefore(int index)
+ test3(); // Test for reverse()
+ test4(); // Test for appendCodePoint(int codePoint)
+ test5(); // Test for codePointCount(int beginIndex, int endIndex)
+ test6(); // Test for offsetByCodePoints(int index, int offset)
+ }
+
+ /* Text strings which are used as input data.
+ * The comment above each text string means the index of each 16-bit char
+ * for convenience.
+ */
+ static final String[] input = {
+ /* 111 1 111111 22222
+ 0123 4 5678 9 012 3 456789 01234 */
+ "abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
+ /* 1 1111 1111 1 222
+ 0 12345 6789 0 1234 5678 9 012 */
+ "\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
+ /* 11 1 1111 1 112 222
+ 0 12345 6 78901 2 3456 7 890 123 */
+ "\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
+ /* 111 111111 1 22 2
+ 0 1 2345 678 9 012 345678 9 01 2 */
+ "\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",
+
+ // includes an undefined supprementary characters in Unicode 4.0.0
+ /* 1 11 1 1111 1
+ 0 1 2345 6 789 0 12 3 4567 8 */
+ "\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
+ };
+
+
+ /* Expected results for:
+ * test1(): for codePointAt()
+ *
+ * Each character in each array is the golden data for each text string
+ * in the above input data. For example, the first data in each array is
+ * for the first input string.
+ */
+ static final int[][] golden1 = {
+ {'a', 0xD800, 0xDC00, 0x10000, 0xE0200}, // codePointAt(0)
+ {0xD800, 0x10000, 'g', 0xDC00, 0xE0202}, // codePointAt(9)
+ {'f', 0xDC00, 0xD800, 0xDC00, 0xDE02}, // codePointAt(length-1)
+ };
+
+ /*
+ * Test for codePointAt(int index) method
+ */
+ static void test1() {
+
+ for (int i = 0; i < input.length; i++) {
+ StringBuffer sb = new StringBuffer(input[i]);
+
+ /*
+ * Normal case
+ */
+ testCodePoint(At, sb, 0, golden1[0][i]);
+ testCodePoint(At, sb, 9, golden1[1][i]);
+ testCodePoint(At, sb, sb.length()-1, golden1[2][i]);
+
+ /*
+ * Abnormal case - verify that an exception is thrown.
+ */
+ testCodePoint(At, sb, -1);
+ testCodePoint(At, sb, sb.length());
+ }
+ }
+
+
+ /* Expected results for:
+ * test2(): for codePointBefore()
+ *
+ * Each character in each array is the golden data for each text string
+ * in the above input data. For example, the first data in each array is
+ * for the first input string.
+ */
+ static final int[][] golden2 = {
+ {'a', 0xD800, 0xDC00, 0xD800, 0xDB40}, // codePointBefore(1)
+ {0xD800, 'l', 0x10000, 0xDC00, 0xDB40}, // codePointBefore(13)
+ {'f', 0xDC00, 0xD800, 0x10000, 0xE0202}, // codePointBefore(length)
+ };
+
+ /*
+ * Test for codePointBefore(int index) method
+ */
+ static void test2() {
+
+ for (int i = 0; i < input.length; i++) {
+ StringBuffer sb = new StringBuffer(input[i]);
+
+ /*
+ * Normal case
+ */
+ testCodePoint(Before, sb, 1, golden2[0][i]);
+ testCodePoint(Before, sb, 13, golden2[1][i]);
+ testCodePoint(Before, sb, sb.length(), golden2[2][i]);
+
+ /*
+ * Abnormal case - verify that an exception is thrown.
+ */
+ testCodePoint(Before, sb, 0);
+ testCodePoint(Before, sb, sb.length()+1);
+ }
+ }
+
+
+ /* Expected results for:
+ * test3(): for reverse()
+ *
+ * Unlike golden1 and golden2, each array is the golden data for each text
+ * string in the above input data. For example, the first array is for
+ * the first input string.
+ */
+ static final String[] golden3 = {
+ "fedcb\uDC00afedc\uD800\uDC00ba\uD800\uD800fed\uD800\uDC00cba",
+ "\uDC00tr\uD800\uDC00pon\uDC00mlk\uD800\uDC00jih\uD800gfed\uD800",
+ "\uD800on\uDC00ml\uDC00\uDC00ki9\uD800\uDC00hgfe\uDBFF\uDFFFdcba\uDC00",
+ "\uD800\uDC00@\\\uDC00^=;><\uDC00+;\uD800\uDC00&%\uD800$#!\uD800\uDC00",
+
+ // includes an undefined supprementary characters in Unicode 4.0.0
+ "\uDB40\uDE02ihg\uDB40\uDE03f\uDB40\uDE02ed\uDB40\uDE01cba\uDB40\uDE00",
+ };
+
+ // Additional input data & expected result for test3()
+ static final String[][] testdata1 = {
+ {"a\uD800\uDC00", "\uD800\uDC00a"},
+ {"a\uDC00\uD800", "\uD800\uDC00a"},
+ {"\uD800\uDC00a", "a\uD800\uDC00"},
+ {"\uDC00\uD800a", "a\uD800\uDC00"},
+ {"\uDC00\uD800\uD801", "\uD801\uD800\uDC00"},
+ {"\uDC00\uD800\uDC01", "\uD800\uDC01\uDC00"},
+ {"\uD801\uD800\uDC00", "\uD800\uDC00\uD801"},
+ {"\uD800\uDC01\uDC00", "\uDC00\uD800\uDC01"},
+ {"\uD800\uDC00\uDC01\uD801", "\uD801\uDC01\uD800\uDC00"},
+ };
+
+ /*
+ * Test for reverse() method
+ */
+ static void test3() {
+ for (int i = 0; i < input.length; i++) {
+ StringBuffer sb = new StringBuffer(input[i]).reverse();
+
+ check(!golden3[i].equals(new String(sb)),
+ "reverse() for <" + toHexString(input[i]) + ">",
+ sb, golden3[i]);
+ }
+
+ for (int i = 0; i < testdata1.length; i++) {
+ StringBuffer sb = new StringBuffer(testdata1[i][0]).reverse();
+
+ check(!testdata1[i][1].equals(new String(sb)),
+ "reverse() for <" + toHexString(testdata1[i][0]) + ">",
+ sb, testdata1[i][1]);
+ }
+ }
+
+ /**
+ * Test for appendCodePoint() method
+ */
+ static void test4() {
+ for (int i = 0; i < input.length; i++) {
+ String s = input[i];
+ StringBuffer sb = new StringBuffer();
+ int c;
+ for (int j = 0; j < s.length(); j += Character.charCount(c)) {
+ c = s.codePointAt(j);
+ StringBuffer rsb = sb.appendCodePoint(c);
+ check(sb != rsb, "appendCodePoint returned a wrong object");
+ int sbc = sb.codePointAt(j);
+ check(sbc != c, "appendCodePoint(j) != c", sbc, c);
+ }
+ check(!s.equals(sb.toString()),
+ "appendCodePoint() produced a wrong result with input["+i+"]");
+ }
+
+ // test exception
+ testAppendCodePoint(-1, IllegalArgumentException.class);
+ testAppendCodePoint(Character.MAX_CODE_POINT+1, IllegalArgumentException.class);
+ }
+
+ /**
+ * Test codePointCount(int, int)
+ *
+ * This test case assumes that
+ * Character.codePointCount(CharSequence, int, int) works
+ * correctly.
+ */
+ static void test5() {
+ for (int i = 0; i < input.length; i++) {
+ String s = input[i];
+ StringBuffer sb = new StringBuffer(s);
+ int length = sb.length();
+ for (int j = 0; j <= length; j++) {
+ int result = sb.codePointCount(j, length);
+ int expected = Character.codePointCount(sb, j, length);
+ check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
+ result, expected);
+ }
+ for (int j = length; j >= 0; j--) {
+ int result = sb.codePointCount(0, j);
+ int expected = Character.codePointCount(sb, 0, j);
+ check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
+ result, expected);
+ }
+
+ // test exceptions
+ testCodePointCount(null, 0, 0, NullPointerException.class);
+ testCodePointCount(sb, -1, length, IndexOutOfBoundsException.class);
+ testCodePointCount(sb, 0, length+1, IndexOutOfBoundsException.class);
+ testCodePointCount(sb, length, length-1, IndexOutOfBoundsException.class);
+ }
+ }
+
+ /**
+ * Test offsetByCodePoints(int, int)
+ *
+ * This test case assumes that
+ * Character.codePointCount(CharSequence, int, int) works
+ * correctly.
+ */
+ static void test6() {
+ for (int i = 0; i < input.length; i++) {
+ String s = input[i];
+ StringBuffer sb = new StringBuffer(s);
+ int length = s.length();
+ for (int j = 0; j <= length; j++) {
+ int nCodePoints = Character.codePointCount(sb, j, length);
+ int result = sb.offsetByCodePoints(j, nCodePoints);
+ check(result != length,
+ "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
+ result, length);
+ result = sb.offsetByCodePoints(length, -nCodePoints);
+ int expected = j;
+ if (j > 0 && j < length) {
+ int cp = sb.codePointBefore(j+1);
+ if (Character.isSupplementaryCodePoint(cp)) {
+ expected--;
+ }
+ }
+ check(result != expected,
+ "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
+ result, expected);
+ }
+ for (int j = length; j >= 0; j--) {
+ int nCodePoints = Character.codePointCount(sb, 0, j);
+ int result = sb.offsetByCodePoints(0, nCodePoints);
+ int expected = j;
+ if (j > 0 && j < length) {
+ int cp = sb.codePointAt(j-1);
+ if (Character.isSupplementaryCodePoint(cp)) {
+ expected++;
+ }
+ }
+ check(result != expected,
+ "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
+ result, expected);
+ result = sb.offsetByCodePoints(j, -nCodePoints);
+ check(result != 0,
+ "offsetBycodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
+ result, 0);
+ }
+
+ // test exceptions
+ testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
+ testOffsetByCodePoints(sb, -1, length, IndexOutOfBoundsException.class);
+ testOffsetByCodePoints(sb, 0, length+1, IndexOutOfBoundsException.class);
+ testOffsetByCodePoints(sb, 1, -2, IndexOutOfBoundsException.class);
+ testOffsetByCodePoints(sb, length, length-1, IndexOutOfBoundsException.class);
+ testOffsetByCodePoints(sb, length, -(length+1), IndexOutOfBoundsException.class);
+ }
+ }
+
+
+ static final boolean At = true, Before = false;
+
+ static void testCodePoint(boolean isAt, StringBuffer sb, int index, int expected) {
+ int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
+
+ check(c != expected,
+ "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
+ + sb + ">", c, expected);
+ }
+
+ static void testCodePoint(boolean isAt, StringBuffer sb, int index) {
+ boolean exceptionOccurred = false;
+
+ try {
+ int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
+ }
+ catch (StringIndexOutOfBoundsException e) {
+ exceptionOccurred = true;
+ }
+ check(!exceptionOccurred,
+ "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
+ + sb + "> should throw StringIndexOutOfBoundsPointerException.");
+ }
+
+ static void testAppendCodePoint(int codePoint, Class expectedException) {
+ try {
+ new StringBuffer().appendCodePoint(codePoint);
+ } catch (Exception e) {
+ if (expectedException.isInstance(e)) {
+ return;
+ }
+ throw new RuntimeException("Error: Unexpected exception", e);
+ }
+ check(true, "appendCodePoint(" + toHexString(codePoint) + ") didn't throw "
+ + expectedException.getName());
+ }
+
+ static void testCodePointCount(StringBuffer sb, int beginIndex, int endIndex,
+ Class expectedException) {
+ try {
+ int n = sb.codePointCount(beginIndex, endIndex);
+ } catch (Exception e) {
+ if (expectedException.isInstance(e)) {
+ return;
+ }
+ throw new RuntimeException("Error: Unexpected exception", e);
+ }
+ check(true, "codePointCount() didn't throw " + expectedException.getName());
+ }
+
+ static void testOffsetByCodePoints(StringBuffer sb, int index, int offset,
+ Class expectedException) {
+ try {
+ int n = sb.offsetByCodePoints(index, offset);
+ } catch (Exception e) {
+ if (expectedException.isInstance(e)) {
+ return;
+ }
+ throw new RuntimeException("Error: Unexpected exception", e);
+ }
+ check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
+ }
+
+ static void check(boolean err, String msg) {
+ if (err) {
+ throw new RuntimeException("Error: " + msg);
+ }
+ }
+
+ static void check(boolean err, String s, int got, int expected) {
+ if (err) {
+ throw new RuntimeException("Error: " + s
+ + " returned an unexpected value. got "
+ + toHexString(got)
+ + ", expected "
+ + toHexString(expected));
+ }
+ }
+
+ static void check(boolean err, String s, StringBuffer got, String expected) {
+ if (err) {
+ throw new RuntimeException("Error: " + s
+ + " returned an unexpected value. got <"
+ + toHexString(new String(got))
+ + ">, expected <"
+ + toHexString(expected)
+ + ">");
+ }
+ }
+
+ private static String toHexString(int c) {
+ return "0x" + Integer.toHexString(c);
+ }
+
+ private static String toHexString(String s) {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+
+ sb.append(" 0x");
+ if (c < 0x10) sb.append('0');
+ if (c < 0x100) sb.append('0');
+ if (c < 0x1000) sb.append('0');
+ sb.append(Integer.toHexString(c));
+ }
+ sb.append(' ');
+ return sb.toString();
+ }
+}
diff --git a/test/java/lang/StringBuffer/Trim.java b/test/java/lang/StringBuffer/Trim.java
new file mode 100644
index 0000000..c3fd831
--- /dev/null
+++ b/test/java/lang/StringBuffer/Trim.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 4546734 5007612
+ * @summary Test StringBuffer.trimToSize
+ */
+
+import java.util.Random;
+
+public class Trim {
+ private static Random generator = new Random();
+
+ public static void main(String[] args) throws Exception {
+ bash();
+ //capacityCheck();
+ }
+
+ // Make sure trimToSize is safe to use; it should never cause an
+ // exception or mutation
+ private static void bash() throws Exception {
+ for (int i=0; i<1000; i++) {
+ StringBuffer sb1 = generateTestBuffer(0, 100);
+ StringBuffer sb2 = new StringBuffer(sb1);
+ sb1.trimToSize();
+ if (!sb1.toString().equals(sb2.toString()))
+ throw new RuntimeException(
+ "trim mutated stringbuffer contents");
+ // Append a random sb
+ StringBuffer sb3 = generateTestBuffer(0, 100);
+ sb1.append(sb3);
+ sb2.append(sb3);
+ if (generator.nextInt(2) == 0)
+ sb1.trimToSize();
+ else
+ sb2.trimToSize();
+ if (!sb1.toString().equals(sb2.toString()))
+ throw new RuntimeException(
+ "trim mutated stringbuffer contents");
+ // Append sb with lots of extra space
+ sb3 = new StringBuffer(100);
+ sb3.append("a");
+ sb1.append(sb3);
+ sb2.append(sb3);
+ if (generator.nextInt(2) == 0)
+ sb1.trimToSize();
+ else
+ sb2.trimToSize();
+ if (!sb1.toString().equals(sb2.toString()))
+ throw new RuntimeException(
+ "trim mutated stringbuffer contents");
+ }
+ }
+
+ // This test gives some assurance that trimToSize is working but
+ // it should not be part of an automated run, and a failure here
+ // is not against spec; this method is provided simply to be run
+ // by hand and assure the engineer that it is working. The test
+ // may stop working at some time in the future depending on
+ // how String and StringBuffer are implemented because it depends
+ // upon the capacity method.
+ private static void capacityCheck() {
+ for (int i=0; i<100; i++) {
+ int sizeNeeded = generator.nextInt(1000)+1;
+ int sizeExtra = generator.nextInt(100) + 1;
+ StringBuffer sb = new StringBuffer(sizeNeeded + sizeExtra);
+ StringBuffer sb2 = generateTestBuffer(sizeNeeded, sizeNeeded);
+ if (sb2.length() != sizeNeeded)
+ throw new RuntimeException("sb generated incorrectly");
+ sb.append(sb2);
+ int oldCapacity = sb.capacity();
+ sb.trimToSize();
+ int newCapacity = sb.capacity();
+ if (oldCapacity == newCapacity)
+ throw new RuntimeException("trim failed");
+ }
+ }
+
+ private static int getRandomIndex(int constraint1, int constraint2) {
+ int range = constraint2 - constraint1;
+ if (range <= 0)
+ return constraint1;
+ int x = generator.nextInt(range);
+ return constraint1 + x;
+ }
+
+ private static StringBuffer generateTestBuffer(int min, int max) {
+ StringBuffer aNewStringBuffer = new StringBuffer();
+ int aNewLength = getRandomIndex(min, max);
+ for(int y=0; y<aNewLength; y++) {
+ int achar = generator.nextInt(30)+30;
+ char test = (char)(achar);
+ aNewStringBuffer.append(test);
+ }
+ return aNewStringBuffer;
+ }
+
+}