Path: blob/master/test/jdk/java/lang/StringBuilder/HugeCapacity.java
41149 views
/*1* Copyright (c) 2016, 2020, 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 8149330 821822726* @summary Capacity should not get close to Integer.MAX_VALUE unless27* necessary28* @requires (sun.arch.data.model == "64" & os.maxMemory >= 6G)29* @run main/othervm -Xms5G -Xmx5G -XX:+CompactStrings HugeCapacity true30* @run main/othervm -Xms5G -Xmx5G -XX:-CompactStrings HugeCapacity false31*/3233public class HugeCapacity {34private static int failures = 0;3536public static void main(String[] args) {37if (args.length == 0) {38throw new IllegalArgumentException("Need the argument");39}40boolean isCompact = Boolean.parseBoolean(args[0]);4142testLatin1(isCompact);43testUtf16();44testHugeInitialString();45testHugeInitialCharSequence();46if (failures > 0) {47throw new RuntimeException(failures + " tests failed");48}49}5051private static void testLatin1(boolean isCompact) {52try {53int divisor = isCompact ? 2 : 4;54StringBuilder sb = new StringBuilder();55sb.ensureCapacity(Integer.MAX_VALUE / divisor);56sb.ensureCapacity(Integer.MAX_VALUE / divisor + 1);57} catch (OutOfMemoryError oom) {58oom.printStackTrace();59failures++;60}61}6263private static void testUtf16() {64try {65StringBuilder sb = new StringBuilder();66sb.append('\u042b');67sb.ensureCapacity(Integer.MAX_VALUE / 4);68sb.ensureCapacity(Integer.MAX_VALUE / 4 + 1);69} catch (OutOfMemoryError oom) {70oom.printStackTrace();71failures++;72}73}7475private static void testHugeInitialString() {76try {77String str = "Z".repeat(Integer.MAX_VALUE - 8);78StringBuilder sb = new StringBuilder(str);79} catch (OutOfMemoryError ignore) {80} catch (Throwable unexpected) {81unexpected.printStackTrace();82failures++;83}84}8586private static void testHugeInitialCharSequence() {87try {88CharSequence seq = new MyHugeCharSeq();89StringBuilder sb = new StringBuilder(seq);90} catch (OutOfMemoryError ignore) {91} catch (Throwable unexpected) {92unexpected.printStackTrace();93failures++;94}95}9697private static class MyHugeCharSeq implements CharSequence {98public char charAt(int i) {99throw new UnsupportedOperationException();100}101public int length() { return Integer.MAX_VALUE; }102public CharSequence subSequence(int st, int e) {103throw new UnsupportedOperationException();104}105public String toString() { return ""; }106}107}108109110