Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Arrays/Correct.java
38812 views
1
/*
2
* Copyright (c) 2002, 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
27
* @summary Check that different sorts give equivalent results.
28
* @key randomness
29
*/
30
31
import java.util.*;
32
33
public class Correct {
34
35
static Random rnd = new Random();
36
static final int ITERATIONS = 1000;
37
static final int TEST_SIZE = 1000;
38
39
public static void main(String[] args) throws Exception {
40
Object[] array1 = null;
41
Object[] array2 = null;
42
43
for (int i=0; i<ITERATIONS; i++) {
44
int size = rnd.nextInt(TEST_SIZE) + 1;
45
array1 = (Object[])getIntegerArray(size);
46
array2 = (Object[])array1.clone();
47
Arrays.sort(array1, array1.length/3, array1.length/2);
48
stupidSort(array2, array2.length/3, array2.length/2);
49
if(!Arrays.equals(array1, array2))
50
throw new RuntimeException("failed!");
51
}
52
53
for (int i=0; i<ITERATIONS; i++) {
54
int size = rnd.nextInt(TEST_SIZE) + 1;
55
array1 = (Object[])getIntegerArray(size);
56
array2 = (Object[])array1.clone();
57
Arrays.sort(array1, array1.length/3, array1.length/2, TEST_ORDER);
58
stupidSort(array2, array2.length/3, array2.length/2);
59
if(!Arrays.equals(array1, array2))
60
throw new RuntimeException("failed!");
61
}
62
}
63
64
static Integer[] getIntegerArray(int size) throws Exception {
65
Integer[] blah = new Integer[size];
66
for (int x=0; x<size; x++) {
67
blah[x] = new Integer(rnd.nextInt());
68
}
69
return blah;
70
}
71
72
static void stupidSort(Object[] a1, int from, int to) throws Exception {
73
for (int x=from; x<to; x++) {
74
Object lowest = new Integer(Integer.MAX_VALUE);
75
int lowestIndex = 0;
76
for (int y=x; y<to; y++) {
77
if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {
78
lowest = a1[y];
79
lowestIndex = y;
80
}
81
}
82
if (lowestIndex != x) {
83
swap(a1, x, lowestIndex);
84
}
85
}
86
}
87
88
static void swap(Object x[], int a, int b) {
89
Object t = x[a];
90
x[a] = x[b];
91
x[b] = t;
92
}
93
94
private static final Comparator TEST_ORDER = new IntegerComparator();
95
96
private static class IntegerComparator implements Comparator {
97
public int compare(Object o1, Object o2) {
98
Comparable c1 = (Comparable)o1;
99
Comparable c2 = (Comparable)o2;
100
return c1.compareTo(c2);
101
}
102
}
103
}
104
105