Path: blob/master/test/jdk/java/lang/StringBuffer/Trim.java
41149 views
/*1* Copyright (c) 2003, 2004, 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 4546734 500761225* @summary Test StringBuffer.trimToSize26* @key randomness27*/2829import java.util.Random;3031public class Trim {32private static Random generator = new Random();3334public static void main(String[] args) throws Exception {35bash();36//capacityCheck();37}3839// Make sure trimToSize is safe to use; it should never cause an40// exception or mutation41private static void bash() throws Exception {42for (int i=0; i<1000; i++) {43StringBuffer sb1 = generateTestBuffer(0, 100);44StringBuffer sb2 = new StringBuffer(sb1);45sb1.trimToSize();46if (!sb1.toString().equals(sb2.toString()))47throw new RuntimeException(48"trim mutated stringbuffer contents");49// Append a random sb50StringBuffer sb3 = generateTestBuffer(0, 100);51sb1.append(sb3);52sb2.append(sb3);53if (generator.nextInt(2) == 0)54sb1.trimToSize();55else56sb2.trimToSize();57if (!sb1.toString().equals(sb2.toString()))58throw new RuntimeException(59"trim mutated stringbuffer contents");60// Append sb with lots of extra space61sb3 = new StringBuffer(100);62sb3.append("a");63sb1.append(sb3);64sb2.append(sb3);65if (generator.nextInt(2) == 0)66sb1.trimToSize();67else68sb2.trimToSize();69if (!sb1.toString().equals(sb2.toString()))70throw new RuntimeException(71"trim mutated stringbuffer contents");72}73}7475// This test gives some assurance that trimToSize is working but76// it should not be part of an automated run, and a failure here77// is not against spec; this method is provided simply to be run78// by hand and assure the engineer that it is working. The test79// may stop working at some time in the future depending on80// how String and StringBuffer are implemented because it depends81// upon the capacity method.82private static void capacityCheck() {83for (int i=0; i<100; i++) {84int sizeNeeded = generator.nextInt(1000)+1;85int sizeExtra = generator.nextInt(100) + 1;86StringBuffer sb = new StringBuffer(sizeNeeded + sizeExtra);87StringBuffer sb2 = generateTestBuffer(sizeNeeded, sizeNeeded);88if (sb2.length() != sizeNeeded)89throw new RuntimeException("sb generated incorrectly");90sb.append(sb2);91int oldCapacity = sb.capacity();92sb.trimToSize();93int newCapacity = sb.capacity();94if (oldCapacity == newCapacity)95throw new RuntimeException("trim failed");96}97}9899private static int getRandomIndex(int constraint1, int constraint2) {100int range = constraint2 - constraint1;101if (range <= 0)102return constraint1;103int x = generator.nextInt(range);104return constraint1 + x;105}106107private static StringBuffer generateTestBuffer(int min, int max) {108StringBuffer aNewStringBuffer = new StringBuffer();109int aNewLength = getRandomIndex(min, max);110for(int y=0; y<aNewLength; y++) {111int achar = generator.nextInt(30)+30;112char test = (char)(achar);113aNewStringBuffer.append(test);114}115return aNewStringBuffer;116}117118}119120121