/* * Copyright (c) 2003, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.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 * @key randomness
*/
// Make deepCopy != a if (size == 0)
deepCopy = new Object[] {"foo"}; elseif (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 instanceofbyte[])
result.add(PrimitiveArrays.asList((byte[])e)); elseif (e instanceofshort[])
result.add(PrimitiveArrays.asList((short[])e)); elseif (e instanceofint[])
result.add(PrimitiveArrays.asList((int[])e)); elseif (e instanceoflong[])
result.add(PrimitiveArrays.asList((long[])e)); elseif (e instanceofchar[])
result.add(PrimitiveArrays.asList((char[])e)); elseif (e instanceofdouble[])
result.add(PrimitiveArrays.asList((double[])e)); elseif (e instanceoffloat[])
result.add(PrimitiveArrays.asList((float[])e)); elseif (e instanceofboolean[])
result.add(PrimitiveArrays.asList((boolean[])e)); elseif (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) { thrownew IllegalArgumentException(e);
}
}
/** * 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 { privatestatic Random rnd = new Random();
publicstaticlong nextLong() { switch(rnd.nextInt(10)) { case 0: return 0; case 1: returnLong.MIN_VALUE; case 2: returnLong.MAX_VALUE; case 3: case 4: case 5: return (long) (rnd.nextInt(20) - 10); default: return rnd.nextLong();
}
}
publicstaticint 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();
}
}
publicstaticshort nextShort() { switch(rnd.nextInt(10)) { case 0: return 0; case 1: returnShort.MIN_VALUE; case 2: returnShort.MAX_VALUE; case 3: case 4: case 5: return (short) (rnd.nextInt(20) - 10); default: return (short) rnd.nextInt();
}
}
publicstaticchar 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();
}
}
publicstaticbyte nextByte() { switch(rnd.nextInt(10)) { case 0: return 0; case 1: returnByte.MIN_VALUE; case 2: returnByte.MAX_VALUE; case 3: case 4: case 5: return (byte) (rnd.nextInt(20) - 10); default: return (byte) rnd.nextInt();
}
}
publicstaticdouble nextDouble() { switch(rnd.nextInt(20)) { case 0: return 0; case 1: return -0.0; case 2: returnDouble.MIN_VALUE; case 3: returnDouble.MAX_VALUE; case 4: returnDouble.NaN; case 5: returnDouble.NEGATIVE_INFINITY; case 6: returnDouble.POSITIVE_INFINITY; case 7: case 8: case 9: return (rnd.nextInt(20) - 10); default: return rnd.nextDouble();
}
}
publicstaticfloat nextFloat() { switch(rnd.nextInt(20)) { case 0: return 0; case 1: return -0.0f; case 2: returnFloat.MIN_VALUE; case 3: returnFloat.MAX_VALUE; case 4: returnFloat.NaN; case 5: returnFloat.NEGATIVE_INFINITY; case 6: returnFloat.POSITIVE_INFINITY; case 7: case 8: case 9: return (rnd.nextInt(20) - 10); default: return rnd.nextFloat();
}
}
publicstatic Object nextObject() { switch(rnd.nextInt(10)) { case 0: returnnull; case 1: return"foo"; case 2: case 3: case 4: returnDouble.valueOf(nextDouble()); default: return Integer.valueOf(nextInt());
}
}
publicstaticlong[] longArray(int length) { long[] result = newlong[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextLong(); return result;
}
publicstaticint[] intArray(int length) { int[] result = newint[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextInt(); return result;
}
publicstaticshort[] shortArray(int length) { short[] result = newshort[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextShort(); return result;
}
publicstaticchar[] charArray(int length) { char[] result = newchar[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextChar(); return result;
}
publicstaticbyte[] byteArray(int length) { byte[] result = newbyte[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextByte(); return result;
}
publicstaticboolean[] booleanArray(int length) { boolean[] result = newboolean[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextBoolean(); return result;
}
publicstaticdouble[] doubleArray(int length) { double[] result = newdouble[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextDouble(); return result;
}
publicstaticfloat[] floatArray(int length) { float[] result = newfloat[length]; for (int i = 0; i < length; i++)
result[i] = Rnd.nextFloat(); return result;
}
publicstatic 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 publicstatic 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.
*/
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.18Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.