Path: blob/master/test/jdk/java/lang/StringBuilder/AppendStringBuffer.java
41152 views
/*1* Copyright (c) 2012, 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 620678025* @summary Test StringBuilder.append(StringBuffer);26* @key randomness27*/2829import java.util.Random;3031public class AppendStringBuffer {32private static Random generator = new Random();3334public static void main(String[] args) throws Exception {35for (int i=0; i<1000; i++) {36StringBuffer sb1 = generateTestBuffer(10, 100);37StringBuffer sb2 = generateTestBuffer(10, 100);38StringBuffer sb3 = generateTestBuffer(10, 100);39String s1 = sb1.toString();40String s2 = sb2.toString();41String s3 = sb3.toString();4243String concatResult = new String(s1+s2+s3);4445StringBuilder test = new StringBuilder();46test.append(sb1);47test.append(sb2);48test.append(sb3);4950if (!test.toString().equals(concatResult))51throw new RuntimeException("StringBuffer.append failure");52}53}5455private static int getRandomIndex(int constraint1, int constraint2) {56int range = constraint2 - constraint1;57int x = generator.nextInt(range);58return constraint1 + x;59}6061private static StringBuffer generateTestBuffer(int min, int max) {62StringBuffer aNewStringBuffer = new StringBuffer(120);63int aNewLength = getRandomIndex(min, max);64for(int y=0; y<aNewLength; y++) {65int achar = generator.nextInt(30)+30;66char test = (char)(achar);67aNewStringBuffer.append(test);68}69return aNewStringBuffer;70}71}727374