Path: blob/master/test/jdk/java/util/Arrays/ArrayObjectMethods.java
41149 views
/*1* Copyright (c) 2003, 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 4906359 623929626* @summary Basic test for content-based array object methods27* @author Josh Bloch, Martin Buchholz28* @key randomness29*/3031import java.util.*;32import java.io.*;3334public class ArrayObjectMethods {35int[] sizes = {0, 10, 100, 200, 1000};3637void test(String[] args) throws Throwable {38equal(Arrays.deepToString(null), "null");39equal(Arrays.deepToString(new Object[]{}), "[]");40equal(Arrays.deepToString(new Object[]{null}), "[null]");41equal(Arrays.deepToString(new Object[]{null, 1}), "[null, 1]");42equal(Arrays.deepToString(new Object[]{1, null}), "[1, null]");43equal(Arrays.deepToString(new Object[]{new Object[]{}, null}), "[[], null]");4445{46Object[] a = {1, null};47a[1] = a;48equal(Arrays.deepToString(a), "[1, [...]]");49a[0] = a;50equal(Arrays.deepToString(a), "[[...], [...]]");51a[0] = a[1] = new Object[]{1, null, a};52equal(Arrays.deepToString(a), "[[1, null, [...]], [1, null, [...]]]");53}5455for (int size : sizes) {56{57long[] a = Rnd.longArray(size);58equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());59equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());60}61{62int[] a = Rnd.intArray(size);63equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());64equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());65}66{67short[] a = Rnd.shortArray(size);68equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());69equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());70}71{72char[] a = Rnd.charArray(size);73equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());74equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());75}76{77byte[] a = Rnd.byteArray(size);78equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());79equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());80}81{82boolean[] a = Rnd.booleanArray(size);83equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());84equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());85}86{87double[] a = Rnd.doubleArray(size);88equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());89equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());90}91{92float[] a = Rnd.floatArray(size);93equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());94equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());95}96{97Object[] a = Rnd.flatObjectArray(size);98equal(Arrays.toString(a), Arrays.asList(a).toString());99equal(Arrays.deepToString(a), Arrays.asList(a).toString());100equal(Arrays.hashCode(a), Arrays.asList(a).hashCode());101}102103if (size <= 200) {104Object[] a = Rnd.nestedObjectArray(size);105List aList = deepToList(a);106equal(Arrays.toString(a), Arrays.asList(a).toString());107equal(Arrays.deepToString(a), aList.toString());108equal(Arrays.deepHashCode(a), aList.hashCode());109equal(Arrays.hashCode(a), Arrays.asList(a).hashCode());110111Object[] deepCopy = (Object[]) deepCopy(a);112check(Arrays.deepEquals(a, deepCopy));113check(Arrays.deepEquals(deepCopy, a));114115// Make deepCopy != a116if (size == 0)117deepCopy = new Object[] {"foo"};118else if (deepCopy[deepCopy.length - 1] == null)119deepCopy[deepCopy.length - 1] = "baz";120else121deepCopy[deepCopy.length - 1] = null;122check(! Arrays.deepEquals(a, deepCopy));123check(! Arrays.deepEquals(deepCopy, a));124}125}126}127128// Utility method to turn an array into a list "deeply," turning129// all primitives into objects130List<Object> deepToList(Object[] a) {131List<Object> result = new ArrayList<Object>();132for (Object e : a) {133if (e instanceof byte[])134result.add(PrimitiveArrays.asList((byte[])e));135else if (e instanceof short[])136result.add(PrimitiveArrays.asList((short[])e));137else if (e instanceof int[])138result.add(PrimitiveArrays.asList((int[])e));139else if (e instanceof long[])140result.add(PrimitiveArrays.asList((long[])e));141else if (e instanceof char[])142result.add(PrimitiveArrays.asList((char[])e));143else if (e instanceof double[])144result.add(PrimitiveArrays.asList((double[])e));145else if (e instanceof float[])146result.add(PrimitiveArrays.asList((float[])e));147else if (e instanceof boolean[])148result.add(PrimitiveArrays.asList((boolean[])e));149else if (e instanceof Object[])150result.add(deepToList((Object[])e));151else152result.add(e);153}154return result;155}156157// Utility method to do a deep copy of an object *very slowly* using158// serialization/deserialization159Object deepCopy(Object oldObj) {160try {161ByteArrayOutputStream bos = new ByteArrayOutputStream();162ObjectOutputStream oos = new ObjectOutputStream(bos);163oos.writeObject(oldObj);164oos.flush();165ByteArrayInputStream bin = new ByteArrayInputStream(166bos.toByteArray());167ObjectInputStream ois = new ObjectInputStream(bin);168return ois.readObject();169} catch(Exception e) {170throw new IllegalArgumentException(e);171}172}173174//--------------------- Infrastructure ---------------------------175volatile int passed = 0, failed = 0;176void pass() {passed++;}177void fail() {failed++; Thread.dumpStack();}178void fail(String msg) {System.err.println(msg); fail();}179void unexpected(Throwable t) {failed++; t.printStackTrace();}180void check(boolean cond) {if (cond) pass(); else fail();}181void equal(Object x, Object y) {182if (x == null ? y == null : x.equals(y)) pass();183else fail(x + " not equal to " + y);}184public static void main(String[] args) throws Throwable {185new ArrayObjectMethods().instanceMain(args);}186void instanceMain(String[] args) throws Throwable {187try {test(args);} catch (Throwable t) {unexpected(t);}188System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);189if (failed > 0) throw new AssertionError("Some tests failed");}190}191192/**193* Methods to generate "interesting" random primitives and primitive194* arrays. Unlike Random.nextXxx, these methods return small values195* and boundary values (e.g., 0, -1, NaN) with greater than normal196* likelihood.197*/198199class Rnd {200private static Random rnd = new Random();201202public static long nextLong() {203switch(rnd.nextInt(10)) {204case 0: return 0;205case 1: return Long.MIN_VALUE;206case 2: return Long.MAX_VALUE;207case 3: case 4: case 5:208return (long) (rnd.nextInt(20) - 10);209default: return rnd.nextLong();210}211}212213public static int nextInt() {214switch(rnd.nextInt(10)) {215case 0: return 0;216case 1: return Integer.MIN_VALUE;217case 2: return Integer.MAX_VALUE;218case 3: case 4: case 5:219return rnd.nextInt(20) - 10;220default: return rnd.nextInt();221}222}223224public static short nextShort() {225switch(rnd.nextInt(10)) {226case 0: return 0;227case 1: return Short.MIN_VALUE;228case 2: return Short.MAX_VALUE;229case 3: case 4: case 5:230return (short) (rnd.nextInt(20) - 10);231default: return (short) rnd.nextInt();232}233}234235public static char nextChar() {236switch(rnd.nextInt(10)) {237case 0: return 0;238case 1: return Character.MIN_VALUE;239case 2: return Character.MAX_VALUE;240case 3: case 4: case 5:241return (char) (rnd.nextInt(20) - 10);242default: return (char) rnd.nextInt();243}244}245246public static byte nextByte() {247switch(rnd.nextInt(10)) {248case 0: return 0;249case 1: return Byte.MIN_VALUE;250case 2: return Byte.MAX_VALUE;251case 3: case 4: case 5:252return (byte) (rnd.nextInt(20) - 10);253default: return (byte) rnd.nextInt();254}255}256257public static boolean nextBoolean() {258return rnd.nextBoolean();259}260261public static double nextDouble() {262switch(rnd.nextInt(20)) {263case 0: return 0;264case 1: return -0.0;265case 2: return Double.MIN_VALUE;266case 3: return Double.MAX_VALUE;267case 4: return Double.NaN;268case 5: return Double.NEGATIVE_INFINITY;269case 6: return Double.POSITIVE_INFINITY;270case 7: case 8: case 9:271return (rnd.nextInt(20) - 10);272default: return rnd.nextDouble();273}274}275276public static float nextFloat() {277switch(rnd.nextInt(20)) {278case 0: return 0;279case 1: return -0.0f;280case 2: return Float.MIN_VALUE;281case 3: return Float.MAX_VALUE;282case 4: return Float.NaN;283case 5: return Float.NEGATIVE_INFINITY;284case 6: return Float.POSITIVE_INFINITY;285case 7: case 8: case 9:286return (rnd.nextInt(20) - 10);287default: return rnd.nextFloat();288}289}290291public static Object nextObject() {292switch(rnd.nextInt(10)) {293case 0: return null;294case 1: return "foo";295case 2: case 3: case 4:296return Double.valueOf(nextDouble());297default: return Integer.valueOf(nextInt());298}299}300301public static long[] longArray(int length) {302long[] result = new long[length];303for (int i = 0; i < length; i++)304result[i] = Rnd.nextLong();305return result;306}307308public static int[] intArray(int length) {309int[] result = new int[length];310for (int i = 0; i < length; i++)311result[i] = Rnd.nextInt();312return result;313}314315public static short[] shortArray(int length) {316short[] result = new short[length];317for (int i = 0; i < length; i++)318result[i] = Rnd.nextShort();319return result;320}321322public static char[] charArray(int length) {323char[] result = new char[length];324for (int i = 0; i < length; i++)325result[i] = Rnd.nextChar();326return result;327}328329public static byte[] byteArray(int length) {330byte[] result = new byte[length];331for (int i = 0; i < length; i++)332result[i] = Rnd.nextByte();333return result;334}335336public static boolean[] booleanArray(int length) {337boolean[] result = new boolean[length];338for (int i = 0; i < length; i++)339result[i] = Rnd.nextBoolean();340return result;341}342343public static double[] doubleArray(int length) {344double[] result = new double[length];345for (int i = 0; i < length; i++)346result[i] = Rnd.nextDouble();347return result;348}349350public static float[] floatArray(int length) {351float[] result = new float[length];352for (int i = 0; i < length; i++)353result[i] = Rnd.nextFloat();354return result;355}356357public static Object[] flatObjectArray(int length) {358Object[] result = new Object[length];359for (int i = 0; i < length; i++)360result[i] = Rnd.nextObject();361return result;362}363364// Calling this for length >> 100 is likely to run out of memory! It365// should be perhaps be tuned to allow for longer arrays366public static Object[] nestedObjectArray(int length) {367Object[] result = new Object[length];368for (int i = 0; i < length; i++) {369switch(rnd.nextInt(16)) {370case 0: result[i] = nestedObjectArray(length/2);371break;372case 1: result[i] = longArray(length/2);373break;374case 2: result[i] = intArray(length/2);375break;376case 3: result[i] = shortArray(length/2);377break;378case 4: result[i] = charArray(length/2);379break;380case 5: result[i] = byteArray(length/2);381break;382case 6: result[i] = floatArray(length/2);383break;384case 7: result[i] = doubleArray(length/2);385break;386case 8: result[i] = longArray(length/2);387break;388default: result[i] = Rnd.nextObject();389}390}391return result;392}393}394395/**396* Primitive arrays viewed as lists. Inefficient but cool.397* This utility should be generally useful in writing regression/unit/basic398* tests.399*/400401class PrimitiveArrays {402public static List<Long> asList(final long[] a) {403return new AbstractList<Long>() {404public Long get(int i) { return a[i]; }405public int size() { return a.length; }406407public Long set(int i, Long e) {408long oldVal = a[i];409a[i] = e;410return oldVal;411}412};413}414415public static List<Integer> asList(final int[] a) {416return new AbstractList<Integer>() {417public Integer get(int i) { return a[i]; }418public int size() { return a.length; }419420public Integer set(int i, Integer e) {421int oldVal = a[i];422a[i] = e;423return oldVal;424}425};426}427428public static List<Short> asList(final short[] a) {429return new AbstractList<Short>() {430public Short get(int i) { return a[i]; }431public int size() { return a.length; }432433public Short set(int i, Short e) {434short oldVal = a[i];435a[i] = e;436return oldVal;437}438};439}440441public static List<Character> asList(final char[] a) {442return new AbstractList<Character>() {443public Character get(int i) { return a[i]; }444public int size() { return a.length; }445446public Character set(int i, Character e) {447Character oldVal = a[i];448a[i] = e;449return oldVal;450}451};452}453454public static List<Byte> asList(final byte[] a) {455return new AbstractList<Byte>() {456public Byte get(int i) { return a[i]; }457public int size() { return a.length; }458459public Byte set(int i, Byte e) {460Byte oldVal = a[i];461a[i] = e;462return oldVal;463}464};465}466467public static List<Boolean> asList(final boolean[] a) {468return new AbstractList<Boolean>() {469public Boolean get(int i) { return a[i]; }470public int size() { return a.length; }471472public Boolean set(int i, Boolean e) {473Boolean oldVal = a[i];474a[i] = e;475return oldVal;476}477};478}479480public static List<Double> asList(final double[] a) {481return new AbstractList<Double>() {482public Double get(int i) { return a[i]; }483public int size() { return a.length; }484485public Double set(int i, Double e) {486Double oldVal = a[i];487a[i] = e;488return oldVal;489}490};491}492493public static List<Float> asList(final float[] a) {494return new AbstractList<Float>() {495public Float get(int i) { return a[i]; }496public int size() { return a.length; }497498public Float set(int i, Float e) {499Float oldVal = a[i];500a[i] = e;501return oldVal;502}503};504}505}506507508