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 }