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