author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
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 | 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 | 29 |
* @key randomness |
2 | 30 |
*/ |
31 |
||
32 |
import java.util.*; |
|
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 | 39 |
public class Correct { |
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 | 42 |
static final int ITERATIONS = 1000; |
43 |
static final int TEST_SIZE = 1000; |
|
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 | 47 |
for (int i=0; i<ITERATIONS; i++) { |
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 | 51 |
Arrays.sort(array1, array1.length/3, array1.length/2); |
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 | 54 |
} |
55 |
} |
|
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 | 70 |
Integer[] blah = new Integer[size]; |
71 |
for (int x=0; x<size; x++) { |
|
72 |
blah[x] = new Integer(rnd.nextInt()); |
|
73 |
} |
|
74 |
return blah; |
|
75 |
} |
|
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 | 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 | 85 |
if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) { |
86 |
lowest = a1[y]; |
|
87 |
lowestIndex = y; |
|
88 |
} |
|
89 |
} |
|
90 |
if (lowestIndex != x) { |
|
91 |
swap(a1, x, lowestIndex); |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
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 | 117 |
x[a] = x[b]; |
118 |
x[b] = t; |
|
119 |
} |
|
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 | 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 | 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 | 147 |
} |