1 /* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4546734 5007612 26 * @summary Test StringBuffer.trimToSize 27 * @key randomness 28 */ 29 30 package test.java.lang.StringBuffer; 31 32 import java.util.Random; 33 import org.testng.annotations.Test; 34 35 public class Trim { 36 private static Random generator = new Random(); 37 38 // Android-changed: Add @Test annotation and remove empty arguments. 39 // public static void main(String[] args) throws Exception { 40 @Test main()41 public static void main() throws Exception { 42 bash(); 43 //capacityCheck(); 44 } 45 46 // Make sure trimToSize is safe to use; it should never cause an 47 // exception or mutation bash()48 private static void bash() throws Exception { 49 for (int i=0; i<1000; i++) { 50 StringBuffer sb1 = generateTestBuffer(0, 100); 51 StringBuffer sb2 = new StringBuffer(sb1); 52 sb1.trimToSize(); 53 if (!sb1.toString().equals(sb2.toString())) 54 throw new RuntimeException( 55 "trim mutated stringbuffer contents"); 56 // Append a random sb 57 StringBuffer sb3 = generateTestBuffer(0, 100); 58 sb1.append(sb3); 59 sb2.append(sb3); 60 if (generator.nextInt(2) == 0) 61 sb1.trimToSize(); 62 else 63 sb2.trimToSize(); 64 if (!sb1.toString().equals(sb2.toString())) 65 throw new RuntimeException( 66 "trim mutated stringbuffer contents"); 67 // Append sb with lots of extra space 68 sb3 = new StringBuffer(100); 69 sb3.append("a"); 70 sb1.append(sb3); 71 sb2.append(sb3); 72 if (generator.nextInt(2) == 0) 73 sb1.trimToSize(); 74 else 75 sb2.trimToSize(); 76 if (!sb1.toString().equals(sb2.toString())) 77 throw new RuntimeException( 78 "trim mutated stringbuffer contents"); 79 } 80 } 81 82 // This test gives some assurance that trimToSize is working but 83 // it should not be part of an automated run, and a failure here 84 // is not against spec; this method is provided simply to be run 85 // by hand and assure the engineer that it is working. The test 86 // may stop working at some time in the future depending on 87 // how String and StringBuffer are implemented because it depends 88 // upon the capacity method. capacityCheck()89 private static void capacityCheck() { 90 for (int i=0; i<100; i++) { 91 int sizeNeeded = generator.nextInt(1000)+1; 92 int sizeExtra = generator.nextInt(100) + 1; 93 StringBuffer sb = new StringBuffer(sizeNeeded + sizeExtra); 94 StringBuffer sb2 = generateTestBuffer(sizeNeeded, sizeNeeded); 95 if (sb2.length() != sizeNeeded) 96 throw new RuntimeException("sb generated incorrectly"); 97 sb.append(sb2); 98 int oldCapacity = sb.capacity(); 99 sb.trimToSize(); 100 int newCapacity = sb.capacity(); 101 if (oldCapacity == newCapacity) 102 throw new RuntimeException("trim failed"); 103 } 104 } 105 getRandomIndex(int constraint1, int constraint2)106 private static int getRandomIndex(int constraint1, int constraint2) { 107 int range = constraint2 - constraint1; 108 if (range <= 0) 109 return constraint1; 110 int x = generator.nextInt(range); 111 return constraint1 + x; 112 } 113 generateTestBuffer(int min, int max)114 private static StringBuffer generateTestBuffer(int min, int max) { 115 StringBuffer aNewStringBuffer = new StringBuffer(); 116 int aNewLength = getRandomIndex(min, max); 117 for(int y=0; y<aNewLength; y++) { 118 int achar = generator.nextInt(30)+30; 119 char test = (char)(achar); 120 aNewStringBuffer.append(test); 121 } 122 return aNewStringBuffer; 123 } 124 125 } 126