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