Initial load
diff --git a/test/java/util/Arrays/ArrayObjectMethods.java b/test/java/util/Arrays/ArrayObjectMethods.java
new file mode 100644
index 0000000..8bc3aab
--- /dev/null
+++ b/test/java/util/Arrays/ArrayObjectMethods.java
@@ -0,0 +1,505 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4906359 6239296
+ * @summary Basic test for content-based array object methods
+ * @author Josh Bloch, Martin Buchholz
+ */
+
+import java.util.*;
+import java.io.*;
+
+public class ArrayObjectMethods {
+ int[] sizes = {0, 10, 100, 200, 1000};
+
+ void test(String[] args) throws Throwable {
+ equal(Arrays.deepToString(null), "null");
+ equal(Arrays.deepToString(new Object[]{}), "[]");
+ equal(Arrays.deepToString(new Object[]{null}), "[null]");
+ equal(Arrays.deepToString(new Object[]{null, 1}), "[null, 1]");
+ equal(Arrays.deepToString(new Object[]{1, null}), "[1, null]");
+ equal(Arrays.deepToString(new Object[]{new Object[]{}, null}), "[[], null]");
+
+ {
+ Object[] a = {1, null};
+ a[1] = a;
+ equal(Arrays.deepToString(a), "[1, [...]]");
+ a[0] = a;
+ equal(Arrays.deepToString(a), "[[...], [...]]");
+ a[0] = a[1] = new Object[]{1, null, a};
+ equal(Arrays.deepToString(a), "[[1, null, [...]], [1, null, [...]]]");
+ }
+
+ for (int size : sizes) {
+ {
+ long[] a = Rnd.longArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ int[] a = Rnd.intArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ short[] a = Rnd.shortArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ char[] a = Rnd.charArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ byte[] a = Rnd.byteArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ boolean[] a = Rnd.booleanArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ double[] a = Rnd.doubleArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ float[] a = Rnd.floatArray(size);
+ equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
+ equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
+ }
+ {
+ Object[] a = Rnd.flatObjectArray(size);
+ equal(Arrays.toString(a), Arrays.asList(a).toString());
+ equal(Arrays.deepToString(a), Arrays.asList(a).toString());
+ equal(Arrays.hashCode(a), Arrays.asList(a).hashCode());
+ }
+
+ if (size <= 200) {
+ Object[] a = Rnd.nestedObjectArray(size);
+ List aList = deepToList(a);
+ equal(Arrays.toString(a), Arrays.asList(a).toString());
+ equal(Arrays.deepToString(a), aList.toString());
+ equal(Arrays.deepHashCode(a), aList.hashCode());
+ equal(Arrays.hashCode(a), Arrays.asList(a).hashCode());
+
+ Object[] deepCopy = (Object[]) deepCopy(a);
+ check(Arrays.deepEquals(a, deepCopy));
+ check(Arrays.deepEquals(deepCopy, a));
+
+ // Make deepCopy != a
+ if (size == 0)
+ deepCopy = new Object[] {"foo"};
+ else if (deepCopy[deepCopy.length - 1] == null)
+ deepCopy[deepCopy.length - 1] = "baz";
+ else
+ deepCopy[deepCopy.length - 1] = null;
+ check(! Arrays.deepEquals(a, deepCopy));
+ check(! Arrays.deepEquals(deepCopy, a));
+ }
+ }
+ }
+
+ // Utility method to turn an array into a list "deeply," turning
+ // all primitives into objects
+ List<Object> deepToList(Object[] a) {
+ List<Object> result = new ArrayList<Object>();
+ for (Object e : a) {
+ if (e instanceof byte[])
+ result.add(PrimitiveArrays.asList((byte[])e));
+ else if (e instanceof short[])
+ result.add(PrimitiveArrays.asList((short[])e));
+ else if (e instanceof int[])
+ result.add(PrimitiveArrays.asList((int[])e));
+ else if (e instanceof long[])
+ result.add(PrimitiveArrays.asList((long[])e));
+ else if (e instanceof char[])
+ result.add(PrimitiveArrays.asList((char[])e));
+ else if (e instanceof double[])
+ result.add(PrimitiveArrays.asList((double[])e));
+ else if (e instanceof float[])
+ result.add(PrimitiveArrays.asList((float[])e));
+ else if (e instanceof boolean[])
+ result.add(PrimitiveArrays.asList((boolean[])e));
+ else if (e instanceof Object[])
+ result.add(deepToList((Object[])e));
+ else
+ result.add(e);
+ }
+ return result;
+ }
+
+ // Utility method to do a deep copy of an object *very slowly* using
+ // serialization/deserialization
+ Object deepCopy(Object oldObj) {
+ try {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(bos);
+ oos.writeObject(oldObj);
+ oos.flush();
+ ByteArrayInputStream bin = new ByteArrayInputStream(
+ bos.toByteArray());
+ ObjectInputStream ois = new ObjectInputStream(bin);
+ return ois.readObject();
+ } catch(Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ volatile int passed = 0, failed = 0;
+ void pass() {passed++;}
+ void fail() {failed++; Thread.dumpStack();}
+ void fail(String msg) {System.err.println(msg); fail();}
+ void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ void check(boolean cond) {if (cond) pass(); else fail();}
+ void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ new ArrayObjectMethods().instanceMain(args);}
+ void instanceMain(String[] args) throws Throwable {
+ try {test(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+}
+
+/**
+ * Methods to generate "interesting" random primitives and primitive
+ * arrays. Unlike Random.nextXxx, these methods return small values
+ * and boundary values (e.g., 0, -1, NaN) with greater than normal
+ * likelihood.
+ */
+
+class Rnd {
+ private static Random rnd = new Random();
+
+ public static long nextLong() {
+ switch(rnd.nextInt(10)) {
+ case 0: return 0;
+ case 1: return Long.MIN_VALUE;
+ case 2: return Long.MAX_VALUE;
+ case 3: case 4: case 5:
+ return (long) (rnd.nextInt(20) - 10);
+ default: return rnd.nextLong();
+ }
+ }
+
+ public static int nextInt() {
+ switch(rnd.nextInt(10)) {
+ case 0: return 0;
+ case 1: return Integer.MIN_VALUE;
+ case 2: return Integer.MAX_VALUE;
+ case 3: case 4: case 5:
+ return rnd.nextInt(20) - 10;
+ default: return rnd.nextInt();
+ }
+ }
+
+ public static short nextShort() {
+ switch(rnd.nextInt(10)) {
+ case 0: return 0;
+ case 1: return Short.MIN_VALUE;
+ case 2: return Short.MAX_VALUE;
+ case 3: case 4: case 5:
+ return (short) (rnd.nextInt(20) - 10);
+ default: return (short) rnd.nextInt();
+ }
+ }
+
+ public static char nextChar() {
+ switch(rnd.nextInt(10)) {
+ case 0: return 0;
+ case 1: return Character.MIN_VALUE;
+ case 2: return Character.MAX_VALUE;
+ case 3: case 4: case 5:
+ return (char) (rnd.nextInt(20) - 10);
+ default: return (char) rnd.nextInt();
+ }
+ }
+
+ public static byte nextByte() {
+ switch(rnd.nextInt(10)) {
+ case 0: return 0;
+ case 1: return Byte.MIN_VALUE;
+ case 2: return Byte.MAX_VALUE;
+ case 3: case 4: case 5:
+ return (byte) (rnd.nextInt(20) - 10);
+ default: return (byte) rnd.nextInt();
+ }
+ }
+
+ public static boolean nextBoolean() {
+ return rnd.nextBoolean();
+ }
+
+ public static double nextDouble() {
+ switch(rnd.nextInt(20)) {
+ case 0: return 0;
+ case 1: return -0.0;
+ case 2: return Double.MIN_VALUE;
+ case 3: return Double.MAX_VALUE;
+ case 4: return Double.NaN;
+ case 5: return Double.NEGATIVE_INFINITY;
+ case 6: return Double.POSITIVE_INFINITY;
+ case 7: case 8: case 9:
+ return (rnd.nextInt(20) - 10);
+ default: return rnd.nextDouble();
+ }
+ }
+
+ public static float nextFloat() {
+ switch(rnd.nextInt(20)) {
+ case 0: return 0;
+ case 1: return -0.0f;
+ case 2: return Float.MIN_VALUE;
+ case 3: return Float.MAX_VALUE;
+ case 4: return Float.NaN;
+ case 5: return Float.NEGATIVE_INFINITY;
+ case 6: return Float.POSITIVE_INFINITY;
+ case 7: case 8: case 9:
+ return (rnd.nextInt(20) - 10);
+ default: return rnd.nextFloat();
+ }
+ }
+
+ public static Object nextObject() {
+ switch(rnd.nextInt(10)) {
+ case 0: return null;
+ case 1: return "foo";
+ case 2: case 3: case 4:
+ return Double.valueOf(nextDouble());
+ default: return Integer.valueOf(nextInt());
+ }
+ }
+
+ public static long[] longArray(int length) {
+ long[] result = new long[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextLong();
+ return result;
+ }
+
+ public static int[] intArray(int length) {
+ int[] result = new int[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextInt();
+ return result;
+ }
+
+ public static short[] shortArray(int length) {
+ short[] result = new short[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextShort();
+ return result;
+ }
+
+ public static char[] charArray(int length) {
+ char[] result = new char[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextChar();
+ return result;
+ }
+
+ public static byte[] byteArray(int length) {
+ byte[] result = new byte[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextByte();
+ return result;
+ }
+
+ public static boolean[] booleanArray(int length) {
+ boolean[] result = new boolean[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextBoolean();
+ return result;
+ }
+
+ public static double[] doubleArray(int length) {
+ double[] result = new double[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextDouble();
+ return result;
+ }
+
+ public static float[] floatArray(int length) {
+ float[] result = new float[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextFloat();
+ return result;
+ }
+
+ public static Object[] flatObjectArray(int length) {
+ Object[] result = new Object[length];
+ for (int i = 0; i < length; i++)
+ result[i] = Rnd.nextObject();
+ return result;
+ }
+
+ // Calling this for length >> 100 is likely to run out of memory! It
+ // should be perhaps be tuned to allow for longer arrays
+ public static Object[] nestedObjectArray(int length) {
+ Object[] result = new Object[length];
+ for (int i = 0; i < length; i++) {
+ switch(rnd.nextInt(16)) {
+ case 0: result[i] = nestedObjectArray(length/2);
+ break;
+ case 1: result[i] = longArray(length/2);
+ break;
+ case 2: result[i] = intArray(length/2);
+ break;
+ case 3: result[i] = shortArray(length/2);
+ break;
+ case 4: result[i] = charArray(length/2);
+ break;
+ case 5: result[i] = byteArray(length/2);
+ break;
+ case 6: result[i] = floatArray(length/2);
+ break;
+ case 7: result[i] = doubleArray(length/2);
+ break;
+ case 8: result[i] = longArray(length/2);
+ break;
+ default: result[i] = Rnd.nextObject();
+ }
+ }
+ return result;
+ }
+}
+
+/**
+ * Primitive arrays viewed as lists. Inefficient but cool.
+ * This utility should be generally useful in writing regression/unit/basic
+ * tests.
+ */
+
+class PrimitiveArrays {
+ public static List<Long> asList(final long[] a) {
+ return new AbstractList<Long>() {
+ public Long get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Long set(int i, Long e) {
+ long oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Integer> asList(final int[] a) {
+ return new AbstractList<Integer>() {
+ public Integer get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Integer set(int i, Integer e) {
+ int oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Short> asList(final short[] a) {
+ return new AbstractList<Short>() {
+ public Short get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Short set(int i, Short e) {
+ short oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Character> asList(final char[] a) {
+ return new AbstractList<Character>() {
+ public Character get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Character set(int i, Character e) {
+ Character oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Byte> asList(final byte[] a) {
+ return new AbstractList<Byte>() {
+ public Byte get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Byte set(int i, Byte e) {
+ Byte oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Boolean> asList(final boolean[] a) {
+ return new AbstractList<Boolean>() {
+ public Boolean get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Boolean set(int i, Boolean e) {
+ Boolean oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Double> asList(final double[] a) {
+ return new AbstractList<Double>() {
+ public Double get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Double set(int i, Double e) {
+ Double oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+
+ public static List<Float> asList(final float[] a) {
+ return new AbstractList<Float>() {
+ public Float get(int i) { return a[i]; }
+ public int size() { return a.length; }
+
+ public Float set(int i, Float e) {
+ Float oldVal = a[i];
+ a[i] = e;
+ return oldVal;
+ }
+ };
+ }
+}
diff --git a/test/java/util/Arrays/Big.java b/test/java/util/Arrays/Big.java
new file mode 100644
index 0000000..ab88c7c
--- /dev/null
+++ b/test/java/util/Arrays/Big.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5045582
+ * @summary arrays larger than 1<<30
+ * @author Martin Buchholz
+ */
+
+// A proper regression test for 5045582 requires too much memory.
+// If you have a really big machine, run like this:
+// java -d64 -Xms25g -Xmx25g Big 30
+
+import java.util.*;
+
+public class Big {
+
+ private static void realMain(String[] args) throws Throwable {
+ final int shift = intArg(args, 0, 10); // "30" is real test
+ final int tasks = intArg(args, 1, ~0); // all tasks
+ final int n = (1<<shift) + 47;
+
+ // To test byte arrays larger than 1<<30, you need 1600MB. Run like:
+ // java -Xms1600m -Xmx1600m Big 30 1
+ if ((tasks & 0x1) != 0) {
+ System.out.println("byte[]");
+ System.gc();
+ byte[] a = new byte[n];
+ a[0] = (byte) -44;
+ a[1] = (byte) -43;
+ a[n-2] = (byte) +43;
+ a[n-1] = (byte) +44;
+ for (int i : new int[] { 0, 1, n-2, n-1 })
+ try { equal(i, Arrays.binarySearch(a, a[i])); }
+ catch (Throwable t) { unexpected(t); }
+ for (int i : new int[] { n-2, n-1 })
+ try { equal(i, Arrays.binarySearch(a, n-5, n, a[i])); }
+ catch (Throwable t) { unexpected(t); }
+
+ a[n-19] = (byte) 45;
+ try { Arrays.sort(a, n-29, n); }
+ catch (Throwable t) { unexpected(t); }
+ equal(a[n-1], (byte) 45);
+ equal(a[n-2], (byte) 44);
+ equal(a[n-3], (byte) 43);
+ equal(a[n-4], (byte) 0);
+ }
+
+ // To test Object arrays larger than 1<<30, you need 13GB. Run like:
+ // java -d64 -Xms13g -Xmx13g Big 30 2
+ if ((tasks & 0x2) != 0) {
+ System.out.println("Integer[]");
+ System.gc();
+ Integer[] a = new Integer[n];
+ Integer ZERO = 0;
+ Arrays.fill(a, ZERO);
+ a[0] = -44;
+ a[1] = -43;
+ a[n-2] = +43;
+ a[n-1] = +44;
+ for (int i : new int[] { 0, 1, n-2, n-1 })
+ try { equal(i, Arrays.binarySearch(a, a[i])); }
+ catch (Throwable t) { unexpected(t); }
+ for (int i : new int[] { n-2, n-1 })
+ try { equal(i, Arrays.binarySearch(a, n-5, n, a[i])); }
+ catch (Throwable t) { unexpected(t); }
+
+ a[n-19] = 45;
+ try { Arrays.sort(a, n-29, n); }
+ catch (Throwable t) { unexpected(t); }
+ equal(a[n-1], 45);
+ equal(a[n-2], 44);
+ equal(a[n-3], 43);
+ equal(a[n-4], 0);
+ }
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ static volatile int passed = 0, failed = 0;
+ static void pass() {passed++;}
+ static void fail() {failed++; Thread.dumpStack();}
+ static void fail(String msg) {System.out.println(msg); fail();}
+ static void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ static void check(boolean cond) {if (cond) pass(); else fail();}
+ static void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ try {realMain(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+ static int intArg(String[] args, int i, int defaultValue) {
+ return args.length > i ? Integer.parseInt(args[i]) : defaultValue;}
+}
diff --git a/test/java/util/Arrays/CopyMethods.java b/test/java/util/Arrays/CopyMethods.java
new file mode 100644
index 0000000..46f5626
--- /dev/null
+++ b/test/java/util/Arrays/CopyMethods.java
@@ -0,0 +1,424 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4655503
+ * @summary Test for array cloning and slicing methods.
+ * @author John Rose
+ */
+
+import java.util.*;
+import java.lang.reflect.*;
+
+public class CopyMethods {
+ static int muzzle; // if !=0, suppresses ("muzzles") messages
+
+ static int maxLen = 40; // maximum length of test arrays
+ static int shortStepsNear = 4; // interesting span near critical values
+ static int downShift = 3;
+
+ static int testCasesRun = 0;
+ static long consing = 0;
+
+ // very simple tests, mainly to test the framework itself
+ static void simpleTests() {
+ int[] a = (int[]) makeArray(3, int.class);
+ if (muzzle == 0)
+ System.out.println("int[] a = "+Arrays.toString(a));
+ check(a.length == 3);
+ check(a[0] == testValues[0]);
+ check(a[1] == testValues[1]);
+ check(a[2] == testValues[2]);
+ checkArray(a, int.class, 3, 0, 3);
+ // negative test of testing framework:
+ for (int bad = -2; bad < a.length; bad++) {
+ try {
+ int[] aa = a.clone();
+ if (bad < 0) aa = new int[4];
+ else aa[bad] = 0;
+ ++muzzle;
+ // the following check should fail!
+ if (bad == -2)
+ checkArray(new String[3], int.class, 0, 0, a.length);
+ else
+ checkArray(aa, int.class, 0, 0, a.length);
+ throw new Error("Should Not Reach Here");
+ } catch (RuntimeException ee) {
+ --muzzle;
+ if (muzzle == 0)
+ System.out.println("Expected: "+ee);
+ }
+ }
+ checkArray(Arrays.copyOf(a, 0), int.class, 0, 0, 3);
+ checkArray(Arrays.copyOf(a, 1), int.class, 1, 0, 3);
+ checkArray(Arrays.copyOf(a, 2), int.class, 2, 0, 3);
+ checkArray(Arrays.copyOf(a, 3), int.class, 3, 0, 3);
+ checkArray(Arrays.copyOf(a, 4), int.class, 4, 0, 3);
+
+ // quick test of copyOfRange
+ int[] ar = Arrays.copyOfRange(a, 1, 3);
+ check(ar.length == 2);
+ check(ar[0] == a[1]);
+ check(ar[1] == a[2]);
+ checkArray(ar, int.class, 2, 1, 2);
+ ar = Arrays.copyOfRange(a, 2, 4);
+ check(ar.length == 2);
+ check(ar[0] == a[2]);
+ check(ar[1] == 0);
+ checkArray(ar, int.class, 2, 2, 1);
+ ar = Arrays.copyOfRange(a, 3, 5);
+ check(ar.length == 2);
+ check(ar[0] == 0);
+ check(ar[1] == 0);
+ checkArray(ar, int.class, 2, 3, 0);
+ byte[] ba = (byte[]) makeArray(3, byte.class);
+ if (muzzle == 0)
+ System.out.println("byte[] ba = "+Arrays.toString(ba));
+ for (int j = 0; j <= ba.length+2; j++) {
+ byte[] bb = Arrays.copyOf(ba, j);
+ if (muzzle == 0)
+ System.out.println("copyOf(ba,"+j+") = "+
+ Arrays.toString(bb));
+ checkArray(bb, byte.class, j, 0, ba.length);
+ byte[] bbr = Arrays.copyOfRange(ba, 0, j);
+ check(Arrays.equals(bb, bbr));
+ }
+ for (int i = 0; i <= a.length; i++) {
+ for (int j = i; j <= a.length+2; j++) {
+ byte[] br = Arrays.copyOfRange(ba, i, j);
+ if (muzzle == 0)
+ System.out.println("copyOfRange(ba,"+i+","+j+") = "+
+ Arrays.toString(br));
+ checkArray(br, byte.class, j-i, i, ba.length-i);
+ }
+ }
+ String[] sa = (String[]) makeArray(3, String.class);
+ if (muzzle == 0)
+ System.out.println("String[] sa = "+Arrays.toString(sa));
+ check(sa[0].equals(Integer.toHexString(testValues[0])));
+ check(sa[1].equals(Integer.toHexString(testValues[1])));
+ check(sa[2].equals(Integer.toHexString(testValues[2])));
+ checkArray(sa, String.class, sa.length, 0, sa.length);
+ String[] sa4 = Arrays.copyOf(sa, sa.length+1);
+ check(sa4[0] == sa[0]);
+ check(sa4[1] == sa[1]);
+ check(sa4[2] == sa[2]);
+ check(sa4[sa.length] == null);
+ checkArray(sa4, String.class, sa4.length, 0, sa.length);
+ String[] sr4 = Arrays.copyOfRange(sa, 1, 5);
+ check(sr4[0] == sa[1]);
+ check(sr4[1] == sa[2]);
+ check(sr4[2] == null);
+ check(sr4[3] == null);
+ checkArray(sr4, String.class, 4, 1, sa.length-1);
+ if (muzzle == 0)
+ System.out.println("simpleTests done");
+ }
+
+ // the framework: a fixed series of test values
+ static final int[] testValues;
+ static {
+ testValues = new int[1000];
+ Random r = new Random();
+ for (int i = 0; i < testValues.length; i++) {
+ testValues[i] = r.nextInt();
+ }
+ }
+ /** Return a canonical test value of a desired index and type.
+ * The original test values are random ints. Derive other test
+ * values as follows:
+ * <pre>
+ * int tv = testValues[i]
+ * (C)tv C is byte, short, char, long, float, double
+ * (tv&1)!=0 C is boolean
+ * (Integer)tv C is Object and tv%16 != 0
+ * null C is Object and tv%16 == 0
+ * Integer.toHexString(tv) C is String and tv != 0
+ * null C is String and tv == 0
+ * </pre>
+ * are derived by ordinary Java coercions, except that boolean
+ * samples the LSB of the int value, and String is the hex numeral.
+ *
+ * (Also, the 0th String is null, and the 0th Object mod 16 is null,
+ * regardless of the original int test value.)
+ */
+ static Object testValue(int i, Class<?> c) {
+ int tv = testValues[i % testValues.length];
+ if (i >= testValues.length) tv ^= i;
+ // Turn the canonical int to a float, boolean, String, whatever:
+ return invoke(coercers.get(c), tv);
+ }
+ /** Build a test array of the given length,
+ * packed with a subsequence of the test values.
+ * The first element of the array is always testValue(0).
+ */
+ static Object makeArray(int len, Class<?> c) {
+ Object a = Array.newInstance(c, len);
+ for (int i = 0; i < len; i++) {
+ Array.set(a, i, testValue(i, c));
+ }
+ return a;
+ }
+ /** Check that the given array has the required length.
+ * Check also that it is packed, up to firstNull, with
+ * a particular subsequence of the canonical test values.
+ * The subsequence must begin with a[0] == testValue(offset).
+ * At a[firstNull] and beyond, the array must contain null values.
+ */
+ static void checkArray(Object a, Class<?> c, int requiredLen, int offset, int firstNull) {
+ check(c == a.getClass().getComponentType());
+ Object nullValue = nullValues.get(c);
+ // Note: asserts in here are not part of the test program.
+ // They verify the integrity of the test method itself.
+ assert(nullValues.containsKey(c));
+
+ int misses = 0;
+ int firstMiss = -1;
+ // Check required length first.
+ int length = Array.getLength(a);
+ if (length != requiredLen && requiredLen != -1) {
+ if (muzzle == 0)
+ System.out.println("*** a.length = "+length+" != "+requiredLen);
+ ++misses;
+ }
+
+ for (int i = 0; i < length; i++) {
+ Object tv = (i >= firstNull) ? nullValue : testValue(i+offset, c);
+ Object ai = Array.get(a, i);
+ if (!eq(ai, tv)) {
+ if (muzzle == 0)
+ System.out.println("*** a["+i+"] = "+ai+" != "+tv);
+ if (misses == 0) firstMiss = i;
+ if (++misses > 10) break;
+ }
+ }
+ if (misses != 0) {
+ Method toString = toStrings.get(c);
+ if (toString == null) toString = toStrings.get(Object.class);
+ throw new RuntimeException("checkArray failed at "+firstMiss
+ +" "+c+"[]"
+ +" : "+invoke(toString, a));
+ }
+ }
+ // Typical comparison helper. Why isn't this a method somewhere.
+ static boolean eq(Object x, Object y) {
+ return x == null? y == null: x.equals(y);
+ }
+ // Exception-ignoring invoke function.
+ static Object invoke(Method m, Object... args) {
+ Exception ex;
+ try {
+ return m.invoke(null, args);
+ } catch (InvocationTargetException ee) {
+ ex = ee;
+ } catch (IllegalAccessException ee) {
+ ex = ee;
+ } catch (IllegalArgumentException ee) {
+ ex = ee;
+ }
+ ArrayList<Object> call = new ArrayList<Object>();
+ call.add(m); Collections.addAll(call, args);
+ throw new RuntimeException(call+" : "+ex);
+ }
+ // version of assert() that runs unconditionally
+ static void check(boolean z) {
+ if (!z) throw new RuntimeException("check failed");
+ }
+
+
+ /** Run about 10**5 distinct parameter combinations
+ * on copyOf and copyOfRange. Use all primitive types,
+ * and String and Object.
+ * Try to all critical values, looking for fencepost errors.
+ */
+ static void fullTests(int maxLen, Class<?> c) {
+ Method cloner = cloners.get(c);
+ assert(cloner != null) : c;
+ Method cloneRanger = cloneRangers.get(c);
+ // Note: asserts in here are not part of the test program.
+ // They verify the integrity of the test method itself.
+ assert(cloneRanger != null) : c;
+ for (int src = 0; src <= maxLen; src = inc(src, 0, maxLen)) {
+ Object a = makeArray(src, c);
+ for (int x : new ArrayList<Integer>()) {}
+ for (int j = 0; j <= maxLen; j = inc(j, src, maxLen)) {
+ // b = Arrays.copyOf(a, j);
+ Object b = invoke(cloner, a, j);
+ checkArray(b, c, j, 0, src);
+ testCasesRun++;
+ consing += j;
+
+ int maxI = Math.min(src, j);
+ for (int i = 0; i <= maxI; i = inc(i, src, maxI)) {
+ // r = Arrays.copyOfRange(a, i, j);
+ Object r = invoke(cloneRanger, a, i, j);
+ checkArray(r, c, j-i, i, src-i);
+ //System.out.println("case c="+c+" src="+src+" i="+i+" j="+j);
+ testCasesRun++;
+ consing += j-i;
+ }
+ }
+ }
+ }
+ // Increment x by at least one. Increment by a little more unless
+ // it is near a critical value, either zero, crit1, or crit2.
+ static int inc(int x, int crit1, int crit2) {
+ int D = shortStepsNear;
+ if (crit1 > crit2) { int t = crit1; crit1 = crit2; crit2 = t; }
+ assert(crit1 <= crit2);
+ assert(x <= crit2); // next1 or next2 must be the limit value
+ x += 1;
+ if (x > D) {
+ if (x < crit1-D) {
+ x += (x << 1) >> downShift; // giant step toward crit1-D
+ if (x > crit1-D) x = crit1-D;
+ } else if (x >= crit1+D && x < crit2-D) {
+ x += (x << 1) >> downShift; // giant step toward crit2-D
+ if (x > crit2-D) x = crit2-D;
+ }
+ }
+ return x;
+ }
+
+ public static void main(String[] av) {
+ boolean verbose = (av.length != 0);
+ muzzle = (verbose? 0: 1);
+ if (muzzle == 0)
+ System.out.println("test values: "+Arrays.toString(Arrays.copyOf(testValues, 5))+"...");
+
+ simpleTests();
+
+ muzzle = 0; // turn on print statements (affects failures only)
+
+ fullTests();
+ if (verbose)
+ System.out.println("ran "+testCasesRun+" tests, avg len="
+ +(float)consing/testCasesRun);
+
+ // test much larger arrays, more sparsely
+ maxLen = 500;
+ shortStepsNear = 2;
+ downShift = 0;
+ testCasesRun = 0;
+ consing = 0;
+ fullTests();
+ if (verbose)
+ System.out.println("ran "+testCasesRun+" tests, avg len="
+ +(float)consing/testCasesRun);
+ }
+
+ static void fullTests() {
+ for (Class<?> c : allTypes) {
+ fullTests(maxLen, c);
+ }
+ }
+
+ // We must run all the our tests on each of 8 distinct primitive types,
+ // and two reference types (Object, String) for good measure.
+ // This would be a pain to write out by hand, statically typed.
+ // So, use reflection. Following are the tables of methods we use.
+ // (The initial simple tests exercise enough of the static typing
+ // features of the API to ensure that they compile as advertised.)
+
+ static Object coerceToObject(int x) { return (x & 0xF) == 0? null: new Integer(x); }
+ static String coerceToString(int x) { return (x == 0)? null: Integer.toHexString(x); }
+ static Integer coerceToInteger(int x) { return (x == 0)? null: x; }
+ static byte coerceToByte(int x) { return (byte)x; }
+ static short coerceToShort(int x) { return (short)x; }
+ static int coerceToInt(int x) { return x; }
+ static long coerceToLong(int x) { return x; }
+ static char coerceToChar(int x) { return (char)x; }
+ static float coerceToFloat(int x) { return x; }
+ static double coerceToDouble(int x) { return x; }
+ static boolean coerceToBoolean(int x) { return (x&1) != 0; }
+
+ static Integer[] copyOfIntegerArray(Object[] a, int len) {
+ // This guy exercises the API based on a type-token.
+ // Note the static typing.
+ return Arrays.copyOf(a, len, Integer[].class);
+ }
+ static Integer[] copyOfIntegerArrayRange(Object[] a, int m, int n) {
+ // This guy exercises the API based on a type-token.
+ // Note the static typing.
+ return Arrays.copyOfRange(a, m, n, Integer[].class);
+ }
+
+ static final List<Class<?>> allTypes
+ = Arrays.asList(new Class<?>[]
+ { Object.class, String.class, Integer.class,
+ byte.class, short.class, int.class, long.class,
+ char.class, float.class, double.class,
+ boolean.class
+ });
+ static final HashMap<Class<?>,Method> coercers;
+ static final HashMap<Class<?>,Method> cloners;
+ static final HashMap<Class<?>,Method> cloneRangers;
+ static final HashMap<Class<?>,Method> toStrings;
+ static final HashMap<Class<?>,Object> nullValues;
+ static {
+ coercers = new HashMap<Class<?>,Method>();
+ Method[] testMethods = CopyMethods.class.getDeclaredMethods();
+ Method cia = null, ciar = null;
+ for (int i = 0; i < testMethods.length; i++) {
+ Method m = testMethods[i];
+ if (!Modifier.isStatic(m.getModifiers())) continue;
+ Class<?> rt = m.getReturnType();
+ if (m.getName().startsWith("coerceTo") && allTypes.contains(rt))
+ coercers.put(m.getReturnType(), m);
+ if (m.getName().equals("copyOfIntegerArray"))
+ cia = m;
+ if (m.getName().equals("copyOfIntegerArrayRange"))
+ ciar = m;
+ }
+ Method[] arrayMethods = Arrays.class.getDeclaredMethods();
+ cloners = new HashMap<Class<?>,Method>();
+ cloneRangers = new HashMap<Class<?>,Method>();
+ toStrings = new HashMap<Class<?>,Method>();
+ for (int i = 0; i < arrayMethods.length; i++) {
+ Method m = arrayMethods[i];
+ if (!Modifier.isStatic(m.getModifiers())) continue;
+ Class<?> rt = m.getReturnType();
+ if (m.getName().equals("copyOf")
+ && m.getParameterTypes().length == 2)
+ cloners.put(rt.getComponentType(), m);
+ if (m.getName().equals("copyOfRange")
+ && m.getParameterTypes().length == 3)
+ cloneRangers.put(rt.getComponentType(), m);
+ if (m.getName().equals("toString")) {
+ Class<?> pt = m.getParameterTypes()[0];
+ toStrings.put(pt.getComponentType(), m);
+ }
+ }
+ cloners.put(String.class, cloners.get(Object.class));
+ cloneRangers.put(String.class, cloneRangers.get(Object.class));
+ assert(cia != null);
+ cloners.put(Integer.class, cia);
+ assert(ciar != null);
+ cloneRangers.put(Integer.class, ciar);
+ nullValues = new HashMap<Class<?>,Object>();
+ for (Class<?> c : allTypes) {
+ nullValues.put(c, invoke(coercers.get(c), 0));
+ }
+ }
+}
diff --git a/test/java/util/Arrays/Correct.java b/test/java/util/Arrays/Correct.java
new file mode 100644
index 0000000..59e2bfe
--- /dev/null
+++ b/test/java/util/Arrays/Correct.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4726380
+ * @summary Check that different sorts give equivalent results.
+ */
+
+import java.util.*;
+
+public class Correct {
+
+ static Random rnd = new Random();
+ static final int ITERATIONS = 1000;
+ static final int TEST_SIZE = 1000;
+
+ public static void main(String[] args) throws Exception {
+ Object[] array1 = null;
+ Object[] array2 = null;
+
+ for (int i=0; i<ITERATIONS; i++) {
+ int size = rnd.nextInt(TEST_SIZE) + 1;
+ array1 = (Object[])getIntegerArray(size);
+ array2 = (Object[])array1.clone();
+ Arrays.sort(array1, array1.length/3, array1.length/2);
+ stupidSort(array2, array2.length/3, array2.length/2);
+ if(!Arrays.equals(array1, array2))
+ throw new RuntimeException("failed!");
+ }
+
+ for (int i=0; i<ITERATIONS; i++) {
+ int size = rnd.nextInt(TEST_SIZE) + 1;
+ array1 = (Object[])getIntegerArray(size);
+ array2 = (Object[])array1.clone();
+ Arrays.sort(array1, array1.length/3, array1.length/2, TEST_ORDER);
+ stupidSort(array2, array2.length/3, array2.length/2);
+ if(!Arrays.equals(array1, array2))
+ throw new RuntimeException("failed!");
+ }
+ }
+
+ static Integer[] getIntegerArray(int size) throws Exception {
+ Integer[] blah = new Integer[size];
+ for (int x=0; x<size; x++) {
+ blah[x] = new Integer(rnd.nextInt());
+ }
+ return blah;
+ }
+
+ static void stupidSort(Object[] a1, int from, int to) throws Exception {
+ for (int x=from; x<to; x++) {
+ Object lowest = new Integer(Integer.MAX_VALUE);
+ int lowestIndex = 0;
+ for (int y=x; y<to; y++) {
+ if (((Comparable)a1[y]).compareTo((Comparable)lowest) < 0) {
+ lowest = a1[y];
+ lowestIndex = y;
+ }
+ }
+ if (lowestIndex != x) {
+ swap(a1, x, lowestIndex);
+ }
+ }
+ }
+
+ static void swap(Object x[], int a, int b) {
+ Object t = x[a];
+ x[a] = x[b];
+ x[b] = t;
+ }
+
+ private static final Comparator TEST_ORDER = new IntegerComparator();
+
+ private static class IntegerComparator implements Comparator {
+ public int compare(Object o1, Object o2) {
+ Comparable c1 = (Comparable)o1;
+ Comparable c2 = (Comparable)o2;
+ return c1.compareTo(c2);
+ }
+ }
+}
diff --git a/test/java/util/Arrays/Fill.java b/test/java/util/Arrays/Fill.java
new file mode 100644
index 0000000..6e7b042
--- /dev/null
+++ b/test/java/util/Arrays/Fill.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4229892
+ * @summary Arrays.fill(Object[], ...) should throw ArrayStoreException
+ * @author Martin Buchholz
+ */
+
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+public class Fill {
+ private static void realMain(String[] args) throws Throwable {
+ try {
+ Arrays.fill(new Integer[3], "foo");
+ fail("Expected ArrayStoreException");
+ }
+ catch (ArrayStoreException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ try {
+ Arrays.fill(new Integer[3], 0, 2, "foo");
+ fail("Expected ArrayStoreException");
+ }
+ catch (ArrayStoreException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ try {
+ Arrays.fill(new Integer[3], 0, 4, "foo");
+ fail("Expected ArrayIndexOutOfBoundsException");
+ }
+ catch (ArrayIndexOutOfBoundsException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ static volatile int passed = 0, failed = 0;
+ static void pass() {passed++;}
+ static void fail() {failed++; Thread.dumpStack();}
+ static void fail(String msg) {System.out.println(msg); fail();}
+ static void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ static void check(boolean cond) {if (cond) pass(); else fail();}
+ static void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ try {realMain(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+}
diff --git a/test/java/util/Arrays/FloatDoubleOrder.java b/test/java/util/Arrays/FloatDoubleOrder.java
new file mode 100644
index 0000000..c161be0
--- /dev/null
+++ b/test/java/util/Arrays/FloatDoubleOrder.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 4143272 6548425
+ * @summary The natural ordering on Float and Double was not even a partial
+ * order (i.e., it violated the contract of Comparable.compareTo).
+ * Now it's a total ordering. Arrays.sort(double[])
+ * and Arrays.sort(double[]) reflect the new ordering. Also,
+ * Arrays.equals(double[], double[]) and
+ * Arrays.equals(float[], float[]) reflect the definition of
+ * equality used by Float and Double.
+ */
+
+import java.util.*;
+
+@SuppressWarnings("unchecked")
+public class FloatDoubleOrder {
+ void test(String[] args) throws Throwable {
+ double[] unsortedDbl = new double[] {1.0d, 3.7d, Double.NaN, -2.0d,
+ Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0d, -0.0d};
+
+ double[] sortedDbl = new double[] {Double.NEGATIVE_INFINITY, -2.0d,
+ -0.0d, 0.0d, 1.0d, 3.7d, Double.POSITIVE_INFINITY, Double.NaN};
+
+ List list = new ArrayList();
+ for (int i=0; i<unsortedDbl.length; i++)
+ list.add(new Double(unsortedDbl[i]));
+ Collections.sort(list);
+
+ List sortedList = new ArrayList();
+ for (int i=0; i<sortedDbl.length; i++)
+ sortedList.add(new Double(sortedDbl[i]));
+
+ check(list.equals(sortedList));
+
+ Arrays.sort(unsortedDbl);
+ check(Arrays.equals(unsortedDbl, sortedDbl));
+
+ double negNan = Double.longBitsToDouble(0xfff8000000000000L);
+ for (int i = 0; i < sortedDbl.length; i++) {
+ equal(Arrays.binarySearch(sortedDbl, sortedDbl[i]), i);
+ if (Double.isNaN(sortedDbl[i]))
+ equal(Arrays.binarySearch(sortedDbl, negNan), i);
+ }
+
+ float[] unsortedFlt = new float[] {1.0f, 3.7f, Float.NaN, -2.0f,
+ Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0f, -0.0f};
+
+ float[] sortedFlt = new float[] {Float.NEGATIVE_INFINITY, -2.0f,
+ -0.0f, 0.0f, 1.0f, 3.7f, Float.POSITIVE_INFINITY, Float.NaN};
+
+ list.clear();
+ for (int i=0; i<unsortedFlt.length; i++)
+ list.add(new Float(unsortedFlt[i]));
+ Collections.sort(list);
+
+ sortedList.clear();
+ for (int i=0; i<sortedFlt.length; i++)
+ sortedList.add(new Float(sortedFlt[i]));
+
+ check(list.equals(sortedList));
+
+ Arrays.sort(unsortedFlt);
+ check(Arrays.equals(unsortedFlt, sortedFlt));
+
+ float negNaN = Float.intBitsToFloat(0xFfc00000);
+ for (int i = 0; i < sortedDbl.length; i++) {
+ equal(Arrays.binarySearch(sortedFlt, sortedFlt[i]), i);
+ if (Float.isNaN(sortedFlt[i]))
+ equal(Arrays.binarySearch(sortedFlt, negNaN), i);
+ }
+
+
+ // 6548425: Arrays.sort incorrectly sorts a double array
+ // containing negative zeros
+ double[] da = {-0.0d, -0.0d, 0.0d, -0.0d};
+ Arrays.sort(da, 1, 4);
+ check(Arrays.equals(da, new double[] {-0.0d, -0.0d, -0.0d, 0.0d}));
+
+ float[] fa = {-0.0f, -0.0f, 0.0f, -0.0f};
+ Arrays.sort(fa, 1, 4);
+ check(Arrays.equals(fa, new float[] {-0.0f, -0.0f, -0.0f, 0.0f}));
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ volatile int passed = 0, failed = 0;
+ void pass() {passed++;}
+ void fail() {failed++; Thread.dumpStack();}
+ void fail(String msg) {System.err.println(msg); fail();}
+ void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ void check(boolean cond) {if (cond) pass(); else fail();}
+ void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ new FloatDoubleOrder().instanceMain(args);}
+ void instanceMain(String[] args) throws Throwable {
+ try {test(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+}