jdk/test/java/util/Arrays/TimSortStackSize2.java
author lpriima
Thu, 26 Feb 2015 18:50:02 -0500
changeset 29124 b8f3a6579ab2
parent 28971 53e56ca0e39d
child 29613 da0ff5cf6e75
permissions -rw-r--r--
8073354: TimSortStackSize2.java: test cleanup: make test run with single argument Reviewed-by: dholmes
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
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    27
 * @run main/othervm -Xms385m TimSortStackSize2 67108864
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    28
 * @summary Test TimSort stack size on big arrays
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    29
 * big tests not for regular execution on all platforms:
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    30
 * run main/othervm -Xmx8g TimSortStackSize2 1073741824
28971
53e56ca0e39d 8073124: Tune test and document TimSort runs length stack size increase
lpriima
parents: 28871
diff changeset
    31
 * run main/othervm -Xmx16g TimSortStackSize2 2147483644
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    32
 */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    33
import java.util.ArrayList;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    34
import java.util.Arrays;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    35
import java.util.Comparator;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    36
import java.util.List;
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    37
import java.util.function.Consumer;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    38
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    39
public class TimSortStackSize2 {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    40
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    41
    public static void main(String[] args) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    42
        int lengthOfTest = Integer.parseInt(args[0]);
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    43
        boolean passed = doTest("TimSort", lengthOfTest,
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    44
            (Integer [] a) -> Arrays.sort(a));
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    45
        passed = doTest("ComparableTimSort", lengthOfTest, (Integer [] a) ->
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    46
            Arrays.sort(a, (Object first, Object second) -> {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    47
                return ((Comparable<Object>)first).compareTo(second);
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    48
            }))
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    49
            && passed;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    50
        if ( !passed ){
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    51
            throw new RuntimeException();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    52
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    53
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    54
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    55
    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
    56
                                  final  Consumer<Integer[]> c){
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    57
        Integer [] a = null;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    58
        try {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    59
            a = new TimSortStackSize2(lengthOfTest).createArray();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    60
            long begin = System.nanoTime();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    61
            c.accept(a);
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    62
            long end = System.nanoTime();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    63
            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
    64
        } catch (ArrayIndexOutOfBoundsException e){
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    65
            System.out.println(msg + " broken:");
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    66
            e.printStackTrace();
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    67
            return false;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    68
        } finally {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    69
            a = null;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    70
        }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    71
        return true;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    72
    }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    73
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    74
    private static final int MIN_MERGE = 32;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    75
    private final int minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    76
    private final int length;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    77
    private final List<Long> runs = new ArrayList<Long>();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    78
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
    79
    public TimSortStackSize2(final int len) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    80
        this.length = len;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    81
        minRun = minRunLength(len);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    82
        fillRunsJDKWorstCase();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    83
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    84
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    85
    private static int minRunLength(int n) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    86
        assert n >= 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    87
        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
    88
        while (n >= MIN_MERGE) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    89
            r |= (n & 1);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    90
            n >>= 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    91
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    92
        return n + r;
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
    /**
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    96
     * 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
    97
     * 1. X = x_1 + ... + x_n <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    98
     * 2. x_j >= minRun for all j <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    99
     * 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
   100
     * 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
   101
     * (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
   102
     * @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
   103
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   104
    private void generateJDKWrongElem(long X) {
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   105
        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
   106
            //Default strategy
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   107
            newTotal = X / 2 + 1;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   108
            //Specialized strategies
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   109
            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
   110
                // 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
   111
                newTotal = 2 * minRun + 1;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   112
            } 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
   113
                // 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
   114
                newTotal = 3 * minRun + 3;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   115
            } 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
   116
                // 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
   117
                newTotal = 5 * minRun + 5;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   118
            } 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
   119
                // 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
   120
                newTotal = 8 * minRun + 9;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   121
            }
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   122
            runs.add(0, X - newTotal);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   123
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   124
        runs.add(0, X);
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
    /**
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   128
     * 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
   129
     * 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
   130
     * 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
   131
     * ... <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   132
     * 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
   133
     * 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
   134
     * 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
   135
     * into an X_i that violates the invariant.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   136
     * 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
   137
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   138
    private void fillRunsJDKWorstCase() {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   139
        long runningTotal = 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   140
        long Y = minRun + 4;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   141
        long X = minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   142
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   143
        while (runningTotal + Y + X <= length) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   144
            runningTotal += X + Y;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   145
            generateJDKWrongElem(X);
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   146
            runs.add(0, Y);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   147
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   148
            // 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
   149
            X = Y + runs.get(1) + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   150
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   151
            // Y_{i+1} = X_{i+1} + Y_i + 1
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   152
            Y += X + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   153
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   154
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   155
        if (runningTotal + X <= length) {
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   156
            runningTotal += X;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   157
            generateJDKWrongElem(X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   158
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   159
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   160
        runs.add(length - runningTotal);
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   161
    }
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
    private Integer [] createArray() {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   164
        Integer [] a = new Integer[length];
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   165
        Arrays.fill(a, 0);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   166
        int endRun = -1;
29124
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   167
        for (long len : runs) {
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   168
            a[endRun += len] = 1;
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   169
        }
b8f3a6579ab2 8073354: TimSortStackSize2.java: test cleanup: make test run with single argument
lpriima
parents: 28971
diff changeset
   170
        a[length - 1] = 0;
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   171
        return a;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   172
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   173
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   174
}