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.compiler;
  26 
  27 import java.lang.invoke.MethodHandle;
  28 import java.lang.invoke.MethodType;
  29 import java.util.Arrays;
  30 import java.util.Collections;
  31 import java.util.List;
  32 import java.util.Map;
  33 import java.util.Random;
  34 
  35 import org.testng.annotations.Test;
  36 
  37 import static org.testng.Assert.assertEquals;
  38 
  39 /**
  40  * @test
  41  * @key randomness
  42  * @run testng CarrierTest
  43  * @summary unit tests for java.lang.compiler.ExtractorCarriers
  44  */
  45 @Test
  46 public class CarrierTest {
  47     static final int N_ITER = 1000;
  48     static final Class<?>[] TYPES = { byte.class, short.class, char.class, int.class, long.class, float.class, double.class, boolean.class, Object.class };
  49 
  50     static Object[] byteVals = { Byte.MIN_VALUE, Byte.MAX_VALUE, (byte) -1, (byte) 0, (byte) 1, (byte) 42 };
  51     static Object[] shortVals = { Short.MIN_VALUE, Short.MAX_VALUE, (short) -1, (short) 0, (short) 1, (short) 42 };
  52     static Object[] charVals = { Character.MIN_VALUE, Character.MAX_VALUE, (char) 0, 'a', 'Z' };
  53     static Object[] intVals = { Integer.MIN_VALUE, Integer.MAX_VALUE, -1, 0, 1, 42 };
  54     static Object[] longVals = { Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L, 42L };
  55     static Object[] floatVals = { Float.MIN_VALUE, Float.MAX_VALUE, -1.0f, 0.0f, 1.0f, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN };
  56     static Object[] doubleVals = { Double.MIN_VALUE, Double.MAX_VALUE, -1.0d, 0.0d, 1.0d, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN };
  57     static Object[] booleanVals = { true, false };
  58     static Object[] objectVals = {null, "", "Foo", "foo", List.of(), Collections.EMPTY_SET };
  59 
  60     // @@@ Should use RandomFactory, but can't get that to link
  61     private static final Random random = new Random(System.nanoTime());
  62 
  63     static Map<Class<?>, Object[]> primVals = Map.of(byte.class, byteVals,
  64                                                      short.class, shortVals,
  65                                                      char.class, charVals,
  66                                                      int.class, intVals,
  67                                                      long.class, longVals,
  68                                                      float.class, floatVals,
  69                                                      double.class, doubleVals,
  70                                                      boolean.class, booleanVals);
  71 
  72     void testCarrier(MethodType type, Object[] values) throws Throwable {
  73         for (ExtractorCarriers.CarrierFactory cf : ExtractorCarriers.CarrierFactories.values()) {
  74             assertEquals(type.parameterCount(), values.length);
  75             Object carrier = cf.constructor(type).invokeWithArguments(values);
  76             for (int i = 0; i < values.length; i++)
  77                 assertEquals(values[i], cf.component(type, i).invoke(carrier));
  78         }
  79     }
  80 
  81     void testCarrier(MethodType type) throws Throwable {
  82         // generate data, in a loop
  83         for (int i=0; i<N_ITER; i++) {
  84             Object[] values = new Object[type.parameterCount()];
  85             for (int j=0; j<type.parameterCount(); j++) {
  86                 Class<?> c = type.parameterType(j);
  87                 Object[] vals = c.isPrimitive() ? primVals.get(c) : objectVals;
  88                 values[j] = vals[random.nextInt(vals.length)];
  89             }
  90             testCarrier(type, values);
  91         }
  92     }
  93 
  94     public void testCarrier() throws Throwable {
  95         Class[] lotsOfInts = new Class[252];
  96         Arrays.fill(lotsOfInts, int.class);
  97 
  98         // known types
  99         for (MethodType mt : List.of(
 100                 MethodType.methodType(Object.class),
 101                 MethodType.methodType(Object.class, int.class),
 102                 MethodType.methodType(Object.class, int.class, int.class),
 103                 MethodType.methodType(Object.class, Object.class),
 104                 MethodType.methodType(Object.class, Object.class, Object.class),
 105                 MethodType.methodType(Object.class, byte.class, short.class, char.class, int.class, long.class, float.class, double.class, boolean.class),
 106                 MethodType.methodType(Object.class, lotsOfInts))) {
 107             testCarrier(mt);
 108         }
 109 
 110         // random types
 111         for (int i=0; i<N_ITER; i++) {
 112             int nTypes = random.nextInt(10);
 113             Class[] paramTypes = new Class[nTypes];
 114             Arrays.setAll(paramTypes, ix -> TYPES[random.nextInt(TYPES.length)]);
 115             testCarrier(MethodType.methodType(Object.class, paramTypes));
 116         }
 117     }
 118 }