Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Arrays/Correct.java
41149 views
1
/*
2
* Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
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
*
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.
22
*/
23
24
/*
25
* @test
26
* @bug 4726380 8037097
27
* @summary Check that different sorts give equivalent results.
28
* @run testng Correct
29
* @key randomness
30
*/
31
32
import java.util.*;
33
34
import org.testng.annotations.Test;
35
import org.testng.annotations.DataProvider;
36
import static org.testng.Assert.fail;
37
import static org.testng.Assert.assertEquals;
38
39
public class Correct {
40
41
static final Random rnd = new Random();
42
static final int ITERATIONS = 1000;
43
static final int TEST_SIZE = 1000;
44
45
@Test
46
public void testDefaultSort() {
47
for (int i=0; i<ITERATIONS; i++) {
48
int size = rnd.nextInt(TEST_SIZE) + 1;
49
Integer[] array1 = getIntegerArray(size);
50
Integer[] array2 = Arrays.copyOf(array1, array1.length);
51
Arrays.sort(array1, array1.length/3, array1.length/2);
52
stupidSort(array2, array2.length/3, array2.length/2);
53
assertEquals(array1, array2, "Arrays did not match. size=" + size);
54
}
55
}
56
57
@Test(dataProvider = "Comparators")
58
public void testComparatorSort(Comparator<Integer> comparator) {
59
for (int i=0; i<ITERATIONS; i++) {
60
int size = rnd.nextInt(TEST_SIZE) + 1;
61
Integer[] array1 = getIntegerArray(size);
62
Integer[] array2 = Arrays.copyOf(array1, array1.length);
63
Arrays.sort(array1, array1.length/3, array1.length/2, comparator);
64
stupidSort(array2, array2.length/3, array2.length/2, comparator);
65
assertEquals(array1, array2, "Arrays did not match. size=" + size);
66
}
67
}
68
69
static Integer[] getIntegerArray(int size) {
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
77
static void stupidSort(Integer[] a1, int from, int to) {
78
if (from > to - 1 )
79
return;
80
81
for (int x=from; x<to; x++) {
82
Integer lowest = a1[x];
83
int lowestIndex = x;
84
for (int y=x + 1; y<to; y++) {
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
96
static void stupidSort(Integer[] a1, int from, int to, Comparator<Integer> comparator) {
97
if (from > to - 1 )
98
return;
99
100
for (int x=from; x<to; x++) {
101
Integer lowest = a1[x];
102
int lowestIndex = x;
103
for (int y=x + 1; y<to; y++) {
104
if (comparator.compare(a1[y], lowest) < 0) {
105
lowest = a1[y];
106
lowestIndex = y;
107
}
108
}
109
if (lowestIndex != x) {
110
swap(a1, x, lowestIndex);
111
}
112
}
113
}
114
115
static <T> void swap(T[] x, int a, int b) {
116
T t = x[a];
117
x[a] = x[b];
118
x[b] = t;
119
}
120
121
@DataProvider(name = "Comparators", parallel = true)
122
public static Iterator<Object[]> comparators() {
123
Object[][] comparators = new Object[][] {
124
new Object[] { Comparator.naturalOrder() },
125
new Object[] { Comparator.<Integer>naturalOrder().reversed() },
126
new Object[] { STANDARD_ORDER },
127
new Object[] { STANDARD_ORDER.reversed() },
128
new Object[] { REVERSE_ORDER },
129
new Object[] { REVERSE_ORDER.reversed() },
130
new Object[] { Comparator.comparingInt(Integer::intValue) }
131
};
132
133
return Arrays.asList(comparators).iterator();
134
}
135
136
private static final Comparator<Integer> STANDARD_ORDER = new Comparator<Integer>() {
137
public int compare(Integer o1, Integer o2) {
138
return o1.compareTo(o2);
139
}
140
};
141
142
private static final Comparator<Integer> REVERSE_ORDER = new Comparator<Integer>() {
143
public int compare(Integer o1, Integer o2) {
144
return - o1.compareTo(o2);
145
}
146
};
147
}
148
149