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