test/jdk/java/util/Arrays/TimSortStackSize2.java
author pli
Tue, 16 Jul 2019 00:57:00 +0000
changeset 55689 8c5c9d86e1d6
parent 54189 dfde3bb48c03
permissions -rw-r--r--
8227512: [TESTBUG] Fix JTReg javac test failures with Graal Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     1
/*
54189
dfde3bb48c03 8220613: java/util/Arrays/TimSortStackSize2.java times out with fastdebug build
rriggs
parents: 52223
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     4
 *
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     8
 *
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    13
 * accompanied this code).
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    14
 *
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    18
 *
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    21
 * questions.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    22
 */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    23
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    24
/*
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    25
 * @test
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    26
 * @bug 8072909
50849
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    27
 * @summary Test TimSort stack size on big arrays
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 50849
diff changeset
    28
 * @library /test/lib
35768
7066da300a08 8149391: Fix module dependences in java/util tests
shurailine
parents: 33832
diff changeset
    29
 * @modules java.management
7066da300a08 8149391: Fix module dependences in java/util tests
shurailine
parents: 33832
diff changeset
    30
 *          java.base/jdk.internal
54189
dfde3bb48c03 8220613: java/util/Arrays/TimSortStackSize2.java times out with fastdebug build
rriggs
parents: 52223
diff changeset
    31
 * @requires (vm.debug == false)
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    32
 * @build TimSortStackSize2
49178
1836bf0c820a 8190679: java/util/Arrays/TimSortStackSize2.java fails with "Initial heap size set to a larger value than the maximum heap size"
iignatyev
parents: 47216
diff changeset
    33
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
1836bf0c820a 8190679: java/util/Arrays/TimSortStackSize2.java fails with "Initial heap size set to a larger value than the maximum heap size"
iignatyev
parents: 47216
diff changeset
    34
 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    35
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    36
 *     -XX:+WhiteBoxAPI TimSortStackSize2
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    37
 */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    38
import java.util.ArrayList;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    39
import java.util.Arrays;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    40
import java.util.List;
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    41
import java.util.function.Consumer;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    42
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 50849
diff changeset
    43
import jdk.test.lib.process.OutputAnalyzer;
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 50849
diff changeset
    44
import jdk.test.lib.process.ProcessTools;
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    45
import sun.hotspot.WhiteBox;
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    46
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    47
public class TimSortStackSize2 {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    48
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    49
    public static void main(String[] args) {
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    50
        if ( args == null || args.length == 0 ){
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    51
            startMeWithArgs();
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    52
        } else {
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    53
            doTestOfTwoTimSorts(Integer.parseInt(args[0]));
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    54
        }
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    55
    }
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    56
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    57
    private static void startMeWithArgs(){
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    58
        /*
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    59
         * big tests not for regular execution on all platforms:
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    60
         * run main/othervm -Xmx8g TimSortStackSize2 1073741824
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    61
         * run main/othervm -Xmx16g TimSortStackSize2 2147483644
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    62
         */
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    63
        try {
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    64
            Boolean compressedOops = WhiteBox.getWhiteBox()
50849
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    65
                                             .getBooleanVMFlag("UseCompressedOops");
49178
1836bf0c820a 8190679: java/util/Arrays/TimSortStackSize2.java fails with "Initial heap size set to a larger value than the maximum heap size"
iignatyev
parents: 47216
diff changeset
    66
            long memory = (compressedOops == null || compressedOops) ? 385 : 770;
50849
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    67
            final String xmsValue = "-Xms" +     memory + "m";
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    68
            final String xmxValue = "-Xmx" + 2 * memory + "m";
49178
1836bf0c820a 8190679: java/util/Arrays/TimSortStackSize2.java fails with "Initial heap size set to a larger value than the maximum heap size"
iignatyev
parents: 47216
diff changeset
    69
1836bf0c820a 8190679: java/util/Arrays/TimSortStackSize2.java fails with "Initial heap size set to a larger value than the maximum heap size"
iignatyev
parents: 47216
diff changeset
    70
            System.out.printf("compressedOops: %s; Test will be started with \"%s %s\"%n",
1836bf0c820a 8190679: java/util/Arrays/TimSortStackSize2.java fails with "Initial heap size set to a larger value than the maximum heap size"
iignatyev
parents: 47216
diff changeset
    71
                              compressedOops, xmsValue, xmxValue);
50849
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    72
            OutputAnalyzer output = ProcessTools.executeTestJava(xmsValue,
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    73
                                                                 xmxValue,
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    74
                                                                 "TimSortStackSize2",
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    75
                                                                 "67108864");
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    76
            System.out.println(output.getOutput());
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    77
            output.shouldHaveExitValue(0);
50849
9b0e2937fac5 8199265: java/util/Arrays/TimSortStackSize2.java fails with OOM
iignatyev
parents: 49820
diff changeset
    78
        } catch (Exception e) {
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    79
            e.printStackTrace();
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    80
            throw new RuntimeException(e);
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    81
        }
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    82
    }
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    83
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    84
    private static void doTestOfTwoTimSorts(final int lengthOfTest){
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    85
        boolean passed = doTest("TimSort", lengthOfTest,
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    86
            (Integer [] a) -> Arrays.sort(a));
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    87
        passed = doTest("ComparableTimSort", lengthOfTest, (Integer [] a) ->
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    88
            Arrays.sort(a, (Object first, Object second) -> {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    89
                return ((Comparable<Object>)first).compareTo(second);
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    90
            }))
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    91
            && passed;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    92
        if ( !passed ){
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    93
            throw new RuntimeException();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    94
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    95
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    96
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    97
    private static boolean doTest(final String msg, final int lengthOfTest,
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    98
                                  final  Consumer<Integer[]> c){
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    99
        Integer [] a = null;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   100
        try {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   101
            a = new TimSortStackSize2(lengthOfTest).createArray();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   102
            long begin = System.nanoTime();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   103
            c.accept(a);
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   104
            long end = System.nanoTime();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   105
            System.out.println(msg + " OK. Time: " + (end - begin) + "ns");
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   106
        } catch (ArrayIndexOutOfBoundsException e){
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   107
            System.out.println(msg + " broken:");
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   108
            e.printStackTrace();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   109
            return false;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   110
        } finally {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   111
            a = null;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   112
        }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   113
        return true;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   114
    }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   115
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   116
    private static final int MIN_MERGE = 32;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   117
    private final int minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   118
    private final int length;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   119
    private final List<Long> runs = new ArrayList<Long>();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   120
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   121
    public TimSortStackSize2(final int len) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   122
        this.length = len;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   123
        minRun = minRunLength(len);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   124
        fillRunsJDKWorstCase();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   125
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   126
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   127
    private static int minRunLength(int n) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   128
        assert n >= 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   129
        int r = 0;      // Becomes 1 if any 1 bits are shifted off
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   130
        while (n >= MIN_MERGE) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   131
            r |= (n & 1);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   132
            n >>= 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   133
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   134
        return n + r;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   135
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   136
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   137
    /**
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   138
     * Adds a sequence x_1, ..., x_n of run lengths to <code>runs</code> such that:<br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   139
     * 1. X = x_1 + ... + x_n <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   140
     * 2. x_j >= minRun for all j <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   141
     * 3. x_1 + ... + x_{j-2}  <  x_j  <  x_1 + ... + x_{j-1} for all j <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   142
     * These conditions guarantee that TimSort merges all x_j's one by one
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   143
     * (resulting in X) using only merges on the second-to-last element.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   144
     * @param X  The sum of the sequence that should be added to runs.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   145
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   146
    private void generateJDKWrongElem(long X) {
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   147
        for(long newTotal; X >= 2 * minRun + 1; X = newTotal) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   148
            //Default strategy
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   149
            newTotal = X / 2 + 1;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   150
            //Specialized strategies
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   151
            if(3 * minRun + 3 <= X && X <= 4*minRun+1) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   152
                // add x_1=MIN+1, x_2=MIN, x_3=X-newTotal  to runs
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   153
                newTotal = 2 * minRun + 1;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   154
            } else if (5 * minRun + 5 <= X && X <= 6 * minRun + 5) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   155
                // add x_1=MIN+1, x_2=MIN, x_3=MIN+2, x_4=X-newTotal  to runs
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   156
                newTotal = 3 * minRun + 3;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   157
            } else if (8 * minRun + 9 <= X && X <= 10 * minRun + 9) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   158
                // add x_1=MIN+1, x_2=MIN, x_3=MIN+2, x_4=2MIN+2, x_5=X-newTotal  to runs
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   159
                newTotal = 5 * minRun + 5;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   160
            } else if (13 * minRun + 15 <= X && X <= 16 * minRun + 17) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   161
                // add x_1=MIN+1, x_2=MIN, x_3=MIN+2, x_4=2MIN+2, x_5=3MIN+4, x_6=X-newTotal  to runs
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   162
                newTotal = 8 * minRun + 9;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   163
            }
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   164
            runs.add(0, X - newTotal);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   165
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   166
        runs.add(0, X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   167
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   168
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   169
    /**
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   170
     * Fills <code>runs</code> with a sequence of run lengths of the form<br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   171
     * Y_n     x_{n,1}   x_{n,2}   ... x_{n,l_n} <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   172
     * Y_{n-1} x_{n-1,1} x_{n-1,2} ... x_{n-1,l_{n-1}} <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   173
     * ... <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   174
     * Y_1     x_{1,1}   x_{1,2}   ... x_{1,l_1}<br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   175
     * The Y_i's are chosen to satisfy the invariant throughout execution,
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   176
     * but the x_{i,j}'s are merged (by <code>TimSort.mergeCollapse</code>)
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   177
     * into an X_i that violates the invariant.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   178
     * X is the sum of all run lengths that will be added to <code>runs</code>.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   179
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   180
    private void fillRunsJDKWorstCase() {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   181
        long runningTotal = 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   182
        long Y = minRun + 4;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   183
        long X = minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   184
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   185
        while (runningTotal + Y + X <= length) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   186
            runningTotal += X + Y;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   187
            generateJDKWrongElem(X);
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   188
            runs.add(0, Y);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   189
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   190
            // X_{i+1} = Y_i + x_{i,1} + 1, since runs.get(1) = x_{i,1}
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   191
            X = Y + runs.get(1) + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   192
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   193
            // Y_{i+1} = X_{i+1} + Y_i + 1
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   194
            Y += X + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   195
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   196
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   197
        if (runningTotal + X <= length) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   198
            runningTotal += X;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   199
            generateJDKWrongElem(X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   200
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   201
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   202
        runs.add(length - runningTotal);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   203
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   204
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   205
    private Integer [] createArray() {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   206
        Integer [] a = new Integer[length];
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   207
        Arrays.fill(a, 0);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   208
        int endRun = -1;
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   209
        for (long len : runs) {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   210
            a[endRun += len] = 1;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   211
        }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   212
        a[length - 1] = 0;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   213
        return a;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   214
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   215
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   216
}