< prev index next >

test/jdk/java/util/Arrays/CopyMethods.java

Print this page

 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     4655503
 27  * @summary Test for array cloning and slicing methods.
 28  * @author  John Rose
 29  * @key randomness

 30  */
 31 

 32 import java.util.*;
 33 import java.lang.reflect.*;
 34 


 35 public class CopyMethods {
 36     static int muzzle;  // if !=0, suppresses ("muzzles") messages
 37 
 38     static int maxLen = 40;  // maximum length of test arrays
 39     static int shortStepsNear = 4;  // interesting span near critical values
 40     static int downShift = 3;
 41 
 42     static int testCasesRun = 0;
 43     static long consing = 0;
 44 



 45     // very simple tests, mainly to test the framework itself
 46     static void simpleTests() {
 47         int[] a = (int[]) makeArray(3, int.class);
 48         if (muzzle == 0)
 49             System.out.println("int[] a = "+Arrays.toString(a));
 50         check(a.length == 3);
 51         check(a[0] == testValues[0]);
 52         check(a[1] == testValues[1]);
 53         check(a[2] == testValues[2]);
 54         checkArray(a, int.class, 3, 0, 3);
 55         // negative test of testing framework:
 56         for (int bad = -2; bad < a.length; bad++) {
 57             try {
 58                 int[] aa = a.clone();
 59                 if (bad < 0)  aa = new int[4];
 60                 else          aa[bad] = 0;
 61                 ++muzzle;
 62                 // the following check should fail!
 63                 if (bad == -2)
 64                     checkArray(new String[3], int.class, 0, 0, a.length);

336     }
337 
338     // We must run all the our tests on each of 8 distinct primitive types,
339     // and two reference types (Object, String) for good measure.
340     // This would be a pain to write out by hand, statically typed.
341     // So, use reflection.  Following are the tables of methods we use.
342     // (The initial simple tests exercise enough of the static typing
343     // features of the API to ensure that they compile as advertised.)
344 
345     static Object  coerceToObject(int x) { return (x & 0xF) == 0? null: new Integer(x); }
346     static String  coerceToString(int x) { return (x == 0)? null: Integer.toHexString(x); }
347     static Integer coerceToInteger(int x) { return (x == 0)? null: x; }
348     static byte    coerceToByte(int x) { return (byte)x; }
349     static short   coerceToShort(int x) { return (short)x; }
350     static int     coerceToInt(int x) { return x; }
351     static long    coerceToLong(int x) { return x; }
352     static char    coerceToChar(int x) { return (char)x; }
353     static float   coerceToFloat(int x) { return x; }
354     static double  coerceToDouble(int x) { return x; }
355     static boolean coerceToBoolean(int x) { return (x&1) != 0; }



356 
357     static Integer[] copyOfIntegerArray(Object[] a, int len) {
358         // This guy exercises the API based on a type-token.
359         // Note the static typing.
360         return Arrays.copyOf(a, len, Integer[].class);
361     }
362     static Integer[] copyOfIntegerArrayRange(Object[] a, int m, int n) {
363         // This guy exercises the API based on a type-token.
364         // Note the static typing.
365         return Arrays.copyOfRange(a, m, n, Integer[].class);
366     }
367 
368     static final List<Class<?>> allTypes
369         = Arrays.asList(new Class<?>[]
370                         {   Object.class, String.class, Integer.class,
371                             byte.class, short.class, int.class, long.class,
372                             char.class, float.class, double.class,
373                             boolean.class

374                         });
375     static final HashMap<Class<?>,Method> coercers;
376     static final HashMap<Class<?>,Method> cloners;
377     static final HashMap<Class<?>,Method> cloneRangers;
378     static final HashMap<Class<?>,Method> toStrings;
379     static final HashMap<Class<?>,Object> nullValues;
380     static {
381         coercers = new HashMap<Class<?>,Method>();
382         Method[] testMethods = CopyMethods.class.getDeclaredMethods();
383         Method cia = null, ciar = null;
384         for (int i = 0; i < testMethods.length; i++) {
385             Method m = testMethods[i];
386             if (!Modifier.isStatic(m.getModifiers()))  continue;
387             Class<?> rt = m.getReturnType();
388             if (m.getName().startsWith("coerceTo") && allTypes.contains(rt))
389                 coercers.put(m.getReturnType(), m);
390             if (m.getName().equals("copyOfIntegerArray"))
391                 cia = m;
392             if (m.getName().equals("copyOfIntegerArrayRange"))
393                 ciar = m;

400             Method m = arrayMethods[i];
401             if (!Modifier.isStatic(m.getModifiers()))  continue;
402             Class<?> rt = m.getReturnType();
403             if (m.getName().equals("copyOf")
404                 && m.getParameterTypes().length == 2)
405                 cloners.put(rt.getComponentType(), m);
406             if (m.getName().equals("copyOfRange")
407                 && m.getParameterTypes().length == 3)
408                 cloneRangers.put(rt.getComponentType(), m);
409             if (m.getName().equals("toString")) {
410                 Class<?> pt = m.getParameterTypes()[0];
411                 toStrings.put(pt.getComponentType(), m);
412             }
413         }
414         cloners.put(String.class, cloners.get(Object.class));
415         cloneRangers.put(String.class, cloneRangers.get(Object.class));
416         assert(cia != null);
417         cloners.put(Integer.class, cia);
418         assert(ciar != null);
419         cloneRangers.put(Integer.class, ciar);






420         nullValues = new HashMap<Class<?>,Object>();
421         for (Class<?> c : allTypes) {
422             nullValues.put(c, invoke(coercers.get(c), 0));
423         }
424     }
425 }

 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     4655503
 27  * @summary Test for array cloning and slicing methods.
 28  * @author  John Rose
 29  * @key randomness
 30  * @library /test/lib
 31  */
 32 
 33 import java.time.LocalDate;
 34 import java.util.*;
 35 import java.lang.reflect.*;
 36 
 37 import jdk.test.lib.valueclass.AsValueClass;
 38 
 39 public class CopyMethods {
 40     static int muzzle;  // if !=0, suppresses ("muzzles") messages
 41 
 42     static int maxLen = 40;  // maximum length of test arrays
 43     static int shortStepsNear = 4;  // interesting span near critical values
 44     static int downShift = 3;
 45 
 46     static int testCasesRun = 0;
 47     static long consing = 0;
 48 
 49     @AsValueClass
 50     record Point(int x, int y) {}
 51 
 52     // very simple tests, mainly to test the framework itself
 53     static void simpleTests() {
 54         int[] a = (int[]) makeArray(3, int.class);
 55         if (muzzle == 0)
 56             System.out.println("int[] a = "+Arrays.toString(a));
 57         check(a.length == 3);
 58         check(a[0] == testValues[0]);
 59         check(a[1] == testValues[1]);
 60         check(a[2] == testValues[2]);
 61         checkArray(a, int.class, 3, 0, 3);
 62         // negative test of testing framework:
 63         for (int bad = -2; bad < a.length; bad++) {
 64             try {
 65                 int[] aa = a.clone();
 66                 if (bad < 0)  aa = new int[4];
 67                 else          aa[bad] = 0;
 68                 ++muzzle;
 69                 // the following check should fail!
 70                 if (bad == -2)
 71                     checkArray(new String[3], int.class, 0, 0, a.length);

343     }
344 
345     // We must run all the our tests on each of 8 distinct primitive types,
346     // and two reference types (Object, String) for good measure.
347     // This would be a pain to write out by hand, statically typed.
348     // So, use reflection.  Following are the tables of methods we use.
349     // (The initial simple tests exercise enough of the static typing
350     // features of the API to ensure that they compile as advertised.)
351 
352     static Object  coerceToObject(int x) { return (x & 0xF) == 0? null: new Integer(x); }
353     static String  coerceToString(int x) { return (x == 0)? null: Integer.toHexString(x); }
354     static Integer coerceToInteger(int x) { return (x == 0)? null: x; }
355     static byte    coerceToByte(int x) { return (byte)x; }
356     static short   coerceToShort(int x) { return (short)x; }
357     static int     coerceToInt(int x) { return x; }
358     static long    coerceToLong(int x) { return x; }
359     static char    coerceToChar(int x) { return (char)x; }
360     static float   coerceToFloat(int x) { return x; }
361     static double  coerceToDouble(int x) { return x; }
362     static boolean coerceToBoolean(int x) { return (x&1) != 0; }
363     static Point coerceToPoint(int x) { return (x == 0) ? null : new Point(x, x); }
364     static LocalDate coerceToLocalDate(int x) { return (x == 0) ? null : LocalDate.ofEpochDay(x); }
365     static Optional coerceToOptional(int x) { return (x == 0) ? null : Optional.of(x); }
366 
367     static Integer[] copyOfIntegerArray(Object[] a, int len) {
368         // This guy exercises the API based on a type-token.
369         // Note the static typing.
370         return Arrays.copyOf(a, len, Integer[].class);
371     }
372     static Integer[] copyOfIntegerArrayRange(Object[] a, int m, int n) {
373         // This guy exercises the API based on a type-token.
374         // Note the static typing.
375         return Arrays.copyOfRange(a, m, n, Integer[].class);
376     }
377 
378     static final List<Class<?>> allTypes
379         = Arrays.asList(new Class<?>[]
380                         {   Object.class, String.class, Integer.class,
381                             byte.class, short.class, int.class, long.class,
382                             char.class, float.class, double.class,
383                             boolean.class, LocalDate.class, Optional.class,
384                             Point.class,
385                         });
386     static final HashMap<Class<?>,Method> coercers;
387     static final HashMap<Class<?>,Method> cloners;
388     static final HashMap<Class<?>,Method> cloneRangers;
389     static final HashMap<Class<?>,Method> toStrings;
390     static final HashMap<Class<?>,Object> nullValues;
391     static {
392         coercers = new HashMap<Class<?>,Method>();
393         Method[] testMethods = CopyMethods.class.getDeclaredMethods();
394         Method cia = null, ciar = null;
395         for (int i = 0; i < testMethods.length; i++) {
396             Method m = testMethods[i];
397             if (!Modifier.isStatic(m.getModifiers()))  continue;
398             Class<?> rt = m.getReturnType();
399             if (m.getName().startsWith("coerceTo") && allTypes.contains(rt))
400                 coercers.put(m.getReturnType(), m);
401             if (m.getName().equals("copyOfIntegerArray"))
402                 cia = m;
403             if (m.getName().equals("copyOfIntegerArrayRange"))
404                 ciar = m;

411             Method m = arrayMethods[i];
412             if (!Modifier.isStatic(m.getModifiers()))  continue;
413             Class<?> rt = m.getReturnType();
414             if (m.getName().equals("copyOf")
415                 && m.getParameterTypes().length == 2)
416                 cloners.put(rt.getComponentType(), m);
417             if (m.getName().equals("copyOfRange")
418                 && m.getParameterTypes().length == 3)
419                 cloneRangers.put(rt.getComponentType(), m);
420             if (m.getName().equals("toString")) {
421                 Class<?> pt = m.getParameterTypes()[0];
422                 toStrings.put(pt.getComponentType(), m);
423             }
424         }
425         cloners.put(String.class, cloners.get(Object.class));
426         cloneRangers.put(String.class, cloneRangers.get(Object.class));
427         assert(cia != null);
428         cloners.put(Integer.class, cia);
429         assert(ciar != null);
430         cloneRangers.put(Integer.class, ciar);
431         cloners.put(Point.class, cloners.get(Object.class));
432         cloneRangers.put(Point.class, cloneRangers.get(Object.class));
433         cloners.put(LocalDate.class, cloners.get(Object.class));
434         cloneRangers.put(LocalDate.class, cloneRangers.get(Object.class));
435         cloners.put(Optional.class, cloners.get(Object.class));
436         cloneRangers.put(Optional.class, cloneRangers.get(Object.class));
437         nullValues = new HashMap<Class<?>,Object>();
438         for (Class<?> c : allTypes) {
439             nullValues.put(c, invoke(coercers.get(c), 0));
440         }
441     }
442 }
< prev index next >