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