jdk/test/java/lang/invoke/MaxTest.java
changeset 13610 28122b96858e
parent 13460 c7aa5cca1c01
child 13611 f40b6b113fab
child 13612 275711a5df25
equal deleted inserted replaced
13460:c7aa5cca1c01 13610:28122b96858e
     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 
       
    26 /* @test
       
    27  * @summary BoundMethodHandle tests with primitive types
       
    28  * @compile MaxTest.java
       
    29  * @run junit/othervm test.java.lang.invoke.MaxTest
       
    30  */
       
    31 
       
    32 package test.java.lang.invoke;
       
    33 
       
    34 import static org.junit.Assert.assertEquals;
       
    35 
       
    36 import java.lang.invoke.MethodHandle;
       
    37 import java.lang.invoke.MethodHandles;
       
    38 import java.lang.invoke.MethodType;
       
    39 
       
    40 import org.junit.Test;
       
    41 
       
    42 public class MaxTest {
       
    43 
       
    44     static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
       
    45 
       
    46     private MethodHandle getMax(Class<?> t) throws Throwable {
       
    47         return LOOKUP.findStatic(Math.class, "max", MethodType.methodType(t, t, t));
       
    48     }
       
    49 
       
    50     static int ITERATION_COUNT = 40000;
       
    51     static {
       
    52         String iterations = System.getProperty(MaxTest.class.getSimpleName() + ".ITERATION_COUNT");
       
    53         if (iterations == null) {
       
    54             iterations = System.getProperty(MaxTest.class.getName() + ".ITERATION_COUNT");
       
    55         }
       
    56         if (iterations != null) {
       
    57             ITERATION_COUNT = Integer.parseInt(iterations);
       
    58         }
       
    59     }
       
    60 
       
    61     @Test
       
    62     public void testMaxLong() throws Throwable {
       
    63         final Class<?> C = long.class;
       
    64         final long P = 23L;
       
    65         final long Q = 42L;
       
    66         final long R = Math.max(P, Q);
       
    67         for (int i = 0; i < ITERATION_COUNT; ++i) {
       
    68             MethodHandle h = getMax(C);
       
    69             assertEquals((long) h.invokeExact(P, Q), R);
       
    70             MethodHandle bh = MethodHandles.insertArguments(h, 0, P);
       
    71             assertEquals((long) bh.invokeExact(Q), R);
       
    72             MethodHandle bbh = MethodHandles.insertArguments(bh, 0, Q);
       
    73             assertEquals((long) bbh.invokeExact(), R);
       
    74             MethodHandle b2h = MethodHandles.insertArguments(h, 1, Q);
       
    75             assertEquals((long) b2h.invokeExact(P), R);
       
    76             MethodHandle bb2h = MethodHandles.insertArguments(b2h, 0, P);
       
    77             assertEquals((long) bb2h.invokeExact(), R);
       
    78         }
       
    79     }
       
    80 
       
    81     @Test
       
    82     public void testMaxInt() throws Throwable {
       
    83         final Class<?> C = int.class;
       
    84         final int P = 23;
       
    85         final int Q = 42;
       
    86         final int R = Math.max(P, Q);
       
    87         for (int i = 0; i < ITERATION_COUNT; ++i) {
       
    88             MethodHandle h = getMax(C);
       
    89             assertEquals((int) h.invokeExact(P, Q), R);
       
    90             MethodHandle bh = MethodHandles.insertArguments(h, 0, P);
       
    91             assertEquals((int) bh.invokeExact(Q), R);
       
    92             MethodHandle bbh = MethodHandles.insertArguments(bh, 0, Q);
       
    93             assertEquals((int) bbh.invokeExact(), R);
       
    94             MethodHandle b2h = MethodHandles.insertArguments(h, 1, Q);
       
    95             assertEquals((int) b2h.invokeExact(P), R);
       
    96             MethodHandle bb2h = MethodHandles.insertArguments(b2h, 0, P);
       
    97             assertEquals((int) bb2h.invokeExact(), R);
       
    98         }
       
    99     }
       
   100 
       
   101     @Test
       
   102     public void testMaxFloat() throws Throwable {
       
   103         final Class<?> C = float.class;
       
   104         final float P = 23F;
       
   105         final float Q = 42F;
       
   106         final float R = Math.max(P, Q);
       
   107         final float D = 0.1F;
       
   108         for (int i = 0; i < ITERATION_COUNT; ++i) {
       
   109             MethodHandle h = getMax(C);
       
   110             assertEquals((float) h.invokeExact(P, Q), R, D);
       
   111             MethodHandle bh = MethodHandles.insertArguments(h, 0, P);
       
   112             assertEquals((float) bh.invokeExact(Q), R, D);
       
   113             MethodHandle bbh = MethodHandles.insertArguments(bh, 0, Q);
       
   114             assertEquals((float) bbh.invokeExact(), R, D);
       
   115             MethodHandle b2h = MethodHandles.insertArguments(h, 1, Q);
       
   116             assertEquals((float) b2h.invokeExact(P), R, D);
       
   117             MethodHandle bb2h = MethodHandles.insertArguments(b2h, 0, P);
       
   118             assertEquals((float) bb2h.invokeExact(), R, D);
       
   119         }
       
   120     }
       
   121 
       
   122     @Test
       
   123     public void testMaxDouble() throws Throwable {
       
   124         final Class<?> C = double.class;
       
   125         final double P = 23F;
       
   126         final double Q = 42F;
       
   127         final double R = Math.max(P, Q);
       
   128         final double D = 0.1;
       
   129         for (int i = 0; i < ITERATION_COUNT; ++i) {
       
   130             MethodHandle h = getMax(C);
       
   131             assertEquals((double) h.invokeExact(P, Q), R, D);
       
   132             MethodHandle bh = MethodHandles.insertArguments(h, 0, P);
       
   133             assertEquals((double) bh.invokeExact(Q), R, D);
       
   134             MethodHandle bbh = MethodHandles.insertArguments(bh, 0, Q);
       
   135             assertEquals((double) bbh.invokeExact(), R, D);
       
   136             MethodHandle b2h = MethodHandles.insertArguments(h, 1, Q);
       
   137             assertEquals((double) b2h.invokeExact(P), R, D);
       
   138             MethodHandle bb2h = MethodHandles.insertArguments(b2h, 0, P);
       
   139             assertEquals((double) bb2h.invokeExact(), R, D);
       
   140         }
       
   141     }
       
   142 
       
   143 }