< prev index next >

test/jdk/java/util/Arrays/ArrayObjectMethods.java

Print this page

  1 /*
  2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug     4906359 6239296
 27  * @summary Basic test for content-based array object methods
 28  * @author  Josh Bloch, Martin Buchholz
 29  * @key randomness

 30  */
 31 
 32 import java.util.*;
 33 import java.io.*;
 34 


 35 public class ArrayObjectMethods {




 36     int[] sizes = {0, 10, 100, 200, 1000};
 37 
 38     void test(String[] args) throws Throwable {
 39         equal(Arrays.deepToString(null), "null");
 40         equal(Arrays.deepToString(new Object[]{}), "[]");
 41         equal(Arrays.deepToString(new Object[]{null}), "[null]");
 42         equal(Arrays.deepToString(new Object[]{null, 1}), "[null, 1]");
 43         equal(Arrays.deepToString(new Object[]{1, null}), "[1, null]");
 44         equal(Arrays.deepToString(new Object[]{new Object[]{}, null}), "[[], null]");
 45 
 46         {
 47             Object[] a = {1, null};
 48             a[1] = a;
 49             equal(Arrays.deepToString(a), "[1, [...]]");
 50             a[0] = a;
 51             equal(Arrays.deepToString(a), "[[...], [...]]");
 52             a[0] = a[1] = new Object[]{1, null, a};
 53             equal(Arrays.deepToString(a), "[[1, null, [...]], [1, null, [...]]]");
 54         }
 55 

273             default: return rnd.nextDouble();
274         }
275     }
276 
277     public static float nextFloat() {
278         switch(rnd.nextInt(20)) {
279             case 0:  return 0;
280             case 1:  return -0.0f;
281             case 2:  return Float.MIN_VALUE;
282             case 3:  return Float.MAX_VALUE;
283             case 4:  return Float.NaN;
284             case 5:  return Float.NEGATIVE_INFINITY;
285             case 6:  return Float.POSITIVE_INFINITY;
286             case 7: case 8: case 9:
287                      return (rnd.nextInt(20) - 10);
288             default: return rnd.nextFloat();
289         }
290     }
291 
292     public static Object nextObject() {
293         switch(rnd.nextInt(10)) {
294             case 0:  return null;
295             case 1:  return "foo";
296             case 2:  case 3: case 4:
297                      return Double.valueOf(nextDouble());
298             default: return Integer.valueOf(nextInt());
299         }
300     }
301 
302     public static long[] longArray(int length) {
303         long[] result = new long[length];
304         for (int i = 0; i < length; i++)
305             result[i] = Rnd.nextLong();
306         return result;
307     }
308 
309     public static int[] intArray(int length) {
310         int[] result = new int[length];
311         for (int i = 0; i < length; i++)
312             result[i] = Rnd.nextInt();
313         return result;
314     }
315 
316     public static short[] shortArray(int length) {
317         short[] result = new short[length];

345         double[] result = new double[length];
346         for (int i = 0; i < length; i++)
347             result[i] = Rnd.nextDouble();
348         return result;
349     }
350 
351     public static float[] floatArray(int length) {
352         float[] result = new float[length];
353         for (int i = 0; i < length; i++)
354             result[i] = Rnd.nextFloat();
355         return result;
356     }
357 
358     public static Object[] flatObjectArray(int length) {
359         Object[] result = new Object[length];
360         for (int i = 0; i < length; i++)
361             result[i] = Rnd.nextObject();
362         return result;
363     }
364 











365     // Calling this for length >> 100 is likely to run out of memory!  It
366     // should be perhaps be tuned to allow for longer arrays
367     public static Object[] nestedObjectArray(int length) {
368         Object[] result = new Object[length];
369         for (int i = 0; i < length; i++) {
370             switch(rnd.nextInt(16)) {
371                 case 0:  result[i] = nestedObjectArray(length/2);
372                          break;
373                 case 1:  result[i] = longArray(length/2);
374                          break;
375                 case 2:  result[i] = intArray(length/2);
376                          break;
377                 case 3:  result[i] = shortArray(length/2);
378                          break;
379                 case 4:  result[i] = charArray(length/2);
380                          break;
381                 case 5:  result[i] = byteArray(length/2);
382                          break;
383                 case 6:  result[i] = floatArray(length/2);
384                          break;
385                 case 7:  result[i] = doubleArray(length/2);
386                          break;
387                 case 8:  result[i] = longArray(length/2);
388                          break;




389                 default: result[i] = Rnd.nextObject();
390             }
391         }
392         return result;
393     }
394 }
395 
396 /**
397  * Primitive arrays viewed as lists.  Inefficient but cool.
398  * This utility should be generally useful in writing regression/unit/basic
399  * tests.
400  */
401 
402 class PrimitiveArrays {
403     public static List<Long> asList(final long[] a) {
404         return new AbstractList<Long>() {
405             public Long get(int i) { return a[i]; }
406             public int size()      { return a.length; }
407 
408             public Long set(int i, Long e) {

  1 /*
  2  * Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug     4906359 6239296
 27  * @summary Basic test for content-based array object methods
 28  * @author  Josh Bloch, Martin Buchholz
 29  * @key randomness
 30  * @library /test/lib
 31  */
 32 
 33 import java.util.*;
 34 import java.io.*;
 35 
 36 import jdk.test.lib.valueclass.AsValueClass;
 37 
 38 public class ArrayObjectMethods {
 39 
 40     @AsValueClass
 41     record V(int x, int y) implements Serializable {}
 42 
 43     int[] sizes = {0, 10, 100, 200, 1000};
 44 
 45     void test(String[] args) throws Throwable {
 46         equal(Arrays.deepToString(null), "null");
 47         equal(Arrays.deepToString(new Object[]{}), "[]");
 48         equal(Arrays.deepToString(new Object[]{null}), "[null]");
 49         equal(Arrays.deepToString(new Object[]{null, 1}), "[null, 1]");
 50         equal(Arrays.deepToString(new Object[]{1, null}), "[1, null]");
 51         equal(Arrays.deepToString(new Object[]{new Object[]{}, null}), "[[], null]");
 52 
 53         {
 54             Object[] a = {1, null};
 55             a[1] = a;
 56             equal(Arrays.deepToString(a), "[1, [...]]");
 57             a[0] = a;
 58             equal(Arrays.deepToString(a), "[[...], [...]]");
 59             a[0] = a[1] = new Object[]{1, null, a};
 60             equal(Arrays.deepToString(a), "[[1, null, [...]], [1, null, [...]]]");
 61         }
 62 

280             default: return rnd.nextDouble();
281         }
282     }
283 
284     public static float nextFloat() {
285         switch(rnd.nextInt(20)) {
286             case 0:  return 0;
287             case 1:  return -0.0f;
288             case 2:  return Float.MIN_VALUE;
289             case 3:  return Float.MAX_VALUE;
290             case 4:  return Float.NaN;
291             case 5:  return Float.NEGATIVE_INFINITY;
292             case 6:  return Float.POSITIVE_INFINITY;
293             case 7: case 8: case 9:
294                      return (rnd.nextInt(20) - 10);
295             default: return rnd.nextFloat();
296         }
297     }
298 
299     public static Object nextObject() {
300         switch(rnd.nextInt(12)) {
301             case 0:  return null;
302             case 1:  return "foo";
303             case 2: case 3: case 4: return Double.valueOf(nextDouble());
304             case 5: case 6: return nextV();
305             default: return Integer.valueOf(nextInt());
306         }
307     }
308 
309     public static long[] longArray(int length) {
310         long[] result = new long[length];
311         for (int i = 0; i < length; i++)
312             result[i] = Rnd.nextLong();
313         return result;
314     }
315 
316     public static int[] intArray(int length) {
317         int[] result = new int[length];
318         for (int i = 0; i < length; i++)
319             result[i] = Rnd.nextInt();
320         return result;
321     }
322 
323     public static short[] shortArray(int length) {
324         short[] result = new short[length];

352         double[] result = new double[length];
353         for (int i = 0; i < length; i++)
354             result[i] = Rnd.nextDouble();
355         return result;
356     }
357 
358     public static float[] floatArray(int length) {
359         float[] result = new float[length];
360         for (int i = 0; i < length; i++)
361             result[i] = Rnd.nextFloat();
362         return result;
363     }
364 
365     public static Object[] flatObjectArray(int length) {
366         Object[] result = new Object[length];
367         for (int i = 0; i < length; i++)
368             result[i] = Rnd.nextObject();
369         return result;
370     }
371 
372     public static ArrayObjectMethods.V nextV() {
373         return new ArrayObjectMethods.V(nextInt(), nextInt());
374     }
375 
376     public static ArrayObjectMethods.V[] vArray(int length) {
377         ArrayObjectMethods.V[] result = new ArrayObjectMethods.V[length];
378         for (int i = 0; i < length; i++)
379             result[i] = nextV();
380         return result;
381     }
382 
383     // Calling this for length >> 100 is likely to run out of memory!  It
384     // should be perhaps be tuned to allow for longer arrays
385     public static Object[] nestedObjectArray(int length) {
386         Object[] result = new Object[length];
387         for (int i = 0; i < length; i++) {
388             switch(rnd.nextInt(16)) {
389                 case 0:  result[i] = nestedObjectArray(length/2);
390                          break;
391                 case 1:  result[i] = longArray(length/2);
392                          break;
393                 case 2:  result[i] = intArray(length/2);
394                          break;
395                 case 3:  result[i] = shortArray(length/2);
396                          break;
397                 case 4:  result[i] = charArray(length/2);
398                          break;
399                 case 5:  result[i] = byteArray(length/2);
400                          break;
401                 case 6:  result[i] = floatArray(length/2);
402                          break;
403                 case 7:  result[i] = doubleArray(length/2);
404                          break;
405                 case 8:  result[i] = longArray(length/2);
406                          break;
407                 case 9:  result[i] = vArray(length/2);
408                          break;
409                 case 10: result[i] = nextV();
410                          break;
411                 default: result[i] = Rnd.nextObject();
412             }
413         }
414         return result;
415     }
416 }
417 
418 /**
419  * Primitive arrays viewed as lists.  Inefficient but cool.
420  * This utility should be generally useful in writing regression/unit/basic
421  * tests.
422  */
423 
424 class PrimitiveArrays {
425     public static List<Long> asList(final long[] a) {
426         return new AbstractList<Long>() {
427             public Long get(int i) { return a[i]; }
428             public int size()      { return a.length; }
429 
430             public Long set(int i, Long e) {
< prev index next >