Path: blob/master/test/jdk/java/lang/invoke/6991596/Test6991596.java
41153 views
/*1* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 699159626* @summary JSR 292 unimplemented adapter_opt_i2i and adapter_opt_l2i on SPARC27*28* @run main/othervm -ea -XX:+UnlockDiagnosticVMOptions -XX:+VerifyMethodHandles Test699159629*/3031import java.lang.invoke.*;3233public class Test6991596 {34private static final Class CLASS = Test6991596.class;35private static final String NAME = "foo";36private static final boolean DEBUG = System.getProperty("DEBUG", "false").equals("true");3738public static void main(String[] args) throws Throwable {39testboolean();40testbyte();41testchar();42testshort();43testint();44testlong();45}4647// Helpers to get various methods.48static MethodHandle getmh1(Class ret, Class arg) throws ReflectiveOperationException {49return MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(ret, arg));50}51static MethodHandle getmh2(MethodHandle mh1, Class ret, Class arg) {52return MethodHandles.explicitCastArguments(mh1, MethodType.methodType(ret, arg));53}54static MethodHandle getmh3(MethodHandle mh1, Class ret, Class arg) {55return MethodHandles.explicitCastArguments(mh1, MethodType.methodType(ret, arg));56}5758// test adapter_opt_i2i59static void testboolean() throws Throwable {60boolean[] a = new boolean[] {61true,62false63};64for (int i = 0; i < a.length; i++) {65doboolean(a[i]);66}67}68static void doboolean(boolean x) throws Throwable {69if (DEBUG) System.out.println("boolean=" + x);7071// boolean72{73MethodHandle mh1 = getmh1( boolean.class, boolean.class);74MethodHandle mh2 = getmh2(mh1, boolean.class, boolean.class);75// TODO add this for all cases when the bugs are fixed.76//MethodHandle mh3 = getmh3(mh1, boolean.class, boolean.class);77boolean a = (boolean) mh1.invokeExact((boolean) x);78boolean b = (boolean) mh2.invokeExact(x);79//boolean c = mh3.<boolean>invokeExact((boolean) x);80check(x, a, b);81//check(x, c, x);82}8384// byte85{86MethodHandle mh1 = getmh1( byte.class, byte.class );87MethodHandle mh2 = getmh2(mh1, byte.class, boolean.class);88byte a = (byte) mh1.invokeExact((byte) (x ? 1 : 0));89byte b = (byte) mh2.invokeExact(x);90check(x, a, b);91}9293// char94{95MethodHandle mh1 = getmh1( char.class, char.class);96MethodHandle mh2 = getmh2(mh1, char.class, boolean.class);97char a = (char) mh1.invokeExact((char) (x ? 1 : 0));98char b = (char) mh2.invokeExact(x);99check(x, a, b);100}101102// short103{104MethodHandle mh1 = getmh1( short.class, short.class);105MethodHandle mh2 = getmh2(mh1, short.class, boolean.class);106short a = (short) mh1.invokeExact((short) (x ? 1 : 0));107short b = (short) mh2.invokeExact(x);108check(x, a, b);109}110}111112static void testbyte() throws Throwable {113byte[] a = new byte[] {114Byte.MIN_VALUE,115Byte.MIN_VALUE + 1,116-0x0F,117-1,1180,1191,1200x0F,121Byte.MAX_VALUE - 1,122Byte.MAX_VALUE123};124for (int i = 0; i < a.length; i++) {125dobyte(a[i]);126}127}128static void dobyte(byte x) throws Throwable {129if (DEBUG) System.out.println("byte=" + x);130131// boolean132{133MethodHandle mh1 = getmh1( boolean.class, boolean.class);134MethodHandle mh2 = getmh2(mh1, boolean.class, byte.class);135boolean a = (boolean) mh1.invokeExact((x & 1) == 1);136boolean b = (boolean) mh2.invokeExact(x);137check(x, a, b);138}139140// byte141{142MethodHandle mh1 = getmh1( byte.class, byte.class);143MethodHandle mh2 = getmh2(mh1, byte.class, byte.class);144byte a = (byte) mh1.invokeExact((byte) x);145byte b = (byte) mh2.invokeExact(x);146check(x, a, b);147}148149// char150{151MethodHandle mh1 = getmh1( char.class, char.class);152MethodHandle mh2 = getmh2(mh1, char.class, byte.class);153char a = (char) mh1.invokeExact((char) x);154char b = (char) mh2.invokeExact(x);155check(x, a, b);156}157158// short159{160MethodHandle mh1 = getmh1( short.class, short.class);161MethodHandle mh2 = getmh2(mh1, short.class, byte.class);162short a = (short) mh1.invokeExact((short) x);163short b = (short) mh2.invokeExact(x);164check(x, a, b);165}166}167168static void testchar() throws Throwable {169char[] a = new char[] {170Character.MIN_VALUE,171Character.MIN_VALUE + 1,1720x000F,1730x00FF,1740x0FFF,175Character.MAX_VALUE - 1,176Character.MAX_VALUE177};178for (int i = 0; i < a.length; i++) {179dochar(a[i]);180}181}182static void dochar(char x) throws Throwable {183if (DEBUG) System.out.println("char=" + x);184185// boolean186{187MethodHandle mh1 = getmh1( boolean.class, boolean.class);188MethodHandle mh2 = getmh2(mh1, boolean.class, char.class);189boolean a = (boolean) mh1.invokeExact((x & 1) == 1);190boolean b = (boolean) mh2.invokeExact(x);191check(x, a, b);192}193194// byte195{196MethodHandle mh1 = getmh1( byte.class, byte.class);197MethodHandle mh2 = getmh2(mh1, byte.class, char.class);198byte a = (byte) mh1.invokeExact((byte) x);199byte b = (byte) mh2.invokeExact(x);200check(x, a, b);201}202203// char204{205MethodHandle mh1 = getmh1( char.class, char.class);206MethodHandle mh2 = getmh2(mh1, char.class, char.class);207char a = (char) mh1.invokeExact((char) x);208char b = (char) mh2.invokeExact(x);209check(x, a, b);210}211212// short213{214MethodHandle mh1 = getmh1( short.class, short.class);215MethodHandle mh2 = getmh2(mh1, short.class, char.class);216short a = (short) mh1.invokeExact((short) x);217short b = (short) mh2.invokeExact(x);218check(x, a, b);219}220}221222static void testshort() throws Throwable {223short[] a = new short[] {224Short.MIN_VALUE,225Short.MIN_VALUE + 1,226-0x0FFF,227-0x00FF,228-0x000F,229-1,2300,2311,2320x000F,2330x00FF,2340x0FFF,235Short.MAX_VALUE - 1,236Short.MAX_VALUE237};238for (int i = 0; i < a.length; i++) {239doshort(a[i]);240}241}242static void doshort(short x) throws Throwable {243if (DEBUG) System.out.println("short=" + x);244245// boolean246{247MethodHandle mh1 = getmh1( boolean.class, boolean.class);248MethodHandle mh2 = getmh2(mh1, boolean.class, short.class);249boolean a = (boolean) mh1.invokeExact((x & 1) == 1);250boolean b = (boolean) mh2.invokeExact(x);251check(x, a, b);252}253254// byte255{256MethodHandle mh1 = getmh1( byte.class, byte.class);257MethodHandle mh2 = getmh2(mh1, byte.class, short.class);258byte a = (byte) mh1.invokeExact((byte) x);259byte b = (byte) mh2.invokeExact(x);260check(x, a, b);261}262263// char264{265MethodHandle mh1 = getmh1( char.class, char.class);266MethodHandle mh2 = getmh2(mh1, char.class, short.class);267char a = (char) mh1.invokeExact((char) x);268char b = (char) mh2.invokeExact(x);269check(x, a, b);270}271272// short273{274MethodHandle mh1 = getmh1( short.class, short.class);275MethodHandle mh2 = getmh2(mh1, short.class, short.class);276short a = (short) mh1.invokeExact((short) x);277short b = (short) mh2.invokeExact(x);278check(x, a, b);279}280}281282static void testint() throws Throwable {283int[] a = new int[] {284Integer.MIN_VALUE,285Integer.MIN_VALUE + 1,286-0x0FFFFFFF,287-0x00FFFFFF,288-0x000FFFFF,289-0x0000FFFF,290-0x00000FFF,291-0x000000FF,292-0x0000000F,293-1,2940,2951,2960x0000000F,2970x000000FF,2980x00000FFF,2990x0000FFFF,3000x000FFFFF,3010x00FFFFFF,3020x0FFFFFFF,303Integer.MAX_VALUE - 1,304Integer.MAX_VALUE305};306for (int i = 0; i < a.length; i++) {307doint(a[i]);308}309}310static void doint(int x) throws Throwable {311if (DEBUG) System.out.println("int=" + x);312313// boolean314{315MethodHandle mh1 = getmh1( boolean.class, boolean.class);316MethodHandle mh2 = getmh2(mh1, boolean.class, int.class);317boolean a = (boolean) mh1.invokeExact((x & 1) == 1);318boolean b = (boolean) mh2.invokeExact(x);319check(x, a, b);320}321322// byte323{324MethodHandle mh1 = getmh1( byte.class, byte.class);325MethodHandle mh2 = getmh2(mh1, byte.class, int.class);326byte a = (byte) mh1.invokeExact((byte) x);327byte b = (byte) mh2.invokeExact(x);328check(x, a, b);329}330331// char332{333MethodHandle mh1 = getmh1( char.class, char.class);334MethodHandle mh2 = getmh2(mh1, char.class, int.class);335char a = (char) mh1.invokeExact((char) x);336char b = (char) mh2.invokeExact(x);337check(x, a, b);338}339340// short341{342MethodHandle mh1 = getmh1( short.class, short.class);343MethodHandle mh2 = getmh2(mh1, short.class, int.class);344short a = (short) mh1.invokeExact((short) x);345short b = (short) mh2.invokeExact(x);346assert a == b : a + " != " + b;347check(x, a, b);348}349350// int351{352MethodHandle mh1 = getmh1( int.class, int.class);353MethodHandle mh2 = getmh2(mh1, int.class, int.class);354int a = (int) mh1.invokeExact((int) x);355int b = (int) mh2.invokeExact(x);356check(x, a, b);357}358}359360// test adapter_opt_l2i361static void testlong() throws Throwable {362long[] a = new long[] {363Long.MIN_VALUE,364Long.MIN_VALUE + 1,365-0x000000000FFFFFFFL,366-0x0000000000FFFFFFL,367-0x00000000000FFFFFL,368-0x000000000000FFFFL,369-0x0000000000000FFFL,370-0x00000000000000FFL,371-0x000000000000000FL,372-1L,3730L,3741L,3750x000000000000000FL,3760x00000000000000FFL,3770x0000000000000FFFL,3780x0000000000000FFFL,3790x000000000000FFFFL,3800x00000000000FFFFFL,3810x0000000000FFFFFFL,3820x000000000FFFFFFFL,383Long.MAX_VALUE - 1,384Long.MAX_VALUE385};386for (int i = 0; i < a.length; i++) {387dolong(a[i]);388}389}390static void dolong(long x) throws Throwable {391if (DEBUG) System.out.println("long=" + x);392393// boolean394{395MethodHandle mh1 = getmh1( boolean.class, boolean.class);396MethodHandle mh2 = getmh2(mh1, boolean.class, long.class);397boolean a = (boolean) mh1.invokeExact((x & 1L) == 1L);398boolean b = (boolean) mh2.invokeExact(x);399check(x, a, b);400}401402// byte403{404MethodHandle mh1 = getmh1( byte.class, byte.class);405MethodHandle mh2 = getmh2(mh1, byte.class, long.class);406byte a = (byte) mh1.invokeExact((byte) x);407byte b = (byte) mh2.invokeExact(x);408check(x, a, b);409}410411// char412{413MethodHandle mh1 = getmh1( char.class, char.class);414MethodHandle mh2 = getmh2(mh1, char.class, long.class);415char a = (char) mh1.invokeExact((char) x);416char b = (char) mh2.invokeExact(x);417check(x, a, b);418}419420// short421{422MethodHandle mh1 = getmh1( short.class, short.class);423MethodHandle mh2 = getmh2(mh1, short.class, long.class);424short a = (short) mh1.invokeExact((short) x);425short b = (short) mh2.invokeExact(x);426check(x, a, b);427}428429// int430{431MethodHandle mh1 = getmh1( int.class, int.class);432MethodHandle mh2 = getmh2(mh1, int.class, long.class);433int a = (int) mh1.invokeExact((int) x);434int b = (int) mh2.invokeExact(x);435check(x, a, b);436}437}438439static void check(boolean x, boolean e, boolean a) { p(z2h(x), z2h(e), z2h(a)); assert e == a : z2h(x) + ": " + z2h(e) + " != " + z2h(a); }440static void check(boolean x, byte e, byte a) { p(z2h(x), i2h(e), i2h(a)); assert e == a : z2h(x) + ": " + i2h(e) + " != " + i2h(a); }441static void check(boolean x, int e, int a) { p(z2h(x), i2h(e), i2h(a)); assert e == a : z2h(x) + ": " + i2h(e) + " != " + i2h(a); }442443static void check(int x, boolean e, boolean a) { p(i2h(x), z2h(e), z2h(a)); assert e == a : i2h(x) + ": " + z2h(e) + " != " + z2h(a); }444static void check(int x, byte e, byte a) { p(i2h(x), i2h(e), i2h(a)); assert e == a : i2h(x) + ": " + i2h(e) + " != " + i2h(a); }445static void check(int x, int e, int a) { p(i2h(x), i2h(e), i2h(a)); assert e == a : i2h(x) + ": " + i2h(e) + " != " + i2h(a); }446447static void check(long x, boolean e, boolean a) { p(l2h(x), z2h(e), z2h(a)); assert e == a : l2h(x) + ": " + z2h(e) + " != " + z2h(a); }448static void check(long x, byte e, byte a) { p(l2h(x), i2h(e), i2h(a)); assert e == a : l2h(x) + ": " + i2h(e) + " != " + i2h(a); }449static void check(long x, int e, int a) { p(l2h(x), i2h(e), i2h(a)); assert e == a : l2h(x) + ": " + i2h(e) + " != " + i2h(a); }450451static void p(String x, String e, String a) { if (DEBUG) System.out.println(x + ": expected: " + e + ", actual: " + a); }452453static String z2h(boolean x) { return x ? "1" : "0"; }454static String i2h(int x) { return Integer.toHexString(x); }455static String l2h(long x) { return Long.toHexString(x); }456457// to int458public static boolean foo(boolean i) { return i; }459public static byte foo(byte i) { return i; }460public static char foo(char i) { return i; }461public static short foo(short i) { return i; }462public static int foo(int i) { return i; }463}464465466