test/jdk/java/util/Arrays/Sorting.java
changeset 59042 8910b995a2ee
parent 47216 71c04702a3d5
equal deleted inserted replaced
59041:d6d8fdc95ed2 59042:8910b995a2ee
     1 /*
     1 /*
     2  * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 6880672 6896573 6899694 6976036 7013585 7018258
    26  * @compile/module=java.base java/util/SortingHelper.java
    27  * @summary Exercise Arrays.sort
    27  * @bug 6880672 6896573 6899694 6976036 7013585 7018258 8003981 8226297
    28  * @build Sorting
    28  * @build Sorting
    29  * @run main Sorting -shortrun
    29  * @run main Sorting -shortrun
       
    30  * @summary Exercise Arrays.sort, Arrays.parallelSort
    30  *
    31  *
    31  * @author Vladimir Yaroslavskiy
    32  * @author Vladimir Yaroslavskiy
    32  * @author Jon Bentley
    33  * @author Jon Bentley
    33  * @author Josh Bloch
    34  * @author Josh Bloch
    34  */
    35  */
    35 
    36 
    36 import java.util.Arrays;
    37 import java.io.PrintStream;
       
    38 import java.util.Comparator;
    37 import java.util.Random;
    39 import java.util.Random;
    38 import java.io.PrintStream;
    40 import java.util.SortingHelper;
    39 
    41 
    40 public class Sorting {
    42 public class Sorting {
       
    43 
    41     private static final PrintStream out = System.out;
    44     private static final PrintStream out = System.out;
    42     private static final PrintStream err = System.err;
    45     private static final PrintStream err = System.err;
    43 
    46 
    44     // Array lengths used in a long run (default)
    47     // Array lengths used in a long run (default)
    45     private static final int[] LONG_RUN_LENGTHS = {
    48     private static final int[] LONG_RUN_LENGTHS = {
    46         1, 2, 3, 5, 8, 13, 21, 34, 55, 100, 1000, 10000, 100000, 1000000 };
    49         1, 3, 8, 21, 55, 100, 1_000, 10_000, 100_000 };
    47 
    50 
    48     // Array lengths used in a short run
    51     // Array lengths used in a short run
    49     private static final int[] SHORT_RUN_LENGTHS = {
    52     private static final int[] SHORT_RUN_LENGTHS = {
    50         1, 2, 3, 21, 55, 1000, 10000 };
    53         1, 8, 55, 100, 10_000 };
    51 
    54 
    52     // Random initial values used in a long run (default)
    55     // Random initial values used in a long run (default)
    53     private static final long[] LONG_RUN_RANDOMS = { 666, 0xC0FFEE, 999 };
    56     private static final TestRandom[] LONG_RUN_RANDOMS = {
       
    57         TestRandom.BABA, TestRandom.DEDA, TestRandom.C0FFEE };
    54 
    58 
    55     // Random initial values used in a short run
    59     // Random initial values used in a short run
    56     private static final long[] SHORT_RUN_RANDOMS = { 666 };
    60     private static final TestRandom[] SHORT_RUN_RANDOMS = {
       
    61         TestRandom.C0FFEE };
       
    62 
       
    63     // Constants used in subarray sorting
       
    64     private static final int A380 = 0xA380;
       
    65     private static final int B747 = 0xB747;
       
    66 
       
    67     private final SortingHelper sortingHelper;
       
    68     private final TestRandom[] randoms;
       
    69     private final int[] lengths;
       
    70     private Object[] gold;
       
    71     private Object[] test;
    57 
    72 
    58     public static void main(String[] args) {
    73     public static void main(String[] args) {
       
    74         long start = System.currentTimeMillis();
    59         boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
    75         boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
    60         long start = System.currentTimeMillis();
    76 
    61 
    77         int[] lengths = shortRun ? SHORT_RUN_LENGTHS : LONG_RUN_LENGTHS;
    62         if (shortRun) {
    78         TestRandom[] randoms = shortRun ? SHORT_RUN_RANDOMS : LONG_RUN_RANDOMS;
    63             testAndCheck(SHORT_RUN_LENGTHS, SHORT_RUN_RANDOMS);
    79 
    64         } else {
    80         new Sorting(SortingHelper.DUAL_PIVOT_QUICKSORT, randoms, lengths).testCore();
    65             testAndCheck(LONG_RUN_LENGTHS, LONG_RUN_RANDOMS);
    81         new Sorting(SortingHelper.PARALLEL_SORT, randoms, lengths).testCore();
    66         }
    82         new Sorting(SortingHelper.HEAP_SORT, randoms, lengths).testBasic();
       
    83         new Sorting(SortingHelper.ARRAYS_SORT, randoms, lengths).testAll();
       
    84         new Sorting(SortingHelper.ARRAYS_PARALLEL_SORT, randoms, lengths).testAll();
       
    85 
    67         long end = System.currentTimeMillis();
    86         long end = System.currentTimeMillis();
    68 
    87         out.format("PASSED in %d sec.\n", (end - start) / 1000);
    69         out.format("PASSED in %d sec.\n", Math.round((end - start) / 1E3));
    88     }
    70     }
    89 
    71 
    90     private Sorting(SortingHelper sortingHelper, TestRandom[] randoms, int[] lengths) {
    72     private static void testAndCheck(int[] lengths, long[] randoms) {
    91         this.sortingHelper = sortingHelper;
       
    92         this.randoms = randoms;
       
    93         this.lengths = lengths;
       
    94     }
       
    95 
       
    96     private void testBasic() {
       
    97         testEmptyArray();
       
    98 
       
    99         for (int length : lengths) {
       
   100             createData(length);
       
   101             testBasic(length);
       
   102         }
       
   103     }
       
   104 
       
   105     private void testBasic(int length) {
       
   106         for (TestRandom random : randoms) {
       
   107             testWithInsertionSort(length, random);
       
   108             testWithCheckSum(length, random);
       
   109             testWithScrambling(length, random);
       
   110         }
       
   111     }
       
   112 
       
   113     private void testCore() {
       
   114         for (int length : lengths) {
       
   115             createData(length);
       
   116             testCore(length);
       
   117         }
       
   118     }
       
   119 
       
   120     private void testCore(int length) {
       
   121         testBasic(length);
       
   122 
       
   123         for (TestRandom random : randoms) {
       
   124             testMergingSort(length, random);
       
   125             testSubArray(length, random);
       
   126             testNegativeZero(length, random);
       
   127             testFloatingPointSorting(length, random);
       
   128         }
       
   129     }
       
   130 
       
   131     private void testAll() {
       
   132         for (int length : lengths) {
       
   133             createData(length);
       
   134             testAll(length);
       
   135         }
       
   136     }
       
   137 
       
   138     private void testAll(int length) {
       
   139         testCore(length);
       
   140 
       
   141         for (TestRandom random : randoms) {
       
   142             testRange(length, random);
       
   143             testStability(length, random);
       
   144         }
       
   145     }
       
   146 
       
   147     private void testEmptyArray() {
    73         testEmptyAndNullIntArray();
   148         testEmptyAndNullIntArray();
    74         testEmptyAndNullLongArray();
   149         testEmptyAndNullLongArray();
       
   150         testEmptyAndNullByteArray();
       
   151         testEmptyAndNullCharArray();
    75         testEmptyAndNullShortArray();
   152         testEmptyAndNullShortArray();
    76         testEmptyAndNullCharArray();
       
    77         testEmptyAndNullByteArray();
       
    78         testEmptyAndNullFloatArray();
   153         testEmptyAndNullFloatArray();
    79         testEmptyAndNullDoubleArray();
   154         testEmptyAndNullDoubleArray();
    80 
   155     }
    81         for (int length : lengths) {
   156 
    82             testMergeSort(length);
   157     private void testStability(int length, TestRandom random) {
    83             testAndCheckRange(length);
   158         printTestName("Test stability", random, length);
    84             testAndCheckSubArray(length);
   159 
    85         }
   160         Pair[] a = build(length, random);
    86         for (long seed : randoms) {
   161         sortingHelper.sort(a);
    87             for (int length : lengths) {
   162         checkSorted(a);
    88                 testAndCheckWithInsertionSort(length, new MyRandom(seed));
   163         checkStable(a);
    89                 testAndCheckWithCheckSum(length, new MyRandom(seed));
   164 
    90                 testAndCheckWithScrambling(length, new MyRandom(seed));
   165         a = build(length, random);
    91                 testAndCheckFloat(length, new MyRandom(seed));
   166         sortingHelper.sort(a, pairComparator);
    92                 testAndCheckDouble(length, new MyRandom(seed));
   167         checkSorted(a);
    93                 testStable(length, new MyRandom(seed));
   168         checkStable(a);
    94             }
   169 
    95         }
   170         out.println();
    96     }
   171     }
    97 
   172 
    98     private static void testEmptyAndNullIntArray() {
   173     private void testEmptyAndNullIntArray() {
    99         ourDescription = "Check empty and null array";
   174         sortingHelper.sort(new int[] {});
   100         Arrays.sort(new int[] {});
   175         sortingHelper.sort(new int[] {}, 0, 0);
   101         Arrays.sort(new int[] {}, 0, 0);
       
   102 
   176 
   103         try {
   177         try {
   104             Arrays.sort((int[]) null);
   178             sortingHelper.sort(null);
   105         } catch (NullPointerException expected) {
   179         } catch (NullPointerException expected) {
   106             try {
   180             try {
   107                 Arrays.sort((int[]) null, 0, 0);
   181                 sortingHelper.sort(null, 0, 0);
   108             } catch (NullPointerException expected2) {
   182             } catch (NullPointerException expected2) {
   109                 return;
   183                 return;
   110             }
   184             }
   111             failed("Arrays.sort(int[],fromIndex,toIndex) shouldn't " +
   185             fail(sortingHelper + "(int[],fromIndex,toIndex) shouldn't " +
   112                 "catch null array");
   186                 "catch null array");
   113         }
   187         }
   114         failed("Arrays.sort(int[]) shouldn't catch null array");
   188         fail(sortingHelper + "(int[]) shouldn't catch null array");
   115     }
   189     }
   116 
   190 
   117     private static void testEmptyAndNullLongArray() {
   191     private void testEmptyAndNullLongArray() {
   118         ourDescription = "Check empty and null array";
   192         sortingHelper.sort(new long[] {});
   119         Arrays.sort(new long[] {});
   193         sortingHelper.sort(new long[] {}, 0, 0);
   120         Arrays.sort(new long[] {}, 0, 0);
       
   121 
   194 
   122         try {
   195         try {
   123             Arrays.sort((long[]) null);
   196             sortingHelper.sort(null);
   124         } catch (NullPointerException expected) {
   197         } catch (NullPointerException expected) {
   125             try {
   198             try {
   126                 Arrays.sort((long[]) null, 0, 0);
   199                 sortingHelper.sort(null, 0, 0);
   127             } catch (NullPointerException expected2) {
   200             } catch (NullPointerException expected2) {
   128                 return;
   201                 return;
   129             }
   202             }
   130             failed("Arrays.sort(long[],fromIndex,toIndex) shouldn't " +
   203             fail(sortingHelper + "(long[],fromIndex,toIndex) shouldn't " +
   131                 "catch null array");
   204                 "catch null array");
   132         }
   205         }
   133         failed("Arrays.sort(long[]) shouldn't catch null array");
   206         fail(sortingHelper + "(long[]) shouldn't catch null array");
   134     }
   207     }
   135 
   208 
   136     private static void testEmptyAndNullShortArray() {
   209     private void testEmptyAndNullByteArray() {
   137         ourDescription = "Check empty and null array";
   210         sortingHelper.sort(new byte[] {});
   138         Arrays.sort(new short[] {});
   211         sortingHelper.sort(new byte[] {}, 0, 0);
   139         Arrays.sort(new short[] {}, 0, 0);
       
   140 
   212 
   141         try {
   213         try {
   142             Arrays.sort((short[]) null);
   214             sortingHelper.sort(null);
   143         } catch (NullPointerException expected) {
   215         } catch (NullPointerException expected) {
   144             try {
   216             try {
   145                 Arrays.sort((short[]) null, 0, 0);
   217                 sortingHelper.sort(null, 0, 0);
   146             } catch (NullPointerException expected2) {
   218             } catch (NullPointerException expected2) {
   147                 return;
   219                 return;
   148             }
   220             }
   149             failed("Arrays.sort(short[],fromIndex,toIndex) shouldn't " +
   221             fail(sortingHelper + "(byte[],fromIndex,toIndex) shouldn't " +
   150                 "catch null array");
   222                 "catch null array");
   151         }
   223         }
   152         failed("Arrays.sort(short[]) shouldn't catch null array");
   224         fail(sortingHelper + "(byte[]) shouldn't catch null array");
   153     }
   225     }
   154 
   226 
   155     private static void testEmptyAndNullCharArray() {
   227     private void testEmptyAndNullCharArray() {
   156         ourDescription = "Check empty and null array";
   228         sortingHelper.sort(new char[] {});
   157         Arrays.sort(new char[] {});
   229         sortingHelper.sort(new char[] {}, 0, 0);
   158         Arrays.sort(new char[] {}, 0, 0);
       
   159 
   230 
   160         try {
   231         try {
   161             Arrays.sort((char[]) null);
   232             sortingHelper.sort(null);
   162         } catch (NullPointerException expected) {
   233         } catch (NullPointerException expected) {
   163             try {
   234             try {
   164                 Arrays.sort((char[]) null, 0, 0);
   235                 sortingHelper.sort(null, 0, 0);
   165             } catch (NullPointerException expected2) {
   236             } catch (NullPointerException expected2) {
   166                 return;
   237                 return;
   167             }
   238             }
   168             failed("Arrays.sort(char[],fromIndex,toIndex) shouldn't " +
   239             fail(sortingHelper + "(char[],fromIndex,toIndex) shouldn't " +
   169                 "catch null array");
   240                 "catch null array");
   170         }
   241         }
   171         failed("Arrays.sort(char[]) shouldn't catch null array");
   242         fail(sortingHelper + "(char[]) shouldn't catch null array");
   172     }
   243     }
   173 
   244 
   174     private static void testEmptyAndNullByteArray() {
   245     private void testEmptyAndNullShortArray() {
   175         ourDescription = "Check empty and null array";
   246         sortingHelper.sort(new short[] {});
   176         Arrays.sort(new byte[] {});
   247         sortingHelper.sort(new short[] {}, 0, 0);
   177         Arrays.sort(new byte[] {}, 0, 0);
       
   178 
   248 
   179         try {
   249         try {
   180             Arrays.sort((byte[]) null);
   250             sortingHelper.sort(null);
   181         } catch (NullPointerException expected) {
   251         } catch (NullPointerException expected) {
   182             try {
   252             try {
   183                 Arrays.sort((byte[]) null, 0, 0);
   253                 sortingHelper.sort(null, 0, 0);
   184             } catch (NullPointerException expected2) {
   254             } catch (NullPointerException expected2) {
   185                 return;
   255                 return;
   186             }
   256             }
   187             failed("Arrays.sort(byte[],fromIndex,toIndex) shouldn't " +
   257             fail(sortingHelper + "(short[],fromIndex,toIndex) shouldn't " +
   188                 "catch null array");
   258                 "catch null array");
   189         }
   259         }
   190         failed("Arrays.sort(byte[]) shouldn't catch null array");
   260         fail(sortingHelper + "(short[]) shouldn't catch null array");
   191     }
   261     }
   192 
   262 
   193     private static void testEmptyAndNullFloatArray() {
   263     private void testEmptyAndNullFloatArray() {
   194         ourDescription = "Check empty and null array";
   264         sortingHelper.sort(new float[] {});
   195         Arrays.sort(new float[] {});
   265         sortingHelper.sort(new float[] {}, 0, 0);
   196         Arrays.sort(new float[] {}, 0, 0);
       
   197 
   266 
   198         try {
   267         try {
   199             Arrays.sort((float[]) null);
   268             sortingHelper.sort(null);
   200         } catch (NullPointerException expected) {
   269         } catch (NullPointerException expected) {
   201             try {
   270             try {
   202                 Arrays.sort((float[]) null, 0, 0);
   271                 sortingHelper.sort(null, 0, 0);
   203             } catch (NullPointerException expected2) {
   272             } catch (NullPointerException expected2) {
   204                 return;
   273                 return;
   205             }
   274             }
   206             failed("Arrays.sort(float[],fromIndex,toIndex) shouldn't " +
   275             fail(sortingHelper + "(float[],fromIndex,toIndex) shouldn't " +
   207                 "catch null array");
   276                 "catch null array");
   208         }
   277         }
   209         failed("Arrays.sort(float[]) shouldn't catch null array");
   278         fail(sortingHelper + "(float[]) shouldn't catch null array");
   210     }
   279     }
   211 
   280 
   212     private static void testEmptyAndNullDoubleArray() {
   281     private void testEmptyAndNullDoubleArray() {
   213         ourDescription = "Check empty and null array";
   282         sortingHelper.sort(new double[] {});
   214         Arrays.sort(new double[] {});
   283         sortingHelper.sort(new double[] {}, 0, 0);
   215         Arrays.sort(new double[] {}, 0, 0);
       
   216 
   284 
   217         try {
   285         try {
   218             Arrays.sort((double[]) null);
   286             sortingHelper.sort(null);
   219         } catch (NullPointerException expected) {
   287         } catch (NullPointerException expected) {
   220             try {
   288             try {
   221                 Arrays.sort((double[]) null, 0, 0);
   289                 sortingHelper.sort(null, 0, 0);
   222             } catch (NullPointerException expected2) {
   290             } catch (NullPointerException expected2) {
   223                 return;
   291                 return;
   224             }
   292             }
   225             failed("Arrays.sort(double[],fromIndex,toIndex) shouldn't " +
   293             fail(sortingHelper + "(double[],fromIndex,toIndex) shouldn't " +
   226                 "catch null array");
   294                 "catch null array");
   227         }
   295         }
   228         failed("Arrays.sort(double[]) shouldn't catch null array");
   296         fail(sortingHelper + "(double[]) shouldn't catch null array");
   229     }
   297     }
   230 
   298 
   231     private static void testAndCheckSubArray(int length) {
   299     private void testSubArray(int length, TestRandom random) {
   232         ourDescription = "Check sorting of subarray";
   300         if (length < 4) {
   233         int[] golden = new int[length];
   301             return;
   234         boolean newLine = false;
   302         }
   235 
   303         for (int m = 1; m < length / 2; m <<= 1) {
   236         for (int m = 1; m < length / 2; m *= 2) {
       
   237             newLine = true;
       
   238             int fromIndex = m;
   304             int fromIndex = m;
   239             int toIndex = length - m;
   305             int toIndex = length - m;
   240 
   306 
   241             prepareSubArray(golden, fromIndex, toIndex, m);
   307             prepareSubArray((int[]) gold[0], fromIndex, toIndex);
   242             int[] test = golden.clone();
   308             convertData(length);
   243 
   309 
   244             for (TypeConverter converter : TypeConverter.values()) {
   310             for (int i = 0; i < test.length; i++) {
   245                 out.println("Test 'subarray': " + converter +
   311                 printTestName("Test subarray", random, length,
   246                    " length = " + length + ", m = " + m);
   312                     ", m = " + m + ", " + getType(i));
   247                 Object convertedGolden = converter.convert(golden);
   313                 sortingHelper.sort(test[i], fromIndex, toIndex);
   248                 Object convertedTest = converter.convert(test);
   314                 checkSubArray(test[i], fromIndex, toIndex);
   249                 sortSubArray(convertedTest, fromIndex, toIndex);
   315             }
   250                 checkSubArray(convertedTest, fromIndex, toIndex, m);
   316         }
   251             }
   317         out.println();
   252         }
   318     }
   253         if (newLine) {
   319 
   254             out.println();
   320     private void testRange(int length, TestRandom random) {
   255         }
   321         if (length < 2) {
   256     }
   322             return;
   257 
   323         }
   258     private static void testAndCheckRange(int length) {
   324         for (int m = 1; m < length; m <<= 1) {
   259         ourDescription = "Check range check";
       
   260         int[] golden = new int[length];
       
   261 
       
   262         for (int m = 1; m < 2 * length; m *= 2) {
       
   263             for (int i = 1; i <= length; i++) {
   325             for (int i = 1; i <= length; i++) {
   264                 golden[i - 1] = i % m + m % i;
   326                 ((int[]) gold[0]) [i - 1] = i % m + m % i;
   265             }
   327             }
   266             for (TypeConverter converter : TypeConverter.values()) {
   328             convertData(length);
   267                 out.println("Test 'range': " + converter +
   329 
   268                    ", length = " + length + ", m = " + m);
   330             for (int i = 0; i < test.length; i++) {
   269                 Object convertedGolden = converter.convert(golden);
   331                 printTestName("Test range check", random, length,
   270                 checkRange(convertedGolden, m);
   332                     ", m = " + m + ", " + getType(i));
       
   333                 checkRange(test[i], m);
   271             }
   334             }
   272         }
   335         }
   273         out.println();
   336         out.println();
   274     }
   337     }
   275 
   338 
   276     private static void testStable(int length, MyRandom random) {
   339     private void checkSorted(Pair[] a) {
   277         ourDescription = "Check if sorting is stable";
       
   278         Pair[] a = build(length, random);
       
   279 
       
   280         out.println("Test 'stable': " + "random = " + random.getSeed() +
       
   281             ", length = " + length);
       
   282         Arrays.sort(a);
       
   283         checkSorted(a);
       
   284         checkStable(a);
       
   285         out.println();
       
   286     }
       
   287 
       
   288     private static void checkSorted(Pair[] a) {
       
   289         for (int i = 0; i < a.length - 1; i++) {
   340         for (int i = 0; i < a.length - 1; i++) {
   290             if (a[i].getKey() > a[i + 1].getKey()) {
   341             if (a[i].getKey() > a[i + 1].getKey()) {
   291                 failedSort(i, "" + a[i].getKey(), "" + a[i + 1].getKey());
   342                 fail("Array is not sorted at " + i + "-th position: " +
   292             }
   343                     a[i].getKey() + " and " + a[i + 1].getKey());
   293         }
   344             }
   294     }
   345         }
   295 
   346     }
   296     private static void checkStable(Pair[] a) {
   347 
       
   348     private void checkStable(Pair[] a) {
   297         for (int i = 0; i < a.length / 4; ) {
   349         for (int i = 0; i < a.length / 4; ) {
   298             int key1 = a[i].getKey();
   350             int key1 = a[i].getKey();
   299             int value1 = a[i++].getValue();
   351             int value1 = a[i++].getValue();
   300             int key2 = a[i].getKey();
   352             int key2 = a[i].getKey();
   301             int value2 = a[i++].getValue();
   353             int value2 = a[i++].getValue();
   303             int value3 = a[i++].getValue();
   355             int value3 = a[i++].getValue();
   304             int key4 = a[i].getKey();
   356             int key4 = a[i].getKey();
   305             int value4 = a[i++].getValue();
   357             int value4 = a[i++].getValue();
   306 
   358 
   307             if (!(key1 == key2 && key2 == key3 && key3 == key4)) {
   359             if (!(key1 == key2 && key2 == key3 && key3 == key4)) {
   308                 failed("On position " + i + " keys are different " +
   360                 fail("Keys are different " + key1 + ", " + key2 + ", " +
   309                     key1 + ", " + key2 + ", " + key3 + ", " + key4);
   361                     key3 + ", " + key4 + " at position " + i);
   310             }
   362             }
   311             if (!(value1 < value2 && value2 < value3 && value3 < value4)) {
   363             if (!(value1 < value2 && value2 < value3 && value3 < value4)) {
   312                 failed("Sorting is not stable at position " + i +
   364                 fail("Sorting is not stable at position " + i +
   313                     ". Second values have been changed: " +  value1 + ", " +
   365                     ". Second values have been changed: " + value1 + ", " +
   314                     value2 + ", " + value3 + ", " + value4);
   366                     value2 + ", " + value3 + ", " + value4);
   315             }
   367             }
   316         }
   368         }
   317     }
   369     }
   318 
   370 
   319     private static Pair[] build(int length, Random random) {
   371     private Pair[] build(int length, Random random) {
   320         Pair[] a = new Pair[length * 4];
   372         Pair[] a = new Pair[length * 4];
   321 
   373 
   322         for (int i = 0; i < a.length; ) {
   374         for (int i = 0; i < a.length; ) {
   323             int key = random.nextInt();
   375             int key = random.nextInt();
   324             a[i++] = new Pair(key, 1);
   376             a[i++] = new Pair(key, 1);
   327             a[i++] = new Pair(key, 4);
   379             a[i++] = new Pair(key, 4);
   328         }
   380         }
   329         return a;
   381         return a;
   330     }
   382     }
   331 
   383 
   332     private static final class Pair implements Comparable<Pair> {
   384     private void testWithInsertionSort(int length, TestRandom random) {
   333         Pair(int key, int value) {
       
   334             myKey = key;
       
   335             myValue = value;
       
   336         }
       
   337 
       
   338         int getKey() {
       
   339             return myKey;
       
   340         }
       
   341 
       
   342         int getValue() {
       
   343             return myValue;
       
   344         }
       
   345 
       
   346         public int compareTo(Pair pair) {
       
   347             if (myKey < pair.myKey) {
       
   348                 return -1;
       
   349             }
       
   350             if (myKey > pair.myKey) {
       
   351                 return 1;
       
   352             }
       
   353             return 0;
       
   354         }
       
   355 
       
   356         @Override
       
   357         public String toString() {
       
   358             return "(" + myKey + ", " + myValue + ")";
       
   359         }
       
   360 
       
   361         private int myKey;
       
   362         private int myValue;
       
   363     }
       
   364 
       
   365 
       
   366     private static void testAndCheckWithInsertionSort(int length, MyRandom random) {
       
   367         if (length > 1000) {
   385         if (length > 1000) {
   368             return;
   386             return;
   369         }
   387         }
   370         ourDescription = "Check sorting with insertion sort";
   388         for (int m = 1; m <= length; m <<= 1) {
   371         int[] golden = new int[length];
       
   372 
       
   373         for (int m = 1; m < 2 * length; m *= 2) {
       
   374             for (UnsortedBuilder builder : UnsortedBuilder.values()) {
   389             for (UnsortedBuilder builder : UnsortedBuilder.values()) {
   375                 builder.build(golden, m, random);
   390                 builder.build((int[]) gold[0], m, random);
   376                 int[] test = golden.clone();
   391                 convertData(length);
   377 
   392 
   378                 for (TypeConverter converter : TypeConverter.values()) {
   393                 for (int i = 0; i < test.length; i++) {
   379                     out.println("Test 'insertion sort': " + converter +
   394                     printTestName("Test with insertion sort", random, length,
   380                         " " + builder + "random = " + random.getSeed() +
   395                         ", m = " + m + ", " + getType(i) + " " + builder);
   381                         ", length = " + length + ", m = " + m);
   396                     sortingHelper.sort(test[i]);
   382                     Object convertedGolden = converter.convert(golden);
   397                     sortByInsertionSort(gold[i]);
   383                     Object convertedTest1 = converter.convert(test);
   398                     compare(test[i], gold[i]);
   384                     Object convertedTest2 = converter.convert(test);
       
   385                     sort(convertedTest1);
       
   386                     sortByInsertionSort(convertedTest2);
       
   387                     compare(convertedTest1, convertedTest2);
       
   388                 }
   399                 }
   389             }
   400             }
   390         }
   401         }
   391         out.println();
   402         out.println();
   392     }
   403     }
   393 
   404 
   394     private static void testMergeSort(int length) {
   405     private void testMergingSort(int length, TestRandom random) {
   395         if (length < 1000) {
   406         if (length < (4 << 10)) { // DualPivotQuicksort.MIN_TRY_MERGE_SIZE
   396             return;
   407             return;
   397         }
   408         }
   398         ourDescription = "Check merge sorting";
   409         final int PERIOD = 50;
   399         int[] golden = new int[length];
   410 
   400         int period = 67; // java.util.DualPivotQuicksort.MAX_RUN_COUNT
   411         for (int m = PERIOD - 2; m <= PERIOD + 2; m++) {
   401 
   412             for (MergingBuilder builder : MergingBuilder.values()) {
   402         for (int m = period - 2; m <= period + 2; m++) {
   413                 builder.build((int[]) gold[0], m);
   403             for (MergeBuilder builder : MergeBuilder.values()) {
   414                 convertData(length);
   404                 builder.build(golden, m);
   415 
   405                 int[] test = golden.clone();
   416                 for (int i = 0; i < test.length; i++) {
   406 
   417                     printTestName("Test merging sort", random, length,
   407                 for (TypeConverter converter : TypeConverter.values()) {
   418                         ", m = " + m + ", " +  getType(i) + " " + builder);
   408                     out.println("Test 'merge sort': " + converter + " " +
   419                     sortingHelper.sort(test[i]);
   409                         builder + "length = " + length + ", m = " + m);
   420                     checkSorted(test[i]);
   410                     Object convertedGolden = converter.convert(golden);
       
   411                     sort(convertedGolden);
       
   412                     checkSorted(convertedGolden);
       
   413                 }
   421                 }
   414             }
   422             }
   415         }
   423         }
   416         out.println();
   424         out.println();
   417     }
   425     }
   418 
   426 
   419     private static void testAndCheckWithCheckSum(int length, MyRandom random) {
   427     private void testWithCheckSum(int length, TestRandom random) {
   420         ourDescription = "Check sorting with check sum";
   428         for (int m = 1; m <= length; m <<= 1) {
   421         int[] golden = new int[length];
       
   422 
       
   423         for (int m = 1; m < 2 * length; m *= 2) {
       
   424             for (UnsortedBuilder builder : UnsortedBuilder.values()) {
   429             for (UnsortedBuilder builder : UnsortedBuilder.values()) {
   425                 builder.build(golden, m, random);
   430                 builder.build((int[]) gold[0], m, random);
   426                 int[] test = golden.clone();
   431                 convertData(length);
   427 
   432 
   428                 for (TypeConverter converter : TypeConverter.values()) {
   433                 for (int i = 0; i < test.length; i++) {
   429                     out.println("Test 'check sum': " + converter +
   434                     printTestName("Test with check sum", random, length,
   430                         " " + builder + "random = " + random.getSeed() +
   435                         ", m = " + m + ", " + getType(i) + " " + builder);
   431                         ", length = " + length + ", m = " + m);
   436                     sortingHelper.sort(test[i]);
   432                     Object convertedGolden = converter.convert(golden);
   437                     checkWithCheckSum(test[i], gold[i]);
   433                     Object convertedTest = converter.convert(test);
       
   434                     sort(convertedTest);
       
   435                     checkWithCheckSum(convertedTest, convertedGolden);
       
   436                 }
   438                 }
   437             }
   439             }
   438         }
   440         }
   439         out.println();
   441         out.println();
   440     }
   442     }
   441 
   443 
   442     private static void testAndCheckWithScrambling(int length, MyRandom random) {
   444     private void testWithScrambling(int length, TestRandom random) {
   443         ourDescription = "Check sorting with scrambling";
   445         for (int m = 1; m <= length; m <<= 1) {
   444         int[] golden = new int[length];
       
   445 
       
   446         for (int m = 1; m <= 7; m++) {
       
   447             if (m > length) {
       
   448                 break;
       
   449             }
       
   450             for (SortedBuilder builder : SortedBuilder.values()) {
   446             for (SortedBuilder builder : SortedBuilder.values()) {
   451                 builder.build(golden, m);
   447                 builder.build((int[]) gold[0], m);
   452                 int[] test = golden.clone();
   448                 convertData(length);
   453                 scramble(test, random);
   449 
   454 
   450                 for (int i = 0; i < test.length; i++) {
   455                 for (TypeConverter converter : TypeConverter.values()) {
   451                     printTestName("Test with scrambling", random, length,
   456                     out.println("Test 'scrambling': " + converter +
   452                         ", m = " + m + ", " + getType(i) + " " + builder);
   457                        " " + builder + "random = " + random.getSeed() +
   453                     scramble(test[i], random);
   458                        ", length = " + length + ", m = " + m);
   454                     sortingHelper.sort(test[i]);
   459                     Object convertedGolden = converter.convert(golden);
   455                     compare(test[i], gold[i]);
   460                     Object convertedTest = converter.convert(test);
       
   461                     sort(convertedTest);
       
   462                     compare(convertedTest, convertedGolden);
       
   463                 }
   456                 }
   464             }
   457             }
   465         }
   458         }
   466         out.println();
   459         out.println();
   467     }
   460     }
   468 
   461 
   469     private static void testAndCheckFloat(int length, MyRandom random) {
   462     private void testNegativeZero(int length, TestRandom random) {
   470         ourDescription = "Check float sorting";
   463         for (int i = 5; i < test.length; i++) {
   471         float[] golden = new float[length];
   464             printTestName("Test negative zero -0.0", random, length, " " + getType(i));
   472         final int MAX = 10;
   465 
   473         boolean newLine = false;
   466             NegativeZeroBuilder builder = NegativeZeroBuilder.values() [i - 5];
   474 
   467             builder.build(test[i], random);
   475         for (int a = 0; a <= MAX; a++) {
   468 
   476             for (int g = 0; g <= MAX; g++) {
   469             sortingHelper.sort(test[i]);
   477                 for (int z = 0; z <= MAX; z++) {
   470             checkNegativeZero(test[i]);
   478                     for (int n = 0; n <= MAX; n++) {
   471         }
   479                         for (int p = 0; p <= MAX; p++) {
   472         out.println();
   480                             if (a + g + z + n + p > length) {
   473     }
       
   474 
       
   475     private void testFloatingPointSorting(int length, TestRandom random) {
       
   476         if (length < 2) {
       
   477             return;
       
   478         }
       
   479         final int MAX = 13;
       
   480 
       
   481         for (int a = 0; a < MAX; a++) {
       
   482             for (int g = 0; g < MAX; g++) {
       
   483                 for (int z = 0; z < MAX; z++) {
       
   484                     for (int n = 0; n < MAX; n++) {
       
   485                         for (int p = 0; p < MAX; p++) {
       
   486                             if (a + g + z + n + p != length) {
   481                                 continue;
   487                                 continue;
   482                             }
   488                             }
   483                             if (a + g + z + n + p < length) {
   489                             for (int i = 5; i < test.length; i++) {
   484                                 continue;
   490                                 printTestName("Test float-pointing sorting", random, length,
       
   491                                     ", a = " + a + ", g = " + g + ", z = " + z +
       
   492                                     ", n = " + n + ", p = " + p + ", " + getType(i));
       
   493                                 FloatingPointBuilder builder = FloatingPointBuilder.values()[i - 5];
       
   494                                 builder.build(gold[i], a, g, z, n, p, random);
       
   495                                 copy(test[i], gold[i]);
       
   496                                 scramble(test[i], random);
       
   497                                 sortingHelper.sort(test[i]);
       
   498                                 compare(test[i], gold[i], a, n, g);
   485                             }
   499                             }
   486                             for (FloatBuilder builder : FloatBuilder.values()) {
       
   487                                 out.println("Test 'float': random = " + random.getSeed() +
       
   488                                    ", length = " + length + ", a = " + a + ", g = " +
       
   489                                    g + ", z = " + z + ", n = " + n + ", p = " + p);
       
   490                                 builder.build(golden, a, g, z, n, p, random);
       
   491                                 float[] test = golden.clone();
       
   492                                 scramble(test, random);
       
   493                                 sort(test);
       
   494                                 compare(test, golden, a, n, g);
       
   495                             }
       
   496                             newLine = true;
       
   497                         }
   500                         }
   498                     }
   501                     }
   499                 }
   502                 }
   500             }
   503             }
   501         }
   504         }
   502         if (newLine) {
   505 
   503             out.println();
   506         for (int m = 13; m > 4; m--) {
   504         }
   507             int t = length / m;
   505     }
   508             int g = t, z = t, n = t, p = t;
   506 
   509             int a = length - g - z - n - p;
   507     private static void testAndCheckDouble(int length, MyRandom random) {
   510 
   508         ourDescription = "Check double sorting";
   511             for (int i = 5; i < test.length; i++) {
   509         double[] golden = new double[length];
   512                 printTestName("Test float-pointing sorting", random, length,
   510         final int MAX = 10;
   513                     ", a = " + a + ", g = " + g + ", z = " + z +
   511         boolean newLine = false;
   514                     ", n = " + n + ", p = " + p + ", " + getType(i));
   512 
   515                 FloatingPointBuilder builder = FloatingPointBuilder.values() [i - 5];
   513         for (int a = 0; a <= MAX; a++) {
   516                 builder.build(gold[i], a, g, z, n, p, random);
   514             for (int g = 0; g <= MAX; g++) {
   517                 copy(test[i], gold[i]);
   515                 for (int z = 0; z <= MAX; z++) {
   518                 scramble(test[i], random);
   516                     for (int n = 0; n <= MAX; n++) {
   519                 sortingHelper.sort(test[i]);
   517                         for (int p = 0; p <= MAX; p++) {
   520                 compare(test[i], gold[i], a, n, g);
   518                             if (a + g + z + n + p > length) {
   521             }
   519                                 continue;
   522         }
   520                             }
   523         out.println();
   521                             if (a + g + z + n + p < length) {
   524     }
   522                                 continue;
   525 
   523                             }
   526     private void prepareSubArray(int[] a, int fromIndex, int toIndex) {
   524                             for (DoubleBuilder builder : DoubleBuilder.values()) {
       
   525                                 out.println("Test 'double': random = " + random.getSeed() +
       
   526                                    ", length = " + length + ", a = " + a + ", g = " +
       
   527                                    g + ", z = " + z + ", n = " + n + ", p = " + p);
       
   528                                 builder.build(golden, a, g, z, n, p, random);
       
   529                                 double[] test = golden.clone();
       
   530                                 scramble(test, random);
       
   531                                 sort(test);
       
   532                                 compare(test, golden, a, n, g);
       
   533                             }
       
   534                             newLine = true;
       
   535                         }
       
   536                     }
       
   537                 }
       
   538             }
       
   539         }
       
   540         if (newLine) {
       
   541             out.println();
       
   542         }
       
   543     }
       
   544 
       
   545     private static void prepareSubArray(int[] a, int fromIndex, int toIndex, int m) {
       
   546         for (int i = 0; i < fromIndex; i++) {
   527         for (int i = 0; i < fromIndex; i++) {
   547             a[i] = 0xDEDA;
   528             a[i] = A380;
   548         }
   529         }
   549         int middle = (fromIndex + toIndex) >>> 1;
   530         int middle = (fromIndex + toIndex) >>> 1;
   550         int k = 0;
   531         int k = 0;
   551 
   532 
   552         for (int i = fromIndex; i < middle; i++) {
   533         for (int i = fromIndex; i < middle; i++) {
   553             a[i] = k++;
   534             a[i] = k++;
   554         }
   535         }
       
   536 
   555         for (int i = middle; i < toIndex; i++) {
   537         for (int i = middle; i < toIndex; i++) {
   556             a[i] = k--;
   538             a[i] = k--;
   557         }
   539         }
       
   540 
   558         for (int i = toIndex; i < a.length; i++) {
   541         for (int i = toIndex; i < a.length; i++) {
   559             a[i] = 0xBABA;
   542             a[i] = B747;
   560         }
   543         }
   561     }
   544     }
   562 
   545 
   563     private static void scramble(int[] a, Random random) {
   546     private void scramble(Object a, Random random) {
       
   547         if (a instanceof int[]) {
       
   548             scramble((int[]) a, random);
       
   549         } else if (a instanceof long[]) {
       
   550             scramble((long[]) a, random);
       
   551         } else if (a instanceof byte[]) {
       
   552             scramble((byte[]) a, random);
       
   553         } else if (a instanceof char[]) {
       
   554             scramble((char[]) a, random);
       
   555         } else if (a instanceof short[]) {
       
   556             scramble((short[]) a, random);
       
   557         } else if (a instanceof float[]) {
       
   558             scramble((float[]) a, random);
       
   559         } else if (a instanceof double[]) {
       
   560             scramble((double[]) a, random);
       
   561         } else {
       
   562             fail("Unknown type of array: " + a.getClass().getName());
       
   563         }
       
   564     }
       
   565 
       
   566     private void scramble(int[] a, Random random) {
   564         for (int i = 0; i < a.length * 7; i++) {
   567         for (int i = 0; i < a.length * 7; i++) {
   565             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   568             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   566         }
   569         }
   567     }
   570     }
   568 
   571 
   569     private static void scramble(float[] a, Random random) {
   572     private void scramble(long[] a, Random random) {
   570         for (int i = 0; i < a.length * 7; i++) {
   573         for (int i = 0; i < a.length * 7; i++) {
   571             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   574             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   572         }
   575         }
   573     }
   576     }
   574 
   577 
   575     private static void scramble(double[] a, Random random) {
   578     private void scramble(byte[] a, Random random) {
   576         for (int i = 0; i < a.length * 7; i++) {
   579         for (int i = 0; i < a.length * 7; i++) {
   577             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   580             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   578         }
   581         }
   579     }
   582     }
   580 
   583 
   581     private static void swap(int[] a, int i, int j) {
   584     private void scramble(char[] a, Random random) {
   582         int t = a[i];
   585         for (int i = 0; i < a.length * 7; i++) {
   583         a[i] = a[j];
   586             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   584         a[j] = t;
   587         }
   585     }
   588     }
   586 
   589 
   587     private static void swap(float[] a, int i, int j) {
   590     private void scramble(short[] a, Random random) {
   588         float t = a[i];
   591         for (int i = 0; i < a.length * 7; i++) {
   589         a[i] = a[j];
   592             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   590         a[j] = t;
   593         }
   591     }
   594     }
   592 
   595 
   593     private static void swap(double[] a, int i, int j) {
   596     private void scramble(float[] a, Random random) {
   594         double t = a[i];
   597         for (int i = 0; i < a.length * 7; i++) {
   595         a[i] = a[j];
   598             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   596         a[j] = t;
   599         }
   597     }
   600     }
   598 
   601 
   599     private static enum TypeConverter {
   602     private void scramble(double[] a, Random random) {
   600         INT {
   603         for (int i = 0; i < a.length * 7; i++) {
   601             Object convert(int[] a) {
   604             swap(a, random.nextInt(a.length), random.nextInt(a.length));
   602                 return a.clone();
   605         }
   603             }
   606     }
   604         },
   607 
   605         LONG {
   608     private void swap(int[] a, int i, int j) {
   606             Object convert(int[] a) {
   609         int t = a[i]; a[i] = a[j]; a[j] = t;
   607                 long[] b = new long[a.length];
   610     }
   608 
   611 
   609                 for (int i = 0; i < a.length; i++) {
   612     private void swap(long[] a, int i, int j) {
   610                     b[i] = (long) a[i];
   613         long t = a[i]; a[i] = a[j]; a[j] = t;
   611                 }
   614     }
   612                 return b;
   615 
   613             }
   616     private void swap(byte[] a, int i, int j) {
   614         },
   617         byte t = a[i]; a[i] = a[j]; a[j] = t;
   615         BYTE {
   618     }
   616             Object convert(int[] a) {
   619 
   617                 byte[] b = new byte[a.length];
   620     private void swap(char[] a, int i, int j) {
   618 
   621         char t = a[i]; a[i] = a[j]; a[j] = t;
   619                 for (int i = 0; i < a.length; i++) {
   622     }
   620                     b[i] = (byte) a[i];
   623 
   621                 }
   624     private void swap(short[] a, int i, int j) {
   622                 return b;
   625         short t = a[i]; a[i] = a[j]; a[j] = t;
   623             }
   626     }
   624         },
   627 
   625         SHORT {
   628     private void swap(float[] a, int i, int j) {
   626             Object convert(int[] a) {
   629         float t = a[i]; a[i] = a[j]; a[j] = t;
   627                 short[] b = new short[a.length];
   630     }
   628 
   631 
   629                 for (int i = 0; i < a.length; i++) {
   632     private void swap(double[] a, int i, int j) {
   630                     b[i] = (short) a[i];
   633         double t = a[i]; a[i] = a[j]; a[j] = t;
   631                 }
   634     }
   632                 return b;
   635 
   633             }
   636     private void checkWithCheckSum(Object test, Object gold) {
   634         },
   637         checkSorted(test);
   635         CHAR {
   638         checkCheckSum(test, gold);
   636             Object convert(int[] a) {
   639     }
   637                 char[] b = new char[a.length];
   640 
   638 
   641     private void fail(String message) {
   639                 for (int i = 0; i < a.length; i++) {
   642         err.format("\n*** TEST FAILED ***\n\n%s\n\n", message);
   640                     b[i] = (char) a[i];
   643         throw new RuntimeException("Test failed");
   641                 }
   644     }
   642                 return b;
   645 
   643             }
   646     private void checkNegativeZero(Object a) {
   644         },
   647         if (a instanceof float[]) {
   645         FLOAT {
   648             checkNegativeZero((float[]) a);
   646             Object convert(int[] a) {
   649         } else if (a instanceof double[]) {
   647                 float[] b = new float[a.length];
   650             checkNegativeZero((double[]) a);
   648 
   651         } else {
   649                 for (int i = 0; i < a.length; i++) {
   652             fail("Unknown type of array: " + a.getClass().getName());
   650                     b[i] = (float) a[i];
   653         }
   651                 }
   654     }
   652                 return b;
   655 
   653             }
   656     private void checkNegativeZero(float[] a) {
   654         },
   657         for (int i = 0; i < a.length - 1; i++) {
   655         DOUBLE {
   658             if (Float.floatToRawIntBits(a[i]) == 0 && Float.floatToRawIntBits(a[i + 1]) < 0) {
   656             Object convert(int[] a) {
   659                 fail(a[i] + " before " + a[i + 1] + " at position " + i);
   657                 double[] b = new double[a.length];
   660             }
   658 
   661         }
   659                 for (int i = 0; i < a.length; i++) {
   662     }
   660                     b[i] = (double) a[i];
   663 
   661                 }
   664     private void checkNegativeZero(double[] a) {
   662                 return b;
   665         for (int i = 0; i < a.length - 1; i++) {
   663             }
   666             if (Double.doubleToRawLongBits(a[i]) == 0 && Double.doubleToRawLongBits(a[i + 1]) < 0) {
   664         },
   667                 fail(a[i] + " before " + a[i + 1] + " at position " + i);
   665         INTEGER {
   668             }
   666             Object convert(int[] a) {
   669         }
   667                 Integer[] b = new Integer[a.length];
   670     }
   668 
   671 
   669                 for (int i = 0; i < a.length; i++) {
   672     private void compare(Object a, Object b, int numNaN, int numNeg, int numNegZero) {
   670                     b[i] = new Integer(a[i]);
   673         if (a instanceof float[]) {
   671                 }
   674             compare((float[]) a, (float[]) b, numNaN, numNeg, numNegZero);
   672                 return b;
   675         } else if (a instanceof double[]) {
   673             }
   676             compare((double[]) a, (double[]) b, numNaN, numNeg, numNegZero);
   674         };
   677         } else {
   675 
   678             fail("Unknown type of array: " + a.getClass().getName());
   676         abstract Object convert(int[] a);
   679         }
   677 
   680     }
   678         @Override public String toString() {
   681 
   679             String name = name();
   682     private void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZero) {
   680 
       
   681             for (int i = name.length(); i < 9; i++) {
       
   682                 name += " ";
       
   683             }
       
   684             return name;
       
   685         }
       
   686     }
       
   687 
       
   688     private static enum FloatBuilder {
       
   689         SIMPLE {
       
   690             void build(float[] x, int a, int g, int z, int n, int p, Random random) {
       
   691                 int fromIndex = 0;
       
   692                 float negativeValue = -random.nextFloat();
       
   693                 float positiveValue =  random.nextFloat();
       
   694 
       
   695                 writeValue(x, negativeValue, fromIndex, n);
       
   696                 fromIndex += n;
       
   697 
       
   698                 writeValue(x, -0.0f, fromIndex, g);
       
   699                 fromIndex += g;
       
   700 
       
   701                 writeValue(x, 0.0f, fromIndex, z);
       
   702                 fromIndex += z;
       
   703 
       
   704                 writeValue(x, positiveValue, fromIndex, p);
       
   705                 fromIndex += p;
       
   706 
       
   707                 writeValue(x, Float.NaN, fromIndex, a);
       
   708             }
       
   709         };
       
   710 
       
   711         abstract void build(float[] x, int a, int g, int z, int n, int p, Random random);
       
   712     }
       
   713 
       
   714     private static enum DoubleBuilder {
       
   715         SIMPLE {
       
   716             void build(double[] x, int a, int g, int z, int n, int p, Random random) {
       
   717                 int fromIndex = 0;
       
   718                 double negativeValue = -random.nextFloat();
       
   719                 double positiveValue =  random.nextFloat();
       
   720 
       
   721                 writeValue(x, negativeValue, fromIndex, n);
       
   722                 fromIndex += n;
       
   723 
       
   724                 writeValue(x, -0.0d, fromIndex, g);
       
   725                 fromIndex += g;
       
   726 
       
   727                 writeValue(x, 0.0d, fromIndex, z);
       
   728                 fromIndex += z;
       
   729 
       
   730                 writeValue(x, positiveValue, fromIndex, p);
       
   731                 fromIndex += p;
       
   732 
       
   733                 writeValue(x, Double.NaN, fromIndex, a);
       
   734             }
       
   735         };
       
   736 
       
   737         abstract void build(double[] x, int a, int g, int z, int n, int p, Random random);
       
   738     }
       
   739 
       
   740     private static void writeValue(float[] a, float value, int fromIndex, int count) {
       
   741         for (int i = fromIndex; i < fromIndex + count; i++) {
       
   742             a[i] = value;
       
   743         }
       
   744     }
       
   745 
       
   746     private static void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZero) {
       
   747         for (int i = a.length - numNaN; i < a.length; i++) {
   683         for (int i = a.length - numNaN; i < a.length; i++) {
   748             if (a[i] == a[i]) {
   684             if (a[i] == a[i]) {
   749                 failed("On position " + i + " must be NaN instead of " + a[i]);
   685                 fail("There must be NaN instead of " + a[i] + " at position " + i);
   750             }
   686             }
   751         }
   687         }
   752         final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f);
   688         final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f);
   753 
   689 
   754         for (int i = numNeg; i < numNeg + numNegZero; i++) {
   690         for (int i = numNeg; i < numNeg + numNegZero; i++) {
   755             if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) {
   691             if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) {
   756                 failed("On position " + i + " must be -0.0 instead of " + a[i]);
   692                 fail("There must be -0.0 instead of " + a[i] + " at position " + i);
   757             }
   693             }
   758         }
   694         }
       
   695 
   759         for (int i = 0; i < a.length - numNaN; i++) {
   696         for (int i = 0; i < a.length - numNaN; i++) {
   760             if (a[i] != b[i]) {
   697             if (a[i] != b[i]) {
   761                 failedCompare(i, "" + a[i], "" + b[i]);
   698                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
   762             }
   699             }
   763         }
   700         }
   764     }
   701     }
   765 
   702 
   766     private static void writeValue(double[] a, double value, int fromIndex, int count) {
   703     private void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) {
   767         for (int i = fromIndex; i < fromIndex + count; i++) {
       
   768             a[i] = value;
       
   769         }
       
   770     }
       
   771 
       
   772     private static void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) {
       
   773         for (int i = a.length - numNaN; i < a.length; i++) {
   704         for (int i = a.length - numNaN; i < a.length; i++) {
   774             if (a[i] == a[i]) {
   705             if (a[i] == a[i]) {
   775                 failed("On position " + i + " must be NaN instead of " + a[i]);
   706                 fail("There must be NaN instead of " + a[i] + " at position " + i);
   776             }
   707             }
   777         }
   708         }
   778         final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d);
   709         final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d);
   779 
   710 
   780         for (int i = numNeg; i < numNeg + numNegZero; i++) {
   711         for (int i = numNeg; i < numNeg + numNegZero; i++) {
   781             if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) {
   712             if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) {
   782                 failed("On position " + i + " must be -0.0 instead of " + a[i]);
   713                 fail("There must be -0.0 instead of " + a[i] + " at position " + i);
   783             }
   714             }
   784         }
   715         }
       
   716 
   785         for (int i = 0; i < a.length - numNaN; i++) {
   717         for (int i = 0; i < a.length - numNaN; i++) {
   786             if (a[i] != b[i]) {
   718             if (a[i] != b[i]) {
   787                 failedCompare(i, "" + a[i], "" + b[i]);
   719                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
   788             }
   720             }
   789         }
   721         }
       
   722     }
       
   723 
       
   724     private void compare(Object a, Object b) {
       
   725         if (a instanceof int[]) {
       
   726             compare((int[]) a, (int[]) b);
       
   727         } else if (a instanceof long[]) {
       
   728             compare((long[]) a, (long[]) b);
       
   729         } else if (a instanceof byte[]) {
       
   730             compare((byte[]) a, (byte[]) b);
       
   731         } else if (a instanceof char[]) {
       
   732             compare((char[]) a, (char[]) b);
       
   733         } else if (a instanceof short[]) {
       
   734             compare((short[]) a, (short[]) b);
       
   735         } else if (a instanceof float[]) {
       
   736             compare((float[]) a, (float[]) b);
       
   737         } else if (a instanceof double[]) {
       
   738             compare((double[]) a, (double[]) b);
       
   739         } else {
       
   740             fail("Unknown type of array: " + a.getClass().getName());
       
   741         }
       
   742     }
       
   743 
       
   744     private void compare(int[] a, int[] b) {
       
   745         for (int i = 0; i < a.length; i++) {
       
   746             if (a[i] != b[i]) {
       
   747                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   748             }
       
   749         }
       
   750     }
       
   751 
       
   752     private void compare(long[] a, long[] b) {
       
   753         for (int i = 0; i < a.length; i++) {
       
   754             if (a[i] != b[i]) {
       
   755                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   756             }
       
   757         }
       
   758     }
       
   759 
       
   760     private void compare(byte[] a, byte[] b) {
       
   761         for (int i = 0; i < a.length; i++) {
       
   762             if (a[i] != b[i]) {
       
   763                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   764             }
       
   765         }
       
   766     }
       
   767 
       
   768     private void compare(char[] a, char[] b) {
       
   769         for (int i = 0; i < a.length; i++) {
       
   770             if (a[i] != b[i]) {
       
   771                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   772             }
       
   773         }
       
   774     }
       
   775 
       
   776     private void compare(short[] a, short[] b) {
       
   777         for (int i = 0; i < a.length; i++) {
       
   778             if (a[i] != b[i]) {
       
   779                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   780             }
       
   781         }
       
   782     }
       
   783 
       
   784     private void compare(float[] a, float[] b) {
       
   785         for (int i = 0; i < a.length; i++) {
       
   786             if (a[i] != b[i]) {
       
   787                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   788             }
       
   789         }
       
   790     }
       
   791 
       
   792     private void compare(double[] a, double[] b) {
       
   793         for (int i = 0; i < a.length; i++) {
       
   794             if (a[i] != b[i]) {
       
   795                 fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i);
       
   796             }
       
   797         }
       
   798     }
       
   799 
       
   800     private String getType(int i) {
       
   801         Object a = test[i];
       
   802 
       
   803         if (a instanceof int[]) {
       
   804             return "INT   ";
       
   805         }
       
   806         if (a instanceof long[]) {
       
   807             return "LONG  ";
       
   808         }
       
   809         if (a instanceof byte[]) {
       
   810             return "BYTE  ";
       
   811         }
       
   812         if (a instanceof char[]) {
       
   813             return "CHAR  ";
       
   814         }
       
   815         if (a instanceof short[]) {
       
   816             return "SHORT ";
       
   817         }
       
   818         if (a instanceof float[]) {
       
   819             return "FLOAT ";
       
   820         }
       
   821         if (a instanceof double[]) {
       
   822             return "DOUBLE";
       
   823         }
       
   824         fail("Unknown type of array: " + a.getClass().getName());
       
   825         return null;
       
   826     }
       
   827 
       
   828     private void checkSorted(Object a) {
       
   829         if (a instanceof int[]) {
       
   830             checkSorted((int[]) a);
       
   831         } else if (a instanceof long[]) {
       
   832             checkSorted((long[]) a);
       
   833         } else if (a instanceof byte[]) {
       
   834             checkSorted((byte[]) a);
       
   835         } else if (a instanceof char[]) {
       
   836             checkSorted((char[]) a);
       
   837         } else if (a instanceof short[]) {
       
   838             checkSorted((short[]) a);
       
   839         } else if (a instanceof float[]) {
       
   840             checkSorted((float[]) a);
       
   841         } else if (a instanceof double[]) {
       
   842             checkSorted((double[]) a);
       
   843         } else {
       
   844             fail("Unknown type of array: " + a.getClass().getName());
       
   845         }
       
   846     }
       
   847 
       
   848     private void checkSorted(int[] a) {
       
   849         for (int i = 0; i < a.length - 1; i++) {
       
   850             if (a[i] > a[i + 1]) {
       
   851                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   852             }
       
   853         }
       
   854     }
       
   855 
       
   856     private void checkSorted(long[] a) {
       
   857         for (int i = 0; i < a.length - 1; i++) {
       
   858             if (a[i] > a[i + 1]) {
       
   859                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   860             }
       
   861         }
       
   862     }
       
   863 
       
   864     private void checkSorted(byte[] a) {
       
   865         for (int i = 0; i < a.length - 1; i++) {
       
   866             if (a[i] > a[i + 1]) {
       
   867                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   868             }
       
   869         }
       
   870     }
       
   871 
       
   872     private void checkSorted(char[] a) {
       
   873         for (int i = 0; i < a.length - 1; i++) {
       
   874             if (a[i] > a[i + 1]) {
       
   875                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   876             }
       
   877         }
       
   878     }
       
   879 
       
   880     private void checkSorted(short[] a) {
       
   881         for (int i = 0; i < a.length - 1; i++) {
       
   882             if (a[i] > a[i + 1]) {
       
   883                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   884             }
       
   885         }
       
   886     }
       
   887 
       
   888     private void checkSorted(float[] a) {
       
   889         for (int i = 0; i < a.length - 1; i++) {
       
   890             if (a[i] > a[i + 1]) {
       
   891                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   892             }
       
   893         }
       
   894     }
       
   895 
       
   896     private void checkSorted(double[] a) {
       
   897         for (int i = 0; i < a.length - 1; i++) {
       
   898             if (a[i] > a[i + 1]) {
       
   899                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
   900             }
       
   901         }
       
   902     }
       
   903 
       
   904     private void checkCheckSum(Object test, Object gold) {
       
   905         if (checkSumXor(test) != checkSumXor(gold)) {
       
   906             fail("Original and sorted arrays are not identical [^]");
       
   907         }
       
   908         if (checkSumPlus(test) != checkSumPlus(gold)) {
       
   909             fail("Original and sorted arrays are not identical [+]");
       
   910         }
       
   911     }
       
   912 
       
   913     private int checkSumXor(Object a) {
       
   914         if (a instanceof int[]) {
       
   915             return checkSumXor((int[]) a);
       
   916         }
       
   917         if (a instanceof long[]) {
       
   918             return checkSumXor((long[]) a);
       
   919         }
       
   920         if (a instanceof byte[]) {
       
   921             return checkSumXor((byte[]) a);
       
   922         }
       
   923         if (a instanceof char[]) {
       
   924             return checkSumXor((char[]) a);
       
   925         }
       
   926         if (a instanceof short[]) {
       
   927             return checkSumXor((short[]) a);
       
   928         }
       
   929         if (a instanceof float[]) {
       
   930             return checkSumXor((float[]) a);
       
   931         }
       
   932         if (a instanceof double[]) {
       
   933             return checkSumXor((double[]) a);
       
   934         }
       
   935         fail("Unknown type of array: " + a.getClass().getName());
       
   936         return -1;
       
   937     }
       
   938 
       
   939     private int checkSumXor(int[] a) {
       
   940         int checkSum = 0;
       
   941 
       
   942         for (int e : a) {
       
   943             checkSum ^= e;
       
   944         }
       
   945         return checkSum;
       
   946     }
       
   947 
       
   948     private int checkSumXor(long[] a) {
       
   949         long checkSum = 0;
       
   950 
       
   951         for (long e : a) {
       
   952             checkSum ^= e;
       
   953         }
       
   954         return (int) checkSum;
       
   955     }
       
   956 
       
   957     private int checkSumXor(byte[] a) {
       
   958         byte checkSum = 0;
       
   959 
       
   960         for (byte e : a) {
       
   961             checkSum ^= e;
       
   962         }
       
   963         return (int) checkSum;
       
   964     }
       
   965 
       
   966     private int checkSumXor(char[] a) {
       
   967         char checkSum = 0;
       
   968 
       
   969         for (char e : a) {
       
   970             checkSum ^= e;
       
   971         }
       
   972         return (int) checkSum;
       
   973     }
       
   974 
       
   975     private int checkSumXor(short[] a) {
       
   976         short checkSum = 0;
       
   977 
       
   978         for (short e : a) {
       
   979             checkSum ^= e;
       
   980         }
       
   981         return (int) checkSum;
       
   982     }
       
   983 
       
   984     private int checkSumXor(float[] a) {
       
   985         int checkSum = 0;
       
   986 
       
   987         for (float e : a) {
       
   988             checkSum ^= (int) e;
       
   989         }
       
   990         return checkSum;
       
   991     }
       
   992 
       
   993     private int checkSumXor(double[] a) {
       
   994         int checkSum = 0;
       
   995 
       
   996         for (double e : a) {
       
   997             checkSum ^= (int) e;
       
   998         }
       
   999         return checkSum;
       
  1000     }
       
  1001 
       
  1002     private int checkSumPlus(Object a) {
       
  1003         if (a instanceof int[]) {
       
  1004             return checkSumPlus((int[]) a);
       
  1005         }
       
  1006         if (a instanceof long[]) {
       
  1007             return checkSumPlus((long[]) a);
       
  1008         }
       
  1009         if (a instanceof byte[]) {
       
  1010             return checkSumPlus((byte[]) a);
       
  1011         }
       
  1012         if (a instanceof char[]) {
       
  1013             return checkSumPlus((char[]) a);
       
  1014         }
       
  1015         if (a instanceof short[]) {
       
  1016             return checkSumPlus((short[]) a);
       
  1017         }
       
  1018         if (a instanceof float[]) {
       
  1019             return checkSumPlus((float[]) a);
       
  1020         }
       
  1021         if (a instanceof double[]) {
       
  1022             return checkSumPlus((double[]) a);
       
  1023         }
       
  1024         fail("Unknown type of array: " + a.getClass().getName());
       
  1025         return -1;
       
  1026     }
       
  1027 
       
  1028     private int checkSumPlus(int[] a) {
       
  1029         int checkSum = 0;
       
  1030 
       
  1031         for (int e : a) {
       
  1032             checkSum += e;
       
  1033         }
       
  1034         return checkSum;
       
  1035     }
       
  1036 
       
  1037     private int checkSumPlus(long[] a) {
       
  1038         long checkSum = 0;
       
  1039 
       
  1040         for (long e : a) {
       
  1041             checkSum += e;
       
  1042         }
       
  1043         return (int) checkSum;
       
  1044     }
       
  1045 
       
  1046     private int checkSumPlus(byte[] a) {
       
  1047         byte checkSum = 0;
       
  1048 
       
  1049         for (byte e : a) {
       
  1050             checkSum += e;
       
  1051         }
       
  1052         return (int) checkSum;
       
  1053     }
       
  1054 
       
  1055     private int checkSumPlus(char[] a) {
       
  1056         char checkSum = 0;
       
  1057 
       
  1058         for (char e : a) {
       
  1059             checkSum += e;
       
  1060         }
       
  1061         return (int) checkSum;
       
  1062     }
       
  1063 
       
  1064     private int checkSumPlus(short[] a) {
       
  1065         short checkSum = 0;
       
  1066 
       
  1067         for (short e : a) {
       
  1068             checkSum += e;
       
  1069         }
       
  1070         return (int) checkSum;
       
  1071     }
       
  1072 
       
  1073     private int checkSumPlus(float[] a) {
       
  1074         int checkSum = 0;
       
  1075 
       
  1076         for (float e : a) {
       
  1077             checkSum += (int) e;
       
  1078         }
       
  1079         return checkSum;
       
  1080     }
       
  1081 
       
  1082     private int checkSumPlus(double[] a) {
       
  1083         int checkSum = 0;
       
  1084 
       
  1085         for (double e : a) {
       
  1086             checkSum += (int) e;
       
  1087         }
       
  1088         return checkSum;
       
  1089     }
       
  1090 
       
  1091     private void sortByInsertionSort(Object a) {
       
  1092         if (a instanceof int[]) {
       
  1093             sortByInsertionSort((int[]) a);
       
  1094         } else if (a instanceof long[]) {
       
  1095             sortByInsertionSort((long[]) a);
       
  1096         } else if (a instanceof byte[]) {
       
  1097             sortByInsertionSort((byte[]) a);
       
  1098         } else if (a instanceof char[]) {
       
  1099             sortByInsertionSort((char[]) a);
       
  1100         } else if (a instanceof short[]) {
       
  1101             sortByInsertionSort((short[]) a);
       
  1102         } else if (a instanceof float[]) {
       
  1103             sortByInsertionSort((float[]) a);
       
  1104         } else if (a instanceof double[]) {
       
  1105             sortByInsertionSort((double[]) a);
       
  1106         } else {
       
  1107             fail("Unknown type of array: " + a.getClass().getName());
       
  1108         }
       
  1109     }
       
  1110 
       
  1111     private void sortByInsertionSort(int[] a) {
       
  1112         for (int j, i = 1; i < a.length; i++) {
       
  1113             int ai = a[i];
       
  1114 
       
  1115             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1116                 a[j + 1] = a[j];
       
  1117             }
       
  1118             a[j + 1] = ai;
       
  1119         }
       
  1120     }
       
  1121 
       
  1122     private void sortByInsertionSort(long[] a) {
       
  1123         for (int j, i = 1; i < a.length; i++) {
       
  1124             long ai = a[i];
       
  1125 
       
  1126             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1127                 a[j + 1] = a[j];
       
  1128             }
       
  1129             a[j + 1] = ai;
       
  1130         }
       
  1131     }
       
  1132 
       
  1133     private void sortByInsertionSort(byte[] a) {
       
  1134         for (int j, i = 1; i < a.length; i++) {
       
  1135             byte ai = a[i];
       
  1136 
       
  1137             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1138                 a[j + 1] = a[j];
       
  1139             }
       
  1140             a[j + 1] = ai;
       
  1141         }
       
  1142     }
       
  1143 
       
  1144     private void sortByInsertionSort(char[] a) {
       
  1145         for (int j, i = 1; i < a.length; i++) {
       
  1146             char ai = a[i];
       
  1147 
       
  1148             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1149                 a[j + 1] = a[j];
       
  1150             }
       
  1151             a[j + 1] = ai;
       
  1152         }
       
  1153     }
       
  1154 
       
  1155     private void sortByInsertionSort(short[] a) {
       
  1156         for (int j, i = 1; i < a.length; i++) {
       
  1157             short ai = a[i];
       
  1158 
       
  1159             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1160                 a[j + 1] = a[j];
       
  1161             }
       
  1162             a[j + 1] = ai;
       
  1163         }
       
  1164     }
       
  1165 
       
  1166     private void sortByInsertionSort(float[] a) {
       
  1167         for (int j, i = 1; i < a.length; i++) {
       
  1168             float ai = a[i];
       
  1169 
       
  1170             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1171                 a[j + 1] = a[j];
       
  1172             }
       
  1173             a[j + 1] = ai;
       
  1174         }
       
  1175     }
       
  1176 
       
  1177     private void sortByInsertionSort(double[] a) {
       
  1178         for (int j, i = 1; i < a.length; i++) {
       
  1179             double ai = a[i];
       
  1180 
       
  1181             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1182                 a[j + 1] = a[j];
       
  1183             }
       
  1184             a[j + 1] = ai;
       
  1185         }
       
  1186     }
       
  1187 
       
  1188     private void checkSubArray(Object a, int fromIndex, int toIndex) {
       
  1189         if (a instanceof int[]) {
       
  1190             checkSubArray((int[]) a, fromIndex, toIndex);
       
  1191         } else if (a instanceof long[]) {
       
  1192             checkSubArray((long[]) a, fromIndex, toIndex);
       
  1193         } else if (a instanceof byte[]) {
       
  1194             checkSubArray((byte[]) a, fromIndex, toIndex);
       
  1195         } else if (a instanceof char[]) {
       
  1196             checkSubArray((char[]) a, fromIndex, toIndex);
       
  1197         } else if (a instanceof short[]) {
       
  1198             checkSubArray((short[]) a, fromIndex, toIndex);
       
  1199         } else if (a instanceof float[]) {
       
  1200             checkSubArray((float[]) a, fromIndex, toIndex);
       
  1201         } else if (a instanceof double[]) {
       
  1202             checkSubArray((double[]) a, fromIndex, toIndex);
       
  1203         } else {
       
  1204             fail("Unknown type of array: " + a.getClass().getName());
       
  1205         }
       
  1206     }
       
  1207 
       
  1208     private void checkSubArray(int[] a, int fromIndex, int toIndex) {
       
  1209         for (int i = 0; i < fromIndex; i++) {
       
  1210             if (a[i] != A380) {
       
  1211                 fail("Range sort changes left element at position " + i + hex(a[i], A380));
       
  1212             }
       
  1213         }
       
  1214 
       
  1215         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1216             if (a[i] > a[i + 1]) {
       
  1217                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1218             }
       
  1219         }
       
  1220 
       
  1221         for (int i = toIndex; i < a.length; i++) {
       
  1222             if (a[i] != B747) {
       
  1223                 fail("Range sort changes right element at position " + i + hex(a[i], B747));
       
  1224             }
       
  1225         }
       
  1226     }
       
  1227 
       
  1228     private void checkSubArray(long[] a, int fromIndex, int toIndex) {
       
  1229         for (int i = 0; i < fromIndex; i++) {
       
  1230             if (a[i] != (long) A380) {
       
  1231                 fail("Range sort changes left element at position " + i + hex(a[i], A380));
       
  1232             }
       
  1233         }
       
  1234 
       
  1235         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1236             if (a[i] > a[i + 1]) {
       
  1237                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1238             }
       
  1239         }
       
  1240 
       
  1241         for (int i = toIndex; i < a.length; i++) {
       
  1242             if (a[i] != (long) B747) {
       
  1243                 fail("Range sort changes right element at position " + i + hex(a[i], B747));
       
  1244             }
       
  1245         }
       
  1246     }
       
  1247 
       
  1248     private void checkSubArray(byte[] a, int fromIndex, int toIndex) {
       
  1249         for (int i = 0; i < fromIndex; i++) {
       
  1250             if (a[i] != (byte) A380) {
       
  1251                 fail("Range sort changes left element at position " + i + hex(a[i], A380));
       
  1252             }
       
  1253         }
       
  1254 
       
  1255         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1256             if (a[i] > a[i + 1]) {
       
  1257                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1258             }
       
  1259         }
       
  1260 
       
  1261         for (int i = toIndex; i < a.length; i++) {
       
  1262             if (a[i] != (byte) B747) {
       
  1263                 fail("Range sort changes right element at position " + i + hex(a[i], B747));
       
  1264             }
       
  1265         }
       
  1266     }
       
  1267 
       
  1268     private void checkSubArray(char[] a, int fromIndex, int toIndex) {
       
  1269         for (int i = 0; i < fromIndex; i++) {
       
  1270             if (a[i] != (char) A380) {
       
  1271                 fail("Range sort changes left element at position " + i + hex(a[i], A380));
       
  1272             }
       
  1273         }
       
  1274 
       
  1275         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1276             if (a[i] > a[i + 1]) {
       
  1277                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1278             }
       
  1279         }
       
  1280 
       
  1281         for (int i = toIndex; i < a.length; i++) {
       
  1282             if (a[i] != (char) B747) {
       
  1283                 fail("Range sort changes right element at position " + i + hex(a[i], B747));
       
  1284             }
       
  1285         }
       
  1286     }
       
  1287 
       
  1288     private void checkSubArray(short[] a, int fromIndex, int toIndex) {
       
  1289         for (int i = 0; i < fromIndex; i++) {
       
  1290             if (a[i] != (short) A380) {
       
  1291                 fail("Range sort changes left element at position " + i + hex(a[i], A380));
       
  1292             }
       
  1293         }
       
  1294 
       
  1295         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1296             if (a[i] > a[i + 1]) {
       
  1297                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1298             }
       
  1299         }
       
  1300 
       
  1301         for (int i = toIndex; i < a.length; i++) {
       
  1302             if (a[i] != (short) B747) {
       
  1303                 fail("Range sort changes right element at position " + i + hex(a[i], B747));
       
  1304             }
       
  1305         }
       
  1306     }
       
  1307 
       
  1308     private void checkSubArray(float[] a, int fromIndex, int toIndex) {
       
  1309         for (int i = 0; i < fromIndex; i++) {
       
  1310             if (a[i] != (float) A380) {
       
  1311                 fail("Range sort changes left element at position " + i + hex((long) a[i], A380));
       
  1312             }
       
  1313         }
       
  1314 
       
  1315         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1316             if (a[i] > a[i + 1]) {
       
  1317                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1318             }
       
  1319         }
       
  1320 
       
  1321         for (int i = toIndex; i < a.length; i++) {
       
  1322             if (a[i] != (float) B747) {
       
  1323                 fail("Range sort changes right element at position " + i + hex((long) a[i], B747));
       
  1324             }
       
  1325         }
       
  1326     }
       
  1327 
       
  1328     private void checkSubArray(double[] a, int fromIndex, int toIndex) {
       
  1329         for (int i = 0; i < fromIndex; i++) {
       
  1330             if (a[i] != (double) A380) {
       
  1331                 fail("Range sort changes left element at position " + i + hex((long) a[i], A380));
       
  1332             }
       
  1333         }
       
  1334 
       
  1335         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1336             if (a[i] > a[i + 1]) {
       
  1337                 fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]);
       
  1338             }
       
  1339         }
       
  1340 
       
  1341         for (int i = toIndex; i < a.length; i++) {
       
  1342             if (a[i] != (double) B747) {
       
  1343                 fail("Range sort changes right element at position " + i + hex((long) a[i], B747));
       
  1344             }
       
  1345         }
       
  1346     }
       
  1347 
       
  1348     private void checkRange(Object a, int m) {
       
  1349         if (a instanceof int[]) {
       
  1350             checkRange((int[]) a, m);
       
  1351         } else if (a instanceof long[]) {
       
  1352             checkRange((long[]) a, m);
       
  1353         } else if (a instanceof byte[]) {
       
  1354             checkRange((byte[]) a, m);
       
  1355         } else if (a instanceof char[]) {
       
  1356             checkRange((char[]) a, m);
       
  1357         } else if (a instanceof short[]) {
       
  1358             checkRange((short[]) a, m);
       
  1359         } else if (a instanceof float[]) {
       
  1360             checkRange((float[]) a, m);
       
  1361         } else if (a instanceof double[]) {
       
  1362             checkRange((double[]) a, m);
       
  1363         } else {
       
  1364             fail("Unknown type of array: " + a.getClass().getName());
       
  1365         }
       
  1366     }
       
  1367 
       
  1368     private void checkRange(int[] a, int m) {
       
  1369         try {
       
  1370             sortingHelper.sort(a, m + 1, m);
       
  1371             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1372                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1373         } catch (IllegalArgumentException iae) {
       
  1374             try {
       
  1375                 sortingHelper.sort(a, -m, a.length);
       
  1376                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1377                     "as expected: fromIndex = " + (-m));
       
  1378             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1379                 try {
       
  1380                     sortingHelper.sort(a, 0, a.length + m);
       
  1381                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1382                         "as expected: toIndex = " + (a.length + m));
       
  1383                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1384             }
       
  1385         }
       
  1386     }
       
  1387 
       
  1388     private void checkRange(long[] a, int m) {
       
  1389         try {
       
  1390             sortingHelper.sort(a, m + 1, m);
       
  1391             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1392                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1393         } catch (IllegalArgumentException iae) {
       
  1394             try {
       
  1395                 sortingHelper.sort(a, -m, a.length);
       
  1396                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1397                     "as expected: fromIndex = " + (-m));
       
  1398             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1399                 try {
       
  1400                     sortingHelper.sort(a, 0, a.length + m);
       
  1401                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1402                         "as expected: toIndex = " + (a.length + m));
       
  1403                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1404             }
       
  1405         }
       
  1406     }
       
  1407 
       
  1408     private void checkRange(byte[] a, int m) {
       
  1409         try {
       
  1410             sortingHelper.sort(a, m + 1, m);
       
  1411             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1412                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1413         } catch (IllegalArgumentException iae) {
       
  1414             try {
       
  1415                 sortingHelper.sort(a, -m, a.length);
       
  1416                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1417                     "as expected: fromIndex = " + (-m));
       
  1418             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1419                 try {
       
  1420                     sortingHelper.sort(a, 0, a.length + m);
       
  1421                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1422                         "as expected: toIndex = " + (a.length + m));
       
  1423                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1424             }
       
  1425         }
       
  1426     }
       
  1427 
       
  1428     private void checkRange(char[] a, int m) {
       
  1429         try {
       
  1430             sortingHelper.sort(a, m + 1, m);
       
  1431             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1432                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1433         } catch (IllegalArgumentException iae) {
       
  1434             try {
       
  1435                 sortingHelper.sort(a, -m, a.length);
       
  1436                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1437                     "as expected: fromIndex = " + (-m));
       
  1438             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1439                 try {
       
  1440                     sortingHelper.sort(a, 0, a.length + m);
       
  1441                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1442                         "as expected: toIndex = " + (a.length + m));
       
  1443                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1444             }
       
  1445         }
       
  1446     }
       
  1447 
       
  1448     private void checkRange(short[] a, int m) {
       
  1449         try {
       
  1450             sortingHelper.sort(a, m + 1, m);
       
  1451             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1452                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1453         } catch (IllegalArgumentException iae) {
       
  1454             try {
       
  1455                 sortingHelper.sort(a, -m, a.length);
       
  1456                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1457                     "as expected: fromIndex = " + (-m));
       
  1458             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1459                 try {
       
  1460                     sortingHelper.sort(a, 0, a.length + m);
       
  1461                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1462                         "as expected: toIndex = " + (a.length + m));
       
  1463                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1464             }
       
  1465         }
       
  1466     }
       
  1467 
       
  1468     private void checkRange(float[] a, int m) {
       
  1469         try {
       
  1470             sortingHelper.sort(a, m + 1, m);
       
  1471             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1472                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1473         } catch (IllegalArgumentException iae) {
       
  1474             try {
       
  1475                 sortingHelper.sort(a, -m, a.length);
       
  1476                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1477                     "as expected: fromIndex = " + (-m));
       
  1478             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1479                 try {
       
  1480                     sortingHelper.sort(a, 0, a.length + m);
       
  1481                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1482                         "as expected: toIndex = " + (a.length + m));
       
  1483                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1484             }
       
  1485         }
       
  1486     }
       
  1487 
       
  1488     private void checkRange(double[] a, int m) {
       
  1489         try {
       
  1490             sortingHelper.sort(a, m + 1, m);
       
  1491             fail(sortingHelper + " does not throw IllegalArgumentException " +
       
  1492                 "as expected: fromIndex = " + (m + 1) + " toIndex = " + m);
       
  1493         } catch (IllegalArgumentException iae) {
       
  1494             try {
       
  1495                 sortingHelper.sort(a, -m, a.length);
       
  1496                 fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1497                     "as expected: fromIndex = " + (-m));
       
  1498             } catch (ArrayIndexOutOfBoundsException aoe) {
       
  1499                 try {
       
  1500                     sortingHelper.sort(a, 0, a.length + m);
       
  1501                     fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " +
       
  1502                         "as expected: toIndex = " + (a.length + m));
       
  1503                 } catch (ArrayIndexOutOfBoundsException expected) {}
       
  1504             }
       
  1505         }
       
  1506     }
       
  1507 
       
  1508     private void copy(Object dst, Object src) {
       
  1509         if (src instanceof float[]) {
       
  1510             copy((float[]) dst, (float[]) src);
       
  1511         } else if (src instanceof double[]) {
       
  1512             copy((double[]) dst, (double[]) src);
       
  1513         } else {
       
  1514             fail("Unknown type of array: " + src.getClass().getName());
       
  1515         }
       
  1516     }
       
  1517 
       
  1518     private void copy(float[] dst, float[] src) {
       
  1519         System.arraycopy(src, 0, dst, 0, src.length);
       
  1520     }
       
  1521 
       
  1522     private void copy(double[] dst, double[] src) {
       
  1523         System.arraycopy(src, 0, dst, 0, src.length);
       
  1524     }
       
  1525 
       
  1526     private void printTestName(String test, TestRandom random, int length) {
       
  1527         printTestName(test, random, length, "");
       
  1528     }
       
  1529 
       
  1530     private void createData(int length) {
       
  1531         gold = new Object[] {
       
  1532             new int[length], new long[length],
       
  1533             new byte[length], new char[length], new short[length],
       
  1534             new float[length], new double[length]
       
  1535         };
       
  1536 
       
  1537         test = new Object[] {
       
  1538             new int[length], new long[length],
       
  1539             new byte[length], new char[length], new short[length],
       
  1540             new float[length], new double[length]
       
  1541         };
       
  1542     }
       
  1543 
       
  1544     private void convertData(int length) {
       
  1545         for (int i = 1; i < gold.length; i++) {
       
  1546             TypeConverter converter = TypeConverter.values()[i - 1];
       
  1547             converter.convert((int[])gold[0], gold[i]);
       
  1548         }
       
  1549 
       
  1550         for (int i = 0; i < gold.length; i++) {
       
  1551             System.arraycopy(gold[i], 0, test[i], 0, length);
       
  1552         }
       
  1553     }
       
  1554 
       
  1555     private String hex(long a, int b) {
       
  1556         return ": " + Long.toHexString(a) + ", must be " + Integer.toHexString(b);
       
  1557     }
       
  1558 
       
  1559     private void printTestName(String test, TestRandom random, int length, String message) {
       
  1560         out.println( "[" + sortingHelper + "] '" + test +
       
  1561             "' length = " + length + ", random = " + random + message);
       
  1562     }
       
  1563 
       
  1564     private static enum TypeConverter {
       
  1565         LONG {
       
  1566             void convert(int[] src, Object dst) {
       
  1567                 long[] b = (long[]) dst;
       
  1568 
       
  1569                 for (int i = 0; i < src.length; i++) {
       
  1570                     b[i] = (long) src[i];
       
  1571                 }
       
  1572             }
       
  1573         },
       
  1574 
       
  1575         BYTE {
       
  1576             void convert(int[] src, Object dst) {
       
  1577                 byte[] b = (byte[]) dst;
       
  1578 
       
  1579                 for (int i = 0; i < src.length; i++) {
       
  1580                     b[i] = (byte) src[i];
       
  1581                 }
       
  1582             }
       
  1583         },
       
  1584 
       
  1585         CHAR {
       
  1586             void convert(int[] src, Object dst) {
       
  1587                 char[] b = (char[]) dst;
       
  1588 
       
  1589                 for (int i = 0; i < src.length; i++) {
       
  1590                     b[i] = (char) src[i];
       
  1591                 }
       
  1592             }
       
  1593         },
       
  1594 
       
  1595         SHORT {
       
  1596             void convert(int[] src, Object dst) {
       
  1597                 short[] b = (short[]) dst;
       
  1598 
       
  1599                 for (int i = 0; i < src.length; i++) {
       
  1600                     b[i] = (short) src[i];
       
  1601                 }
       
  1602             }
       
  1603         },
       
  1604 
       
  1605         FLOAT {
       
  1606             void convert(int[] src, Object dst) {
       
  1607                 float[] b = (float[]) dst;
       
  1608 
       
  1609                 for (int i = 0; i < src.length; i++) {
       
  1610                     b[i] = (float) src[i];
       
  1611                 }
       
  1612             }
       
  1613         },
       
  1614 
       
  1615         DOUBLE {
       
  1616             void convert(int[] src, Object dst) {
       
  1617                 double[] b = (double[]) dst;
       
  1618 
       
  1619                 for (int i = 0; i < src.length; i++) {
       
  1620                     b[i] = (double) src[i];
       
  1621                 }
       
  1622             }
       
  1623         };
       
  1624 
       
  1625         abstract void convert(int[] src, Object dst);
   790     }
  1626     }
   791 
  1627 
   792     private static enum SortedBuilder {
  1628     private static enum SortedBuilder {
   793         REPEATED {
  1629         STEPS {
   794             void build(int[] a, int m) {
  1630             void build(int[] a, int m) {
   795                 int period = a.length / m;
  1631                 for (int i = 0; i < m; i++) {
   796                 int i = 0;
  1632                     a[i] = 0;
   797                 int k = 0;
  1633                 }
   798 
  1634 
   799                 while (true) {
  1635                 for (int i = m; i < a.length; i++) {
   800                     for (int t = 1; t <= period; t++) {
  1636                     a[i] = 1;
   801                         if (i >= a.length) {
       
   802                             return;
       
   803                         }
       
   804                         a[i++] = k;
       
   805                     }
       
   806                     if (i >= a.length) {
       
   807                         return;
       
   808                     }
       
   809                     k++;
       
   810                 }
       
   811             }
       
   812         },
       
   813         ORGAN_PIPES {
       
   814             void build(int[] a, int m) {
       
   815                 int i = 0;
       
   816                 int k = m;
       
   817 
       
   818                 while (true) {
       
   819                     for (int t = 1; t <= m; t++) {
       
   820                         if (i >= a.length) {
       
   821                             return;
       
   822                         }
       
   823                         a[i++] = k;
       
   824                     }
       
   825                 }
  1637                 }
   826             }
  1638             }
   827         };
  1639         };
   828 
  1640 
   829         abstract void build(int[] a, int m);
  1641         abstract void build(int[] a, int m);
   830 
       
   831         @Override public String toString() {
       
   832             String name = name();
       
   833 
       
   834             for (int i = name.length(); i < 12; i++) {
       
   835                 name += " ";
       
   836             }
       
   837             return name;
       
   838         }
       
   839     }
       
   840 
       
   841     private static enum MergeBuilder {
       
   842         ASCENDING {
       
   843             void build(int[] a, int m) {
       
   844                 int period = a.length / m;
       
   845                 int v = 1, i = 0;
       
   846 
       
   847                 for (int k = 0; k < m; k++) {
       
   848                     v = 1;
       
   849                     for (int p = 0; p < period; p++) {
       
   850                         a[i++] = v++;
       
   851                     }
       
   852                 }
       
   853                 for (int j = i; j < a.length - 1; j++) {
       
   854                     a[j] = v++;
       
   855                 }
       
   856                 a[a.length - 1] = 0;
       
   857             }
       
   858         },
       
   859         DESCENDING {
       
   860             void build(int[] a, int m) {
       
   861                 int period = a.length / m;
       
   862                 int v = -1, i = 0;
       
   863 
       
   864                 for (int k = 0; k < m; k++) {
       
   865                     v = -1;
       
   866                     for (int p = 0; p < period; p++) {
       
   867                         a[i++] = v--;
       
   868                     }
       
   869                 }
       
   870                 for (int j = i; j < a.length - 1; j++) {
       
   871                     a[j] = v--;
       
   872                 }
       
   873                 a[a.length - 1] = 0;
       
   874             }
       
   875         };
       
   876 
       
   877         abstract void build(int[] a, int m);
       
   878 
       
   879         @Override public String toString() {
       
   880             String name = name();
       
   881 
       
   882             for (int i = name.length(); i < 12; i++) {
       
   883                 name += " ";
       
   884             }
       
   885             return name;
       
   886         }
       
   887     }
  1642     }
   888 
  1643 
   889     private static enum UnsortedBuilder {
  1644     private static enum UnsortedBuilder {
   890         RANDOM {
  1645         RANDOM {
   891             void build(int[] a, int m, Random random) {
  1646             void build(int[] a, int m, Random random) {
   892                 for (int i = 0; i < a.length; i++) {
  1647                 for (int i = 0; i < a.length; i++) {
   893                     a[i] = random.nextInt();
  1648                     a[i] = random.nextInt();
   894                 }
  1649                 }
   895             }
  1650             }
   896         },
  1651         },
       
  1652 
   897         ASCENDING {
  1653         ASCENDING {
   898             void build(int[] a, int m, Random random) {
  1654             void build(int[] a, int m, Random random) {
   899                 for (int i = 0; i < a.length; i++) {
  1655                 for (int i = 0; i < a.length; i++) {
   900                     a[i] = m + i;
  1656                     a[i] = m + i;
   901                 }
  1657                 }
   902             }
  1658             }
   903         },
  1659         },
       
  1660 
   904         DESCENDING {
  1661         DESCENDING {
   905             void build(int[] a, int m, Random random) {
  1662             void build(int[] a, int m, Random random) {
   906                 for (int i = 0; i < a.length; i++) {
  1663                 for (int i = 0; i < a.length; i++) {
   907                     a[i] = a.length - m - i;
  1664                     a[i] = a.length - m - i;
   908                 }
  1665                 }
   909             }
  1666             }
   910         },
  1667         },
   911         ALL_EQUAL {
  1668 
       
  1669         EQUAL {
   912             void build(int[] a, int m, Random random) {
  1670             void build(int[] a, int m, Random random) {
   913                 for (int i = 0; i < a.length; i++) {
  1671                 for (int i = 0; i < a.length; i++) {
   914                     a[i] = m;
  1672                     a[i] = m;
   915                 }
  1673                 }
   916             }
  1674             }
   917         },
  1675         },
       
  1676 
   918         SAW {
  1677         SAW {
   919             void build(int[] a, int m, Random random) {
  1678             void build(int[] a, int m, Random random) {
   920                 int incCount = 1;
  1679                 int incCount = 1;
   921                 int decCount = a.length;
  1680                 int decCount = a.length;
   922                 int i = 0;
  1681                 int i = 0;
   939                     }
  1698                     }
   940                     period += m;
  1699                     period += m;
   941                 }
  1700                 }
   942             }
  1701             }
   943         },
  1702         },
       
  1703 
   944         REPEATED {
  1704         REPEATED {
   945             void build(int[] a, int m, Random random) {
  1705             void build(int[] a, int m, Random random) {
   946                 for (int i = 0; i < a.length; i++) {
  1706                 for (int i = 0; i < a.length; i++) {
   947                     a[i] = i % m;
  1707                     a[i] = i % m;
   948                 }
  1708                 }
   949             }
  1709             }
   950         },
  1710         },
       
  1711 
   951         DUPLICATED {
  1712         DUPLICATED {
   952             void build(int[] a, int m, Random random) {
  1713             void build(int[] a, int m, Random random) {
   953                 for (int i = 0; i < a.length; i++) {
  1714                 for (int i = 0; i < a.length; i++) {
   954                     a[i] = random.nextInt(m);
  1715                     a[i] = random.nextInt(m);
   955                 }
  1716                 }
   956             }
  1717             }
   957         },
  1718         },
       
  1719 
   958         ORGAN_PIPES {
  1720         ORGAN_PIPES {
   959             void build(int[] a, int m, Random random) {
  1721             void build(int[] a, int m, Random random) {
   960                 int middle = a.length / (m + 1);
  1722                 int middle = a.length / (m + 1);
   961 
  1723 
   962                 for (int i = 0; i < middle; i++) {
  1724                 for (int i = 0; i < middle; i++) {
   963                     a[i] = i;
  1725                     a[i] = i;
   964                 }
  1726                 }
       
  1727 
   965                 for (int i = middle; i < a.length; i++) {
  1728                 for (int i = middle; i < a.length; i++) {
   966                     a[i] = a.length - i - 1;
  1729                     a[i] = a.length - i - 1;
   967                 }
  1730                 }
   968             }
  1731             }
   969         },
  1732         },
       
  1733 
   970         STAGGER {
  1734         STAGGER {
   971             void build(int[] a, int m, Random random) {
  1735             void build(int[] a, int m, Random random) {
   972                 for (int i = 0; i < a.length; i++) {
  1736                 for (int i = 0; i < a.length; i++) {
   973                     a[i] = (i * m + i) % a.length;
  1737                     a[i] = (i * m + i) % a.length;
   974                 }
  1738                 }
   975             }
  1739             }
   976         },
  1740         },
       
  1741 
   977         PLATEAU {
  1742         PLATEAU {
   978             void build(int[] a, int m, Random random) {
  1743             void build(int[] a, int m, Random random) {
   979                 for (int i = 0; i < a.length; i++) {
  1744                 for (int i = 0; i < a.length; i++) {
   980                     a[i] = Math.min(i, m);
  1745                     a[i] = Math.min(i, m);
   981                 }
  1746                 }
   982             }
  1747             }
   983         },
  1748         },
       
  1749 
   984         SHUFFLE {
  1750         SHUFFLE {
   985             void build(int[] a, int m, Random random) {
  1751             void build(int[] a, int m, Random random) {
   986                 int x = 0, y = 0;
  1752                 int x = 0, y = 0;
       
  1753 
   987                 for (int i = 0; i < a.length; i++) {
  1754                 for (int i = 0; i < a.length; i++) {
   988                     a[i] = random.nextBoolean() ? (x += 2) : (y += 2);
  1755                     a[i] = random.nextBoolean() ? (x += 2) : (y += 2);
   989                 }
  1756                 }
   990             }
  1757             }
       
  1758         },
       
  1759 
       
  1760         LATCH {
       
  1761             void build(int[] a, int m, Random random) {
       
  1762                 int max = a.length / m;
       
  1763                 max = max < 2 ? 2 : max;
       
  1764 
       
  1765                 for (int i = 0; i < a.length; i++) {
       
  1766                     a[i] = i % max;
       
  1767                 }
       
  1768             }
   991         };
  1769         };
   992 
  1770 
   993         abstract void build(int[] a, int m, Random random);
  1771         abstract void build(int[] a, int m, Random random);
   994 
  1772     }
   995         @Override public String toString() {
  1773 
   996             String name = name();
  1774     private static enum MergingBuilder {
   997 
  1775         ASCENDING {
   998             for (int i = name.length(); i < 12; i++) {
  1776             void build(int[] a, int m) {
   999                 name += " ";
  1777                 int period = a.length / m;
  1000             }
  1778                 int v = 1, i = 0;
  1001             return name;
  1779 
  1002         }
  1780                 for (int k = 0; k < m; k++) {
  1003     }
  1781                     v = 1;
  1004 
  1782 
  1005     private static void checkWithCheckSum(Object test, Object golden) {
  1783                     for (int p = 0; p < period; p++) {
  1006         checkSorted(test);
  1784                         a[i++] = v++;
  1007         checkCheckSum(test, golden);
  1785                     }
  1008     }
  1786                 }
  1009 
  1787 
  1010     private static void failed(String message) {
  1788                 for (int j = i; j < a.length - 1; j++) {
  1011         err.format("\n*** TEST FAILED - %s.\n\n%s.\n\n", ourDescription, message);
  1789                     a[j] = v++;
  1012         throw new RuntimeException("Test failed - see log file for details");
  1790                 }
  1013     }
  1791 
  1014 
  1792                 a[a.length - 1] = 0;
  1015     private static void failedSort(int index, String value1, String value2) {
  1793             }
  1016         failed("Array is not sorted at " + index + "-th position: " +
  1794         },
  1017             value1 + " and " + value2);
  1795 
  1018     }
  1796         DESCENDING {
  1019 
  1797             void build(int[] a, int m) {
  1020     private static void failedCompare(int index, String value1, String value2) {
  1798                 int period = a.length / m;
  1021         failed("On position " + index + " must be " + value2 + " instead of " + value1);
  1799                 int v = -1, i = 0;
  1022     }
  1800 
  1023 
  1801                 for (int k = 0; k < m; k++) {
  1024     private static void compare(Object test, Object golden) {
  1802                     v = -1;
  1025         if (test instanceof int[]) {
  1803 
  1026             compare((int[]) test, (int[]) golden);
  1804                     for (int p = 0; p < period; p++) {
  1027         } else if (test instanceof long[]) {
  1805                         a[i++] = v--;
  1028             compare((long[]) test, (long[]) golden);
  1806                     }
  1029         } else if (test instanceof short[]) {
  1807                 }
  1030             compare((short[]) test, (short[]) golden);
  1808 
  1031         } else if (test instanceof byte[]) {
  1809                 for (int j = i; j < a.length - 1; j++) {
  1032             compare((byte[]) test, (byte[]) golden);
  1810                     a[j] = v--;
  1033         } else if (test instanceof char[]) {
  1811                 }
  1034             compare((char[]) test, (char[]) golden);
  1812 
  1035         } else if (test instanceof float[]) {
  1813                 a[a.length - 1] = 0;
  1036             compare((float[]) test, (float[]) golden);
  1814             }
  1037         } else if (test instanceof double[]) {
  1815         },
  1038             compare((double[]) test, (double[]) golden);
  1816 
  1039         } else if (test instanceof Integer[]) {
  1817         POINT {
  1040             compare((Integer[]) test, (Integer[]) golden);
  1818             void build(int[] a, int m) {
  1041         } else {
  1819                 for (int i = 0; i < a.length; i++) {
  1042             failed("Unknow type of array: " + test + " of class " +
  1820                     a[i] = 0;
  1043                 test.getClass().getName());
  1821                 }
  1044         }
  1822                 a[a.length / 2] = m;
  1045     }
  1823             }
  1046 
  1824         },
  1047     private static void compare(int[] a, int[] b) {
  1825 
  1048         for (int i = 0; i < a.length; i++) {
  1826         LINE {
  1049             if (a[i] != b[i]) {
  1827             void build(int[] a, int m) {
  1050                 failedCompare(i, "" + a[i], "" + b[i]);
  1828                 for (int i = 0; i < a.length; i++) {
  1051             }
  1829                     a[i] = i;
  1052         }
  1830                 }
  1053     }
  1831                 reverse(a, 0, a.length - 1);
  1054 
  1832             }
  1055     private static void compare(long[] a, long[] b) {
  1833         },
  1056         for (int i = 0; i < a.length; i++) {
  1834 
  1057             if (a[i] != b[i]) {
  1835         PEARL {
  1058                 failedCompare(i, "" + a[i], "" + b[i]);
  1836             void build(int[] a, int m) {
  1059             }
  1837                 for (int i = 0; i < a.length; i++) {
  1060         }
  1838                     a[i] = i;
  1061     }
  1839                 }
  1062 
  1840                 reverse(a, 0, 2);
  1063     private static void compare(short[] a, short[] b) {
  1841             }
  1064         for (int i = 0; i < a.length; i++) {
  1842         },
  1065             if (a[i] != b[i]) {
  1843 
  1066                 failedCompare(i, "" + a[i], "" + b[i]);
  1844         RING {
  1067             }
  1845             void build(int[] a, int m) {
  1068         }
  1846                 int k1 = a.length / 3;
  1069     }
  1847                 int k2 = a.length / 3 * 2;
  1070 
  1848                 int level = a.length / 3;
  1071     private static void compare(byte[] a, byte[] b) {
  1849 
  1072         for (int i = 0; i < a.length; i++) {
  1850                 for (int i = 0, k = level; i < k1; i++) {
  1073             if (a[i] != b[i]) {
  1851                     a[i] = k--;
  1074                 failedCompare(i, "" + a[i], "" + b[i]);
  1852                 }
  1075             }
  1853 
  1076         }
  1854                 for (int i = k1; i < k2; i++) {
  1077     }
  1855                     a[i] = 0;
  1078 
  1856                 }
  1079     private static void compare(char[] a, char[] b) {
  1857 
  1080         for (int i = 0; i < a.length; i++) {
  1858                 for (int i = k2, k = level; i < a.length; i++) {
  1081             if (a[i] != b[i]) {
  1859                     a[i] = k--;
  1082                 failedCompare(i, "" + a[i], "" + b[i]);
  1860                 }
  1083             }
  1861             }
  1084         }
  1862         };
  1085     }
  1863 
  1086 
  1864         abstract void build(int[] a, int m);
  1087     private static void compare(float[] a, float[] b) {
  1865 
  1088         for (int i = 0; i < a.length; i++) {
  1866         private static void reverse(int[] a, int lo, int hi) {
  1089             if (a[i] != b[i]) {
  1867             for (--hi; lo < hi; ) {
  1090                 failedCompare(i, "" + a[i], "" + b[i]);
  1868                 int tmp = a[lo];
  1091             }
  1869                 a[lo++] = a[hi];
  1092         }
  1870                 a[hi--] = tmp;
  1093     }
  1871             }
  1094 
  1872         }
  1095     private static void compare(double[] a, double[] b) {
  1873     }
  1096         for (int i = 0; i < a.length; i++) {
  1874 
  1097             if (a[i] != b[i]) {
  1875     private static enum NegativeZeroBuilder {
  1098                 failedCompare(i, "" + a[i], "" + b[i]);
  1876         FLOAT {
  1099             }
  1877             void build(Object o, Random random) {
  1100         }
  1878                 float[] a = (float[]) o;
  1101     }
  1879 
  1102 
  1880                 for (int i = 0; i < a.length; i++) {
  1103     private static void compare(Integer[] a, Integer[] b) {
  1881                     a[i] = random.nextBoolean() ? -0.0f : 0.0f;
  1104         for (int i = 0; i < a.length; i++) {
  1882                 }
  1105             if (a[i].compareTo(b[i]) != 0) {
  1883             }
  1106                 failedCompare(i, "" + a[i], "" + b[i]);
  1884         },
  1107             }
  1885 
  1108         }
  1886         DOUBLE {
  1109     }
  1887             void build(Object o, Random random) {
  1110 
  1888                 double[] a = (double[]) o;
  1111     private static void checkSorted(Object object) {
  1889 
  1112         if (object instanceof int[]) {
  1890                 for (int i = 0; i < a.length; i++) {
  1113             checkSorted((int[]) object);
  1891                     a[i] = random.nextBoolean() ? -0.0d : 0.0d;
  1114         } else if (object instanceof long[]) {
  1892                 }
  1115             checkSorted((long[]) object);
  1893             }
  1116         } else if (object instanceof short[]) {
  1894         };
  1117             checkSorted((short[]) object);
  1895 
  1118         } else if (object instanceof byte[]) {
  1896         abstract void build(Object o, Random random);
  1119             checkSorted((byte[]) object);
  1897     }
  1120         } else if (object instanceof char[]) {
  1898 
  1121             checkSorted((char[]) object);
  1899     private static enum FloatingPointBuilder {
  1122         } else if (object instanceof float[]) {
  1900         FLOAT {
  1123             checkSorted((float[]) object);
  1901             void build(Object o, int a, int g, int z, int n, int p, Random random) {
  1124         } else if (object instanceof double[]) {
  1902                 float negativeValue = -random.nextFloat();
  1125             checkSorted((double[]) object);
  1903                 float positiveValue =  random.nextFloat();
  1126         } else if (object instanceof Integer[]) {
  1904                 float[] x = (float[]) o;
  1127             checkSorted((Integer[]) object);
  1905                 int fromIndex = 0;
  1128         } else {
  1906 
  1129             failed("Unknow type of array: " + object + " of class " +
  1907                 writeValue(x, negativeValue, fromIndex, n);
  1130                 object.getClass().getName());
  1908                 fromIndex += n;
  1131         }
  1909 
  1132     }
  1910                 writeValue(x, -0.0f, fromIndex, g);
  1133 
  1911                 fromIndex += g;
  1134     private static void checkSorted(int[] a) {
  1912 
  1135         for (int i = 0; i < a.length - 1; i++) {
  1913                 writeValue(x, 0.0f, fromIndex, z);
  1136             if (a[i] > a[i + 1]) {
  1914                 fromIndex += z;
  1137                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1915 
  1138             }
  1916                 writeValue(x, positiveValue, fromIndex, p);
  1139         }
  1917                 fromIndex += p;
  1140     }
  1918 
  1141 
  1919                 writeValue(x, Float.NaN, fromIndex, a);
  1142     private static void checkSorted(long[] a) {
  1920             }
  1143         for (int i = 0; i < a.length - 1; i++) {
  1921         },
  1144             if (a[i] > a[i + 1]) {
  1922 
  1145                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1923         DOUBLE {
  1146             }
  1924             void build(Object o, int a, int g, int z, int n, int p, Random random) {
  1147         }
  1925                 double negativeValue = -random.nextFloat();
  1148     }
  1926                 double positiveValue =  random.nextFloat();
  1149 
  1927                 double[] x = (double[]) o;
  1150     private static void checkSorted(short[] a) {
  1928                 int fromIndex = 0;
  1151         for (int i = 0; i < a.length - 1; i++) {
  1929 
  1152             if (a[i] > a[i + 1]) {
  1930                 writeValue(x, negativeValue, fromIndex, n);
  1153                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1931                 fromIndex += n;
  1154             }
  1932 
  1155         }
  1933                 writeValue(x, -0.0d, fromIndex, g);
  1156     }
  1934                 fromIndex += g;
  1157 
  1935 
  1158     private static void checkSorted(byte[] a) {
  1936                 writeValue(x, 0.0d, fromIndex, z);
  1159         for (int i = 0; i < a.length - 1; i++) {
  1937                 fromIndex += z;
  1160             if (a[i] > a[i + 1]) {
  1938 
  1161                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1939                 writeValue(x, positiveValue, fromIndex, p);
  1162             }
  1940                 fromIndex += p;
  1163         }
  1941 
  1164     }
  1942                 writeValue(x, Double.NaN, fromIndex, a);
  1165 
  1943             }
  1166     private static void checkSorted(char[] a) {
  1944         };
  1167         for (int i = 0; i < a.length - 1; i++) {
  1945 
  1168             if (a[i] > a[i + 1]) {
  1946         abstract void build(Object o, int a, int g, int z, int n, int p, Random random);
  1169                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1947 
  1170             }
  1948         private static void writeValue(float[] a, float value, int fromIndex, int count) {
  1171         }
  1949             for (int i = fromIndex; i < fromIndex + count; i++) {
  1172     }
  1950                 a[i] = value;
  1173 
  1951             }
  1174     private static void checkSorted(float[] a) {
  1952         }
  1175         for (int i = 0; i < a.length - 1; i++) {
  1953 
  1176             if (a[i] > a[i + 1]) {
  1954         private static void writeValue(double[] a, double value, int fromIndex, int count) {
  1177                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1955             for (int i = fromIndex; i < fromIndex + count; i++) {
  1178             }
  1956                 a[i] = value;
  1179         }
  1957             }
  1180     }
  1958         }
  1181 
  1959     }
  1182     private static void checkSorted(double[] a) {
  1960 
  1183         for (int i = 0; i < a.length - 1; i++) {
  1961     private static Comparator<Pair> pairComparator = new Comparator<Pair>() {
  1184             if (a[i] > a[i + 1]) {
  1962 
  1185                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1963         @Override
  1186             }
  1964         public int compare(Pair p1, Pair p2) {
  1187         }
  1965             return p1.compareTo(p2);
  1188     }
  1966         }
  1189 
  1967     };
  1190     private static void checkSorted(Integer[] a) {
  1968 
  1191         for (int i = 0; i < a.length - 1; i++) {
  1969     private static class Pair implements Comparable<Pair> {
  1192             if (a[i].intValue() > a[i + 1].intValue()) {
  1970 
  1193                 failedSort(i, "" + a[i], "" + a[i + 1]);
  1971         private Pair(int key, int value) {
  1194             }
  1972             this.key = key;
  1195         }
  1973             this.value = value;
  1196     }
  1974         }
  1197 
  1975 
  1198     private static void checkCheckSum(Object test, Object golden) {
  1976         int getKey() {
  1199         if (checkSumXor(test) != checkSumXor(golden)) {
  1977             return key;
  1200             failed("Original and sorted arrays are not identical [xor]");
  1978         }
  1201         }
  1979 
  1202         if (checkSumPlus(test) != checkSumPlus(golden)) {
  1980         int getValue() {
  1203             failed("Original and sorted arrays are not identical [plus]");
  1981             return value;
  1204         }
  1982         }
  1205     }
  1983 
  1206 
  1984         @Override
  1207     private static int checkSumXor(Object object) {
  1985         public int compareTo(Pair pair) {
  1208         if (object instanceof int[]) {
  1986             return Integer.compare(key, pair.key);
  1209             return checkSumXor((int[]) object);
  1987         }
  1210         } else if (object instanceof long[]) {
  1988 
  1211             return checkSumXor((long[]) object);
  1989         @Override
  1212         } else if (object instanceof short[]) {
  1990         public String toString() {
  1213             return checkSumXor((short[]) object);
  1991             return "(" + key + ", " + value + ")";
  1214         } else if (object instanceof byte[]) {
  1992         }
  1215             return checkSumXor((byte[]) object);
  1993 
  1216         } else if (object instanceof char[]) {
  1994         private int key;
  1217             return checkSumXor((char[]) object);
  1995         private int value;
  1218         } else if (object instanceof float[]) {
  1996     }
  1219             return checkSumXor((float[]) object);
  1997 
  1220         } else if (object instanceof double[]) {
  1998     private static class TestRandom extends Random {
  1221             return checkSumXor((double[]) object);
  1999 
  1222         } else if (object instanceof Integer[]) {
  2000         private static final TestRandom BABA = new TestRandom(0xBABA);
  1223             return checkSumXor((Integer[]) object);
  2001         private static final TestRandom DEDA = new TestRandom(0xDEDA);
  1224         } else {
  2002         private static final TestRandom C0FFEE = new TestRandom(0xC0FFEE);
  1225             failed("Unknow type of array: " + object + " of class " +
  2003 
  1226                 object.getClass().getName());
  2004         private TestRandom(long seed) {
  1227             return -1;
       
  1228         }
       
  1229     }
       
  1230 
       
  1231     private static int checkSumXor(Integer[] a) {
       
  1232         int checkSum = 0;
       
  1233 
       
  1234         for (Integer e : a) {
       
  1235             checkSum ^= e.intValue();
       
  1236         }
       
  1237         return checkSum;
       
  1238     }
       
  1239 
       
  1240     private static int checkSumXor(int[] a) {
       
  1241         int checkSum = 0;
       
  1242 
       
  1243         for (int e : a) {
       
  1244             checkSum ^= e;
       
  1245         }
       
  1246         return checkSum;
       
  1247     }
       
  1248 
       
  1249     private static int checkSumXor(long[] a) {
       
  1250         long checkSum = 0;
       
  1251 
       
  1252         for (long e : a) {
       
  1253             checkSum ^= e;
       
  1254         }
       
  1255         return (int) checkSum;
       
  1256     }
       
  1257 
       
  1258     private static int checkSumXor(short[] a) {
       
  1259         short checkSum = 0;
       
  1260 
       
  1261         for (short e : a) {
       
  1262             checkSum ^= e;
       
  1263         }
       
  1264         return (int) checkSum;
       
  1265     }
       
  1266 
       
  1267     private static int checkSumXor(byte[] a) {
       
  1268         byte checkSum = 0;
       
  1269 
       
  1270         for (byte e : a) {
       
  1271             checkSum ^= e;
       
  1272         }
       
  1273         return (int) checkSum;
       
  1274     }
       
  1275 
       
  1276     private static int checkSumXor(char[] a) {
       
  1277         char checkSum = 0;
       
  1278 
       
  1279         for (char e : a) {
       
  1280             checkSum ^= e;
       
  1281         }
       
  1282         return (int) checkSum;
       
  1283     }
       
  1284 
       
  1285     private static int checkSumXor(float[] a) {
       
  1286         int checkSum = 0;
       
  1287 
       
  1288         for (float e : a) {
       
  1289             checkSum ^= (int) e;
       
  1290         }
       
  1291         return checkSum;
       
  1292     }
       
  1293 
       
  1294     private static int checkSumXor(double[] a) {
       
  1295         int checkSum = 0;
       
  1296 
       
  1297         for (double e : a) {
       
  1298             checkSum ^= (int) e;
       
  1299         }
       
  1300         return checkSum;
       
  1301     }
       
  1302 
       
  1303     private static int checkSumPlus(Object object) {
       
  1304         if (object instanceof int[]) {
       
  1305             return checkSumPlus((int[]) object);
       
  1306         } else if (object instanceof long[]) {
       
  1307             return checkSumPlus((long[]) object);
       
  1308         } else if (object instanceof short[]) {
       
  1309             return checkSumPlus((short[]) object);
       
  1310         } else if (object instanceof byte[]) {
       
  1311             return checkSumPlus((byte[]) object);
       
  1312         } else if (object instanceof char[]) {
       
  1313             return checkSumPlus((char[]) object);
       
  1314         } else if (object instanceof float[]) {
       
  1315             return checkSumPlus((float[]) object);
       
  1316         } else if (object instanceof double[]) {
       
  1317             return checkSumPlus((double[]) object);
       
  1318         } else if (object instanceof Integer[]) {
       
  1319             return checkSumPlus((Integer[]) object);
       
  1320         } else {
       
  1321             failed("Unknow type of array: " + object + " of class " +
       
  1322                 object.getClass().getName());
       
  1323             return -1;
       
  1324         }
       
  1325     }
       
  1326 
       
  1327     private static int checkSumPlus(int[] a) {
       
  1328         int checkSum = 0;
       
  1329 
       
  1330         for (int e : a) {
       
  1331             checkSum += e;
       
  1332         }
       
  1333         return checkSum;
       
  1334     }
       
  1335 
       
  1336     private static int checkSumPlus(long[] a) {
       
  1337         long checkSum = 0;
       
  1338 
       
  1339         for (long e : a) {
       
  1340             checkSum += e;
       
  1341         }
       
  1342         return (int) checkSum;
       
  1343     }
       
  1344 
       
  1345     private static int checkSumPlus(short[] a) {
       
  1346         short checkSum = 0;
       
  1347 
       
  1348         for (short e : a) {
       
  1349             checkSum += e;
       
  1350         }
       
  1351         return (int) checkSum;
       
  1352     }
       
  1353 
       
  1354     private static int checkSumPlus(byte[] a) {
       
  1355         byte checkSum = 0;
       
  1356 
       
  1357         for (byte e : a) {
       
  1358             checkSum += e;
       
  1359         }
       
  1360         return (int) checkSum;
       
  1361     }
       
  1362 
       
  1363     private static int checkSumPlus(char[] a) {
       
  1364         char checkSum = 0;
       
  1365 
       
  1366         for (char e : a) {
       
  1367             checkSum += e;
       
  1368         }
       
  1369         return (int) checkSum;
       
  1370     }
       
  1371 
       
  1372     private static int checkSumPlus(float[] a) {
       
  1373         int checkSum = 0;
       
  1374 
       
  1375         for (float e : a) {
       
  1376             checkSum += (int) e;
       
  1377         }
       
  1378         return checkSum;
       
  1379     }
       
  1380 
       
  1381     private static int checkSumPlus(double[] a) {
       
  1382         int checkSum = 0;
       
  1383 
       
  1384         for (double e : a) {
       
  1385             checkSum += (int) e;
       
  1386         }
       
  1387         return checkSum;
       
  1388     }
       
  1389 
       
  1390     private static int checkSumPlus(Integer[] a) {
       
  1391         int checkSum = 0;
       
  1392 
       
  1393         for (Integer e : a) {
       
  1394             checkSum += e.intValue();
       
  1395         }
       
  1396         return checkSum;
       
  1397     }
       
  1398 
       
  1399     private static void sortByInsertionSort(Object object) {
       
  1400         if (object instanceof int[]) {
       
  1401             sortByInsertionSort((int[]) object);
       
  1402         } else if (object instanceof long[]) {
       
  1403             sortByInsertionSort((long[]) object);
       
  1404         } else if (object instanceof short[]) {
       
  1405             sortByInsertionSort((short[]) object);
       
  1406         } else if (object instanceof byte[]) {
       
  1407             sortByInsertionSort((byte[]) object);
       
  1408         } else if (object instanceof char[]) {
       
  1409             sortByInsertionSort((char[]) object);
       
  1410         } else if (object instanceof float[]) {
       
  1411             sortByInsertionSort((float[]) object);
       
  1412         } else if (object instanceof double[]) {
       
  1413             sortByInsertionSort((double[]) object);
       
  1414         } else if (object instanceof Integer[]) {
       
  1415             sortByInsertionSort((Integer[]) object);
       
  1416         } else {
       
  1417             failed("Unknow type of array: " + object + " of class " +
       
  1418                 object.getClass().getName());
       
  1419         }
       
  1420     }
       
  1421 
       
  1422     private static void sortByInsertionSort(int[] a) {
       
  1423         for (int j, i = 1; i < a.length; i++) {
       
  1424             int ai = a[i];
       
  1425             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1426                 a[j + 1] = a[j];
       
  1427             }
       
  1428             a[j + 1] = ai;
       
  1429         }
       
  1430     }
       
  1431 
       
  1432     private static void sortByInsertionSort(long[] a) {
       
  1433         for (int j, i = 1; i < a.length; i++) {
       
  1434             long ai = a[i];
       
  1435             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1436                 a[j + 1] = a[j];
       
  1437             }
       
  1438             a[j + 1] = ai;
       
  1439         }
       
  1440     }
       
  1441 
       
  1442     private static void sortByInsertionSort(short[] a) {
       
  1443         for (int j, i = 1; i < a.length; i++) {
       
  1444             short ai = a[i];
       
  1445             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1446                 a[j + 1] = a[j];
       
  1447             }
       
  1448             a[j + 1] = ai;
       
  1449         }
       
  1450     }
       
  1451 
       
  1452     private static void sortByInsertionSort(byte[] a) {
       
  1453         for (int j, i = 1; i < a.length; i++) {
       
  1454             byte ai = a[i];
       
  1455             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1456                 a[j + 1] = a[j];
       
  1457             }
       
  1458             a[j + 1] = ai;
       
  1459         }
       
  1460     }
       
  1461 
       
  1462     private static void sortByInsertionSort(char[] a) {
       
  1463         for (int j, i = 1; i < a.length; i++) {
       
  1464             char ai = a[i];
       
  1465             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1466                 a[j + 1] = a[j];
       
  1467             }
       
  1468             a[j + 1] = ai;
       
  1469         }
       
  1470     }
       
  1471 
       
  1472     private static void sortByInsertionSort(float[] a) {
       
  1473         for (int j, i = 1; i < a.length; i++) {
       
  1474             float ai = a[i];
       
  1475             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1476                 a[j + 1] = a[j];
       
  1477             }
       
  1478             a[j + 1] = ai;
       
  1479         }
       
  1480     }
       
  1481 
       
  1482     private static void sortByInsertionSort(double[] a) {
       
  1483         for (int j, i = 1; i < a.length; i++) {
       
  1484             double ai = a[i];
       
  1485             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1486                 a[j + 1] = a[j];
       
  1487             }
       
  1488             a[j + 1] = ai;
       
  1489         }
       
  1490     }
       
  1491 
       
  1492     private static void sortByInsertionSort(Integer[] a) {
       
  1493         for (int j, i = 1; i < a.length; i++) {
       
  1494             Integer ai = a[i];
       
  1495             for (j = i - 1; j >= 0 && ai < a[j]; j--) {
       
  1496                 a[j + 1] = a[j];
       
  1497             }
       
  1498             a[j + 1] = ai;
       
  1499         }
       
  1500     }
       
  1501 
       
  1502     private static void sort(Object object) {
       
  1503         if (object instanceof int[]) {
       
  1504             Arrays.sort((int[]) object);
       
  1505         } else if (object instanceof long[]) {
       
  1506             Arrays.sort((long[]) object);
       
  1507         } else if (object instanceof short[]) {
       
  1508             Arrays.sort((short[]) object);
       
  1509         } else if (object instanceof byte[]) {
       
  1510             Arrays.sort((byte[]) object);
       
  1511         } else if (object instanceof char[]) {
       
  1512             Arrays.sort((char[]) object);
       
  1513         } else if (object instanceof float[]) {
       
  1514             Arrays.sort((float[]) object);
       
  1515         } else if (object instanceof double[]) {
       
  1516             Arrays.sort((double[]) object);
       
  1517         } else if (object instanceof Integer[]) {
       
  1518             Arrays.sort((Integer[]) object);
       
  1519         } else {
       
  1520             failed("Unknow type of array: " + object + " of class " +
       
  1521                 object.getClass().getName());
       
  1522         }
       
  1523     }
       
  1524 
       
  1525     private static void sortSubArray(Object object, int fromIndex, int toIndex) {
       
  1526         if (object instanceof int[]) {
       
  1527             Arrays.sort((int[]) object, fromIndex, toIndex);
       
  1528         } else if (object instanceof long[]) {
       
  1529             Arrays.sort((long[]) object, fromIndex, toIndex);
       
  1530         } else if (object instanceof short[]) {
       
  1531             Arrays.sort((short[]) object, fromIndex, toIndex);
       
  1532         } else if (object instanceof byte[]) {
       
  1533             Arrays.sort((byte[]) object, fromIndex, toIndex);
       
  1534         } else if (object instanceof char[]) {
       
  1535             Arrays.sort((char[]) object, fromIndex, toIndex);
       
  1536         } else if (object instanceof float[]) {
       
  1537             Arrays.sort((float[]) object, fromIndex, toIndex);
       
  1538         } else if (object instanceof double[]) {
       
  1539             Arrays.sort((double[]) object, fromIndex, toIndex);
       
  1540         } else if (object instanceof Integer[]) {
       
  1541             Arrays.sort((Integer[]) object, fromIndex, toIndex);
       
  1542         } else {
       
  1543             failed("Unknow type of array: " + object + " of class " +
       
  1544                 object.getClass().getName());
       
  1545         }
       
  1546     }
       
  1547 
       
  1548     private static void checkSubArray(Object object, int fromIndex, int toIndex, int m) {
       
  1549         if (object instanceof int[]) {
       
  1550             checkSubArray((int[]) object, fromIndex, toIndex, m);
       
  1551         } else if (object instanceof long[]) {
       
  1552             checkSubArray((long[]) object, fromIndex, toIndex, m);
       
  1553         } else if (object instanceof short[]) {
       
  1554             checkSubArray((short[]) object, fromIndex, toIndex, m);
       
  1555         } else if (object instanceof byte[]) {
       
  1556             checkSubArray((byte[]) object, fromIndex, toIndex, m);
       
  1557         } else if (object instanceof char[]) {
       
  1558             checkSubArray((char[]) object, fromIndex, toIndex, m);
       
  1559         } else if (object instanceof float[]) {
       
  1560             checkSubArray((float[]) object, fromIndex, toIndex, m);
       
  1561         } else if (object instanceof double[]) {
       
  1562             checkSubArray((double[]) object, fromIndex, toIndex, m);
       
  1563         } else if (object instanceof Integer[]) {
       
  1564             checkSubArray((Integer[]) object, fromIndex, toIndex, m);
       
  1565         } else {
       
  1566             failed("Unknow type of array: " + object + " of class " +
       
  1567                 object.getClass().getName());
       
  1568         }
       
  1569     }
       
  1570 
       
  1571     private static void checkSubArray(Integer[] a, int fromIndex, int toIndex, int m) {
       
  1572         for (int i = 0; i < fromIndex; i++) {
       
  1573             if (a[i].intValue() != 0xDEDA) {
       
  1574                 failed("Range sort changes left element on position " + i +
       
  1575                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1576             }
       
  1577         }
       
  1578 
       
  1579         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1580             if (a[i].intValue() > a[i + 1].intValue()) {
       
  1581                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1582             }
       
  1583         }
       
  1584 
       
  1585         for (int i = toIndex; i < a.length; i++) {
       
  1586             if (a[i].intValue() != 0xBABA) {
       
  1587                 failed("Range sort changes right element on position " + i +
       
  1588                     ": " + a[i] + ", must be " + 0xBABA);
       
  1589             }
       
  1590         }
       
  1591     }
       
  1592 
       
  1593     private static void checkSubArray(int[] a, int fromIndex, int toIndex, int m) {
       
  1594         for (int i = 0; i < fromIndex; i++) {
       
  1595             if (a[i] != 0xDEDA) {
       
  1596                 failed("Range sort changes left element on position " + i +
       
  1597                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1598             }
       
  1599         }
       
  1600 
       
  1601         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1602             if (a[i] > a[i + 1]) {
       
  1603                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1604             }
       
  1605         }
       
  1606 
       
  1607         for (int i = toIndex; i < a.length; i++) {
       
  1608             if (a[i] != 0xBABA) {
       
  1609                 failed("Range sort changes right element on position " + i +
       
  1610                     ": " + a[i] + ", must be " + 0xBABA);
       
  1611             }
       
  1612         }
       
  1613     }
       
  1614 
       
  1615     private static void checkSubArray(byte[] a, int fromIndex, int toIndex, int m) {
       
  1616         for (int i = 0; i < fromIndex; i++) {
       
  1617             if (a[i] != (byte) 0xDEDA) {
       
  1618                 failed("Range sort changes left element on position " + i +
       
  1619                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1620             }
       
  1621         }
       
  1622 
       
  1623         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1624             if (a[i] > a[i + 1]) {
       
  1625                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1626             }
       
  1627         }
       
  1628 
       
  1629         for (int i = toIndex; i < a.length; i++) {
       
  1630             if (a[i] != (byte) 0xBABA) {
       
  1631                 failed("Range sort changes right element on position " + i +
       
  1632                     ": " + a[i] + ", must be " + 0xBABA);
       
  1633             }
       
  1634         }
       
  1635     }
       
  1636 
       
  1637     private static void checkSubArray(long[] a, int fromIndex, int toIndex, int m) {
       
  1638         for (int i = 0; i < fromIndex; i++) {
       
  1639             if (a[i] != (long) 0xDEDA) {
       
  1640                 failed("Range sort changes left element on position " + i +
       
  1641                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1642             }
       
  1643         }
       
  1644 
       
  1645         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1646             if (a[i] > a[i + 1]) {
       
  1647                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1648             }
       
  1649         }
       
  1650 
       
  1651         for (int i = toIndex; i < a.length; i++) {
       
  1652             if (a[i] != (long) 0xBABA) {
       
  1653                 failed("Range sort changes right element on position " + i +
       
  1654                     ": " + a[i] + ", must be " + 0xBABA);
       
  1655             }
       
  1656         }
       
  1657     }
       
  1658 
       
  1659     private static void checkSubArray(char[] a, int fromIndex, int toIndex, int m) {
       
  1660         for (int i = 0; i < fromIndex; i++) {
       
  1661             if (a[i] != (char) 0xDEDA) {
       
  1662                 failed("Range sort changes left element on position " + i +
       
  1663                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1664             }
       
  1665         }
       
  1666 
       
  1667         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1668             if (a[i] > a[i + 1]) {
       
  1669                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1670             }
       
  1671         }
       
  1672 
       
  1673         for (int i = toIndex; i < a.length; i++) {
       
  1674             if (a[i] != (char) 0xBABA) {
       
  1675                 failed("Range sort changes right element on position " + i +
       
  1676                     ": " + a[i] + ", must be " + 0xBABA);
       
  1677             }
       
  1678         }
       
  1679     }
       
  1680 
       
  1681     private static void checkSubArray(short[] a, int fromIndex, int toIndex, int m) {
       
  1682         for (int i = 0; i < fromIndex; i++) {
       
  1683             if (a[i] != (short) 0xDEDA) {
       
  1684                 failed("Range sort changes left element on position " + i +
       
  1685                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1686             }
       
  1687         }
       
  1688 
       
  1689         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1690             if (a[i] > a[i + 1]) {
       
  1691                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1692             }
       
  1693         }
       
  1694 
       
  1695         for (int i = toIndex; i < a.length; i++) {
       
  1696             if (a[i] != (short) 0xBABA) {
       
  1697                 failed("Range sort changes right element on position " + i +
       
  1698                     ": " + a[i] + ", must be " + 0xBABA);
       
  1699             }
       
  1700         }
       
  1701     }
       
  1702 
       
  1703     private static void checkSubArray(float[] a, int fromIndex, int toIndex, int m) {
       
  1704         for (int i = 0; i < fromIndex; i++) {
       
  1705             if (a[i] != (float) 0xDEDA) {
       
  1706                 failed("Range sort changes left element on position " + i +
       
  1707                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1708             }
       
  1709         }
       
  1710 
       
  1711         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1712             if (a[i] > a[i + 1]) {
       
  1713                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1714             }
       
  1715         }
       
  1716 
       
  1717         for (int i = toIndex; i < a.length; i++) {
       
  1718             if (a[i] != (float) 0xBABA) {
       
  1719                 failed("Range sort changes right element on position " + i +
       
  1720                     ": " + a[i] + ", must be " + 0xBABA);
       
  1721             }
       
  1722         }
       
  1723     }
       
  1724 
       
  1725     private static void checkSubArray(double[] a, int fromIndex, int toIndex, int m) {
       
  1726         for (int i = 0; i < fromIndex; i++) {
       
  1727             if (a[i] != (double) 0xDEDA) {
       
  1728                 failed("Range sort changes left element on position " + i +
       
  1729                     ": " + a[i] + ", must be " + 0xDEDA);
       
  1730             }
       
  1731         }
       
  1732 
       
  1733         for (int i = fromIndex; i < toIndex - 1; i++) {
       
  1734             if (a[i] > a[i + 1]) {
       
  1735                 failedSort(i, "" + a[i], "" + a[i + 1]);
       
  1736             }
       
  1737         }
       
  1738 
       
  1739         for (int i = toIndex; i < a.length; i++) {
       
  1740             if (a[i] != (double) 0xBABA) {
       
  1741                 failed("Range sort changes right element on position " + i +
       
  1742                     ": " + a[i] + ", must be " + 0xBABA);
       
  1743             }
       
  1744         }
       
  1745     }
       
  1746 
       
  1747     private static void checkRange(Object object, int m) {
       
  1748         if (object instanceof int[]) {
       
  1749             checkRange((int[]) object, m);
       
  1750         } else if (object instanceof long[]) {
       
  1751             checkRange((long[]) object, m);
       
  1752         } else if (object instanceof short[]) {
       
  1753             checkRange((short[]) object, m);
       
  1754         } else if (object instanceof byte[]) {
       
  1755             checkRange((byte[]) object, m);
       
  1756         } else if (object instanceof char[]) {
       
  1757             checkRange((char[]) object, m);
       
  1758         } else if (object instanceof float[]) {
       
  1759             checkRange((float[]) object, m);
       
  1760         } else if (object instanceof double[]) {
       
  1761             checkRange((double[]) object, m);
       
  1762         } else if (object instanceof Integer[]) {
       
  1763             checkRange((Integer[]) object, m);
       
  1764         } else {
       
  1765             failed("Unknow type of array: " + object + " of class " +
       
  1766                 object.getClass().getName());
       
  1767         }
       
  1768     }
       
  1769 
       
  1770     private static void checkRange(Integer[] a, int m) {
       
  1771         try {
       
  1772             Arrays.sort(a, m + 1, m);
       
  1773 
       
  1774             failed("Sort does not throw IllegalArgumentException " +
       
  1775                 " as expected: fromIndex = " + (m + 1) +
       
  1776                 " toIndex = " + m);
       
  1777         }
       
  1778         catch (IllegalArgumentException iae) {
       
  1779             try {
       
  1780                 Arrays.sort(a, -m, a.length);
       
  1781 
       
  1782                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1783                     " as expected: fromIndex = " + (-m));
       
  1784             }
       
  1785             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1786                 try {
       
  1787                     Arrays.sort(a, 0, a.length + m);
       
  1788 
       
  1789                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1790                         " as expected: toIndex = " + (a.length + m));
       
  1791                 }
       
  1792                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1793                     return;
       
  1794                 }
       
  1795             }
       
  1796         }
       
  1797     }
       
  1798 
       
  1799     private static void checkRange(int[] a, int m) {
       
  1800         try {
       
  1801             Arrays.sort(a, m + 1, m);
       
  1802 
       
  1803             failed("Sort does not throw IllegalArgumentException " +
       
  1804                 " as expected: fromIndex = " + (m + 1) +
       
  1805                 " toIndex = " + m);
       
  1806         }
       
  1807         catch (IllegalArgumentException iae) {
       
  1808             try {
       
  1809                 Arrays.sort(a, -m, a.length);
       
  1810 
       
  1811                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1812                     " as expected: fromIndex = " + (-m));
       
  1813             }
       
  1814             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1815                 try {
       
  1816                     Arrays.sort(a, 0, a.length + m);
       
  1817 
       
  1818                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1819                         " as expected: toIndex = " + (a.length + m));
       
  1820                 }
       
  1821                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1822                     return;
       
  1823                 }
       
  1824             }
       
  1825         }
       
  1826     }
       
  1827 
       
  1828     private static void checkRange(long[] a, int m) {
       
  1829         try {
       
  1830             Arrays.sort(a, m + 1, m);
       
  1831 
       
  1832             failed("Sort does not throw IllegalArgumentException " +
       
  1833                 " as expected: fromIndex = " + (m + 1) +
       
  1834                 " toIndex = " + m);
       
  1835         }
       
  1836         catch (IllegalArgumentException iae) {
       
  1837             try {
       
  1838                 Arrays.sort(a, -m, a.length);
       
  1839 
       
  1840                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1841                     " as expected: fromIndex = " + (-m));
       
  1842             }
       
  1843             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1844                 try {
       
  1845                     Arrays.sort(a, 0, a.length + m);
       
  1846 
       
  1847                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1848                         " as expected: toIndex = " + (a.length + m));
       
  1849                 }
       
  1850                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1851                     return;
       
  1852                 }
       
  1853             }
       
  1854         }
       
  1855     }
       
  1856 
       
  1857     private static void checkRange(byte[] a, int m) {
       
  1858         try {
       
  1859             Arrays.sort(a, m + 1, m);
       
  1860 
       
  1861             failed("Sort does not throw IllegalArgumentException " +
       
  1862                 " as expected: fromIndex = " + (m + 1) +
       
  1863                 " toIndex = " + m);
       
  1864         }
       
  1865         catch (IllegalArgumentException iae) {
       
  1866             try {
       
  1867                 Arrays.sort(a, -m, a.length);
       
  1868 
       
  1869                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1870                     " as expected: fromIndex = " + (-m));
       
  1871             }
       
  1872             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1873                 try {
       
  1874                     Arrays.sort(a, 0, a.length + m);
       
  1875 
       
  1876                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1877                         " as expected: toIndex = " + (a.length + m));
       
  1878                 }
       
  1879                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1880                     return;
       
  1881                 }
       
  1882             }
       
  1883         }
       
  1884     }
       
  1885 
       
  1886     private static void checkRange(short[] a, int m) {
       
  1887         try {
       
  1888             Arrays.sort(a, m + 1, m);
       
  1889 
       
  1890             failed("Sort does not throw IllegalArgumentException " +
       
  1891                 " as expected: fromIndex = " + (m + 1) +
       
  1892                 " toIndex = " + m);
       
  1893         }
       
  1894         catch (IllegalArgumentException iae) {
       
  1895             try {
       
  1896                 Arrays.sort(a, -m, a.length);
       
  1897 
       
  1898                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1899                     " as expected: fromIndex = " + (-m));
       
  1900             }
       
  1901             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1902                 try {
       
  1903                     Arrays.sort(a, 0, a.length + m);
       
  1904 
       
  1905                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1906                         " as expected: toIndex = " + (a.length + m));
       
  1907                 }
       
  1908                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1909                     return;
       
  1910                 }
       
  1911             }
       
  1912         }
       
  1913     }
       
  1914 
       
  1915     private static void checkRange(char[] a, int m) {
       
  1916         try {
       
  1917             Arrays.sort(a, m + 1, m);
       
  1918 
       
  1919             failed("Sort does not throw IllegalArgumentException " +
       
  1920                 " as expected: fromIndex = " + (m + 1) +
       
  1921                 " toIndex = " + m);
       
  1922         }
       
  1923         catch (IllegalArgumentException iae) {
       
  1924             try {
       
  1925                 Arrays.sort(a, -m, a.length);
       
  1926 
       
  1927                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1928                     " as expected: fromIndex = " + (-m));
       
  1929             }
       
  1930             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1931                 try {
       
  1932                     Arrays.sort(a, 0, a.length + m);
       
  1933 
       
  1934                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1935                         " as expected: toIndex = " + (a.length + m));
       
  1936                 }
       
  1937                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1938                     return;
       
  1939                 }
       
  1940             }
       
  1941         }
       
  1942     }
       
  1943 
       
  1944     private static void checkRange(float[] a, int m) {
       
  1945         try {
       
  1946             Arrays.sort(a, m + 1, m);
       
  1947 
       
  1948             failed("Sort does not throw IllegalArgumentException " +
       
  1949                 " as expected: fromIndex = " + (m + 1) +
       
  1950                 " toIndex = " + m);
       
  1951         }
       
  1952         catch (IllegalArgumentException iae) {
       
  1953             try {
       
  1954                 Arrays.sort(a, -m, a.length);
       
  1955 
       
  1956                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1957                     " as expected: fromIndex = " + (-m));
       
  1958             }
       
  1959             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1960                 try {
       
  1961                     Arrays.sort(a, 0, a.length + m);
       
  1962 
       
  1963                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1964                         " as expected: toIndex = " + (a.length + m));
       
  1965                 }
       
  1966                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1967                     return;
       
  1968                 }
       
  1969             }
       
  1970         }
       
  1971     }
       
  1972 
       
  1973     private static void checkRange(double[] a, int m) {
       
  1974         try {
       
  1975             Arrays.sort(a, m + 1, m);
       
  1976 
       
  1977             failed("Sort does not throw IllegalArgumentException " +
       
  1978                 " as expected: fromIndex = " + (m + 1) +
       
  1979                 " toIndex = " + m);
       
  1980         }
       
  1981         catch (IllegalArgumentException iae) {
       
  1982             try {
       
  1983                 Arrays.sort(a, -m, a.length);
       
  1984 
       
  1985                 failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1986                     " as expected: fromIndex = " + (-m));
       
  1987             }
       
  1988             catch (ArrayIndexOutOfBoundsException aoe) {
       
  1989                 try {
       
  1990                     Arrays.sort(a, 0, a.length + m);
       
  1991 
       
  1992                     failed("Sort does not throw ArrayIndexOutOfBoundsException " +
       
  1993                         " as expected: toIndex = " + (a.length + m));
       
  1994                 }
       
  1995                 catch (ArrayIndexOutOfBoundsException aie) {
       
  1996                     return;
       
  1997                 }
       
  1998             }
       
  1999         }
       
  2000     }
       
  2001 
       
  2002     private static void outArray(Object[] a) {
       
  2003         for (int i = 0; i < a.length; i++) {
       
  2004             out.print(a[i] + " ");
       
  2005         }
       
  2006         out.println();
       
  2007     }
       
  2008 
       
  2009     private static void outArray(int[] a) {
       
  2010         for (int i = 0; i < a.length; i++) {
       
  2011             out.print(a[i] + " ");
       
  2012         }
       
  2013         out.println();
       
  2014     }
       
  2015 
       
  2016     private static void outArray(float[] a) {
       
  2017         for (int i = 0; i < a.length; i++) {
       
  2018             out.print(a[i] + " ");
       
  2019         }
       
  2020         out.println();
       
  2021     }
       
  2022 
       
  2023     private static void outArray(double[] a) {
       
  2024         for (int i = 0; i < a.length; i++) {
       
  2025             out.print(a[i] + " ");
       
  2026         }
       
  2027         out.println();
       
  2028     }
       
  2029 
       
  2030     private static class MyRandom extends Random {
       
  2031         MyRandom(long seed) {
       
  2032             super(seed);
  2005             super(seed);
  2033             mySeed = seed;
  2006             this.seed = Long.toHexString(seed).toUpperCase();
  2034         }
  2007         }
  2035 
  2008 
  2036         long getSeed() {
  2009         @Override
  2037             return mySeed;
  2010         public String toString() {
  2038         }
  2011             return seed;
  2039 
  2012         }
  2040         private long mySeed;
  2013 
  2041     }
  2014         private String seed;
  2042 
  2015     }
  2043     private static String ourDescription;
       
  2044 }
  2016 }