Path: blob/master/test/jdk/java/lang/StringBuilder/Capacity.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. 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 814933026* @summary Basic set of tests of capacity management27* @run testng Capacity28*/2930import java.lang.reflect.Field;31import java.util.AbstractList;32import java.util.ArrayList;33import java.util.Collections;34import java.util.List;35import java.util.SplittableRandom;3637import org.testng.annotations.Test;38import org.testng.annotations.DataProvider;39import static org.testng.Assert.*;4041public class Capacity {42static final int DEFAULT_CAPACITY = 16;4344private static int newCapacity(int oldCapacity,45int desiredCapacity)46{47return Math.max(oldCapacity * 2 + 2, desiredCapacity);48}4950private static int nextNewCapacity(int oldCapacity) {51return newCapacity(oldCapacity, oldCapacity + 1);52}5354@Test(dataProvider = "singleChar")55public void defaultCapacity(Character ch) {56StringBuilder sb = new StringBuilder();57assertEquals(sb.capacity(), DEFAULT_CAPACITY);58for (int i = 0; i < DEFAULT_CAPACITY; i++) {59sb.append(ch);60assertEquals(sb.capacity(), DEFAULT_CAPACITY);61}62sb.append(ch);63assertEquals(sb.capacity(), nextNewCapacity(DEFAULT_CAPACITY));64}6566@Test(dataProvider = "charCapacity")67public void explicitCapacity(Character ch, int initCapacity) {68StringBuilder sb = new StringBuilder(initCapacity);69assertEquals(sb.capacity(), initCapacity);70for (int i = 0; i < initCapacity; i++) {71sb.append(ch);72assertEquals(sb.capacity(), initCapacity);73}74sb.append(ch);75assertEquals(sb.capacity(), nextNewCapacity(initCapacity));76}7778@Test(dataProvider = "singleChar")79public void sbFromString(Character ch) {80String s = "string " + ch;81int expectedCapacity = s.length() + DEFAULT_CAPACITY;82StringBuilder sb = new StringBuilder(s);83assertEquals(sb.capacity(), expectedCapacity);84for (int i = 0; i < DEFAULT_CAPACITY; i++) {85sb.append(ch);86assertEquals(sb.capacity(), expectedCapacity);87}88sb.append(ch);89assertEquals(sb.capacity(), nextNewCapacity(expectedCapacity));90}9192@Test(dataProvider = "singleChar")93public void sbFromCharSeq(Character ch) {94CharSequence cs = new MyCharSeq("char seq " + ch);95int expectedCapacity = cs.length() + DEFAULT_CAPACITY;96StringBuilder sb = new StringBuilder(cs);97assertEquals(sb.capacity(), expectedCapacity);98for (int i = 0; i < DEFAULT_CAPACITY; i++) {99sb.append(ch);100assertEquals(sb.capacity(), expectedCapacity);101}102sb.append(ch);103assertEquals(sb.capacity(), nextNewCapacity(expectedCapacity));104}105106@Test(dataProvider = "charCapacity")107public void ensureCapacity(Character ch, int cap) {108StringBuilder sb = new StringBuilder(0);109assertEquals(sb.capacity(), 0);110sb.ensureCapacity(cap); // only has effect if cap > 0111int newCap = (cap == 0) ? 0 : newCapacity(0, cap);112assertEquals(sb.capacity(), newCap);113sb.ensureCapacity(newCap + 1);114assertEquals(sb.capacity(), nextNewCapacity(newCap));115sb.append(ch);116assertEquals(sb.capacity(), nextNewCapacity(newCap));117}118119@Test(dataProvider = "negativeCapacity",120expectedExceptions = NegativeArraySizeException.class)121public void negativeInitialCapacity(int negCap) {122StringBuilder sb = new StringBuilder(negCap);123}124125@Test(dataProvider = "negativeCapacity")126public void ensureNegativeCapacity(int negCap) {127StringBuilder sb = new StringBuilder();128sb.ensureCapacity(negCap);129assertEquals(sb.capacity(), DEFAULT_CAPACITY);130}131132@Test(dataProvider = "charCapacity")133public void trimToSize(Character ch, int cap) {134StringBuilder sb = new StringBuilder(cap);135int halfOfCap = cap / 2;136for (int i = 0; i < halfOfCap; i++) {137sb.append(ch);138}139sb.trimToSize();140// according to the spec, capacity doesn't have to141// become exactly the size142assertTrue(sb.capacity() >= halfOfCap);143}144145@DataProvider146public Object[][] singleChar() {147return new Object[][] { {'J'}, {'\u042b'} };148}149150@DataProvider151public Object[][] charCapacity() {152return new Object[][] {153{'J', 0},154{'J', 1},155{'J', 15},156{'J', DEFAULT_CAPACITY},157{'J', 1024},158{'\u042b', 0},159{'\u042b', 1},160{'\u042b', 15},161{'\u042b', DEFAULT_CAPACITY},162{'\u042b', 1024},163};164}165166@DataProvider167public Object[][] negativeCapacity() {168return new Object[][] { {-1}, {Integer.MIN_VALUE} };169}170171private static class MyCharSeq implements CharSequence {172private CharSequence s;173public MyCharSeq(CharSequence s) { this.s = s; }174public char charAt(int i) { return s.charAt(i); }175public int length() { return s.length(); }176public CharSequence subSequence(int st, int e) {177return s.subSequence(st, e);178}179public String toString() { return s.toString(); }180}181}182183184