test/jdk/java/lang/StringBuilder/HugeCapacity.java
author igerasim
Tue, 05 Feb 2019 17:05:40 -0800
changeset 53653 868611f0adc3
parent 47216 71c04702a3d5
permissions -rw-r--r--
8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36217
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     1
/*
53653
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
36217
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     4
 *
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     8
 *
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    13
 * accompanied this code).
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    14
 *
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    18
 *
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    21
 * questions.
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    22
 */
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    23
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    24
/**
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    25
 * @test
53653
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    26
 * @bug 8149330 8218227
36217
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    27
 * @summary Capacity should not get close to Integer.MAX_VALUE unless
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    28
 *          necessary
53653
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    29
 * @requires os.maxMemory >= 6G
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    30
 * @run main/othervm -Xms5G -Xmx5G HugeCapacity
36217
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    31
 * @ignore This test has huge memory requirements
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    32
 */
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    33
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    34
public class HugeCapacity {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    35
    private static int failures = 0;
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    36
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    37
    public static void main(String[] args) {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    38
        testLatin1();
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    39
        testUtf16();
53653
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    40
        testHugeInitialString();
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    41
        testHugeInitialCharSequence();
36217
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    42
        if (failures > 0) {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    43
            throw new RuntimeException(failures + " tests failed");
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    44
        }
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    45
    }
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    46
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    47
    private static void testLatin1() {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    48
        try {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    49
            StringBuilder sb = new StringBuilder();
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    50
            sb.ensureCapacity(Integer.MAX_VALUE / 2);
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    51
            sb.ensureCapacity(Integer.MAX_VALUE / 2 + 1);
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    52
        } catch (OutOfMemoryError oom) {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    53
            oom.printStackTrace();
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    54
            failures++;
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    55
        }
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    56
    }
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    57
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    58
    private static void testUtf16() {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    59
        try {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    60
            StringBuilder sb = new StringBuilder();
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    61
            sb.append('\u042b');
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    62
            sb.ensureCapacity(Integer.MAX_VALUE / 4);
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    63
            sb.ensureCapacity(Integer.MAX_VALUE / 4 + 1);
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    64
        } catch (OutOfMemoryError oom) {
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    65
            oom.printStackTrace();
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    66
            failures++;
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    67
        }
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
    68
    }
53653
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    69
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    70
    private static void testHugeInitialString() {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    71
        try {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    72
            String str = "Z".repeat(Integer.MAX_VALUE - 8);
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    73
            StringBuilder sb = new StringBuilder(str);
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    74
        } catch (OutOfMemoryError ignore) {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    75
        } catch (Throwable unexpected) {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    76
            unexpected.printStackTrace();
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    77
            failures++;
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    78
        }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    79
    }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    80
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    81
    private static void testHugeInitialCharSequence() {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    82
        try {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    83
            CharSequence seq = new MyHugeCharSeq();
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    84
            StringBuilder sb = new StringBuilder(seq);
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    85
        } catch (OutOfMemoryError ignore) {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    86
        } catch (Throwable unexpected) {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    87
            unexpected.printStackTrace();
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    88
            failures++;
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    89
        }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    90
    }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    91
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    92
    private static class MyHugeCharSeq implements CharSequence {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    93
        public char charAt(int i) {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    94
            throw new UnsupportedOperationException();
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    95
        }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    96
        public int length() { return Integer.MAX_VALUE; }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    97
        public CharSequence subSequence(int st, int e) {
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    98
            throw new UnsupportedOperationException();
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
    99
        }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
   100
        public String toString() { return ""; }
868611f0adc3 8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException
igerasim
parents: 47216
diff changeset
   101
    }
36217
cc64f9fb2062 8149330: Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
igerasim
parents:
diff changeset
   102
}