J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4546734 5007612 |
| 26 | * @summary Test StringBuffer.trimToSize |
| 27 | */ |
| 28 | |
| 29 | import java.util.Random; |
| 30 | |
| 31 | public class Trim { |
| 32 | private static Random generator = new Random(); |
| 33 | |
| 34 | public static void main(String[] args) throws Exception { |
| 35 | bash(); |
| 36 | //capacityCheck(); |
| 37 | } |
| 38 | |
| 39 | // Make sure trimToSize is safe to use; it should never cause an |
| 40 | // exception or mutation |
| 41 | private static void bash() throws Exception { |
| 42 | for (int i=0; i<1000; i++) { |
| 43 | StringBuffer sb1 = generateTestBuffer(0, 100); |
| 44 | StringBuffer sb2 = new StringBuffer(sb1); |
| 45 | sb1.trimToSize(); |
| 46 | if (!sb1.toString().equals(sb2.toString())) |
| 47 | throw new RuntimeException( |
| 48 | "trim mutated stringbuffer contents"); |
| 49 | // Append a random sb |
| 50 | StringBuffer sb3 = generateTestBuffer(0, 100); |
| 51 | sb1.append(sb3); |
| 52 | sb2.append(sb3); |
| 53 | if (generator.nextInt(2) == 0) |
| 54 | sb1.trimToSize(); |
| 55 | else |
| 56 | sb2.trimToSize(); |
| 57 | if (!sb1.toString().equals(sb2.toString())) |
| 58 | throw new RuntimeException( |
| 59 | "trim mutated stringbuffer contents"); |
| 60 | // Append sb with lots of extra space |
| 61 | sb3 = new StringBuffer(100); |
| 62 | sb3.append("a"); |
| 63 | sb1.append(sb3); |
| 64 | sb2.append(sb3); |
| 65 | if (generator.nextInt(2) == 0) |
| 66 | sb1.trimToSize(); |
| 67 | else |
| 68 | sb2.trimToSize(); |
| 69 | if (!sb1.toString().equals(sb2.toString())) |
| 70 | throw new RuntimeException( |
| 71 | "trim mutated stringbuffer contents"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // This test gives some assurance that trimToSize is working but |
| 76 | // it should not be part of an automated run, and a failure here |
| 77 | // is not against spec; this method is provided simply to be run |
| 78 | // by hand and assure the engineer that it is working. The test |
| 79 | // may stop working at some time in the future depending on |
| 80 | // how String and StringBuffer are implemented because it depends |
| 81 | // upon the capacity method. |
| 82 | private static void capacityCheck() { |
| 83 | for (int i=0; i<100; i++) { |
| 84 | int sizeNeeded = generator.nextInt(1000)+1; |
| 85 | int sizeExtra = generator.nextInt(100) + 1; |
| 86 | StringBuffer sb = new StringBuffer(sizeNeeded + sizeExtra); |
| 87 | StringBuffer sb2 = generateTestBuffer(sizeNeeded, sizeNeeded); |
| 88 | if (sb2.length() != sizeNeeded) |
| 89 | throw new RuntimeException("sb generated incorrectly"); |
| 90 | sb.append(sb2); |
| 91 | int oldCapacity = sb.capacity(); |
| 92 | sb.trimToSize(); |
| 93 | int newCapacity = sb.capacity(); |
| 94 | if (oldCapacity == newCapacity) |
| 95 | throw new RuntimeException("trim failed"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private static int getRandomIndex(int constraint1, int constraint2) { |
| 100 | int range = constraint2 - constraint1; |
| 101 | if (range <= 0) |
| 102 | return constraint1; |
| 103 | int x = generator.nextInt(range); |
| 104 | return constraint1 + x; |
| 105 | } |
| 106 | |
| 107 | private static StringBuffer generateTestBuffer(int min, int max) { |
| 108 | StringBuffer aNewStringBuffer = new StringBuffer(); |
| 109 | int aNewLength = getRandomIndex(min, max); |
| 110 | for(int y=0; y<aNewLength; y++) { |
| 111 | int achar = generator.nextInt(30)+30; |
| 112 | char test = (char)(achar); |
| 113 | aNewStringBuffer.append(test); |
| 114 | } |
| 115 | return aNewStringBuffer; |
| 116 | } |
| 117 | |
| 118 | } |