jdk/test/java/util/Arrays/TimSortStackSize.java
author rriggs
Mon, 26 Aug 2013 11:46:05 -0400
changeset 19590 36f2469d5c39
permissions -rw-r--r--
8011944: Sort fails with ArrayIndexOutOfBoundsException Summary: Increase the size of pending stack and add test cases Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19590
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     1
/*
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     4
 *
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     8
 *
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    13
 * accompanied this code).
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    14
 *
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    18
 *
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    21
 * questions.
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    22
 */
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    23
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    24
/*
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    25
 * @test
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    26
 * @bug 8011944
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    27
 * @summary Test TimSort stack size
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    28
 */
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    29
import java.util.Arrays;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    30
import java.util.ArrayDeque;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    31
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    32
public class TimSortStackSize {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    33
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    34
    public static void main(String[] args) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    35
        testComparableTimSort();
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    36
        testTimSort();
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    37
    }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    38
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    39
    static void testComparableTimSort() {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    40
        System.out.printf("testComparableTimSort()%n");
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    41
        Arrays.sort(genData());
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    42
    }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    43
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    44
    static void testTimSort() {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    45
        System.out.printf("testTimSort()%n");
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    46
        Arrays.sort(genData(), Integer::compare);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    47
    }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    48
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    49
    private static final int MIN = 16;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    50
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    51
    private static final int BOUND1 = 2 * MIN + 1;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    52
    private static final int BOUND2 = BOUND1 + MIN + 2;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    53
    private static final int BOUND3 = BOUND1 + 1 + BOUND2;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    54
    private static final int BOUND4 = BOUND2 + 1 + BOUND3;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    55
    private static final int BOUND5 = BOUND3 + 1 + BOUND4;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    56
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    57
    static int build(int size, int B, ArrayDeque<Integer> chunks) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    58
        chunks.addFirst(B);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    59
        if (size < BOUND1) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    60
            chunks.addFirst(size);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    61
            return size;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    62
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    63
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    64
        int asize = (size + 2) / 2;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    65
        if (size >= BOUND2 && asize < BOUND1) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    66
            asize = BOUND1;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    67
        } else if (size >= BOUND3 && asize < BOUND2) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    68
            asize = BOUND2;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    69
        } else if (size >= BOUND4 && asize < BOUND3) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    70
            asize = BOUND3;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    71
        } else if (size >= BOUND5 && asize < BOUND4) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    72
            asize = BOUND4;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    73
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    74
        if (size - asize >= B) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    75
            throw new AssertionError(" " + size + " , " + asize + " , " + B);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    76
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    77
        return build(asize, size - asize, chunks);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    78
    }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    79
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    80
    static Integer[] genData() {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    81
        ArrayDeque<Integer> chunks = new ArrayDeque<Integer>();
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    82
        chunks.addFirst(MIN);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    83
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    84
        int B = MIN + 4;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    85
        int A = B + MIN + 1;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    86
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    87
        for (int i = 0; i < 8; i++) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    88
            int eps = build(A, B, chunks);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    89
            B = B + A + 1;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    90
            A = B + eps + 1;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    91
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    92
        chunks.addFirst(B);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    93
        chunks.addFirst(A);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    94
        int total = 0;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    95
        for (Integer len : chunks) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    96
            total += len;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    97
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    98
        int pow = MIN;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
    99
        while (pow < total) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   100
            pow += pow;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   101
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   102
        chunks.addLast(pow - total);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   103
        System.out.println(" Total: " + total);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   104
        Integer[] array = new Integer[pow];
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   105
        int off = 0;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   106
        int pos = 0;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   107
        for (Integer len : chunks) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   108
            for (int i = 0; i < len; i++) {
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   109
                array[pos++] = Integer.valueOf(i == 0 ? 0 : 1);
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   110
            }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   111
            off++;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   112
        }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   113
        return array;
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   114
    }
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   115
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents:
diff changeset
   116
}