Path: blob/master/test/jdk/java/util/Arrays/Correct.java
41149 views
/*1* Copyright (c) 2002, 2014, 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 4726380 803709726* @summary Check that different sorts give equivalent results.27* @run testng Correct28* @key randomness29*/3031import java.util.*;3233import org.testng.annotations.Test;34import org.testng.annotations.DataProvider;35import static org.testng.Assert.fail;36import static org.testng.Assert.assertEquals;3738public class Correct {3940static final Random rnd = new Random();41static final int ITERATIONS = 1000;42static final int TEST_SIZE = 1000;4344@Test45public void testDefaultSort() {46for (int i=0; i<ITERATIONS; i++) {47int size = rnd.nextInt(TEST_SIZE) + 1;48Integer[] array1 = getIntegerArray(size);49Integer[] array2 = Arrays.copyOf(array1, array1.length);50Arrays.sort(array1, array1.length/3, array1.length/2);51stupidSort(array2, array2.length/3, array2.length/2);52assertEquals(array1, array2, "Arrays did not match. size=" + size);53}54}5556@Test(dataProvider = "Comparators")57public void testComparatorSort(Comparator<Integer> comparator) {58for (int i=0; i<ITERATIONS; i++) {59int size = rnd.nextInt(TEST_SIZE) + 1;60Integer[] array1 = getIntegerArray(size);61Integer[] array2 = Arrays.copyOf(array1, array1.length);62Arrays.sort(array1, array1.length/3, array1.length/2, comparator);63stupidSort(array2, array2.length/3, array2.length/2, comparator);64assertEquals(array1, array2, "Arrays did not match. size=" + size);65}66}6768static Integer[] getIntegerArray(int size) {69Integer[] blah = new Integer[size];70for (int x=0; x<size; x++) {71blah[x] = new Integer(rnd.nextInt());72}73return blah;74}7576static void stupidSort(Integer[] a1, int from, int to) {77if (from > to - 1 )78return;7980for (int x=from; x<to; x++) {81Integer lowest = a1[x];82int lowestIndex = x;83for (int y=x + 1; y<to; y++) {84if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {85lowest = a1[y];86lowestIndex = y;87}88}89if (lowestIndex != x) {90swap(a1, x, lowestIndex);91}92}93}9495static void stupidSort(Integer[] a1, int from, int to, Comparator<Integer> comparator) {96if (from > to - 1 )97return;9899for (int x=from; x<to; x++) {100Integer lowest = a1[x];101int lowestIndex = x;102for (int y=x + 1; y<to; y++) {103if (comparator.compare(a1[y], lowest) < 0) {104lowest = a1[y];105lowestIndex = y;106}107}108if (lowestIndex != x) {109swap(a1, x, lowestIndex);110}111}112}113114static <T> void swap(T[] x, int a, int b) {115T t = x[a];116x[a] = x[b];117x[b] = t;118}119120@DataProvider(name = "Comparators", parallel = true)121public static Iterator<Object[]> comparators() {122Object[][] comparators = new Object[][] {123new Object[] { Comparator.naturalOrder() },124new Object[] { Comparator.<Integer>naturalOrder().reversed() },125new Object[] { STANDARD_ORDER },126new Object[] { STANDARD_ORDER.reversed() },127new Object[] { REVERSE_ORDER },128new Object[] { REVERSE_ORDER.reversed() },129new Object[] { Comparator.comparingInt(Integer::intValue) }130};131132return Arrays.asList(comparators).iterator();133}134135private static final Comparator<Integer> STANDARD_ORDER = new Comparator<Integer>() {136public int compare(Integer o1, Integer o2) {137return o1.compareTo(o2);138}139};140141private static final Comparator<Integer> REVERSE_ORDER = new Comparator<Integer>() {142public int compare(Integer o1, Integer o2) {143return - o1.compareTo(o2);144}145};146}147148149