author | chegar |
Tue, 09 Aug 2011 16:39:04 +0100 | |
changeset 10150 | dee4cb73adc3 |
parent 6374 | e214162c907e |
child 30046 | cf2c86e1819e |
permissions | -rw-r--r-- |
5603
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
1 |
/* |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
2 |
* Copyright 2010 Google Inc. All Rights Reserved. |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
4 |
* |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
8 |
* |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
13 |
* accompanied this code). |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
14 |
* |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
18 |
* |
6374
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5603
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5603
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5603
diff
changeset
|
21 |
* questions. |
5603
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
22 |
*/ |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
23 |
|
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
24 |
/* |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
25 |
* @test |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
26 |
* @bug 6952330 |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
27 |
* @summary Test StringBuffer/StringBuilder capacity handling. |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
28 |
*/ |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
29 |
|
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
30 |
import java.util.Random; |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
31 |
|
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
32 |
public class Capacity { |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
33 |
void test(String[] args) throws Throwable { |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
34 |
Random rnd = new Random(); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
35 |
int[] sizes = { 0, 1, rnd.nextInt(10), rnd.nextInt(1000) }; |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
36 |
for (int size : sizes) { |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
37 |
equal(16, new StringBuffer().capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
38 |
equal(16, new StringBuilder().capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
39 |
StringBuffer buff = new StringBuffer(size); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
40 |
StringBuilder bild = new StringBuilder(size); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
41 |
equal(size, buff.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
42 |
equal(size, bild.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
43 |
buff.ensureCapacity(size); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
44 |
bild.ensureCapacity(size); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
45 |
equal(size, buff.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
46 |
equal(size, bild.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
47 |
buff.ensureCapacity(size+1); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
48 |
bild.ensureCapacity(size+1); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
49 |
equal(size*2+2, buff.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
50 |
equal(size*2+2, bild.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
51 |
size = buff.capacity(); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
52 |
buff.ensureCapacity(size*2+1); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
53 |
bild.ensureCapacity(size*2+1); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
54 |
equal(size*2+2, buff.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
55 |
equal(size*2+2, bild.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
56 |
size = buff.capacity(); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
57 |
int newSize = size * 2 + 3; |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
58 |
buff.ensureCapacity(newSize); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
59 |
bild.ensureCapacity(newSize); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
60 |
equal(newSize, buff.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
61 |
equal(newSize, bild.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
62 |
buff.ensureCapacity(0); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
63 |
bild.ensureCapacity(0); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
64 |
equal(newSize, buff.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
65 |
equal(newSize, bild.capacity()); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
66 |
} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
67 |
} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
68 |
|
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
69 |
//--------------------- Infrastructure --------------------------- |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
70 |
volatile int passed = 0, failed = 0; |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
71 |
void pass() {passed++;} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
72 |
void fail() {failed++; Thread.dumpStack();} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
73 |
void fail(String msg) {System.err.println(msg); fail();} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
74 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
75 |
void check(boolean cond) {if (cond) pass(); else fail();} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
76 |
void equal(Object x, Object y) { |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
77 |
if (x == null ? y == null : x.equals(y)) pass(); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
78 |
else fail(x + " not equal to " + y);} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
79 |
public static void main(String[] args) throws Throwable { |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
80 |
new Capacity().instanceMain(args);} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
81 |
public void instanceMain(String[] args) throws Throwable { |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
82 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
83 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
84 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
682e3deac7ce
6952330: Fix for 6933217 broke contract of StringBuffer.ensureCapacity
martin
parents:
diff
changeset
|
85 |
} |