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 */
29
30 import java.util.*;
31
32 public class Correct {
33
34 static Random rnd = new Random();
35 static final int ITERATIONS = 1000;
36 static final int TEST_SIZE = 1000;
37
38 public static void main(String[] args) throws Exception {
39 Object[] array1 = null;
40 Object[] array2 = null;
41
42 for (int i=0; i<ITERATIONS; i++) {
43 int size = rnd.nextInt(TEST_SIZE) + 1;
44 array1 = (Object[])getIntegerArray(size);
45 array2 = (Object[])array1.clone();
46 Arrays.sort(array1, array1.length/3, array1.length/2);
47 stupidSort(array2, array2.length/3, array2.length/2);
48 if(!Arrays.equals(array1, array2))
49 throw new RuntimeException("failed!");
50 }
51
52 for (int i=0; i<ITERATIONS; i++) {
53 int size = rnd.nextInt(TEST_SIZE) + 1;
54 array1 = (Object[])getIntegerArray(size);
55 array2 = (Object[])array1.clone();
56 Arrays.sort(array1, array1.length/3, array1.length/2, TEST_ORDER);
57 stupidSort(array2, array2.length/3, array2.length/2);
58 if(!Arrays.equals(array1, array2))
59 throw new RuntimeException("failed!");
60 }
61 }
62
63 static Integer[] getIntegerArray(int size) throws Exception {
64 Integer[] blah = new Integer[size];
65 for (int x=0; x<size; x++) {
66 blah[x] = new Integer(rnd.nextInt());
67 }
68 return blah;
69 }
70
71 static void stupidSort(Object[] a1, int from, int to) throws Exception {
72 for (int x=from; x<to; x++) {
73 Object lowest = new Integer(Integer.MAX_VALUE);
74 int lowestIndex = 0;
75 for (int y=x; y<to; y++) {
76 if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {
77 lowest = a1[y];
78 lowestIndex = y;
79 }
80 }
81 if (lowestIndex != x) {
82 swap(a1, x, lowestIndex);
83 }
84 }
85 }
86
87 static void swap(Object x[], int a, int b) {
88 Object t = x[a];
89 x[a] = x[b];
90 x[b] = t;
91 }
92
93 private static final Comparator TEST_ORDER = new IntegerComparator();
94
95 private static class IntegerComparator implements Comparator {
96 public int compare(Object o1, Object o2) {
97 Comparable c1 = (Comparable)o1;
98 Comparable c2 = (Comparable)o2;
99 return c1.compareTo(c2);
100 }
101 }
102 }
|
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 */
30
31 import java.util.*;
32
33 import org.testng.annotations.Test;
34 import org.testng.annotations.DataProvider;
35 import static org.testng.Assert.fail;
36 import static org.testng.Assert.assertEquals;
37
38 public class Correct {
39
40 static final Random rnd = new Random();
41 static final int ITERATIONS = 1000;
42 static final int TEST_SIZE = 1000;
43
44 @Test
45 public void testDefaultSort() {
46 for (int i=0; i<ITERATIONS; i++) {
47 int size = rnd.nextInt(TEST_SIZE) + 1;
48 Integer[] array1 = getIntegerArray(size);
49 Integer[] array2 = Arrays.copyOf(array1, array1.length);
50 Arrays.sort(array1, array1.length/3, array1.length/2);
51 stupidSort(array2, array2.length/3, array2.length/2);
52 assertEquals(array1, array2, "Arrays did not match");
53 }
54 }
55
56 @Test(dataProvider = "Comparators")
57 public void testComparatorSort(Comparator<Integer> comparator) {
58 for (int i=0; i<ITERATIONS; i++) {
59 int size = rnd.nextInt(TEST_SIZE) + 1;
60 Integer[] array1 = getIntegerArray(size);
61 Integer[] array2 = Arrays.copyOf(array1, array1.length);
62 Arrays.sort(array1, array1.length/3, array1.length/2, comparator);
63 stupidSort(array2, array2.length/3, array2.length/2, comparator);
64 assertEquals(array1, array2, "Arrays did not match");
65 }
66 }
67
68 static Integer[] getIntegerArray(int size) {
69 Integer[] blah = new Integer[size];
70 for (int x=0; x<size; x++) {
71 blah[x] = new Integer(rnd.nextInt());
72 }
73 return blah;
74 }
75
76 static void stupidSort(Integer[] a1, int from, int to) {
77 if (from > to - 1 )
78 return;
79
80 for (int x=from; x<to; x++) {
81 Integer lowest = a1[x];
82 int lowestIndex = x;
83 for (int y=x + 1; y<to; y++) {
84 if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {
85 lowest = a1[y];
86 lowestIndex = y;
87 }
88 }
89 if (lowestIndex != x) {
90 swap(a1, x, lowestIndex);
91 }
92 }
93 }
94
95 static void stupidSort(Integer[] a1, int from, int to, Comparator<Integer> comparator) {
96 if (from > to - 1 )
97 return;
98
99 for (int x=from; x<to; x++) {
100 Integer lowest = a1[x];
101 int lowestIndex = x;
102 for (int y=x + 1; y<to; y++) {
103 if (comparator.compare(a1[y], lowest) < 0) {
104 lowest = a1[y];
105 lowestIndex = y;
106 }
107 }
108 if (lowestIndex != x) {
109 swap(a1, x, lowestIndex);
110 }
111 }
112 }
113
114 static <T> void swap(T[] x, int a, int b) {
115 T t = x[a];
116 x[a] = x[b];
117 x[b] = t;
118 }
119
120 @DataProvider(name = "Comparators", parallel = true)
121 public static Iterator<Object[]> comparators() {
122 Object[][] comparators = new Object[][] {
123 new Object[] { Comparator.naturalOrder() },
124 new Object[] { Comparator.<Integer>naturalOrder().reversed() },
125 new Object[] { STANDARD_ORDER },
126 new Object[] { STANDARD_ORDER.reversed() },
127 new Object[] { REVERSE_ORDER },
128 new Object[] { REVERSE_ORDER.reversed() },
129 new Object[] { Comparator.comparingInt(Integer::intValue) }
130 };
131
132 return Arrays.asList(comparators).iterator();
133 }
134
135 private static final Comparator<Integer> STANDARD_ORDER = new Comparator<Integer>() {
136 public int compare(Integer o1, Integer o2) {
137 return o1.compareTo(o2);
138 }
139 };
140
141 private static final Comparator<Integer> REVERSE_ORDER = new Comparator<Integer>() {
142 public int compare(Integer o1, Integer o2) {
143 return - o1.compareTo(o2);
144 }
145 };
146 }
|