test/jdk/java/util/Arrays/Correct.java
author pli
Tue, 16 Jul 2019 00:57:00 +0000
changeset 55689 8c5c9d86e1d6
parent 47216 71c04702a3d5
permissions -rw-r--r--
8227512: [TESTBUG] Fix JTReg javac test failures with Graal Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 30046
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    26
 * @bug 4726380 8037097
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that different sorts give equivalent results.
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    28
 * @run testng Correct
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 24129
diff changeset
    29
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    34
import org.testng.annotations.Test;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    35
import org.testng.annotations.DataProvider;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    36
import static org.testng.Assert.fail;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    37
import static org.testng.Assert.assertEquals;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Correct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    41
    static final Random rnd = new Random();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static final int ITERATIONS = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final int TEST_SIZE = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    45
    @Test
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    46
    public void testDefaultSort() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            int size = rnd.nextInt(TEST_SIZE) + 1;
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    49
            Integer[] array1 = getIntegerArray(size);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    50
            Integer[] array2 = Arrays.copyOf(array1, array1.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            Arrays.sort(array1, array1.length/3, array1.length/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            stupidSort(array2, array2.length/3, array2.length/2);
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    53
            assertEquals(array1, array2, "Arrays did not match. size=" + size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    57
    @Test(dataProvider = "Comparators")
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    58
    public void testComparatorSort(Comparator<Integer> comparator) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    59
        for (int i=0; i<ITERATIONS; i++) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    60
            int size = rnd.nextInt(TEST_SIZE) + 1;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    61
            Integer[] array1 = getIntegerArray(size);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    62
            Integer[] array2 = Arrays.copyOf(array1, array1.length);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    63
            Arrays.sort(array1, array1.length/3, array1.length/2, comparator);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    64
            stupidSort(array2, array2.length/3, array2.length/2, comparator);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    65
            assertEquals(array1, array2, "Arrays did not match. size=" + size);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    66
        }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    67
    }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    68
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    69
    static Integer[] getIntegerArray(int size) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Integer[] blah = new Integer[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int x=0; x<size; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            blah[x] = new Integer(rnd.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return blah;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    77
    static void stupidSort(Integer[] a1, int from, int to) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    78
        if (from > to - 1 )
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    79
          return;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    80
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        for (int x=from; x<to; x++) {
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    82
            Integer lowest = a1[x];
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    83
            int lowestIndex = x;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    84
            for (int y=x + 1; y<to; y++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    lowest = a1[y];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    lowestIndex = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (lowestIndex != x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                swap(a1, x, lowestIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    96
    static void stupidSort(Integer[] a1, int from, int to, Comparator<Integer> comparator) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    97
        if (from > to - 1 )
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    98
          return;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
    99
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   100
        for (int x=from; x<to; x++) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   101
            Integer lowest = a1[x];
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   102
            int lowestIndex = x;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   103
            for (int y=x + 1; y<to; y++) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   104
                if (comparator.compare(a1[y], lowest) < 0) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   105
                    lowest = a1[y];
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   106
                    lowestIndex = y;
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   107
                }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   108
            }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   109
            if (lowestIndex != x) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   110
                swap(a1, x, lowestIndex);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   111
            }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   112
        }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   113
    }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   114
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   115
    static <T> void swap(T[] x, int a, int b) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   116
        T t = x[a];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   121
    @DataProvider(name = "Comparators", parallel = true)
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   122
    public static Iterator<Object[]> comparators() {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   123
        Object[][] comparators = new Object[][] {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   124
            new Object[] { Comparator.naturalOrder() },
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   125
            new Object[] { Comparator.<Integer>naturalOrder().reversed() },
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   126
            new Object[] { STANDARD_ORDER },
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   127
            new Object[] { STANDARD_ORDER.reversed() },
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   128
            new Object[] { REVERSE_ORDER },
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   129
            new Object[] { REVERSE_ORDER.reversed() },
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   130
            new Object[] { Comparator.comparingInt(Integer::intValue) }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   131
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   133
        return Arrays.asList(comparators).iterator();
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   134
    }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   135
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   136
    private static final Comparator<Integer> STANDARD_ORDER = new Comparator<Integer>() {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   137
        public int compare(Integer o1, Integer o2) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   138
            return  o1.compareTo(o2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
24129
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   140
    };
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   141
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   142
    private static final Comparator<Integer> REVERSE_ORDER = new Comparator<Integer>() {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   143
        public int compare(Integer o1, Integer o2) {
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   144
            return - o1.compareTo(o2);
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   145
        }
23131cd76ca3 8037097: Improve diagnosability of test failures for java/util/Arrays/Correct.java
mduigou
parents: 5506
diff changeset
   146
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}