1 /*
   2  * Copyright (c) 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang.runtime;
  26 
  27 import java.lang.invoke.MethodType;
  28 import java.util.Arrays;
  29 import java.util.Collections;
  30 import java.util.List;
  31 import java.util.Map;
  32 import java.util.Random;
  33 
  34 import org.testng.annotations.Test;
  35 
  36 import static org.testng.Assert.assertEquals;
  37 
  38 /**
  39  * @test
  40  * @key randomness
  41  * @run testng CarrierTest
  42  * @summary unit tests for java.lang.runtime.ExtractorCarriers
  43  */
  44 @Test
  45 public class CarrierTest {
  46     static final int N_ITER = 1000;
  47     static final Class<?>[] TYPES = { byte.class, short.class, char.class, int.class, long.class, float.class, double.class, boolean.class, Object.class };
  48 
  49     static Object[] byteVals = { Byte.MIN_VALUE, Byte.MAX_VALUE, (byte) -1, (byte) 0, (byte) 1, (byte) 42 };
  50     static Object[] shortVals = { Short.MIN_VALUE, Short.MAX_VALUE, (short) -1, (short) 0, (short) 1, (short) 42 };
  51     static Object[] charVals = { Character.MIN_VALUE, Character.MAX_VALUE, (char) 0, 'a', 'Z' };
  52     static Object[] intVals = { Integer.MIN_VALUE, Integer.MAX_VALUE, -1, 0, 1, 42 };
  53     static Object[] longVals = { Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L, 42L };
  54     static Object[] floatVals = { Float.MIN_VALUE, Float.MAX_VALUE, -1.0f, 0.0f, 1.0f, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN };
  55     static Object[] doubleVals = { Double.MIN_VALUE, Double.MAX_VALUE, -1.0d, 0.0d, 1.0d, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN };
  56     static Object[] booleanVals = { true, false };
  57     static Object[] objectVals = {null, "", "Foo", "foo", List.of(), Collections.EMPTY_SET };
  58 
  59     // @@@ Should use RandomFactory, but can't get that to link
  60     private static final Random random = new Random(System.nanoTime());
  61 
  62     static Map<Class<?>, Object[]> primVals = Map.of(byte.class, byteVals,
  63                                                      short.class, shortVals,
  64                                                      char.class, charVals,
  65                                                      int.class, intVals,
  66                                                      long.class, longVals,
  67                                                      float.class, floatVals,
  68                                                      double.class, doubleVals,
  69                                                      boolean.class, booleanVals);
  70 
  71     void testCarrier(MethodType type, Object[] values) throws Throwable {
  72         for (PatternCarriers.CarrierFactory cf : PatternCarriers.CarrierFactories.values()) {
  73             assertEquals(type.parameterCount(), values.length);
  74             Object carrier = cf.constructor(type).invokeWithArguments(values);
  75             for (int i = 0; i < values.length; i++)
  76                 assertEquals(values[i], cf.component(type, i).invoke(carrier));
  77         }
  78     }
  79 
  80     void testCarrier(MethodType type) throws Throwable {
  81         // generate data, in a loop
  82         for (int i=0; i<N_ITER; i++) {
  83             Object[] values = new Object[type.parameterCount()];
  84             for (int j=0; j<type.parameterCount(); j++) {
  85                 Class<?> c = type.parameterType(j);
  86                 Object[] vals = c.isPrimitive() ? primVals.get(c) : objectVals;
  87                 values[j] = vals[random.nextInt(vals.length)];
  88             }
  89             testCarrier(type, values);
  90         }
  91     }
  92 
  93     public void testCarrier() throws Throwable {
  94         Class[] lotsOfInts = new Class[252];
  95         Arrays.fill(lotsOfInts, int.class);
  96 
  97         // known types
  98         for (MethodType mt : List.of(
  99                 MethodType.methodType(Object.class),
 100                 MethodType.methodType(Object.class, int.class),
 101                 MethodType.methodType(Object.class, int.class, int.class),
 102                 MethodType.methodType(Object.class, Object.class),
 103                 MethodType.methodType(Object.class, Object.class, Object.class),
 104                 MethodType.methodType(Object.class, byte.class, short.class, char.class, int.class, long.class, float.class, double.class, boolean.class),
 105                 MethodType.methodType(Object.class, lotsOfInts))) {
 106             testCarrier(mt);
 107         }
 108 
 109         // random types
 110         for (int i=0; i<N_ITER; i++) {
 111             int nTypes = random.nextInt(10);
 112             Class[] paramTypes = new Class[nTypes];
 113             Arrays.setAll(paramTypes, ix -> TYPES[random.nextInt(TYPES.length)]);
 114             testCarrier(MethodType.methodType(Object.class, paramTypes));
 115         }
 116     }
 117 }