Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Arrays/Correct.java
38812 views
/*1* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 472638026* @summary Check that different sorts give equivalent results.27* @key randomness28*/2930import java.util.*;3132public class Correct {3334static Random rnd = new Random();35static final int ITERATIONS = 1000;36static final int TEST_SIZE = 1000;3738public static void main(String[] args) throws Exception {39Object[] array1 = null;40Object[] array2 = null;4142for (int i=0; i<ITERATIONS; i++) {43int size = rnd.nextInt(TEST_SIZE) + 1;44array1 = (Object[])getIntegerArray(size);45array2 = (Object[])array1.clone();46Arrays.sort(array1, array1.length/3, array1.length/2);47stupidSort(array2, array2.length/3, array2.length/2);48if(!Arrays.equals(array1, array2))49throw new RuntimeException("failed!");50}5152for (int i=0; i<ITERATIONS; i++) {53int size = rnd.nextInt(TEST_SIZE) + 1;54array1 = (Object[])getIntegerArray(size);55array2 = (Object[])array1.clone();56Arrays.sort(array1, array1.length/3, array1.length/2, TEST_ORDER);57stupidSort(array2, array2.length/3, array2.length/2);58if(!Arrays.equals(array1, array2))59throw new RuntimeException("failed!");60}61}6263static Integer[] getIntegerArray(int size) throws Exception {64Integer[] blah = new Integer[size];65for (int x=0; x<size; x++) {66blah[x] = new Integer(rnd.nextInt());67}68return blah;69}7071static void stupidSort(Object[] a1, int from, int to) throws Exception {72for (int x=from; x<to; x++) {73Object lowest = new Integer(Integer.MAX_VALUE);74int lowestIndex = 0;75for (int y=x; y<to; y++) {76if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {77lowest = a1[y];78lowestIndex = y;79}80}81if (lowestIndex != x) {82swap(a1, x, lowestIndex);83}84}85}8687static void swap(Object x[], int a, int b) {88Object t = x[a];89x[a] = x[b];90x[b] = t;91}9293private static final Comparator TEST_ORDER = new IntegerComparator();9495private static class IntegerComparator implements Comparator {96public int compare(Object o1, Object o2) {97Comparable c1 = (Comparable)o1;98Comparable c2 = (Comparable)o2;99return c1.compareTo(c2);100}101}102}103104105