1 /* 2 * Copyright 2010 Google 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 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 /* 25 * @test 26 * @bug 6952330 27 * @summary Test StringBuffer/StringBuilder capacity handling. 28 * @key randomness 29 */ 30 31 import java.util.Random; 32 33 public class Capacity { 34 void test(String[] args) throws Throwable { 35 Random rnd = new Random(); 36 int[] sizes = { 0, 1, rnd.nextInt(10), rnd.nextInt(1000) }; 37 for (int size : sizes) { 38 equal(16, new StringBuffer().capacity()); 39 equal(16, new StringBuilder().capacity()); 40 StringBuffer buff = new StringBuffer(size); 41 StringBuilder bild = new StringBuilder(size); 42 equal(size, buff.capacity()); 43 equal(size, bild.capacity()); 44 buff.ensureCapacity(size); 45 bild.ensureCapacity(size); 46 equal(size, buff.capacity()); 47 equal(size, bild.capacity()); 48 buff.ensureCapacity(size+1); 49 bild.ensureCapacity(size+1); 50 equal(size*2+2, buff.capacity()); 51 equal(size*2+2, bild.capacity()); 52 size = buff.capacity(); 53 buff.ensureCapacity(size*2+1); 54 bild.ensureCapacity(size*2+1); 55 equal(size*2+2, buff.capacity()); 56 equal(size*2+2, bild.capacity()); 57 size = buff.capacity(); 58 int newSize = size * 2 + 3; 59 buff.ensureCapacity(newSize); 60 bild.ensureCapacity(newSize); 61 equal(newSize, buff.capacity()); 62 equal(newSize, bild.capacity()); 63 buff.ensureCapacity(0); 64 bild.ensureCapacity(0); 65 equal(newSize, buff.capacity()); 66 equal(newSize, bild.capacity()); 67 } 68 } 69 70 //--------------------- Infrastructure --------------------------- 71 volatile int passed = 0, failed = 0; 72 void pass() {passed++;} 73 void fail() {failed++; Thread.dumpStack();} 74 void fail(String msg) {System.err.println(msg); fail();} 75 void unexpected(Throwable t) {failed++; t.printStackTrace();} 76 void check(boolean cond) {if (cond) pass(); else fail();} 77 void equal(Object x, Object y) { 78 if (x == null ? y == null : x.equals(y)) pass(); 79 else fail(x + " not equal to " + y);} 80 public static void main(String[] args) throws Throwable { 81 new Capacity().instanceMain(args);} 82 public void instanceMain(String[] args) throws Throwable { 83 try {test(args);} catch (Throwable t) {unexpected(t);} 84 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 85 if (failed > 0) throw new AssertionError("Some tests failed");} 86 }