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