jdk/test/java/util/Arrays/TimSortStackSize2.java
author cjplummer
Thu, 29 Oct 2015 12:02:53 -0700
changeset 33832 7dde7a271492
parent 29613 da0ff5cf6e75
child 35768 7066da300a08
permissions -rw-r--r--
8140189: [TESTBUG] Get rid of "@library /../../test/lib" in jtreg tests Summary: Use new external.lib.roots property in TEST.ROOT so /../../test/lib is not needed. Reviewed-by: mseledtsov, sla, iklam
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
/*
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
33832
7dde7a271492 8140189: [TESTBUG] Get rid of "@library /../../test/lib" in jtreg tests
cjplummer
parents: 29613
diff changeset
    27
 * @library /lib/testlibrary /test/lib
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    28
 * @build jdk.testlibrary.*
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    29
 * @build TimSortStackSize2
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    30
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    31
 * @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
    32
 *     -XX:+WhiteBoxAPI TimSortStackSize2
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    33
 * @summary Test TimSort stack size on big arrays
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    34
 */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    35
import java.util.ArrayList;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    36
import java.util.Arrays;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    37
import java.util.List;
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    38
import java.util.function.Consumer;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    39
29613
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    40
import jdk.testlibrary.OutputAnalyzer;
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    41
import jdk.testlibrary.ProcessTools;
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    42
import jdk.testlibrary.Utils;
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    43
import sun.hotspot.WhiteBox;
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    44
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    45
public class TimSortStackSize2 {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    46
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    47
    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
    48
        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
    49
            startMeWithArgs();
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    50
        } else {
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    51
            doTestOfTwoTimSorts(Integer.parseInt(args[0]));
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    52
        }
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    53
    }
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
    private static void startMeWithArgs(){
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
         * 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
    58
         * 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
    59
         * 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
    60
         */
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    61
        try {
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    62
            Boolean compressedOops = WhiteBox.getWhiteBox()
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    63
                .getBooleanVMFlag("UseCompressedOops");
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    64
            final String xmsValue = "-Xms" +
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    65
                ((compressedOops == null || compressedOops) ? "385" : "770")
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    66
                + "m";
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    67
            System.out.println( "compressedOops: " + compressedOops
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    68
                + "; Test will be started with \"" + xmsValue + "\"");
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    69
            ProcessBuilder processBuilder = ProcessTools
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    70
                .createJavaProcessBuilder(Utils.addTestJavaOpts(xmsValue,
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    71
                    "TimSortStackSize2", "67108864"
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    72
                )
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    73
            );
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    74
            OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    75
            System.out.println(output.getOutput());
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    76
            output.shouldHaveExitValue(0);
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    77
        } catch( Exception e ){
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    78
            e.printStackTrace();
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    79
            throw new RuntimeException(e);
da0ff5cf6e75 8075071: [TEST_BUG] TimSortStackSize2.java: OOME: Java heap space: MaxHeap shrinked by MaxRAMFraction
lpriima
parents: 29124
diff changeset
    80
        }
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
    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
    84
        boolean passed = doTest("TimSort", lengthOfTest,
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    85
            (Integer [] a) -> Arrays.sort(a));
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    86
        passed = doTest("ComparableTimSort", lengthOfTest, (Integer [] a) ->
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    87
            Arrays.sort(a, (Object first, Object second) -> {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    88
                return ((Comparable<Object>)first).compareTo(second);
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    89
            }))
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    90
            && passed;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    91
        if ( !passed ){
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    92
            throw new RuntimeException();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    93
        }
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
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    96
    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
    97
                                  final  Consumer<Integer[]> c){
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    98
        Integer [] a = null;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    99
        try {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   100
            a = new TimSortStackSize2(lengthOfTest).createArray();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   101
            long begin = System.nanoTime();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   102
            c.accept(a);
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   103
            long end = System.nanoTime();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   104
            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
   105
        } catch (ArrayIndexOutOfBoundsException e){
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   106
            System.out.println(msg + " broken:");
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   107
            e.printStackTrace();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   108
            return false;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   109
        } finally {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   110
            a = null;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   111
        }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   112
        return true;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   113
    }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   114
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   115
    private static final int MIN_MERGE = 32;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   116
    private final int minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   117
    private final int length;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   118
    private final List<Long> runs = new ArrayList<Long>();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   119
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   120
    public TimSortStackSize2(final int len) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   121
        this.length = len;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   122
        minRun = minRunLength(len);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   123
        fillRunsJDKWorstCase();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   124
    }
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
    private static int minRunLength(int n) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   127
        assert n >= 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   128
        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
   129
        while (n >= MIN_MERGE) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   130
            r |= (n & 1);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   131
            n >>= 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   132
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   133
        return n + r;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   134
    }
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
     * 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
   138
     * 1. X = x_1 + ... + x_n <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   139
     * 2. x_j >= minRun for all j <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   140
     * 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
   141
     * 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
   142
     * (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
   143
     * @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
   144
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   145
    private void generateJDKWrongElem(long X) {
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   146
        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
   147
            //Default strategy
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   148
            newTotal = X / 2 + 1;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   149
            //Specialized strategies
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   150
            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
   151
                // 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
   152
                newTotal = 2 * minRun + 1;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   153
            } 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
   154
                // 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
   155
                newTotal = 3 * minRun + 3;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   156
            } 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
   157
                // 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
   158
                newTotal = 5 * minRun + 5;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   159
            } 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
   160
                // 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
   161
                newTotal = 8 * minRun + 9;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   162
            }
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   163
            runs.add(0, X - newTotal);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   164
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   165
        runs.add(0, X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   166
    }
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
     * 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
   170
     * 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
   171
     * 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
   172
     * ... <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   173
     * 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
   174
     * 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
   175
     * 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
   176
     * into an X_i that violates the invariant.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   177
     * 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
   178
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   179
    private void fillRunsJDKWorstCase() {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   180
        long runningTotal = 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   181
        long Y = minRun + 4;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   182
        long X = minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   183
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   184
        while (runningTotal + Y + X <= length) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   185
            runningTotal += X + Y;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   186
            generateJDKWrongElem(X);
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   187
            runs.add(0, Y);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   188
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   189
            // 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
   190
            X = Y + runs.get(1) + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   191
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   192
            // Y_{i+1} = X_{i+1} + Y_i + 1
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   193
            Y += X + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   194
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   195
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   196
        if (runningTotal + X <= length) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   197
            runningTotal += X;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   198
            generateJDKWrongElem(X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   199
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   200
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   201
        runs.add(length - runningTotal);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   202
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   203
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   204
    private Integer [] createArray() {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   205
        Integer [] a = new Integer[length];
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   206
        Arrays.fill(a, 0);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   207
        int endRun = -1;
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   208
        for (long len : runs) {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   209
            a[endRun += len] = 1;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   210
        }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   211
        a[length - 1] = 0;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   212
        return a;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   213
    }
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
}