jdk/test/java/util/Arrays/TimSortStackSize2.java
author lpriima
Thu, 12 Feb 2015 10:34:35 -0500
changeset 28871 d04fa3bde1d0
child 28971 53e56ca0e39d
permissions -rw-r--r--
8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays Reviewed-by: rriggs, 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
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    27
 * @run main/othervm TimSortStackSize2 67108864
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    28
 * not for regular execution on all platforms:
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    29
 * run main/othervm -Xmx8g TimSortStackSize2 1073741824
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    30
 * run main/othervm -Xmx32g TimSortStackSize2 2147483644
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    31
 * @summary Test TimSort stack size on big arrays
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;
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
public class TimSortStackSize2 {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    39
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    40
    public static void main(String[] args) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    41
        int lengthOfTest = Integer.parseInt(args[0]);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    42
        boolean passed = true;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    43
        try {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    44
            Arrays.sort(new TimSortStackSize2(lengthOfTest).createArray(),
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    45
                new Comparator<Object>() {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    46
                    @SuppressWarnings("unchecked")
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    47
                    public int compare(Object first, Object second) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    48
                        return ((Comparable<Object>)first).compareTo(second);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    49
                    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    50
                });
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    51
            System.out.println("TimSort OK");
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    52
        } catch (ArrayIndexOutOfBoundsException e){
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    53
            System.out.println("TimSort broken");
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    54
            e.printStackTrace();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    55
            passed = false;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    56
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    57
        try {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    58
            Arrays.sort(new TimSortStackSize2(lengthOfTest).createArray());
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    59
            System.out.println("ComparableTimSort OK");
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    60
        } catch (ArrayIndexOutOfBoundsException e){
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    61
            System.out.println("ComparableTimSort broken:");
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    62
            e.printStackTrace();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    63
            passed = false;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    64
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    65
        if ( !passed ){
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    66
            throw new RuntimeException();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    67
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    68
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    69
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    70
    private static final int MIN_MERGE = 32;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    71
    private final int minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    72
    private final int length;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    73
    private final List<Long> runs = new ArrayList<Long>();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    74
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    75
    public TimSortStackSize2(int len) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    76
        this.length = len;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    77
        minRun = minRunLength(len);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    78
        fillRunsJDKWorstCase();
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    79
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    80
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    81
    private static int minRunLength(int n) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    82
        assert n >= 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    83
        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
    84
        while (n >= MIN_MERGE) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    85
            r |= (n & 1);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    86
            n >>= 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    87
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    88
        return n + r;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    89
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    90
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
     * 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
    93
     * 1. X = x_1 + ... + x_n <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    94
     * 2. x_j >= minRun for all j <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
    95
     * 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
    96
     * 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
    97
     * (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
    98
     * @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
    99
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   100
    private void generateJDKWrongElem(long X) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   101
        for(long newTotal; X >= 2*minRun+1; X = newTotal) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   102
            //Default strategy
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   103
            newTotal = X/2 + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   104
            //Specialized strategies
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   105
            if(3*minRun+3 <= X && X <= 4*minRun+1) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   106
                // add x_1=MIN+1, x_2=MIN, x_3=X-newTotal  to runs
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   107
                newTotal = 2*minRun+1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   108
            } else if(5*minRun+5 <= X && X <= 6*minRun+5) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   109
                // add x_1=MIN+1, x_2=MIN, x_3=MIN+2, x_4=X-newTotal  to runs
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   110
                newTotal = 3*minRun+3;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   111
            } else if(8*minRun+9 <= X && X <= 10*minRun+9) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   112
                // add x_1=MIN+1, x_2=MIN, x_3=MIN+2, x_4=2MIN+2, x_5=X-newTotal  to runs
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   113
                newTotal = 5*minRun+5;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   114
            } else if(13*minRun+15 <= X && X <= 16*minRun+17) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   115
                // 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
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   116
                newTotal = 8*minRun+9;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   117
            }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   118
            runs.add(0, X-newTotal);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   119
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   120
        runs.add(0, X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   121
    }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   122
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
     * 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
   125
     * 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
   126
     * 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
   127
     * ... <br>
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   128
     * 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
   129
     * 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
   130
     * 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
   131
     * into an X_i that violates the invariant.
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   132
     * 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
   133
     */
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   134
    private void fillRunsJDKWorstCase() {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   135
        long runningTotal = 0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   136
        long Y = minRun + 4;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   137
        long X = minRun;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   138
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   139
        while(runningTotal+Y+X <= length) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   140
            runningTotal += X + Y;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   141
            generateJDKWrongElem(X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   142
            runs.add(0,Y);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   143
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   144
            // 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
   145
            X = Y + runs.get(1) + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   146
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   147
            // Y_{i+1} = X_{i+1} + Y_i + 1
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   148
            Y += X + 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   149
        }
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
        if(runningTotal + X <= length) {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   152
            runningTotal += X;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   153
            generateJDKWrongElem(X);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   154
        }
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   155
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   156
        runs.add(length-runningTotal);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   157
    }
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
    private Integer[] createArray() {
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   160
        Integer[] a = new Integer[length];
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   161
        Arrays.fill(a, 0);
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   162
        int endRun = -1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   163
        for(long len : runs)
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   164
            a[endRun+=len] = 1;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   165
        a[length-1]=0;
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents:
diff changeset
   166
        return a;
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
}