Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Arrays/ParallelSorting.java
38811 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* Adapted from test/java/util/Arrays/Sorting.java24*25* Where that test checks Arrays.sort against manual quicksort routines,26* this test checks parallelSort against either Arrays.sort or manual27* quicksort routines.28*/2930/*31* @test32* @bug 800398133* @run main ParallelSorting -shortrun34* @summary Exercise Arrays.parallelSort (adapted from test Sorting)35*36* @author Vladimir Yaroslavskiy37* @author Jon Bentley38* @author Josh Bloch39*/4041import java.util.Arrays;42import java.util.Random;43import java.io.PrintStream;44import java.util.Comparator;4546public class ParallelSorting {47private static final PrintStream out = System.out;48private static final PrintStream err = System.err;4950// Array lengths used in a long run (default)51private static final int[] LONG_RUN_LENGTHS = {521000, 10000, 100000, 1000000 };5354// Array lengths used in a short run55private static final int[] SHORT_RUN_LENGTHS = {565000, 9000, 10000, 12000 };5758// Random initial values used in a long run (default)59private static final long[] LONG_RUN_RANDOMS = { 666, 0xC0FFEE, 999 };6061// Random initial values used in a short run62private static final long[] SHORT_RUN_RANDOMS = { 666 };6364public static void main(String[] args) {65boolean shortRun = args.length > 0 && args[0].equals("-shortrun");66long start = System.currentTimeMillis();6768if (shortRun) {69testAndCheck(SHORT_RUN_LENGTHS, SHORT_RUN_RANDOMS);70} else {71testAndCheck(LONG_RUN_LENGTHS, LONG_RUN_RANDOMS);72}73long end = System.currentTimeMillis();7475out.format("PASSED in %d sec.\n", Math.round((end - start) / 1E3));76}7778private static void testAndCheck(int[] lengths, long[] randoms) {79testEmptyAndNullIntArray();80testEmptyAndNullLongArray();81testEmptyAndNullShortArray();82testEmptyAndNullCharArray();83testEmptyAndNullByteArray();84testEmptyAndNullFloatArray();85testEmptyAndNullDoubleArray();8687for (int length : lengths) {88testMergeSort(length);89testAndCheckRange(length);90testAndCheckSubArray(length);91}92for (long seed : randoms) {93for (int length : lengths) {94testAndCheckWithInsertionSort(length, new MyRandom(seed));95testAndCheckWithCheckSum(length, new MyRandom(seed));96testAndCheckWithScrambling(length, new MyRandom(seed));97testAndCheckFloat(length, new MyRandom(seed));98testAndCheckDouble(length, new MyRandom(seed));99testStable(length, new MyRandom(seed));100}101}102}103104private static void testEmptyAndNullIntArray() {105ourDescription = "Check empty and null array";106Arrays.parallelSort(new int[]{});107Arrays.parallelSort(new int[]{}, 0, 0);108109try {110Arrays.parallelSort((int[]) null);111} catch (NullPointerException expected) {112try {113Arrays.parallelSort((int[]) null, 0, 0);114} catch (NullPointerException expected2) {115return;116}117failed("Arrays.parallelSort(int[],fromIndex,toIndex) shouldn't " +118"catch null array");119}120failed("Arrays.parallelSort(int[]) shouldn't catch null array");121}122123private static void testEmptyAndNullLongArray() {124ourDescription = "Check empty and null array";125Arrays.parallelSort(new long[]{});126Arrays.parallelSort(new long[]{}, 0, 0);127128try {129Arrays.parallelSort((long[]) null);130} catch (NullPointerException expected) {131try {132Arrays.parallelSort((long[]) null, 0, 0);133} catch (NullPointerException expected2) {134return;135}136failed("Arrays.parallelSort(long[],fromIndex,toIndex) shouldn't " +137"catch null array");138}139failed("Arrays.parallelSort(long[]) shouldn't catch null array");140}141142private static void testEmptyAndNullShortArray() {143ourDescription = "Check empty and null array";144Arrays.parallelSort(new short[]{});145Arrays.parallelSort(new short[]{}, 0, 0);146147try {148Arrays.parallelSort((short[]) null);149} catch (NullPointerException expected) {150try {151Arrays.parallelSort((short[]) null, 0, 0);152} catch (NullPointerException expected2) {153return;154}155failed("Arrays.parallelSort(short[],fromIndex,toIndex) shouldn't " +156"catch null array");157}158failed("Arrays.parallelSort(short[]) shouldn't catch null array");159}160161private static void testEmptyAndNullCharArray() {162ourDescription = "Check empty and null array";163Arrays.parallelSort(new char[]{});164Arrays.parallelSort(new char[]{}, 0, 0);165166try {167Arrays.parallelSort((char[]) null);168} catch (NullPointerException expected) {169try {170Arrays.parallelSort((char[]) null, 0, 0);171} catch (NullPointerException expected2) {172return;173}174failed("Arrays.parallelSort(char[],fromIndex,toIndex) shouldn't " +175"catch null array");176}177failed("Arrays.parallelSort(char[]) shouldn't catch null array");178}179180private static void testEmptyAndNullByteArray() {181ourDescription = "Check empty and null array";182Arrays.parallelSort(new byte[]{});183Arrays.parallelSort(new byte[]{}, 0, 0);184185try {186Arrays.parallelSort((byte[]) null);187} catch (NullPointerException expected) {188try {189Arrays.parallelSort((byte[]) null, 0, 0);190} catch (NullPointerException expected2) {191return;192}193failed("Arrays.parallelSort(byte[],fromIndex,toIndex) shouldn't " +194"catch null array");195}196failed("Arrays.parallelSort(byte[]) shouldn't catch null array");197}198199private static void testEmptyAndNullFloatArray() {200ourDescription = "Check empty and null array";201Arrays.parallelSort(new float[]{});202Arrays.parallelSort(new float[]{}, 0, 0);203204try {205Arrays.parallelSort((float[]) null);206} catch (NullPointerException expected) {207try {208Arrays.parallelSort((float[]) null, 0, 0);209} catch (NullPointerException expected2) {210return;211}212failed("Arrays.parallelSort(float[],fromIndex,toIndex) shouldn't " +213"catch null array");214}215failed("Arrays.parallelSort(float[]) shouldn't catch null array");216}217218private static void testEmptyAndNullDoubleArray() {219ourDescription = "Check empty and null array";220Arrays.parallelSort(new double[]{});221Arrays.parallelSort(new double[]{}, 0, 0);222223try {224Arrays.parallelSort((double[]) null);225} catch (NullPointerException expected) {226try {227Arrays.parallelSort((double[]) null, 0, 0);228} catch (NullPointerException expected2) {229return;230}231failed("Arrays.parallelSort(double[],fromIndex,toIndex) shouldn't " +232"catch null array");233}234failed("Arrays.parallelSort(double[]) shouldn't catch null array");235}236237private static void testAndCheckSubArray(int length) {238ourDescription = "Check sorting of subarray";239int[] golden = new int[length];240boolean newLine = false;241242for (int m = 1; m < length / 2; m *= 2) {243newLine = true;244int fromIndex = m;245int toIndex = length - m;246247prepareSubArray(golden, fromIndex, toIndex, m);248int[] test = golden.clone();249250for (TypeConverter converter : TypeConverter.values()) {251out.println("Test 'subarray': " + converter +252" length = " + length + ", m = " + m);253Object convertedGolden = converter.convert(golden);254Object convertedTest = converter.convert(test);255sortSubArray(convertedTest, fromIndex, toIndex);256checkSubArray(convertedTest, fromIndex, toIndex, m);257}258}259if (newLine) {260out.println();261}262}263264private static void testAndCheckRange(int length) {265ourDescription = "Check range check";266int[] golden = new int[length];267268for (int m = 1; m < 2 * length; m *= 2) {269for (int i = 1; i <= length; i++) {270golden[i - 1] = i % m + m % i;271}272for (TypeConverter converter : TypeConverter.values()) {273out.println("Test 'range': " + converter +274", length = " + length + ", m = " + m);275Object convertedGolden = converter.convert(golden);276checkRange(convertedGolden, m);277}278}279out.println();280}281282private static void testStable(int length, MyRandom random) {283ourDescription = "Check if sorting is stable";284Pair[] a = build(length, random);285286out.println("Test 'stable': " + "random = " + random.getSeed() +287", length = " + length);288Arrays.parallelSort(a);289checkSorted(a);290checkStable(a);291out.println();292293a = build(length, random);294295out.println("Test 'stable' comparator: " + "random = " + random.getSeed() +296", length = " + length);297Arrays.parallelSort(a, pairCmp);298checkSorted(a);299checkStable(a);300out.println();301302}303304private static void checkSorted(Pair[] a) {305for (int i = 0; i < a.length - 1; i++) {306if (a[i].getKey() > a[i + 1].getKey()) {307failedSort(i, "" + a[i].getKey(), "" + a[i + 1].getKey());308}309}310}311312private static void checkStable(Pair[] a) {313for (int i = 0; i < a.length / 4; ) {314int key1 = a[i].getKey();315int value1 = a[i++].getValue();316int key2 = a[i].getKey();317int value2 = a[i++].getValue();318int key3 = a[i].getKey();319int value3 = a[i++].getValue();320int key4 = a[i].getKey();321int value4 = a[i++].getValue();322323if (!(key1 == key2 && key2 == key3 && key3 == key4)) {324failed("On position " + i + " keys are different " +325key1 + ", " + key2 + ", " + key3 + ", " + key4);326}327if (!(value1 < value2 && value2 < value3 && value3 < value4)) {328failed("Sorting is not stable at position " + i +329". Second values have been changed: " + value1 + ", " +330value2 + ", " + value3 + ", " + value4);331}332}333}334335private static Pair[] build(int length, Random random) {336Pair[] a = new Pair[length * 4];337338for (int i = 0; i < a.length; ) {339int key = random.nextInt();340a[i++] = new Pair(key, 1);341a[i++] = new Pair(key, 2);342a[i++] = new Pair(key, 3);343a[i++] = new Pair(key, 4);344}345return a;346}347348private static Comparator<Pair> pairCmp = new Comparator<Pair>() {349public int compare(Pair p1, Pair p2) {350return p1.compareTo(p2);351}352};353354private static final class Pair implements Comparable<Pair> {355Pair(int key, int value) {356myKey = key;357myValue = value;358}359360int getKey() {361return myKey;362}363364int getValue() {365return myValue;366}367368public int compareTo(Pair pair) {369if (myKey < pair.myKey) {370return -1;371}372if (myKey > pair.myKey) {373return 1;374}375return 0;376}377378@Override379public String toString() {380return "(" + myKey + ", " + myValue + ")";381}382383private int myKey;384private int myValue;385}386387388private static void testAndCheckWithInsertionSort(int length, MyRandom random) {389if (length > 1000) {390return;391}392ourDescription = "Check sorting with insertion sort";393int[] golden = new int[length];394395for (int m = 1; m < 2 * length; m *= 2) {396for (UnsortedBuilder builder : UnsortedBuilder.values()) {397builder.build(golden, m, random);398int[] test = golden.clone();399400for (TypeConverter converter : TypeConverter.values()) {401out.println("Test 'insertion sort': " + converter +402" " + builder + "random = " + random.getSeed() +403", length = " + length + ", m = " + m);404Object convertedGolden = converter.convert(golden);405Object convertedTest1 = converter.convert(test);406Object convertedTest2 = converter.convert(test);407sort(convertedTest1);408sortByInsertionSort(convertedTest2);409compare(convertedTest1, convertedTest2);410}411}412}413out.println();414}415416private static void testMergeSort(int length) {417if (length < 1000) {418return;419}420ourDescription = "Check merge sorting";421int[] golden = new int[length];422int period = 67; // java.util.DualPivotQuicksort.MAX_RUN_COUNT423424for (int m = period - 2; m <= period + 2; m++) {425for (MergeBuilder builder : MergeBuilder.values()) {426builder.build(golden, m);427int[] test = golden.clone();428429for (TypeConverter converter : TypeConverter.values()) {430out.println("Test 'merge sort': " + converter + " " +431builder + "length = " + length + ", m = " + m);432Object convertedGolden = converter.convert(golden);433sort(convertedGolden);434checkSorted(convertedGolden);435}436}437}438out.println();439}440441private static void testAndCheckWithCheckSum(int length, MyRandom random) {442ourDescription = "Check sorting with check sum";443int[] golden = new int[length];444445for (int m = 1; m < 2 * length; m *= 2) {446for (UnsortedBuilder builder : UnsortedBuilder.values()) {447builder.build(golden, m, random);448int[] test = golden.clone();449450for (TypeConverter converter : TypeConverter.values()) {451out.println("Test 'check sum': " + converter +452" " + builder + "random = " + random.getSeed() +453", length = " + length + ", m = " + m);454Object convertedGolden = converter.convert(golden);455Object convertedTest = converter.convert(test);456sort(convertedTest);457checkWithCheckSum(convertedTest, convertedGolden);458}459}460}461out.println();462}463464private static void testAndCheckWithScrambling(int length, MyRandom random) {465ourDescription = "Check sorting with scrambling";466int[] golden = new int[length];467468for (int m = 1; m <= 7; m++) {469if (m > length) {470break;471}472for (SortedBuilder builder : SortedBuilder.values()) {473builder.build(golden, m);474int[] test = golden.clone();475scramble(test, random);476477for (TypeConverter converter : TypeConverter.values()) {478out.println("Test 'scrambling': " + converter +479" " + builder + "random = " + random.getSeed() +480", length = " + length + ", m = " + m);481Object convertedGolden = converter.convert(golden);482Object convertedTest = converter.convert(test);483sort(convertedTest);484compare(convertedTest, convertedGolden);485}486}487}488out.println();489}490491private static void testAndCheckFloat(int length, MyRandom random) {492ourDescription = "Check float sorting";493float[] golden = new float[length];494final int MAX = 10;495boolean newLine = false;496497for (int a = 0; a <= MAX; a++) {498for (int g = 0; g <= MAX; g++) {499for (int z = 0; z <= MAX; z++) {500for (int n = 0; n <= MAX; n++) {501for (int p = 0; p <= MAX; p++) {502if (a + g + z + n + p > length) {503continue;504}505if (a + g + z + n + p < length) {506continue;507}508for (FloatBuilder builder : FloatBuilder.values()) {509out.println("Test 'float': random = " + random.getSeed() +510", length = " + length + ", a = " + a + ", g = " +511g + ", z = " + z + ", n = " + n + ", p = " + p);512builder.build(golden, a, g, z, n, p, random);513float[] test = golden.clone();514scramble(test, random);515sort(test);516compare(test, golden, a, n, g);517}518newLine = true;519}520}521}522}523}524if (newLine) {525out.println();526}527}528529private static void testAndCheckDouble(int length, MyRandom random) {530ourDescription = "Check double sorting";531double[] golden = new double[length];532final int MAX = 10;533boolean newLine = false;534535for (int a = 0; a <= MAX; a++) {536for (int g = 0; g <= MAX; g++) {537for (int z = 0; z <= MAX; z++) {538for (int n = 0; n <= MAX; n++) {539for (int p = 0; p <= MAX; p++) {540if (a + g + z + n + p > length) {541continue;542}543if (a + g + z + n + p < length) {544continue;545}546for (DoubleBuilder builder : DoubleBuilder.values()) {547out.println("Test 'double': random = " + random.getSeed() +548", length = " + length + ", a = " + a + ", g = " +549g + ", z = " + z + ", n = " + n + ", p = " + p);550builder.build(golden, a, g, z, n, p, random);551double[] test = golden.clone();552scramble(test, random);553sort(test);554compare(test, golden, a, n, g);555}556newLine = true;557}558}559}560}561}562if (newLine) {563out.println();564}565}566567private static void prepareSubArray(int[] a, int fromIndex, int toIndex, int m) {568for (int i = 0; i < fromIndex; i++) {569a[i] = 0xDEDA;570}571int middle = (fromIndex + toIndex) >>> 1;572int k = 0;573574for (int i = fromIndex; i < middle; i++) {575a[i] = k++;576}577for (int i = middle; i < toIndex; i++) {578a[i] = k--;579}580for (int i = toIndex; i < a.length; i++) {581a[i] = 0xBABA;582}583}584585private static void scramble(int[] a, Random random) {586for (int i = 0; i < a.length * 7; i++) {587swap(a, random.nextInt(a.length), random.nextInt(a.length));588}589}590591private static void scramble(float[] a, Random random) {592for (int i = 0; i < a.length * 7; i++) {593swap(a, random.nextInt(a.length), random.nextInt(a.length));594}595}596597private static void scramble(double[] a, Random random) {598for (int i = 0; i < a.length * 7; i++) {599swap(a, random.nextInt(a.length), random.nextInt(a.length));600}601}602603private static void swap(int[] a, int i, int j) {604int t = a[i];605a[i] = a[j];606a[j] = t;607}608609private static void swap(float[] a, int i, int j) {610float t = a[i];611a[i] = a[j];612a[j] = t;613}614615private static void swap(double[] a, int i, int j) {616double t = a[i];617a[i] = a[j];618a[j] = t;619}620621private static enum TypeConverter {622INT {623Object convert(int[] a) {624return a.clone();625}626},627LONG {628Object convert(int[] a) {629long[] b = new long[a.length];630631for (int i = 0; i < a.length; i++) {632b[i] = (long) a[i];633}634return b;635}636},637BYTE {638Object convert(int[] a) {639byte[] b = new byte[a.length];640641for (int i = 0; i < a.length; i++) {642b[i] = (byte) a[i];643}644return b;645}646},647SHORT {648Object convert(int[] a) {649short[] b = new short[a.length];650651for (int i = 0; i < a.length; i++) {652b[i] = (short) a[i];653}654return b;655}656},657CHAR {658Object convert(int[] a) {659char[] b = new char[a.length];660661for (int i = 0; i < a.length; i++) {662b[i] = (char) a[i];663}664return b;665}666},667FLOAT {668Object convert(int[] a) {669float[] b = new float[a.length];670671for (int i = 0; i < a.length; i++) {672b[i] = (float) a[i];673}674return b;675}676},677DOUBLE {678Object convert(int[] a) {679double[] b = new double[a.length];680681for (int i = 0; i < a.length; i++) {682b[i] = (double) a[i];683}684return b;685}686},687INTEGER {688Object convert(int[] a) {689Integer[] b = new Integer[a.length];690691for (int i = 0; i < a.length; i++) {692b[i] = new Integer(a[i]);693}694return b;695}696};697698abstract Object convert(int[] a);699700@Override public String toString() {701String name = name();702703for (int i = name.length(); i < 9; i++) {704name += " ";705}706return name;707}708}709710private static enum FloatBuilder {711SIMPLE {712void build(float[] x, int a, int g, int z, int n, int p, Random random) {713int fromIndex = 0;714float negativeValue = -random.nextFloat();715float positiveValue = random.nextFloat();716717writeValue(x, negativeValue, fromIndex, n);718fromIndex += n;719720writeValue(x, -0.0f, fromIndex, g);721fromIndex += g;722723writeValue(x, 0.0f, fromIndex, z);724fromIndex += z;725726writeValue(x, positiveValue, fromIndex, p);727fromIndex += p;728729writeValue(x, Float.NaN, fromIndex, a);730}731};732733abstract void build(float[] x, int a, int g, int z, int n, int p, Random random);734}735736private static enum DoubleBuilder {737SIMPLE {738void build(double[] x, int a, int g, int z, int n, int p, Random random) {739int fromIndex = 0;740double negativeValue = -random.nextFloat();741double positiveValue = random.nextFloat();742743writeValue(x, negativeValue, fromIndex, n);744fromIndex += n;745746writeValue(x, -0.0d, fromIndex, g);747fromIndex += g;748749writeValue(x, 0.0d, fromIndex, z);750fromIndex += z;751752writeValue(x, positiveValue, fromIndex, p);753fromIndex += p;754755writeValue(x, Double.NaN, fromIndex, a);756}757};758759abstract void build(double[] x, int a, int g, int z, int n, int p, Random random);760}761762private static void writeValue(float[] a, float value, int fromIndex, int count) {763for (int i = fromIndex; i < fromIndex + count; i++) {764a[i] = value;765}766}767768private static void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZero) {769for (int i = a.length - numNaN; i < a.length; i++) {770if (a[i] == a[i]) {771failed("On position " + i + " must be NaN instead of " + a[i]);772}773}774final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f);775776for (int i = numNeg; i < numNeg + numNegZero; i++) {777if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) {778failed("On position " + i + " must be -0.0 instead of " + a[i]);779}780}781for (int i = 0; i < a.length - numNaN; i++) {782if (a[i] != b[i]) {783failedCompare(i, "" + a[i], "" + b[i]);784}785}786}787788private static void writeValue(double[] a, double value, int fromIndex, int count) {789for (int i = fromIndex; i < fromIndex + count; i++) {790a[i] = value;791}792}793794private static void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) {795for (int i = a.length - numNaN; i < a.length; i++) {796if (a[i] == a[i]) {797failed("On position " + i + " must be NaN instead of " + a[i]);798}799}800final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d);801802for (int i = numNeg; i < numNeg + numNegZero; i++) {803if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) {804failed("On position " + i + " must be -0.0 instead of " + a[i]);805}806}807for (int i = 0; i < a.length - numNaN; i++) {808if (a[i] != b[i]) {809failedCompare(i, "" + a[i], "" + b[i]);810}811}812}813814private static enum SortedBuilder {815REPEATED {816void build(int[] a, int m) {817int period = a.length / m;818int i = 0;819int k = 0;820821while (true) {822for (int t = 1; t <= period; t++) {823if (i >= a.length) {824return;825}826a[i++] = k;827}828if (i >= a.length) {829return;830}831k++;832}833}834},835ORGAN_PIPES {836void build(int[] a, int m) {837int i = 0;838int k = m;839840while (true) {841for (int t = 1; t <= m; t++) {842if (i >= a.length) {843return;844}845a[i++] = k;846}847}848}849};850851abstract void build(int[] a, int m);852853@Override public String toString() {854String name = name();855856for (int i = name.length(); i < 12; i++) {857name += " ";858}859return name;860}861}862863private static enum MergeBuilder {864ASCENDING {865void build(int[] a, int m) {866int period = a.length / m;867int v = 1, i = 0;868869for (int k = 0; k < m; k++) {870v = 1;871for (int p = 0; p < period; p++) {872a[i++] = v++;873}874}875for (int j = i; j < a.length - 1; j++) {876a[j] = v++;877}878a[a.length - 1] = 0;879}880},881DESCENDING {882void build(int[] a, int m) {883int period = a.length / m;884int v = -1, i = 0;885886for (int k = 0; k < m; k++) {887v = -1;888for (int p = 0; p < period; p++) {889a[i++] = v--;890}891}892for (int j = i; j < a.length - 1; j++) {893a[j] = v--;894}895a[a.length - 1] = 0;896}897};898899abstract void build(int[] a, int m);900901@Override public String toString() {902String name = name();903904for (int i = name.length(); i < 12; i++) {905name += " ";906}907return name;908}909}910911private static enum UnsortedBuilder {912RANDOM {913void build(int[] a, int m, Random random) {914for (int i = 0; i < a.length; i++) {915a[i] = random.nextInt();916}917}918},919ASCENDING {920void build(int[] a, int m, Random random) {921for (int i = 0; i < a.length; i++) {922a[i] = m + i;923}924}925},926DESCENDING {927void build(int[] a, int m, Random random) {928for (int i = 0; i < a.length; i++) {929a[i] = a.length - m - i;930}931}932},933ALL_EQUAL {934void build(int[] a, int m, Random random) {935for (int i = 0; i < a.length; i++) {936a[i] = m;937}938}939},940SAW {941void build(int[] a, int m, Random random) {942int incCount = 1;943int decCount = a.length;944int i = 0;945int period = m--;946947while (true) {948for (int k = 1; k <= period; k++) {949if (i >= a.length) {950return;951}952a[i++] = incCount++;953}954period += m;955956for (int k = 1; k <= period; k++) {957if (i >= a.length) {958return;959}960a[i++] = decCount--;961}962period += m;963}964}965},966REPEATED {967void build(int[] a, int m, Random random) {968for (int i = 0; i < a.length; i++) {969a[i] = i % m;970}971}972},973DUPLICATED {974void build(int[] a, int m, Random random) {975for (int i = 0; i < a.length; i++) {976a[i] = random.nextInt(m);977}978}979},980ORGAN_PIPES {981void build(int[] a, int m, Random random) {982int middle = a.length / (m + 1);983984for (int i = 0; i < middle; i++) {985a[i] = i;986}987for (int i = middle; i < a.length; i++) {988a[i] = a.length - i - 1;989}990}991},992STAGGER {993void build(int[] a, int m, Random random) {994for (int i = 0; i < a.length; i++) {995a[i] = (i * m + i) % a.length;996}997}998},999PLATEAU {1000void build(int[] a, int m, Random random) {1001for (int i = 0; i < a.length; i++) {1002a[i] = Math.min(i, m);1003}1004}1005},1006SHUFFLE {1007void build(int[] a, int m, Random random) {1008int x = 0, y = 0;1009for (int i = 0; i < a.length; i++) {1010a[i] = random.nextBoolean() ? (x += 2) : (y += 2);1011}1012}1013};10141015abstract void build(int[] a, int m, Random random);10161017@Override public String toString() {1018String name = name();10191020for (int i = name.length(); i < 12; i++) {1021name += " ";1022}1023return name;1024}1025}10261027private static void checkWithCheckSum(Object test, Object golden) {1028checkSorted(test);1029checkCheckSum(test, golden);1030}10311032private static void failed(String message) {1033err.format("\n*** TEST FAILED - %s.\n\n%s.\n\n", ourDescription, message);1034throw new RuntimeException("Test failed - see log file for details");1035}10361037private static void failedSort(int index, String value1, String value2) {1038failed("Array is not sorted at " + index + "-th position: " +1039value1 + " and " + value2);1040}10411042private static void failedCompare(int index, String value1, String value2) {1043failed("On position " + index + " must be " + value2 + " instead of " + value1);1044}10451046private static void compare(Object test, Object golden) {1047if (test instanceof int[]) {1048compare((int[]) test, (int[]) golden);1049} else if (test instanceof long[]) {1050compare((long[]) test, (long[]) golden);1051} else if (test instanceof short[]) {1052compare((short[]) test, (short[]) golden);1053} else if (test instanceof byte[]) {1054compare((byte[]) test, (byte[]) golden);1055} else if (test instanceof char[]) {1056compare((char[]) test, (char[]) golden);1057} else if (test instanceof float[]) {1058compare((float[]) test, (float[]) golden);1059} else if (test instanceof double[]) {1060compare((double[]) test, (double[]) golden);1061} else if (test instanceof Integer[]) {1062compare((Integer[]) test, (Integer[]) golden);1063} else {1064failed("Unknow type of array: " + test + " of class " +1065test.getClass().getName());1066}1067}10681069private static void compare(int[] a, int[] b) {1070for (int i = 0; i < a.length; i++) {1071if (a[i] != b[i]) {1072failedCompare(i, "" + a[i], "" + b[i]);1073}1074}1075}10761077private static void compare(long[] a, long[] b) {1078for (int i = 0; i < a.length; i++) {1079if (a[i] != b[i]) {1080failedCompare(i, "" + a[i], "" + b[i]);1081}1082}1083}10841085private static void compare(short[] a, short[] b) {1086for (int i = 0; i < a.length; i++) {1087if (a[i] != b[i]) {1088failedCompare(i, "" + a[i], "" + b[i]);1089}1090}1091}10921093private static void compare(byte[] a, byte[] b) {1094for (int i = 0; i < a.length; i++) {1095if (a[i] != b[i]) {1096failedCompare(i, "" + a[i], "" + b[i]);1097}1098}1099}11001101private static void compare(char[] a, char[] b) {1102for (int i = 0; i < a.length; i++) {1103if (a[i] != b[i]) {1104failedCompare(i, "" + a[i], "" + b[i]);1105}1106}1107}11081109private static void compare(float[] a, float[] b) {1110for (int i = 0; i < a.length; i++) {1111if (a[i] != b[i]) {1112failedCompare(i, "" + a[i], "" + b[i]);1113}1114}1115}11161117private static void compare(double[] a, double[] b) {1118for (int i = 0; i < a.length; i++) {1119if (a[i] != b[i]) {1120failedCompare(i, "" + a[i], "" + b[i]);1121}1122}1123}11241125private static void compare(Integer[] a, Integer[] b) {1126for (int i = 0; i < a.length; i++) {1127if (a[i].compareTo(b[i]) != 0) {1128failedCompare(i, "" + a[i], "" + b[i]);1129}1130}1131}11321133private static void checkSorted(Object object) {1134if (object instanceof int[]) {1135checkSorted((int[]) object);1136} else if (object instanceof long[]) {1137checkSorted((long[]) object);1138} else if (object instanceof short[]) {1139checkSorted((short[]) object);1140} else if (object instanceof byte[]) {1141checkSorted((byte[]) object);1142} else if (object instanceof char[]) {1143checkSorted((char[]) object);1144} else if (object instanceof float[]) {1145checkSorted((float[]) object);1146} else if (object instanceof double[]) {1147checkSorted((double[]) object);1148} else if (object instanceof Integer[]) {1149checkSorted((Integer[]) object);1150} else {1151failed("Unknow type of array: " + object + " of class " +1152object.getClass().getName());1153}1154}11551156private static void checkSorted(int[] a) {1157for (int i = 0; i < a.length - 1; i++) {1158if (a[i] > a[i + 1]) {1159failedSort(i, "" + a[i], "" + a[i + 1]);1160}1161}1162}11631164private static void checkSorted(long[] a) {1165for (int i = 0; i < a.length - 1; i++) {1166if (a[i] > a[i + 1]) {1167failedSort(i, "" + a[i], "" + a[i + 1]);1168}1169}1170}11711172private static void checkSorted(short[] a) {1173for (int i = 0; i < a.length - 1; i++) {1174if (a[i] > a[i + 1]) {1175failedSort(i, "" + a[i], "" + a[i + 1]);1176}1177}1178}11791180private static void checkSorted(byte[] a) {1181for (int i = 0; i < a.length - 1; i++) {1182if (a[i] > a[i + 1]) {1183failedSort(i, "" + a[i], "" + a[i + 1]);1184}1185}1186}11871188private static void checkSorted(char[] a) {1189for (int i = 0; i < a.length - 1; i++) {1190if (a[i] > a[i + 1]) {1191failedSort(i, "" + a[i], "" + a[i + 1]);1192}1193}1194}11951196private static void checkSorted(float[] a) {1197for (int i = 0; i < a.length - 1; i++) {1198if (a[i] > a[i + 1]) {1199failedSort(i, "" + a[i], "" + a[i + 1]);1200}1201}1202}12031204private static void checkSorted(double[] a) {1205for (int i = 0; i < a.length - 1; i++) {1206if (a[i] > a[i + 1]) {1207failedSort(i, "" + a[i], "" + a[i + 1]);1208}1209}1210}12111212private static void checkSorted(Integer[] a) {1213for (int i = 0; i < a.length - 1; i++) {1214if (a[i].intValue() > a[i + 1].intValue()) {1215failedSort(i, "" + a[i], "" + a[i + 1]);1216}1217}1218}12191220private static void checkCheckSum(Object test, Object golden) {1221if (checkSumXor(test) != checkSumXor(golden)) {1222failed("Original and sorted arrays are not identical [xor]");1223}1224if (checkSumPlus(test) != checkSumPlus(golden)) {1225failed("Original and sorted arrays are not identical [plus]");1226}1227}12281229private static int checkSumXor(Object object) {1230if (object instanceof int[]) {1231return checkSumXor((int[]) object);1232} else if (object instanceof long[]) {1233return checkSumXor((long[]) object);1234} else if (object instanceof short[]) {1235return checkSumXor((short[]) object);1236} else if (object instanceof byte[]) {1237return checkSumXor((byte[]) object);1238} else if (object instanceof char[]) {1239return checkSumXor((char[]) object);1240} else if (object instanceof float[]) {1241return checkSumXor((float[]) object);1242} else if (object instanceof double[]) {1243return checkSumXor((double[]) object);1244} else if (object instanceof Integer[]) {1245return checkSumXor((Integer[]) object);1246} else {1247failed("Unknow type of array: " + object + " of class " +1248object.getClass().getName());1249return -1;1250}1251}12521253private static int checkSumXor(Integer[] a) {1254int checkSum = 0;12551256for (Integer e : a) {1257checkSum ^= e.intValue();1258}1259return checkSum;1260}12611262private static int checkSumXor(int[] a) {1263int checkSum = 0;12641265for (int e : a) {1266checkSum ^= e;1267}1268return checkSum;1269}12701271private static int checkSumXor(long[] a) {1272long checkSum = 0;12731274for (long e : a) {1275checkSum ^= e;1276}1277return (int) checkSum;1278}12791280private static int checkSumXor(short[] a) {1281short checkSum = 0;12821283for (short e : a) {1284checkSum ^= e;1285}1286return (int) checkSum;1287}12881289private static int checkSumXor(byte[] a) {1290byte checkSum = 0;12911292for (byte e : a) {1293checkSum ^= e;1294}1295return (int) checkSum;1296}12971298private static int checkSumXor(char[] a) {1299char checkSum = 0;13001301for (char e : a) {1302checkSum ^= e;1303}1304return (int) checkSum;1305}13061307private static int checkSumXor(float[] a) {1308int checkSum = 0;13091310for (float e : a) {1311checkSum ^= (int) e;1312}1313return checkSum;1314}13151316private static int checkSumXor(double[] a) {1317int checkSum = 0;13181319for (double e : a) {1320checkSum ^= (int) e;1321}1322return checkSum;1323}13241325private static int checkSumPlus(Object object) {1326if (object instanceof int[]) {1327return checkSumPlus((int[]) object);1328} else if (object instanceof long[]) {1329return checkSumPlus((long[]) object);1330} else if (object instanceof short[]) {1331return checkSumPlus((short[]) object);1332} else if (object instanceof byte[]) {1333return checkSumPlus((byte[]) object);1334} else if (object instanceof char[]) {1335return checkSumPlus((char[]) object);1336} else if (object instanceof float[]) {1337return checkSumPlus((float[]) object);1338} else if (object instanceof double[]) {1339return checkSumPlus((double[]) object);1340} else if (object instanceof Integer[]) {1341return checkSumPlus((Integer[]) object);1342} else {1343failed("Unknow type of array: " + object + " of class " +1344object.getClass().getName());1345return -1;1346}1347}13481349private static int checkSumPlus(int[] a) {1350int checkSum = 0;13511352for (int e : a) {1353checkSum += e;1354}1355return checkSum;1356}13571358private static int checkSumPlus(long[] a) {1359long checkSum = 0;13601361for (long e : a) {1362checkSum += e;1363}1364return (int) checkSum;1365}13661367private static int checkSumPlus(short[] a) {1368short checkSum = 0;13691370for (short e : a) {1371checkSum += e;1372}1373return (int) checkSum;1374}13751376private static int checkSumPlus(byte[] a) {1377byte checkSum = 0;13781379for (byte e : a) {1380checkSum += e;1381}1382return (int) checkSum;1383}13841385private static int checkSumPlus(char[] a) {1386char checkSum = 0;13871388for (char e : a) {1389checkSum += e;1390}1391return (int) checkSum;1392}13931394private static int checkSumPlus(float[] a) {1395int checkSum = 0;13961397for (float e : a) {1398checkSum += (int) e;1399}1400return checkSum;1401}14021403private static int checkSumPlus(double[] a) {1404int checkSum = 0;14051406for (double e : a) {1407checkSum += (int) e;1408}1409return checkSum;1410}14111412private static int checkSumPlus(Integer[] a) {1413int checkSum = 0;14141415for (Integer e : a) {1416checkSum += e.intValue();1417}1418return checkSum;1419}14201421private static void sortByInsertionSort(Object object) {1422if (object instanceof int[]) {1423sortByInsertionSort((int[]) object);1424} else if (object instanceof long[]) {1425sortByInsertionSort((long[]) object);1426} else if (object instanceof short[]) {1427sortByInsertionSort((short[]) object);1428} else if (object instanceof byte[]) {1429sortByInsertionSort((byte[]) object);1430} else if (object instanceof char[]) {1431sortByInsertionSort((char[]) object);1432} else if (object instanceof float[]) {1433sortByInsertionSort((float[]) object);1434} else if (object instanceof double[]) {1435sortByInsertionSort((double[]) object);1436} else if (object instanceof Integer[]) {1437sortByInsertionSort((Integer[]) object);1438} else {1439failed("Unknow type of array: " + object + " of class " +1440object.getClass().getName());1441}1442}14431444private static void sortByInsertionSort(int[] a) {1445for (int j, i = 1; i < a.length; i++) {1446int ai = a[i];1447for (j = i - 1; j >= 0 && ai < a[j]; j--) {1448a[j + 1] = a[j];1449}1450a[j + 1] = ai;1451}1452}14531454private static void sortByInsertionSort(long[] a) {1455for (int j, i = 1; i < a.length; i++) {1456long ai = a[i];1457for (j = i - 1; j >= 0 && ai < a[j]; j--) {1458a[j + 1] = a[j];1459}1460a[j + 1] = ai;1461}1462}14631464private static void sortByInsertionSort(short[] a) {1465for (int j, i = 1; i < a.length; i++) {1466short ai = a[i];1467for (j = i - 1; j >= 0 && ai < a[j]; j--) {1468a[j + 1] = a[j];1469}1470a[j + 1] = ai;1471}1472}14731474private static void sortByInsertionSort(byte[] a) {1475for (int j, i = 1; i < a.length; i++) {1476byte ai = a[i];1477for (j = i - 1; j >= 0 && ai < a[j]; j--) {1478a[j + 1] = a[j];1479}1480a[j + 1] = ai;1481}1482}14831484private static void sortByInsertionSort(char[] a) {1485for (int j, i = 1; i < a.length; i++) {1486char ai = a[i];1487for (j = i - 1; j >= 0 && ai < a[j]; j--) {1488a[j + 1] = a[j];1489}1490a[j + 1] = ai;1491}1492}14931494private static void sortByInsertionSort(float[] a) {1495for (int j, i = 1; i < a.length; i++) {1496float ai = a[i];1497for (j = i - 1; j >= 0 && ai < a[j]; j--) {1498a[j + 1] = a[j];1499}1500a[j + 1] = ai;1501}1502}15031504private static void sortByInsertionSort(double[] a) {1505for (int j, i = 1; i < a.length; i++) {1506double ai = a[i];1507for (j = i - 1; j >= 0 && ai < a[j]; j--) {1508a[j + 1] = a[j];1509}1510a[j + 1] = ai;1511}1512}15131514private static void sortByInsertionSort(Integer[] a) {1515for (int j, i = 1; i < a.length; i++) {1516Integer ai = a[i];1517for (j = i - 1; j >= 0 && ai < a[j]; j--) {1518a[j + 1] = a[j];1519}1520a[j + 1] = ai;1521}1522}15231524private static void sort(Object object) {1525if (object instanceof int[]) {1526Arrays.parallelSort((int[]) object);1527} else if (object instanceof long[]) {1528Arrays.parallelSort((long[]) object);1529} else if (object instanceof short[]) {1530Arrays.parallelSort((short[]) object);1531} else if (object instanceof byte[]) {1532Arrays.parallelSort((byte[]) object);1533} else if (object instanceof char[]) {1534Arrays.parallelSort((char[]) object);1535} else if (object instanceof float[]) {1536Arrays.parallelSort((float[]) object);1537} else if (object instanceof double[]) {1538Arrays.parallelSort((double[]) object);1539} else if (object instanceof Integer[]) {1540Arrays.parallelSort((Integer[]) object);1541} else {1542failed("Unknow type of array: " + object + " of class " +1543object.getClass().getName());1544}1545}15461547private static void sortSubArray(Object object, int fromIndex, int toIndex) {1548if (object instanceof int[]) {1549Arrays.parallelSort((int[]) object, fromIndex, toIndex);1550} else if (object instanceof long[]) {1551Arrays.parallelSort((long[]) object, fromIndex, toIndex);1552} else if (object instanceof short[]) {1553Arrays.parallelSort((short[]) object, fromIndex, toIndex);1554} else if (object instanceof byte[]) {1555Arrays.parallelSort((byte[]) object, fromIndex, toIndex);1556} else if (object instanceof char[]) {1557Arrays.parallelSort((char[]) object, fromIndex, toIndex);1558} else if (object instanceof float[]) {1559Arrays.parallelSort((float[]) object, fromIndex, toIndex);1560} else if (object instanceof double[]) {1561Arrays.parallelSort((double[]) object, fromIndex, toIndex);1562} else if (object instanceof Integer[]) {1563Arrays.parallelSort((Integer[]) object, fromIndex, toIndex);1564} else {1565failed("Unknow type of array: " + object + " of class " +1566object.getClass().getName());1567}1568}15691570private static void checkSubArray(Object object, int fromIndex, int toIndex, int m) {1571if (object instanceof int[]) {1572checkSubArray((int[]) object, fromIndex, toIndex, m);1573} else if (object instanceof long[]) {1574checkSubArray((long[]) object, fromIndex, toIndex, m);1575} else if (object instanceof short[]) {1576checkSubArray((short[]) object, fromIndex, toIndex, m);1577} else if (object instanceof byte[]) {1578checkSubArray((byte[]) object, fromIndex, toIndex, m);1579} else if (object instanceof char[]) {1580checkSubArray((char[]) object, fromIndex, toIndex, m);1581} else if (object instanceof float[]) {1582checkSubArray((float[]) object, fromIndex, toIndex, m);1583} else if (object instanceof double[]) {1584checkSubArray((double[]) object, fromIndex, toIndex, m);1585} else if (object instanceof Integer[]) {1586checkSubArray((Integer[]) object, fromIndex, toIndex, m);1587} else {1588failed("Unknow type of array: " + object + " of class " +1589object.getClass().getName());1590}1591}15921593private static void checkSubArray(Integer[] a, int fromIndex, int toIndex, int m) {1594for (int i = 0; i < fromIndex; i++) {1595if (a[i].intValue() != 0xDEDA) {1596failed("Range sort changes left element on position " + i +1597": " + a[i] + ", must be " + 0xDEDA);1598}1599}16001601for (int i = fromIndex; i < toIndex - 1; i++) {1602if (a[i].intValue() > a[i + 1].intValue()) {1603failedSort(i, "" + a[i], "" + a[i + 1]);1604}1605}16061607for (int i = toIndex; i < a.length; i++) {1608if (a[i].intValue() != 0xBABA) {1609failed("Range sort changes right element on position " + i +1610": " + a[i] + ", must be " + 0xBABA);1611}1612}1613}16141615private static void checkSubArray(int[] a, int fromIndex, int toIndex, int m) {1616for (int i = 0; i < fromIndex; i++) {1617if (a[i] != 0xDEDA) {1618failed("Range sort changes left element on position " + i +1619": " + a[i] + ", must be " + 0xDEDA);1620}1621}16221623for (int i = fromIndex; i < toIndex - 1; i++) {1624if (a[i] > a[i + 1]) {1625failedSort(i, "" + a[i], "" + a[i + 1]);1626}1627}16281629for (int i = toIndex; i < a.length; i++) {1630if (a[i] != 0xBABA) {1631failed("Range sort changes right element on position " + i +1632": " + a[i] + ", must be " + 0xBABA);1633}1634}1635}16361637private static void checkSubArray(byte[] a, int fromIndex, int toIndex, int m) {1638for (int i = 0; i < fromIndex; i++) {1639if (a[i] != (byte) 0xDEDA) {1640failed("Range sort changes left element on position " + i +1641": " + a[i] + ", must be " + 0xDEDA);1642}1643}16441645for (int i = fromIndex; i < toIndex - 1; i++) {1646if (a[i] > a[i + 1]) {1647failedSort(i, "" + a[i], "" + a[i + 1]);1648}1649}16501651for (int i = toIndex; i < a.length; i++) {1652if (a[i] != (byte) 0xBABA) {1653failed("Range sort changes right element on position " + i +1654": " + a[i] + ", must be " + 0xBABA);1655}1656}1657}16581659private static void checkSubArray(long[] a, int fromIndex, int toIndex, int m) {1660for (int i = 0; i < fromIndex; i++) {1661if (a[i] != (long) 0xDEDA) {1662failed("Range sort changes left element on position " + i +1663": " + a[i] + ", must be " + 0xDEDA);1664}1665}16661667for (int i = fromIndex; i < toIndex - 1; i++) {1668if (a[i] > a[i + 1]) {1669failedSort(i, "" + a[i], "" + a[i + 1]);1670}1671}16721673for (int i = toIndex; i < a.length; i++) {1674if (a[i] != (long) 0xBABA) {1675failed("Range sort changes right element on position " + i +1676": " + a[i] + ", must be " + 0xBABA);1677}1678}1679}16801681private static void checkSubArray(char[] a, int fromIndex, int toIndex, int m) {1682for (int i = 0; i < fromIndex; i++) {1683if (a[i] != (char) 0xDEDA) {1684failed("Range sort changes left element on position " + i +1685": " + a[i] + ", must be " + 0xDEDA);1686}1687}16881689for (int i = fromIndex; i < toIndex - 1; i++) {1690if (a[i] > a[i + 1]) {1691failedSort(i, "" + a[i], "" + a[i + 1]);1692}1693}16941695for (int i = toIndex; i < a.length; i++) {1696if (a[i] != (char) 0xBABA) {1697failed("Range sort changes right element on position " + i +1698": " + a[i] + ", must be " + 0xBABA);1699}1700}1701}17021703private static void checkSubArray(short[] a, int fromIndex, int toIndex, int m) {1704for (int i = 0; i < fromIndex; i++) {1705if (a[i] != (short) 0xDEDA) {1706failed("Range sort changes left element on position " + i +1707": " + a[i] + ", must be " + 0xDEDA);1708}1709}17101711for (int i = fromIndex; i < toIndex - 1; i++) {1712if (a[i] > a[i + 1]) {1713failedSort(i, "" + a[i], "" + a[i + 1]);1714}1715}17161717for (int i = toIndex; i < a.length; i++) {1718if (a[i] != (short) 0xBABA) {1719failed("Range sort changes right element on position " + i +1720": " + a[i] + ", must be " + 0xBABA);1721}1722}1723}17241725private static void checkSubArray(float[] a, int fromIndex, int toIndex, int m) {1726for (int i = 0; i < fromIndex; i++) {1727if (a[i] != (float) 0xDEDA) {1728failed("Range sort changes left element on position " + i +1729": " + a[i] + ", must be " + 0xDEDA);1730}1731}17321733for (int i = fromIndex; i < toIndex - 1; i++) {1734if (a[i] > a[i + 1]) {1735failedSort(i, "" + a[i], "" + a[i + 1]);1736}1737}17381739for (int i = toIndex; i < a.length; i++) {1740if (a[i] != (float) 0xBABA) {1741failed("Range sort changes right element on position " + i +1742": " + a[i] + ", must be " + 0xBABA);1743}1744}1745}17461747private static void checkSubArray(double[] a, int fromIndex, int toIndex, int m) {1748for (int i = 0; i < fromIndex; i++) {1749if (a[i] != (double) 0xDEDA) {1750failed("Range sort changes left element on position " + i +1751": " + a[i] + ", must be " + 0xDEDA);1752}1753}17541755for (int i = fromIndex; i < toIndex - 1; i++) {1756if (a[i] > a[i + 1]) {1757failedSort(i, "" + a[i], "" + a[i + 1]);1758}1759}17601761for (int i = toIndex; i < a.length; i++) {1762if (a[i] != (double) 0xBABA) {1763failed("Range sort changes right element on position " + i +1764": " + a[i] + ", must be " + 0xBABA);1765}1766}1767}17681769private static void checkRange(Object object, int m) {1770if (object instanceof int[]) {1771checkRange((int[]) object, m);1772} else if (object instanceof long[]) {1773checkRange((long[]) object, m);1774} else if (object instanceof short[]) {1775checkRange((short[]) object, m);1776} else if (object instanceof byte[]) {1777checkRange((byte[]) object, m);1778} else if (object instanceof char[]) {1779checkRange((char[]) object, m);1780} else if (object instanceof float[]) {1781checkRange((float[]) object, m);1782} else if (object instanceof double[]) {1783checkRange((double[]) object, m);1784} else if (object instanceof Integer[]) {1785checkRange((Integer[]) object, m);1786} else {1787failed("Unknow type of array: " + object + " of class " +1788object.getClass().getName());1789}1790}17911792private static void checkRange(Integer[] a, int m) {1793try {1794Arrays.parallelSort(a, m + 1, m);17951796failed("ParallelSort does not throw IllegalArgumentException " +1797" as expected: fromIndex = " + (m + 1) +1798" toIndex = " + m);1799}1800catch (IllegalArgumentException iae) {1801try {1802Arrays.parallelSort(a, -m, a.length);18031804failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1805" as expected: fromIndex = " + (-m));1806}1807catch (ArrayIndexOutOfBoundsException aoe) {1808try {1809Arrays.parallelSort(a, 0, a.length + m);18101811failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1812" as expected: toIndex = " + (a.length + m));1813}1814catch (ArrayIndexOutOfBoundsException aie) {1815return;1816}1817}1818}1819}18201821private static void checkRange(int[] a, int m) {1822try {1823Arrays.parallelSort(a, m + 1, m);18241825failed("ParallelSort does not throw IllegalArgumentException " +1826" as expected: fromIndex = " + (m + 1) +1827" toIndex = " + m);1828}1829catch (IllegalArgumentException iae) {1830try {1831Arrays.parallelSort(a, -m, a.length);18321833failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1834" as expected: fromIndex = " + (-m));1835}1836catch (ArrayIndexOutOfBoundsException aoe) {1837try {1838Arrays.parallelSort(a, 0, a.length + m);18391840failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1841" as expected: toIndex = " + (a.length + m));1842}1843catch (ArrayIndexOutOfBoundsException aie) {1844return;1845}1846}1847}1848}18491850private static void checkRange(long[] a, int m) {1851try {1852Arrays.parallelSort(a, m + 1, m);18531854failed("ParallelSort does not throw IllegalArgumentException " +1855" as expected: fromIndex = " + (m + 1) +1856" toIndex = " + m);1857}1858catch (IllegalArgumentException iae) {1859try {1860Arrays.parallelSort(a, -m, a.length);18611862failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1863" as expected: fromIndex = " + (-m));1864}1865catch (ArrayIndexOutOfBoundsException aoe) {1866try {1867Arrays.parallelSort(a, 0, a.length + m);18681869failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1870" as expected: toIndex = " + (a.length + m));1871}1872catch (ArrayIndexOutOfBoundsException aie) {1873return;1874}1875}1876}1877}18781879private static void checkRange(byte[] a, int m) {1880try {1881Arrays.parallelSort(a, m + 1, m);18821883failed("ParallelSort does not throw IllegalArgumentException " +1884" as expected: fromIndex = " + (m + 1) +1885" toIndex = " + m);1886}1887catch (IllegalArgumentException iae) {1888try {1889Arrays.parallelSort(a, -m, a.length);18901891failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1892" as expected: fromIndex = " + (-m));1893}1894catch (ArrayIndexOutOfBoundsException aoe) {1895try {1896Arrays.parallelSort(a, 0, a.length + m);18971898failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1899" as expected: toIndex = " + (a.length + m));1900}1901catch (ArrayIndexOutOfBoundsException aie) {1902return;1903}1904}1905}1906}19071908private static void checkRange(short[] a, int m) {1909try {1910Arrays.parallelSort(a, m + 1, m);19111912failed("ParallelSort does not throw IllegalArgumentException " +1913" as expected: fromIndex = " + (m + 1) +1914" toIndex = " + m);1915}1916catch (IllegalArgumentException iae) {1917try {1918Arrays.parallelSort(a, -m, a.length);19191920failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1921" as expected: fromIndex = " + (-m));1922}1923catch (ArrayIndexOutOfBoundsException aoe) {1924try {1925Arrays.parallelSort(a, 0, a.length + m);19261927failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1928" as expected: toIndex = " + (a.length + m));1929}1930catch (ArrayIndexOutOfBoundsException aie) {1931return;1932}1933}1934}1935}19361937private static void checkRange(char[] a, int m) {1938try {1939Arrays.parallelSort(a, m + 1, m);19401941failed("ParallelSort does not throw IllegalArgumentException " +1942" as expected: fromIndex = " + (m + 1) +1943" toIndex = " + m);1944}1945catch (IllegalArgumentException iae) {1946try {1947Arrays.parallelSort(a, -m, a.length);19481949failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1950" as expected: fromIndex = " + (-m));1951}1952catch (ArrayIndexOutOfBoundsException aoe) {1953try {1954Arrays.parallelSort(a, 0, a.length + m);19551956failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1957" as expected: toIndex = " + (a.length + m));1958}1959catch (ArrayIndexOutOfBoundsException aie) {1960return;1961}1962}1963}1964}19651966private static void checkRange(float[] a, int m) {1967try {1968Arrays.parallelSort(a, m + 1, m);19691970failed("ParallelSort does not throw IllegalArgumentException " +1971" as expected: fromIndex = " + (m + 1) +1972" toIndex = " + m);1973}1974catch (IllegalArgumentException iae) {1975try {1976Arrays.parallelSort(a, -m, a.length);19771978failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1979" as expected: fromIndex = " + (-m));1980}1981catch (ArrayIndexOutOfBoundsException aoe) {1982try {1983Arrays.parallelSort(a, 0, a.length + m);19841985failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +1986" as expected: toIndex = " + (a.length + m));1987}1988catch (ArrayIndexOutOfBoundsException aie) {1989return;1990}1991}1992}1993}19941995private static void checkRange(double[] a, int m) {1996try {1997Arrays.parallelSort(a, m + 1, m);19981999failed("ParallelSort does not throw IllegalArgumentException " +2000" as expected: fromIndex = " + (m + 1) +2001" toIndex = " + m);2002}2003catch (IllegalArgumentException iae) {2004try {2005Arrays.parallelSort(a, -m, a.length);20062007failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +2008" as expected: fromIndex = " + (-m));2009}2010catch (ArrayIndexOutOfBoundsException aoe) {2011try {2012Arrays.parallelSort(a, 0, a.length + m);20132014failed("ParallelSort does not throw ArrayIndexOutOfBoundsException " +2015" as expected: toIndex = " + (a.length + m));2016}2017catch (ArrayIndexOutOfBoundsException aie) {2018return;2019}2020}2021}2022}20232024private static void outArray(Object[] a) {2025for (int i = 0; i < a.length; i++) {2026out.print(a[i] + " ");2027}2028out.println();2029}20302031private static void outArray(int[] a) {2032for (int i = 0; i < a.length; i++) {2033out.print(a[i] + " ");2034}2035out.println();2036}20372038private static void outArray(float[] a) {2039for (int i = 0; i < a.length; i++) {2040out.print(a[i] + " ");2041}2042out.println();2043}20442045private static void outArray(double[] a) {2046for (int i = 0; i < a.length; i++) {2047out.print(a[i] + " ");2048}2049out.println();2050}20512052private static class MyRandom extends Random {2053MyRandom(long seed) {2054super(seed);2055mySeed = seed;2056}20572058long getSeed() {2059return mySeed;2060}20612062private long mySeed;2063}20642065private static String ourDescription;2066}206720682069