Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Arrays/Sorting.java
38812 views
/*1* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6880672 6896573 6899694 6976036 7013585 701825826* @summary Exercise Arrays.sort27* @build Sorting28* @run main Sorting -shortrun29*30* @author Vladimir Yaroslavskiy31* @author Jon Bentley32* @author Josh Bloch33*/3435import java.util.Arrays;36import java.util.Random;37import java.io.PrintStream;3839public class Sorting {40private static final PrintStream out = System.out;41private static final PrintStream err = System.err;4243// Array lengths used in a long run (default)44private static final int[] LONG_RUN_LENGTHS = {451, 2, 3, 5, 8, 13, 21, 34, 55, 100, 1000, 10000, 100000, 1000000 };4647// Array lengths used in a short run48private static final int[] SHORT_RUN_LENGTHS = {491, 2, 3, 21, 55, 1000, 10000 };5051// Random initial values used in a long run (default)52private static final long[] LONG_RUN_RANDOMS = { 666, 0xC0FFEE, 999 };5354// Random initial values used in a short run55private static final long[] SHORT_RUN_RANDOMS = { 666 };5657public static void main(String[] args) {58boolean shortRun = args.length > 0 && args[0].equals("-shortrun");59long start = System.currentTimeMillis();6061if (shortRun) {62testAndCheck(SHORT_RUN_LENGTHS, SHORT_RUN_RANDOMS);63} else {64testAndCheck(LONG_RUN_LENGTHS, LONG_RUN_RANDOMS);65}66long end = System.currentTimeMillis();6768out.format("PASSED in %d sec.\n", Math.round((end - start) / 1E3));69}7071private static void testAndCheck(int[] lengths, long[] randoms) {72testEmptyAndNullIntArray();73testEmptyAndNullLongArray();74testEmptyAndNullShortArray();75testEmptyAndNullCharArray();76testEmptyAndNullByteArray();77testEmptyAndNullFloatArray();78testEmptyAndNullDoubleArray();7980for (int length : lengths) {81testMergeSort(length);82testAndCheckRange(length);83testAndCheckSubArray(length);84}85for (long seed : randoms) {86for (int length : lengths) {87testAndCheckWithInsertionSort(length, new MyRandom(seed));88testAndCheckWithCheckSum(length, new MyRandom(seed));89testAndCheckWithScrambling(length, new MyRandom(seed));90testAndCheckFloat(length, new MyRandom(seed));91testAndCheckDouble(length, new MyRandom(seed));92testStable(length, new MyRandom(seed));93}94}95}9697private static void testEmptyAndNullIntArray() {98ourDescription = "Check empty and null array";99Arrays.sort(new int[] {});100Arrays.sort(new int[] {}, 0, 0);101102try {103Arrays.sort((int[]) null);104} catch (NullPointerException expected) {105try {106Arrays.sort((int[]) null, 0, 0);107} catch (NullPointerException expected2) {108return;109}110failed("Arrays.sort(int[],fromIndex,toIndex) shouldn't " +111"catch null array");112}113failed("Arrays.sort(int[]) shouldn't catch null array");114}115116private static void testEmptyAndNullLongArray() {117ourDescription = "Check empty and null array";118Arrays.sort(new long[] {});119Arrays.sort(new long[] {}, 0, 0);120121try {122Arrays.sort((long[]) null);123} catch (NullPointerException expected) {124try {125Arrays.sort((long[]) null, 0, 0);126} catch (NullPointerException expected2) {127return;128}129failed("Arrays.sort(long[],fromIndex,toIndex) shouldn't " +130"catch null array");131}132failed("Arrays.sort(long[]) shouldn't catch null array");133}134135private static void testEmptyAndNullShortArray() {136ourDescription = "Check empty and null array";137Arrays.sort(new short[] {});138Arrays.sort(new short[] {}, 0, 0);139140try {141Arrays.sort((short[]) null);142} catch (NullPointerException expected) {143try {144Arrays.sort((short[]) null, 0, 0);145} catch (NullPointerException expected2) {146return;147}148failed("Arrays.sort(short[],fromIndex,toIndex) shouldn't " +149"catch null array");150}151failed("Arrays.sort(short[]) shouldn't catch null array");152}153154private static void testEmptyAndNullCharArray() {155ourDescription = "Check empty and null array";156Arrays.sort(new char[] {});157Arrays.sort(new char[] {}, 0, 0);158159try {160Arrays.sort((char[]) null);161} catch (NullPointerException expected) {162try {163Arrays.sort((char[]) null, 0, 0);164} catch (NullPointerException expected2) {165return;166}167failed("Arrays.sort(char[],fromIndex,toIndex) shouldn't " +168"catch null array");169}170failed("Arrays.sort(char[]) shouldn't catch null array");171}172173private static void testEmptyAndNullByteArray() {174ourDescription = "Check empty and null array";175Arrays.sort(new byte[] {});176Arrays.sort(new byte[] {}, 0, 0);177178try {179Arrays.sort((byte[]) null);180} catch (NullPointerException expected) {181try {182Arrays.sort((byte[]) null, 0, 0);183} catch (NullPointerException expected2) {184return;185}186failed("Arrays.sort(byte[],fromIndex,toIndex) shouldn't " +187"catch null array");188}189failed("Arrays.sort(byte[]) shouldn't catch null array");190}191192private static void testEmptyAndNullFloatArray() {193ourDescription = "Check empty and null array";194Arrays.sort(new float[] {});195Arrays.sort(new float[] {}, 0, 0);196197try {198Arrays.sort((float[]) null);199} catch (NullPointerException expected) {200try {201Arrays.sort((float[]) null, 0, 0);202} catch (NullPointerException expected2) {203return;204}205failed("Arrays.sort(float[],fromIndex,toIndex) shouldn't " +206"catch null array");207}208failed("Arrays.sort(float[]) shouldn't catch null array");209}210211private static void testEmptyAndNullDoubleArray() {212ourDescription = "Check empty and null array";213Arrays.sort(new double[] {});214Arrays.sort(new double[] {}, 0, 0);215216try {217Arrays.sort((double[]) null);218} catch (NullPointerException expected) {219try {220Arrays.sort((double[]) null, 0, 0);221} catch (NullPointerException expected2) {222return;223}224failed("Arrays.sort(double[],fromIndex,toIndex) shouldn't " +225"catch null array");226}227failed("Arrays.sort(double[]) shouldn't catch null array");228}229230private static void testAndCheckSubArray(int length) {231ourDescription = "Check sorting of subarray";232int[] golden = new int[length];233boolean newLine = false;234235for (int m = 1; m < length / 2; m *= 2) {236newLine = true;237int fromIndex = m;238int toIndex = length - m;239240prepareSubArray(golden, fromIndex, toIndex, m);241int[] test = golden.clone();242243for (TypeConverter converter : TypeConverter.values()) {244out.println("Test 'subarray': " + converter +245" length = " + length + ", m = " + m);246Object convertedGolden = converter.convert(golden);247Object convertedTest = converter.convert(test);248sortSubArray(convertedTest, fromIndex, toIndex);249checkSubArray(convertedTest, fromIndex, toIndex, m);250}251}252if (newLine) {253out.println();254}255}256257private static void testAndCheckRange(int length) {258ourDescription = "Check range check";259int[] golden = new int[length];260261for (int m = 1; m < 2 * length; m *= 2) {262for (int i = 1; i <= length; i++) {263golden[i - 1] = i % m + m % i;264}265for (TypeConverter converter : TypeConverter.values()) {266out.println("Test 'range': " + converter +267", length = " + length + ", m = " + m);268Object convertedGolden = converter.convert(golden);269checkRange(convertedGolden, m);270}271}272out.println();273}274275private static void testStable(int length, MyRandom random) {276ourDescription = "Check if sorting is stable";277Pair[] a = build(length, random);278279out.println("Test 'stable': " + "random = " + random.getSeed() +280", length = " + length);281Arrays.sort(a);282checkSorted(a);283checkStable(a);284out.println();285}286287private static void checkSorted(Pair[] a) {288for (int i = 0; i < a.length - 1; i++) {289if (a[i].getKey() > a[i + 1].getKey()) {290failedSort(i, "" + a[i].getKey(), "" + a[i + 1].getKey());291}292}293}294295private static void checkStable(Pair[] a) {296for (int i = 0; i < a.length / 4; ) {297int key1 = a[i].getKey();298int value1 = a[i++].getValue();299int key2 = a[i].getKey();300int value2 = a[i++].getValue();301int key3 = a[i].getKey();302int value3 = a[i++].getValue();303int key4 = a[i].getKey();304int value4 = a[i++].getValue();305306if (!(key1 == key2 && key2 == key3 && key3 == key4)) {307failed("On position " + i + " keys are different " +308key1 + ", " + key2 + ", " + key3 + ", " + key4);309}310if (!(value1 < value2 && value2 < value3 && value3 < value4)) {311failed("Sorting is not stable at position " + i +312". Second values have been changed: " + value1 + ", " +313value2 + ", " + value3 + ", " + value4);314}315}316}317318private static Pair[] build(int length, Random random) {319Pair[] a = new Pair[length * 4];320321for (int i = 0; i < a.length; ) {322int key = random.nextInt();323a[i++] = new Pair(key, 1);324a[i++] = new Pair(key, 2);325a[i++] = new Pair(key, 3);326a[i++] = new Pair(key, 4);327}328return a;329}330331private static final class Pair implements Comparable<Pair> {332Pair(int key, int value) {333myKey = key;334myValue = value;335}336337int getKey() {338return myKey;339}340341int getValue() {342return myValue;343}344345public int compareTo(Pair pair) {346if (myKey < pair.myKey) {347return -1;348}349if (myKey > pair.myKey) {350return 1;351}352return 0;353}354355@Override356public String toString() {357return "(" + myKey + ", " + myValue + ")";358}359360private int myKey;361private int myValue;362}363364365private static void testAndCheckWithInsertionSort(int length, MyRandom random) {366if (length > 1000) {367return;368}369ourDescription = "Check sorting with insertion sort";370int[] golden = new int[length];371372for (int m = 1; m < 2 * length; m *= 2) {373for (UnsortedBuilder builder : UnsortedBuilder.values()) {374builder.build(golden, m, random);375int[] test = golden.clone();376377for (TypeConverter converter : TypeConverter.values()) {378out.println("Test 'insertion sort': " + converter +379" " + builder + "random = " + random.getSeed() +380", length = " + length + ", m = " + m);381Object convertedGolden = converter.convert(golden);382Object convertedTest1 = converter.convert(test);383Object convertedTest2 = converter.convert(test);384sort(convertedTest1);385sortByInsertionSort(convertedTest2);386compare(convertedTest1, convertedTest2);387}388}389}390out.println();391}392393private static void testMergeSort(int length) {394if (length < 1000) {395return;396}397ourDescription = "Check merge sorting";398int[] golden = new int[length];399int period = 67; // java.util.DualPivotQuicksort.MAX_RUN_COUNT400401for (int m = period - 2; m <= period + 2; m++) {402for (MergeBuilder builder : MergeBuilder.values()) {403builder.build(golden, m);404int[] test = golden.clone();405406for (TypeConverter converter : TypeConverter.values()) {407out.println("Test 'merge sort': " + converter + " " +408builder + "length = " + length + ", m = " + m);409Object convertedGolden = converter.convert(golden);410sort(convertedGolden);411checkSorted(convertedGolden);412}413}414}415out.println();416}417418private static void testAndCheckWithCheckSum(int length, MyRandom random) {419ourDescription = "Check sorting with check sum";420int[] golden = new int[length];421422for (int m = 1; m < 2 * length; m *= 2) {423for (UnsortedBuilder builder : UnsortedBuilder.values()) {424builder.build(golden, m, random);425int[] test = golden.clone();426427for (TypeConverter converter : TypeConverter.values()) {428out.println("Test 'check sum': " + converter +429" " + builder + "random = " + random.getSeed() +430", length = " + length + ", m = " + m);431Object convertedGolden = converter.convert(golden);432Object convertedTest = converter.convert(test);433sort(convertedTest);434checkWithCheckSum(convertedTest, convertedGolden);435}436}437}438out.println();439}440441private static void testAndCheckWithScrambling(int length, MyRandom random) {442ourDescription = "Check sorting with scrambling";443int[] golden = new int[length];444445for (int m = 1; m <= 7; m++) {446if (m > length) {447break;448}449for (SortedBuilder builder : SortedBuilder.values()) {450builder.build(golden, m);451int[] test = golden.clone();452scramble(test, random);453454for (TypeConverter converter : TypeConverter.values()) {455out.println("Test 'scrambling': " + converter +456" " + builder + "random = " + random.getSeed() +457", length = " + length + ", m = " + m);458Object convertedGolden = converter.convert(golden);459Object convertedTest = converter.convert(test);460sort(convertedTest);461compare(convertedTest, convertedGolden);462}463}464}465out.println();466}467468private static void testAndCheckFloat(int length, MyRandom random) {469ourDescription = "Check float sorting";470float[] golden = new float[length];471final int MAX = 10;472boolean newLine = false;473474for (int a = 0; a <= MAX; a++) {475for (int g = 0; g <= MAX; g++) {476for (int z = 0; z <= MAX; z++) {477for (int n = 0; n <= MAX; n++) {478for (int p = 0; p <= MAX; p++) {479if (a + g + z + n + p > length) {480continue;481}482if (a + g + z + n + p < length) {483continue;484}485for (FloatBuilder builder : FloatBuilder.values()) {486out.println("Test 'float': random = " + random.getSeed() +487", length = " + length + ", a = " + a + ", g = " +488g + ", z = " + z + ", n = " + n + ", p = " + p);489builder.build(golden, a, g, z, n, p, random);490float[] test = golden.clone();491scramble(test, random);492sort(test);493compare(test, golden, a, n, g);494}495newLine = true;496}497}498}499}500}501if (newLine) {502out.println();503}504}505506private static void testAndCheckDouble(int length, MyRandom random) {507ourDescription = "Check double sorting";508double[] golden = new double[length];509final int MAX = 10;510boolean newLine = false;511512for (int a = 0; a <= MAX; a++) {513for (int g = 0; g <= MAX; g++) {514for (int z = 0; z <= MAX; z++) {515for (int n = 0; n <= MAX; n++) {516for (int p = 0; p <= MAX; p++) {517if (a + g + z + n + p > length) {518continue;519}520if (a + g + z + n + p < length) {521continue;522}523for (DoubleBuilder builder : DoubleBuilder.values()) {524out.println("Test 'double': random = " + random.getSeed() +525", length = " + length + ", a = " + a + ", g = " +526g + ", z = " + z + ", n = " + n + ", p = " + p);527builder.build(golden, a, g, z, n, p, random);528double[] test = golden.clone();529scramble(test, random);530sort(test);531compare(test, golden, a, n, g);532}533newLine = true;534}535}536}537}538}539if (newLine) {540out.println();541}542}543544private static void prepareSubArray(int[] a, int fromIndex, int toIndex, int m) {545for (int i = 0; i < fromIndex; i++) {546a[i] = 0xDEDA;547}548int middle = (fromIndex + toIndex) >>> 1;549int k = 0;550551for (int i = fromIndex; i < middle; i++) {552a[i] = k++;553}554for (int i = middle; i < toIndex; i++) {555a[i] = k--;556}557for (int i = toIndex; i < a.length; i++) {558a[i] = 0xBABA;559}560}561562private static void scramble(int[] a, Random random) {563for (int i = 0; i < a.length * 7; i++) {564swap(a, random.nextInt(a.length), random.nextInt(a.length));565}566}567568private static void scramble(float[] a, Random random) {569for (int i = 0; i < a.length * 7; i++) {570swap(a, random.nextInt(a.length), random.nextInt(a.length));571}572}573574private static void scramble(double[] a, Random random) {575for (int i = 0; i < a.length * 7; i++) {576swap(a, random.nextInt(a.length), random.nextInt(a.length));577}578}579580private static void swap(int[] a, int i, int j) {581int t = a[i];582a[i] = a[j];583a[j] = t;584}585586private static void swap(float[] a, int i, int j) {587float t = a[i];588a[i] = a[j];589a[j] = t;590}591592private static void swap(double[] a, int i, int j) {593double t = a[i];594a[i] = a[j];595a[j] = t;596}597598private static enum TypeConverter {599INT {600Object convert(int[] a) {601return a.clone();602}603},604LONG {605Object convert(int[] a) {606long[] b = new long[a.length];607608for (int i = 0; i < a.length; i++) {609b[i] = (long) a[i];610}611return b;612}613},614BYTE {615Object convert(int[] a) {616byte[] b = new byte[a.length];617618for (int i = 0; i < a.length; i++) {619b[i] = (byte) a[i];620}621return b;622}623},624SHORT {625Object convert(int[] a) {626short[] b = new short[a.length];627628for (int i = 0; i < a.length; i++) {629b[i] = (short) a[i];630}631return b;632}633},634CHAR {635Object convert(int[] a) {636char[] b = new char[a.length];637638for (int i = 0; i < a.length; i++) {639b[i] = (char) a[i];640}641return b;642}643},644FLOAT {645Object convert(int[] a) {646float[] b = new float[a.length];647648for (int i = 0; i < a.length; i++) {649b[i] = (float) a[i];650}651return b;652}653},654DOUBLE {655Object convert(int[] a) {656double[] b = new double[a.length];657658for (int i = 0; i < a.length; i++) {659b[i] = (double) a[i];660}661return b;662}663},664INTEGER {665Object convert(int[] a) {666Integer[] b = new Integer[a.length];667668for (int i = 0; i < a.length; i++) {669b[i] = new Integer(a[i]);670}671return b;672}673};674675abstract Object convert(int[] a);676677@Override public String toString() {678String name = name();679680for (int i = name.length(); i < 9; i++) {681name += " ";682}683return name;684}685}686687private static enum FloatBuilder {688SIMPLE {689void build(float[] x, int a, int g, int z, int n, int p, Random random) {690int fromIndex = 0;691float negativeValue = -random.nextFloat();692float positiveValue = random.nextFloat();693694writeValue(x, negativeValue, fromIndex, n);695fromIndex += n;696697writeValue(x, -0.0f, fromIndex, g);698fromIndex += g;699700writeValue(x, 0.0f, fromIndex, z);701fromIndex += z;702703writeValue(x, positiveValue, fromIndex, p);704fromIndex += p;705706writeValue(x, Float.NaN, fromIndex, a);707}708};709710abstract void build(float[] x, int a, int g, int z, int n, int p, Random random);711}712713private static enum DoubleBuilder {714SIMPLE {715void build(double[] x, int a, int g, int z, int n, int p, Random random) {716int fromIndex = 0;717double negativeValue = -random.nextFloat();718double positiveValue = random.nextFloat();719720writeValue(x, negativeValue, fromIndex, n);721fromIndex += n;722723writeValue(x, -0.0d, fromIndex, g);724fromIndex += g;725726writeValue(x, 0.0d, fromIndex, z);727fromIndex += z;728729writeValue(x, positiveValue, fromIndex, p);730fromIndex += p;731732writeValue(x, Double.NaN, fromIndex, a);733}734};735736abstract void build(double[] x, int a, int g, int z, int n, int p, Random random);737}738739private static void writeValue(float[] a, float value, int fromIndex, int count) {740for (int i = fromIndex; i < fromIndex + count; i++) {741a[i] = value;742}743}744745private static void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZero) {746for (int i = a.length - numNaN; i < a.length; i++) {747if (a[i] == a[i]) {748failed("On position " + i + " must be NaN instead of " + a[i]);749}750}751final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f);752753for (int i = numNeg; i < numNeg + numNegZero; i++) {754if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) {755failed("On position " + i + " must be -0.0 instead of " + a[i]);756}757}758for (int i = 0; i < a.length - numNaN; i++) {759if (a[i] != b[i]) {760failedCompare(i, "" + a[i], "" + b[i]);761}762}763}764765private static void writeValue(double[] a, double value, int fromIndex, int count) {766for (int i = fromIndex; i < fromIndex + count; i++) {767a[i] = value;768}769}770771private static void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) {772for (int i = a.length - numNaN; i < a.length; i++) {773if (a[i] == a[i]) {774failed("On position " + i + " must be NaN instead of " + a[i]);775}776}777final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d);778779for (int i = numNeg; i < numNeg + numNegZero; i++) {780if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) {781failed("On position " + i + " must be -0.0 instead of " + a[i]);782}783}784for (int i = 0; i < a.length - numNaN; i++) {785if (a[i] != b[i]) {786failedCompare(i, "" + a[i], "" + b[i]);787}788}789}790791private static enum SortedBuilder {792REPEATED {793void build(int[] a, int m) {794int period = a.length / m;795int i = 0;796int k = 0;797798while (true) {799for (int t = 1; t <= period; t++) {800if (i >= a.length) {801return;802}803a[i++] = k;804}805if (i >= a.length) {806return;807}808k++;809}810}811},812ORGAN_PIPES {813void build(int[] a, int m) {814int i = 0;815int k = m;816817while (true) {818for (int t = 1; t <= m; t++) {819if (i >= a.length) {820return;821}822a[i++] = k;823}824}825}826};827828abstract void build(int[] a, int m);829830@Override public String toString() {831String name = name();832833for (int i = name.length(); i < 12; i++) {834name += " ";835}836return name;837}838}839840private static enum MergeBuilder {841ASCENDING {842void build(int[] a, int m) {843int period = a.length / m;844int v = 1, i = 0;845846for (int k = 0; k < m; k++) {847v = 1;848for (int p = 0; p < period; p++) {849a[i++] = v++;850}851}852for (int j = i; j < a.length - 1; j++) {853a[j] = v++;854}855a[a.length - 1] = 0;856}857},858DESCENDING {859void build(int[] a, int m) {860int period = a.length / m;861int v = -1, i = 0;862863for (int k = 0; k < m; k++) {864v = -1;865for (int p = 0; p < period; p++) {866a[i++] = v--;867}868}869for (int j = i; j < a.length - 1; j++) {870a[j] = v--;871}872a[a.length - 1] = 0;873}874};875876abstract void build(int[] a, int m);877878@Override public String toString() {879String name = name();880881for (int i = name.length(); i < 12; i++) {882name += " ";883}884return name;885}886}887888private static enum UnsortedBuilder {889RANDOM {890void build(int[] a, int m, Random random) {891for (int i = 0; i < a.length; i++) {892a[i] = random.nextInt();893}894}895},896ASCENDING {897void build(int[] a, int m, Random random) {898for (int i = 0; i < a.length; i++) {899a[i] = m + i;900}901}902},903DESCENDING {904void build(int[] a, int m, Random random) {905for (int i = 0; i < a.length; i++) {906a[i] = a.length - m - i;907}908}909},910ALL_EQUAL {911void build(int[] a, int m, Random random) {912for (int i = 0; i < a.length; i++) {913a[i] = m;914}915}916},917SAW {918void build(int[] a, int m, Random random) {919int incCount = 1;920int decCount = a.length;921int i = 0;922int period = m--;923924while (true) {925for (int k = 1; k <= period; k++) {926if (i >= a.length) {927return;928}929a[i++] = incCount++;930}931period += m;932933for (int k = 1; k <= period; k++) {934if (i >= a.length) {935return;936}937a[i++] = decCount--;938}939period += m;940}941}942},943REPEATED {944void build(int[] a, int m, Random random) {945for (int i = 0; i < a.length; i++) {946a[i] = i % m;947}948}949},950DUPLICATED {951void build(int[] a, int m, Random random) {952for (int i = 0; i < a.length; i++) {953a[i] = random.nextInt(m);954}955}956},957ORGAN_PIPES {958void build(int[] a, int m, Random random) {959int middle = a.length / (m + 1);960961for (int i = 0; i < middle; i++) {962a[i] = i;963}964for (int i = middle; i < a.length; i++) {965a[i] = a.length - i - 1;966}967}968},969STAGGER {970void build(int[] a, int m, Random random) {971for (int i = 0; i < a.length; i++) {972a[i] = (i * m + i) % a.length;973}974}975},976PLATEAU {977void build(int[] a, int m, Random random) {978for (int i = 0; i < a.length; i++) {979a[i] = Math.min(i, m);980}981}982},983SHUFFLE {984void build(int[] a, int m, Random random) {985int x = 0, y = 0;986for (int i = 0; i < a.length; i++) {987a[i] = random.nextBoolean() ? (x += 2) : (y += 2);988}989}990};991992abstract void build(int[] a, int m, Random random);993994@Override public String toString() {995String name = name();996997for (int i = name.length(); i < 12; i++) {998name += " ";999}1000return name;1001}1002}10031004private static void checkWithCheckSum(Object test, Object golden) {1005checkSorted(test);1006checkCheckSum(test, golden);1007}10081009private static void failed(String message) {1010err.format("\n*** TEST FAILED - %s.\n\n%s.\n\n", ourDescription, message);1011throw new RuntimeException("Test failed - see log file for details");1012}10131014private static void failedSort(int index, String value1, String value2) {1015failed("Array is not sorted at " + index + "-th position: " +1016value1 + " and " + value2);1017}10181019private static void failedCompare(int index, String value1, String value2) {1020failed("On position " + index + " must be " + value2 + " instead of " + value1);1021}10221023private static void compare(Object test, Object golden) {1024if (test instanceof int[]) {1025compare((int[]) test, (int[]) golden);1026} else if (test instanceof long[]) {1027compare((long[]) test, (long[]) golden);1028} else if (test instanceof short[]) {1029compare((short[]) test, (short[]) golden);1030} else if (test instanceof byte[]) {1031compare((byte[]) test, (byte[]) golden);1032} else if (test instanceof char[]) {1033compare((char[]) test, (char[]) golden);1034} else if (test instanceof float[]) {1035compare((float[]) test, (float[]) golden);1036} else if (test instanceof double[]) {1037compare((double[]) test, (double[]) golden);1038} else if (test instanceof Integer[]) {1039compare((Integer[]) test, (Integer[]) golden);1040} else {1041failed("Unknow type of array: " + test + " of class " +1042test.getClass().getName());1043}1044}10451046private static void compare(int[] a, int[] b) {1047for (int i = 0; i < a.length; i++) {1048if (a[i] != b[i]) {1049failedCompare(i, "" + a[i], "" + b[i]);1050}1051}1052}10531054private static void compare(long[] a, long[] b) {1055for (int i = 0; i < a.length; i++) {1056if (a[i] != b[i]) {1057failedCompare(i, "" + a[i], "" + b[i]);1058}1059}1060}10611062private static void compare(short[] a, short[] b) {1063for (int i = 0; i < a.length; i++) {1064if (a[i] != b[i]) {1065failedCompare(i, "" + a[i], "" + b[i]);1066}1067}1068}10691070private static void compare(byte[] a, byte[] b) {1071for (int i = 0; i < a.length; i++) {1072if (a[i] != b[i]) {1073failedCompare(i, "" + a[i], "" + b[i]);1074}1075}1076}10771078private static void compare(char[] a, char[] b) {1079for (int i = 0; i < a.length; i++) {1080if (a[i] != b[i]) {1081failedCompare(i, "" + a[i], "" + b[i]);1082}1083}1084}10851086private static void compare(float[] a, float[] b) {1087for (int i = 0; i < a.length; i++) {1088if (a[i] != b[i]) {1089failedCompare(i, "" + a[i], "" + b[i]);1090}1091}1092}10931094private static void compare(double[] a, double[] b) {1095for (int i = 0; i < a.length; i++) {1096if (a[i] != b[i]) {1097failedCompare(i, "" + a[i], "" + b[i]);1098}1099}1100}11011102private static void compare(Integer[] a, Integer[] b) {1103for (int i = 0; i < a.length; i++) {1104if (a[i].compareTo(b[i]) != 0) {1105failedCompare(i, "" + a[i], "" + b[i]);1106}1107}1108}11091110private static void checkSorted(Object object) {1111if (object instanceof int[]) {1112checkSorted((int[]) object);1113} else if (object instanceof long[]) {1114checkSorted((long[]) object);1115} else if (object instanceof short[]) {1116checkSorted((short[]) object);1117} else if (object instanceof byte[]) {1118checkSorted((byte[]) object);1119} else if (object instanceof char[]) {1120checkSorted((char[]) object);1121} else if (object instanceof float[]) {1122checkSorted((float[]) object);1123} else if (object instanceof double[]) {1124checkSorted((double[]) object);1125} else if (object instanceof Integer[]) {1126checkSorted((Integer[]) object);1127} else {1128failed("Unknow type of array: " + object + " of class " +1129object.getClass().getName());1130}1131}11321133private static void checkSorted(int[] a) {1134for (int i = 0; i < a.length - 1; i++) {1135if (a[i] > a[i + 1]) {1136failedSort(i, "" + a[i], "" + a[i + 1]);1137}1138}1139}11401141private static void checkSorted(long[] a) {1142for (int i = 0; i < a.length - 1; i++) {1143if (a[i] > a[i + 1]) {1144failedSort(i, "" + a[i], "" + a[i + 1]);1145}1146}1147}11481149private static void checkSorted(short[] a) {1150for (int i = 0; i < a.length - 1; i++) {1151if (a[i] > a[i + 1]) {1152failedSort(i, "" + a[i], "" + a[i + 1]);1153}1154}1155}11561157private static void checkSorted(byte[] a) {1158for (int i = 0; i < a.length - 1; i++) {1159if (a[i] > a[i + 1]) {1160failedSort(i, "" + a[i], "" + a[i + 1]);1161}1162}1163}11641165private static void checkSorted(char[] a) {1166for (int i = 0; i < a.length - 1; i++) {1167if (a[i] > a[i + 1]) {1168failedSort(i, "" + a[i], "" + a[i + 1]);1169}1170}1171}11721173private static void checkSorted(float[] a) {1174for (int i = 0; i < a.length - 1; i++) {1175if (a[i] > a[i + 1]) {1176failedSort(i, "" + a[i], "" + a[i + 1]);1177}1178}1179}11801181private static void checkSorted(double[] a) {1182for (int i = 0; i < a.length - 1; i++) {1183if (a[i] > a[i + 1]) {1184failedSort(i, "" + a[i], "" + a[i + 1]);1185}1186}1187}11881189private static void checkSorted(Integer[] a) {1190for (int i = 0; i < a.length - 1; i++) {1191if (a[i].intValue() > a[i + 1].intValue()) {1192failedSort(i, "" + a[i], "" + a[i + 1]);1193}1194}1195}11961197private static void checkCheckSum(Object test, Object golden) {1198if (checkSumXor(test) != checkSumXor(golden)) {1199failed("Original and sorted arrays are not identical [xor]");1200}1201if (checkSumPlus(test) != checkSumPlus(golden)) {1202failed("Original and sorted arrays are not identical [plus]");1203}1204}12051206private static int checkSumXor(Object object) {1207if (object instanceof int[]) {1208return checkSumXor((int[]) object);1209} else if (object instanceof long[]) {1210return checkSumXor((long[]) object);1211} else if (object instanceof short[]) {1212return checkSumXor((short[]) object);1213} else if (object instanceof byte[]) {1214return checkSumXor((byte[]) object);1215} else if (object instanceof char[]) {1216return checkSumXor((char[]) object);1217} else if (object instanceof float[]) {1218return checkSumXor((float[]) object);1219} else if (object instanceof double[]) {1220return checkSumXor((double[]) object);1221} else if (object instanceof Integer[]) {1222return checkSumXor((Integer[]) object);1223} else {1224failed("Unknow type of array: " + object + " of class " +1225object.getClass().getName());1226return -1;1227}1228}12291230private static int checkSumXor(Integer[] a) {1231int checkSum = 0;12321233for (Integer e : a) {1234checkSum ^= e.intValue();1235}1236return checkSum;1237}12381239private static int checkSumXor(int[] a) {1240int checkSum = 0;12411242for (int e : a) {1243checkSum ^= e;1244}1245return checkSum;1246}12471248private static int checkSumXor(long[] a) {1249long checkSum = 0;12501251for (long e : a) {1252checkSum ^= e;1253}1254return (int) checkSum;1255}12561257private static int checkSumXor(short[] a) {1258short checkSum = 0;12591260for (short e : a) {1261checkSum ^= e;1262}1263return (int) checkSum;1264}12651266private static int checkSumXor(byte[] a) {1267byte checkSum = 0;12681269for (byte e : a) {1270checkSum ^= e;1271}1272return (int) checkSum;1273}12741275private static int checkSumXor(char[] a) {1276char checkSum = 0;12771278for (char e : a) {1279checkSum ^= e;1280}1281return (int) checkSum;1282}12831284private static int checkSumXor(float[] a) {1285int checkSum = 0;12861287for (float e : a) {1288checkSum ^= (int) e;1289}1290return checkSum;1291}12921293private static int checkSumXor(double[] a) {1294int checkSum = 0;12951296for (double e : a) {1297checkSum ^= (int) e;1298}1299return checkSum;1300}13011302private static int checkSumPlus(Object object) {1303if (object instanceof int[]) {1304return checkSumPlus((int[]) object);1305} else if (object instanceof long[]) {1306return checkSumPlus((long[]) object);1307} else if (object instanceof short[]) {1308return checkSumPlus((short[]) object);1309} else if (object instanceof byte[]) {1310return checkSumPlus((byte[]) object);1311} else if (object instanceof char[]) {1312return checkSumPlus((char[]) object);1313} else if (object instanceof float[]) {1314return checkSumPlus((float[]) object);1315} else if (object instanceof double[]) {1316return checkSumPlus((double[]) object);1317} else if (object instanceof Integer[]) {1318return checkSumPlus((Integer[]) object);1319} else {1320failed("Unknow type of array: " + object + " of class " +1321object.getClass().getName());1322return -1;1323}1324}13251326private static int checkSumPlus(int[] a) {1327int checkSum = 0;13281329for (int e : a) {1330checkSum += e;1331}1332return checkSum;1333}13341335private static int checkSumPlus(long[] a) {1336long checkSum = 0;13371338for (long e : a) {1339checkSum += e;1340}1341return (int) checkSum;1342}13431344private static int checkSumPlus(short[] a) {1345short checkSum = 0;13461347for (short e : a) {1348checkSum += e;1349}1350return (int) checkSum;1351}13521353private static int checkSumPlus(byte[] a) {1354byte checkSum = 0;13551356for (byte e : a) {1357checkSum += e;1358}1359return (int) checkSum;1360}13611362private static int checkSumPlus(char[] a) {1363char checkSum = 0;13641365for (char e : a) {1366checkSum += e;1367}1368return (int) checkSum;1369}13701371private static int checkSumPlus(float[] a) {1372int checkSum = 0;13731374for (float e : a) {1375checkSum += (int) e;1376}1377return checkSum;1378}13791380private static int checkSumPlus(double[] a) {1381int checkSum = 0;13821383for (double e : a) {1384checkSum += (int) e;1385}1386return checkSum;1387}13881389private static int checkSumPlus(Integer[] a) {1390int checkSum = 0;13911392for (Integer e : a) {1393checkSum += e.intValue();1394}1395return checkSum;1396}13971398private static void sortByInsertionSort(Object object) {1399if (object instanceof int[]) {1400sortByInsertionSort((int[]) object);1401} else if (object instanceof long[]) {1402sortByInsertionSort((long[]) object);1403} else if (object instanceof short[]) {1404sortByInsertionSort((short[]) object);1405} else if (object instanceof byte[]) {1406sortByInsertionSort((byte[]) object);1407} else if (object instanceof char[]) {1408sortByInsertionSort((char[]) object);1409} else if (object instanceof float[]) {1410sortByInsertionSort((float[]) object);1411} else if (object instanceof double[]) {1412sortByInsertionSort((double[]) object);1413} else if (object instanceof Integer[]) {1414sortByInsertionSort((Integer[]) object);1415} else {1416failed("Unknow type of array: " + object + " of class " +1417object.getClass().getName());1418}1419}14201421private static void sortByInsertionSort(int[] a) {1422for (int j, i = 1; i < a.length; i++) {1423int ai = a[i];1424for (j = i - 1; j >= 0 && ai < a[j]; j--) {1425a[j + 1] = a[j];1426}1427a[j + 1] = ai;1428}1429}14301431private static void sortByInsertionSort(long[] a) {1432for (int j, i = 1; i < a.length; i++) {1433long ai = a[i];1434for (j = i - 1; j >= 0 && ai < a[j]; j--) {1435a[j + 1] = a[j];1436}1437a[j + 1] = ai;1438}1439}14401441private static void sortByInsertionSort(short[] a) {1442for (int j, i = 1; i < a.length; i++) {1443short ai = a[i];1444for (j = i - 1; j >= 0 && ai < a[j]; j--) {1445a[j + 1] = a[j];1446}1447a[j + 1] = ai;1448}1449}14501451private static void sortByInsertionSort(byte[] a) {1452for (int j, i = 1; i < a.length; i++) {1453byte ai = a[i];1454for (j = i - 1; j >= 0 && ai < a[j]; j--) {1455a[j + 1] = a[j];1456}1457a[j + 1] = ai;1458}1459}14601461private static void sortByInsertionSort(char[] a) {1462for (int j, i = 1; i < a.length; i++) {1463char ai = a[i];1464for (j = i - 1; j >= 0 && ai < a[j]; j--) {1465a[j + 1] = a[j];1466}1467a[j + 1] = ai;1468}1469}14701471private static void sortByInsertionSort(float[] a) {1472for (int j, i = 1; i < a.length; i++) {1473float ai = a[i];1474for (j = i - 1; j >= 0 && ai < a[j]; j--) {1475a[j + 1] = a[j];1476}1477a[j + 1] = ai;1478}1479}14801481private static void sortByInsertionSort(double[] a) {1482for (int j, i = 1; i < a.length; i++) {1483double ai = a[i];1484for (j = i - 1; j >= 0 && ai < a[j]; j--) {1485a[j + 1] = a[j];1486}1487a[j + 1] = ai;1488}1489}14901491private static void sortByInsertionSort(Integer[] a) {1492for (int j, i = 1; i < a.length; i++) {1493Integer ai = a[i];1494for (j = i - 1; j >= 0 && ai < a[j]; j--) {1495a[j + 1] = a[j];1496}1497a[j + 1] = ai;1498}1499}15001501private static void sort(Object object) {1502if (object instanceof int[]) {1503Arrays.sort((int[]) object);1504} else if (object instanceof long[]) {1505Arrays.sort((long[]) object);1506} else if (object instanceof short[]) {1507Arrays.sort((short[]) object);1508} else if (object instanceof byte[]) {1509Arrays.sort((byte[]) object);1510} else if (object instanceof char[]) {1511Arrays.sort((char[]) object);1512} else if (object instanceof float[]) {1513Arrays.sort((float[]) object);1514} else if (object instanceof double[]) {1515Arrays.sort((double[]) object);1516} else if (object instanceof Integer[]) {1517Arrays.sort((Integer[]) object);1518} else {1519failed("Unknow type of array: " + object + " of class " +1520object.getClass().getName());1521}1522}15231524private static void sortSubArray(Object object, int fromIndex, int toIndex) {1525if (object instanceof int[]) {1526Arrays.sort((int[]) object, fromIndex, toIndex);1527} else if (object instanceof long[]) {1528Arrays.sort((long[]) object, fromIndex, toIndex);1529} else if (object instanceof short[]) {1530Arrays.sort((short[]) object, fromIndex, toIndex);1531} else if (object instanceof byte[]) {1532Arrays.sort((byte[]) object, fromIndex, toIndex);1533} else if (object instanceof char[]) {1534Arrays.sort((char[]) object, fromIndex, toIndex);1535} else if (object instanceof float[]) {1536Arrays.sort((float[]) object, fromIndex, toIndex);1537} else if (object instanceof double[]) {1538Arrays.sort((double[]) object, fromIndex, toIndex);1539} else if (object instanceof Integer[]) {1540Arrays.sort((Integer[]) object, fromIndex, toIndex);1541} else {1542failed("Unknow type of array: " + object + " of class " +1543object.getClass().getName());1544}1545}15461547private static void checkSubArray(Object object, int fromIndex, int toIndex, int m) {1548if (object instanceof int[]) {1549checkSubArray((int[]) object, fromIndex, toIndex, m);1550} else if (object instanceof long[]) {1551checkSubArray((long[]) object, fromIndex, toIndex, m);1552} else if (object instanceof short[]) {1553checkSubArray((short[]) object, fromIndex, toIndex, m);1554} else if (object instanceof byte[]) {1555checkSubArray((byte[]) object, fromIndex, toIndex, m);1556} else if (object instanceof char[]) {1557checkSubArray((char[]) object, fromIndex, toIndex, m);1558} else if (object instanceof float[]) {1559checkSubArray((float[]) object, fromIndex, toIndex, m);1560} else if (object instanceof double[]) {1561checkSubArray((double[]) object, fromIndex, toIndex, m);1562} else if (object instanceof Integer[]) {1563checkSubArray((Integer[]) object, fromIndex, toIndex, m);1564} else {1565failed("Unknow type of array: " + object + " of class " +1566object.getClass().getName());1567}1568}15691570private static void checkSubArray(Integer[] a, int fromIndex, int toIndex, int m) {1571for (int i = 0; i < fromIndex; i++) {1572if (a[i].intValue() != 0xDEDA) {1573failed("Range sort changes left element on position " + i +1574": " + a[i] + ", must be " + 0xDEDA);1575}1576}15771578for (int i = fromIndex; i < toIndex - 1; i++) {1579if (a[i].intValue() > a[i + 1].intValue()) {1580failedSort(i, "" + a[i], "" + a[i + 1]);1581}1582}15831584for (int i = toIndex; i < a.length; i++) {1585if (a[i].intValue() != 0xBABA) {1586failed("Range sort changes right element on position " + i +1587": " + a[i] + ", must be " + 0xBABA);1588}1589}1590}15911592private static void checkSubArray(int[] a, int fromIndex, int toIndex, int m) {1593for (int i = 0; i < fromIndex; i++) {1594if (a[i] != 0xDEDA) {1595failed("Range sort changes left element on position " + i +1596": " + a[i] + ", must be " + 0xDEDA);1597}1598}15991600for (int i = fromIndex; i < toIndex - 1; i++) {1601if (a[i] > a[i + 1]) {1602failedSort(i, "" + a[i], "" + a[i + 1]);1603}1604}16051606for (int i = toIndex; i < a.length; i++) {1607if (a[i] != 0xBABA) {1608failed("Range sort changes right element on position " + i +1609": " + a[i] + ", must be " + 0xBABA);1610}1611}1612}16131614private static void checkSubArray(byte[] a, int fromIndex, int toIndex, int m) {1615for (int i = 0; i < fromIndex; i++) {1616if (a[i] != (byte) 0xDEDA) {1617failed("Range sort changes left element on position " + i +1618": " + a[i] + ", must be " + 0xDEDA);1619}1620}16211622for (int i = fromIndex; i < toIndex - 1; i++) {1623if (a[i] > a[i + 1]) {1624failedSort(i, "" + a[i], "" + a[i + 1]);1625}1626}16271628for (int i = toIndex; i < a.length; i++) {1629if (a[i] != (byte) 0xBABA) {1630failed("Range sort changes right element on position " + i +1631": " + a[i] + ", must be " + 0xBABA);1632}1633}1634}16351636private static void checkSubArray(long[] a, int fromIndex, int toIndex, int m) {1637for (int i = 0; i < fromIndex; i++) {1638if (a[i] != (long) 0xDEDA) {1639failed("Range sort changes left element on position " + i +1640": " + a[i] + ", must be " + 0xDEDA);1641}1642}16431644for (int i = fromIndex; i < toIndex - 1; i++) {1645if (a[i] > a[i + 1]) {1646failedSort(i, "" + a[i], "" + a[i + 1]);1647}1648}16491650for (int i = toIndex; i < a.length; i++) {1651if (a[i] != (long) 0xBABA) {1652failed("Range sort changes right element on position " + i +1653": " + a[i] + ", must be " + 0xBABA);1654}1655}1656}16571658private static void checkSubArray(char[] a, int fromIndex, int toIndex, int m) {1659for (int i = 0; i < fromIndex; i++) {1660if (a[i] != (char) 0xDEDA) {1661failed("Range sort changes left element on position " + i +1662": " + a[i] + ", must be " + 0xDEDA);1663}1664}16651666for (int i = fromIndex; i < toIndex - 1; i++) {1667if (a[i] > a[i + 1]) {1668failedSort(i, "" + a[i], "" + a[i + 1]);1669}1670}16711672for (int i = toIndex; i < a.length; i++) {1673if (a[i] != (char) 0xBABA) {1674failed("Range sort changes right element on position " + i +1675": " + a[i] + ", must be " + 0xBABA);1676}1677}1678}16791680private static void checkSubArray(short[] a, int fromIndex, int toIndex, int m) {1681for (int i = 0; i < fromIndex; i++) {1682if (a[i] != (short) 0xDEDA) {1683failed("Range sort changes left element on position " + i +1684": " + a[i] + ", must be " + 0xDEDA);1685}1686}16871688for (int i = fromIndex; i < toIndex - 1; i++) {1689if (a[i] > a[i + 1]) {1690failedSort(i, "" + a[i], "" + a[i + 1]);1691}1692}16931694for (int i = toIndex; i < a.length; i++) {1695if (a[i] != (short) 0xBABA) {1696failed("Range sort changes right element on position " + i +1697": " + a[i] + ", must be " + 0xBABA);1698}1699}1700}17011702private static void checkSubArray(float[] a, int fromIndex, int toIndex, int m) {1703for (int i = 0; i < fromIndex; i++) {1704if (a[i] != (float) 0xDEDA) {1705failed("Range sort changes left element on position " + i +1706": " + a[i] + ", must be " + 0xDEDA);1707}1708}17091710for (int i = fromIndex; i < toIndex - 1; i++) {1711if (a[i] > a[i + 1]) {1712failedSort(i, "" + a[i], "" + a[i + 1]);1713}1714}17151716for (int i = toIndex; i < a.length; i++) {1717if (a[i] != (float) 0xBABA) {1718failed("Range sort changes right element on position " + i +1719": " + a[i] + ", must be " + 0xBABA);1720}1721}1722}17231724private static void checkSubArray(double[] a, int fromIndex, int toIndex, int m) {1725for (int i = 0; i < fromIndex; i++) {1726if (a[i] != (double) 0xDEDA) {1727failed("Range sort changes left element on position " + i +1728": " + a[i] + ", must be " + 0xDEDA);1729}1730}17311732for (int i = fromIndex; i < toIndex - 1; i++) {1733if (a[i] > a[i + 1]) {1734failedSort(i, "" + a[i], "" + a[i + 1]);1735}1736}17371738for (int i = toIndex; i < a.length; i++) {1739if (a[i] != (double) 0xBABA) {1740failed("Range sort changes right element on position " + i +1741": " + a[i] + ", must be " + 0xBABA);1742}1743}1744}17451746private static void checkRange(Object object, int m) {1747if (object instanceof int[]) {1748checkRange((int[]) object, m);1749} else if (object instanceof long[]) {1750checkRange((long[]) object, m);1751} else if (object instanceof short[]) {1752checkRange((short[]) object, m);1753} else if (object instanceof byte[]) {1754checkRange((byte[]) object, m);1755} else if (object instanceof char[]) {1756checkRange((char[]) object, m);1757} else if (object instanceof float[]) {1758checkRange((float[]) object, m);1759} else if (object instanceof double[]) {1760checkRange((double[]) object, m);1761} else if (object instanceof Integer[]) {1762checkRange((Integer[]) object, m);1763} else {1764failed("Unknow type of array: " + object + " of class " +1765object.getClass().getName());1766}1767}17681769private static void checkRange(Integer[] a, int m) {1770try {1771Arrays.sort(a, m + 1, m);17721773failed("Sort does not throw IllegalArgumentException " +1774" as expected: fromIndex = " + (m + 1) +1775" toIndex = " + m);1776}1777catch (IllegalArgumentException iae) {1778try {1779Arrays.sort(a, -m, a.length);17801781failed("Sort does not throw ArrayIndexOutOfBoundsException " +1782" as expected: fromIndex = " + (-m));1783}1784catch (ArrayIndexOutOfBoundsException aoe) {1785try {1786Arrays.sort(a, 0, a.length + m);17871788failed("Sort does not throw ArrayIndexOutOfBoundsException " +1789" as expected: toIndex = " + (a.length + m));1790}1791catch (ArrayIndexOutOfBoundsException aie) {1792return;1793}1794}1795}1796}17971798private static void checkRange(int[] a, int m) {1799try {1800Arrays.sort(a, m + 1, m);18011802failed("Sort does not throw IllegalArgumentException " +1803" as expected: fromIndex = " + (m + 1) +1804" toIndex = " + m);1805}1806catch (IllegalArgumentException iae) {1807try {1808Arrays.sort(a, -m, a.length);18091810failed("Sort does not throw ArrayIndexOutOfBoundsException " +1811" as expected: fromIndex = " + (-m));1812}1813catch (ArrayIndexOutOfBoundsException aoe) {1814try {1815Arrays.sort(a, 0, a.length + m);18161817failed("Sort does not throw ArrayIndexOutOfBoundsException " +1818" as expected: toIndex = " + (a.length + m));1819}1820catch (ArrayIndexOutOfBoundsException aie) {1821return;1822}1823}1824}1825}18261827private static void checkRange(long[] a, int m) {1828try {1829Arrays.sort(a, m + 1, m);18301831failed("Sort does not throw IllegalArgumentException " +1832" as expected: fromIndex = " + (m + 1) +1833" toIndex = " + m);1834}1835catch (IllegalArgumentException iae) {1836try {1837Arrays.sort(a, -m, a.length);18381839failed("Sort does not throw ArrayIndexOutOfBoundsException " +1840" as expected: fromIndex = " + (-m));1841}1842catch (ArrayIndexOutOfBoundsException aoe) {1843try {1844Arrays.sort(a, 0, a.length + m);18451846failed("Sort does not throw ArrayIndexOutOfBoundsException " +1847" as expected: toIndex = " + (a.length + m));1848}1849catch (ArrayIndexOutOfBoundsException aie) {1850return;1851}1852}1853}1854}18551856private static void checkRange(byte[] a, int m) {1857try {1858Arrays.sort(a, m + 1, m);18591860failed("Sort does not throw IllegalArgumentException " +1861" as expected: fromIndex = " + (m + 1) +1862" toIndex = " + m);1863}1864catch (IllegalArgumentException iae) {1865try {1866Arrays.sort(a, -m, a.length);18671868failed("Sort does not throw ArrayIndexOutOfBoundsException " +1869" as expected: fromIndex = " + (-m));1870}1871catch (ArrayIndexOutOfBoundsException aoe) {1872try {1873Arrays.sort(a, 0, a.length + m);18741875failed("Sort does not throw ArrayIndexOutOfBoundsException " +1876" as expected: toIndex = " + (a.length + m));1877}1878catch (ArrayIndexOutOfBoundsException aie) {1879return;1880}1881}1882}1883}18841885private static void checkRange(short[] a, int m) {1886try {1887Arrays.sort(a, m + 1, m);18881889failed("Sort does not throw IllegalArgumentException " +1890" as expected: fromIndex = " + (m + 1) +1891" toIndex = " + m);1892}1893catch (IllegalArgumentException iae) {1894try {1895Arrays.sort(a, -m, a.length);18961897failed("Sort does not throw ArrayIndexOutOfBoundsException " +1898" as expected: fromIndex = " + (-m));1899}1900catch (ArrayIndexOutOfBoundsException aoe) {1901try {1902Arrays.sort(a, 0, a.length + m);19031904failed("Sort does not throw ArrayIndexOutOfBoundsException " +1905" as expected: toIndex = " + (a.length + m));1906}1907catch (ArrayIndexOutOfBoundsException aie) {1908return;1909}1910}1911}1912}19131914private static void checkRange(char[] a, int m) {1915try {1916Arrays.sort(a, m + 1, m);19171918failed("Sort does not throw IllegalArgumentException " +1919" as expected: fromIndex = " + (m + 1) +1920" toIndex = " + m);1921}1922catch (IllegalArgumentException iae) {1923try {1924Arrays.sort(a, -m, a.length);19251926failed("Sort does not throw ArrayIndexOutOfBoundsException " +1927" as expected: fromIndex = " + (-m));1928}1929catch (ArrayIndexOutOfBoundsException aoe) {1930try {1931Arrays.sort(a, 0, a.length + m);19321933failed("Sort does not throw ArrayIndexOutOfBoundsException " +1934" as expected: toIndex = " + (a.length + m));1935}1936catch (ArrayIndexOutOfBoundsException aie) {1937return;1938}1939}1940}1941}19421943private static void checkRange(float[] a, int m) {1944try {1945Arrays.sort(a, m + 1, m);19461947failed("Sort does not throw IllegalArgumentException " +1948" as expected: fromIndex = " + (m + 1) +1949" toIndex = " + m);1950}1951catch (IllegalArgumentException iae) {1952try {1953Arrays.sort(a, -m, a.length);19541955failed("Sort does not throw ArrayIndexOutOfBoundsException " +1956" as expected: fromIndex = " + (-m));1957}1958catch (ArrayIndexOutOfBoundsException aoe) {1959try {1960Arrays.sort(a, 0, a.length + m);19611962failed("Sort does not throw ArrayIndexOutOfBoundsException " +1963" as expected: toIndex = " + (a.length + m));1964}1965catch (ArrayIndexOutOfBoundsException aie) {1966return;1967}1968}1969}1970}19711972private static void checkRange(double[] a, int m) {1973try {1974Arrays.sort(a, m + 1, m);19751976failed("Sort does not throw IllegalArgumentException " +1977" as expected: fromIndex = " + (m + 1) +1978" toIndex = " + m);1979}1980catch (IllegalArgumentException iae) {1981try {1982Arrays.sort(a, -m, a.length);19831984failed("Sort does not throw ArrayIndexOutOfBoundsException " +1985" as expected: fromIndex = " + (-m));1986}1987catch (ArrayIndexOutOfBoundsException aoe) {1988try {1989Arrays.sort(a, 0, a.length + m);19901991failed("Sort does not throw ArrayIndexOutOfBoundsException " +1992" as expected: toIndex = " + (a.length + m));1993}1994catch (ArrayIndexOutOfBoundsException aie) {1995return;1996}1997}1998}1999}20002001private static void outArray(Object[] a) {2002for (int i = 0; i < a.length; i++) {2003out.print(a[i] + " ");2004}2005out.println();2006}20072008private static void outArray(int[] a) {2009for (int i = 0; i < a.length; i++) {2010out.print(a[i] + " ");2011}2012out.println();2013}20142015private static void outArray(float[] a) {2016for (int i = 0; i < a.length; i++) {2017out.print(a[i] + " ");2018}2019out.println();2020}20212022private static void outArray(double[] a) {2023for (int i = 0; i < a.length; i++) {2024out.print(a[i] + " ");2025}2026out.println();2027}20282029private static class MyRandom extends Random {2030MyRandom(long seed) {2031super(seed);2032mySeed = seed;2033}20342035long getSeed() {2036return mySeed;2037}20382039private long mySeed;2040}20412042private static String ourDescription;2043}204420452046