Path: blob/master/test/jdk/java/lang/StringBuffer/AppendCharSequence.java
41149 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4812591 4705328 5019111 821822825* @summary Test append and insert methods with CharSequence params26* @key randomness27*/2829import java.util.Random;3031public class AppendCharSequence {32private static Random generator = new Random();3334public static void main(String[] args) throws Exception {35bash();36checkNulls();37checkOffsets();38checkConstructor();39}4041// Sanity test of contents42private static void bash() throws Exception {43for (int i=0; i<1000; i++) {44StringBuffer sb1 = generateTestBuffer(0, 100);45StringBuffer sb2 = generateTestBuffer(0, 100);46StringBuffer sb3 = generateTestBuffer(0, 100);47StringBuffer sb4 = generateTestBuffer(0, 100);48StringBuffer sb5 = new StringBuffer();4950String s1 = sb1.toString();51String s2 = sb2.toString();52String s3 = sb3.toString();53String s4 = sb4.toString();54String s5 = null;5556// append(CharSequence cs)57sb5.append((CharSequence)sb1);58s5 = sb1.toString();5960if (!sb5.toString().equals(s5))61throw new RuntimeException("StringBuffer.append failure 1");6263// append (CharSequence cs, int start, int end)64int index = generator.nextInt(100);65int len = generator.nextInt(100);66while (index > sb2.length() - len) {67index = generator.nextInt(100);68len = generator.nextInt(100);69}70sb5.append((CharSequence)sb2, index, index + len);71s5 = s5 + sb2.toString().substring(index, index + len);7273if (!sb5.toString().equals(s5))74throw new RuntimeException("StringBuffer.append failure 2");7576// insert(int dstOffset, CharSequence cs)77index = generator.nextInt(100);78while (index > s5.length()) {79index = generator.nextInt(100);80}81sb5.insert(index, (CharSequence)sb3);82s5 = new StringBuffer(s5).insert(index, sb3).toString();8384if (!sb5.toString().equals(s5))85throw new RuntimeException("StringBuffer.insert failure 1");8687// insert(int dstOffset, CharSequence s, int start, int end)88int index1 = generator.nextInt(100);89while (index1 > s5.length()) {90index1 = generator.nextInt(100);91}92int index2 = generator.nextInt(100);93len = generator.nextInt(100);94while (index2 > sb4.length() - len) {95index2 = generator.nextInt(100);96len = generator.nextInt(100);97}98sb5.insert(index1, (CharSequence)sb4, index2, index2 + len);99s5 = new StringBuffer(s5).insert(index1, s4.toCharArray(),100index2, len).toString();101102if (!sb5.toString().equals(s5))103throw new RuntimeException("StringBuffer.insert failure 2");104}105}106107private static int getRandomIndex(int constraint1, int constraint2) {108int range = constraint2 - constraint1;109int x = generator.nextInt(range);110return constraint1 + x;111}112113private static StringBuffer generateTestBuffer(int min, int max) {114StringBuffer aNewStringBuffer = new StringBuffer();115int aNewLength = getRandomIndex(min, max);116for(int y=0; y<aNewLength; y++) {117int achar = generator.nextInt(30)+30;118char test = (char)(achar);119aNewStringBuffer.append(test);120}121return aNewStringBuffer;122}123124// Check handling of null as "null"125private static void checkNulls() throws Exception {126StringBuffer sb1 = new StringBuffer();127CharSequence cs = null;128sb1.append("test");129sb1.append(cs);130if (!sb1.toString().equals("testnull"))131throw new RuntimeException("StringBuffer.append failure 3");132133sb1 = new StringBuffer();134sb1.append("test", 0, 2);135sb1.append(cs, 0, 2);136if (!sb1.toString().equals("tenu"))137throw new RuntimeException("StringBuffer.append failure 4");138139sb1 = new StringBuffer("test");140sb1.insert(2, cs);141if (!sb1.toString().equals("tenullst"))142throw new RuntimeException("StringBuffer.insert failure 3");143144sb1 = new StringBuffer("test");145sb1.insert(2, cs, 0, 2);146if (!sb1.toString().equals("tenust"))147throw new RuntimeException("StringBuffer.insert failure 4");148}149150// Test the bounds checking151private static void checkOffsets() throws Exception {152153// append (CharSeqeunce cs, int start, int end)154for (int i=0; i<100; i++) {155StringBuffer sb = generateTestBuffer(0, 80);156CharSequence cs = (CharSequence)generateTestBuffer(0, 80);157int index = 0;158int len = 0;159while (index <= cs.length() - len) {160index = generator.nextInt(100) - 50;161len = generator.nextInt(100) - 50;162if (index < 0)163break;164if (len < 0)165break;166}167try {168sb.append(cs, index, index + len);169throw new RuntimeException("Append bounds checking failure");170} catch (IndexOutOfBoundsException e) {171// Correct result172}173}174175// insert(int dstOffset, CharSequence cs)176for (int i=0; i<100; i++) {177StringBuffer sb = new StringBuffer("test1");178CharSequence cs = (CharSequence)new StringBuffer("test2");179int index = 0;180while (index <= sb.length()) {181index = generator.nextInt(100) - 50;182if (index < 0)183break;184}185try {186sb.insert(index, cs);187throw new RuntimeException("Insert bounds checking failure");188} catch (IndexOutOfBoundsException e) {189// Correct result190}191}192193// insert(int dstOffset, CharSequence s, int start, int end)194for (int i=0; i<100; i++) {195StringBuffer sb = new StringBuffer("test1");196CharSequence cs = (CharSequence)new StringBuffer("test2");197int index1 = 0;198while (index1 <= sb.length()) {199index1 = generator.nextInt(100) - 50;200if (index1 < 0)201break;202}203int index2 = 0;204int len = 0;205while (index2 < sb.length() - len) {206index2 = generator.nextInt(100) - 50;207len = generator.nextInt(100) - 50;208if (index2 < 0)209break;210if (len < 0)211break;212}213try {214sb.insert(index1, cs, index2, index2 + len);215throw new RuntimeException("Insert bounds checking failure");216} catch (IndexOutOfBoundsException e) {217// Correct result218}219}220}221222// Test the CharSequence constructor223private static void checkConstructor() throws Exception {224for (int i=0; i<100; i++) {225StringBuffer sb = generateTestBuffer(0, 100);226CharSequence cs = (CharSequence)sb;227StringBuffer sb2 = new StringBuffer(cs);228if (!sb.toString().equals(sb2.toString())) {229throw new RuntimeException("CharSequence constructor failure");230}231}232checkNegativeLenCharSeq(-1);233checkNegativeLenCharSeq(-16);234checkNegativeLenCharSeq(-17);235checkNegativeLenCharSeq(Integer.MIN_VALUE);236}237238// Test constructing from CharSequence of negative length239private static void checkNegativeLenCharSeq(int len) {240try {241CharSequence seq = new MyNegativeLenCharSeq(len);242StringBuffer sb = new StringBuffer(seq);243} catch (NegativeArraySizeException expected) {244} catch (Throwable exc) {245throw new RuntimeException("Unexpected: " + exc, exc);246}247}248249private static class MyNegativeLenCharSeq implements CharSequence {250int length;251MyNegativeLenCharSeq(int length) {252this.length = length;253}254public char charAt(int i) {255throw new UnsupportedOperationException();256}257public int length() { return length; }258public CharSequence subSequence(int st, int e) {259throw new UnsupportedOperationException();260}261public String toString() { return ""; }262}263}264265266