Path: blob/master/test/jdk/java/lang/StringBuffer/Capacity.java
41152 views
/*1* Copyright 2010 Google Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 695233026* @summary Test StringBuffer/StringBuilder capacity handling.27* @key randomness28*/2930import java.util.Random;3132public class Capacity {33void test(String[] args) throws Throwable {34Random rnd = new Random();35int[] sizes = { 0, 1, rnd.nextInt(10), rnd.nextInt(1000) };36for (int size : sizes) {37equal(16, new StringBuffer().capacity());38equal(16, new StringBuilder().capacity());39StringBuffer buff = new StringBuffer(size);40StringBuilder bild = new StringBuilder(size);41equal(size, buff.capacity());42equal(size, bild.capacity());43buff.ensureCapacity(size);44bild.ensureCapacity(size);45equal(size, buff.capacity());46equal(size, bild.capacity());47buff.ensureCapacity(size+1);48bild.ensureCapacity(size+1);49equal(size*2+2, buff.capacity());50equal(size*2+2, bild.capacity());51size = buff.capacity();52buff.ensureCapacity(size*2+1);53bild.ensureCapacity(size*2+1);54equal(size*2+2, buff.capacity());55equal(size*2+2, bild.capacity());56size = buff.capacity();57int newSize = size * 2 + 3;58buff.ensureCapacity(newSize);59bild.ensureCapacity(newSize);60equal(newSize, buff.capacity());61equal(newSize, bild.capacity());62buff.ensureCapacity(0);63bild.ensureCapacity(0);64equal(newSize, buff.capacity());65equal(newSize, bild.capacity());66}67}6869//--------------------- Infrastructure ---------------------------70volatile int passed = 0, failed = 0;71void pass() {passed++;}72void fail() {failed++; Thread.dumpStack();}73void fail(String msg) {System.err.println(msg); fail();}74void unexpected(Throwable t) {failed++; t.printStackTrace();}75void check(boolean cond) {if (cond) pass(); else fail();}76void equal(Object x, Object y) {77if (x == null ? y == null : x.equals(y)) pass();78else fail(x + " not equal to " + y);}79public static void main(String[] args) throws Throwable {80new Capacity().instanceMain(args);}81public void instanceMain(String[] args) throws Throwable {82try {test(args);} catch (Throwable t) {unexpected(t);}83System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);84if (failed > 0) throw new AssertionError("Some tests failed");}85}868788