1 /*
   2  * Copyright (c) 2010, 2011, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 /**
  26  * @test
  27  * @bug 6987555
  28  * @summary JSR 292 unboxing to a boolean value fails on big-endian SPARC
  29  *
  30  * @run main/othervm -Xint -ea -XX:+UnlockDiagnosticVMOptions -XX:+VerifyMethodHandles Test6987555
  31  */
  32 
  33 import java.lang.invoke.*;
  34 
  35 public class Test6987555 {
  36     private static final Class   CLASS = Test6987555.class;
  37     private static final String  NAME  = "foo";
  38     private static final boolean DEBUG = false;
  39 
  40     public static void main(String[] args) throws Throwable {
  41         testboolean();
  42         testbyte();
  43         testchar();
  44         testshort();
  45         testint();
  46     }
  47 
  48     // boolean
  49     static void testboolean() throws Throwable {
  50         doboolean(false);
  51         doboolean(true);
  52     }
  53     static void doboolean(boolean x) throws Throwable {
  54         if (DEBUG)  System.out.println("boolean=" + x);
  55         MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(boolean.class, boolean.class));
  56         MethodHandle mh2 = mh1.asType(MethodType.methodType(boolean.class, Boolean.class));
  57         boolean a = (boolean) mh1.invokeExact(x);
  58         boolean b = (boolean) mh2.invokeExact(Boolean.valueOf(x));
  59         assert a == b : a + " != " + b;
  60     }
  61 
  62     // byte
  63     static void testbyte() throws Throwable {
  64         byte[] a = new byte[] {
  65             Byte.MIN_VALUE,
  66             Byte.MIN_VALUE + 1,
  67             -0x0F,
  68             -1,
  69             0,
  70             1,
  71             0x0F,
  72             Byte.MAX_VALUE - 1,
  73             Byte.MAX_VALUE
  74         };
  75         for (int i = 0; i < a.length; i++) {
  76             dobyte(a[i]);
  77         }
  78     }
  79     static void dobyte(byte x) throws Throwable {
  80         if (DEBUG)  System.out.println("byte=" + x);
  81         MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(byte.class, byte.class));
  82         MethodHandle mh2 = mh1.asType(MethodType.methodType(byte.class, Byte.class));
  83         byte a = (byte) mh1.invokeExact(x);
  84         byte b = (byte) mh2.invokeExact(Byte.valueOf(x));
  85         assert a == b : a + " != " + b;
  86     }
  87 
  88     // char
  89     static void testchar() throws Throwable {
  90         char[] a = new char[] {
  91             Character.MIN_VALUE,
  92             Character.MIN_VALUE + 1,
  93             0x000F,
  94             0x00FF,
  95             0x0FFF,
  96             Character.MAX_VALUE - 1,
  97             Character.MAX_VALUE
  98         };
  99         for (int i = 0; i < a.length; i++) {
 100             dochar(a[i]);
 101         }
 102     }
 103     static void dochar(char x) throws Throwable {
 104         if (DEBUG)  System.out.println("char=" + x);
 105         MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(char.class, char.class));
 106         MethodHandle mh2 = mh1.asType(MethodType.methodType(char.class, Character.class));
 107         char a = (char) mh1.invokeExact(x);
 108         char b = (char) mh2.invokeExact(Character.valueOf(x));
 109         assert a == b : a + " != " + b;
 110     }
 111 
 112     // short
 113     static void testshort() throws Throwable {
 114         short[] a = new short[] {
 115             Short.MIN_VALUE,
 116             Short.MIN_VALUE + 1,
 117             -0x0FFF,
 118             -0x00FF,
 119             -0x000F,
 120             -1,
 121             0,
 122             1,
 123             0x000F,
 124             0x00FF,
 125             0x0FFF,
 126             Short.MAX_VALUE - 1,
 127             Short.MAX_VALUE
 128         };
 129         for (int i = 0; i < a.length; i++) {
 130             doshort(a[i]);
 131         }
 132     }
 133     static void doshort(short x) throws Throwable {
 134         if (DEBUG)  System.out.println("short=" + x);
 135         MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(short.class, short.class));
 136         MethodHandle mh2 = mh1.asType(MethodType.methodType(short.class, Short.class));
 137         short a = (short) mh1.invokeExact(x);
 138         short b = (short) mh2.invokeExact(Short.valueOf(x));
 139         assert a == b : a + " != " + b;
 140     }
 141 
 142     // int
 143     static void testint() throws Throwable {
 144         int[] a = new int[] {
 145             Integer.MIN_VALUE,
 146             Integer.MIN_VALUE + 1,
 147             -0x00000FFF,
 148             -0x000000FF,
 149             -0x0000000F,
 150             -1,
 151             0,
 152             1,
 153             0x0000000F,
 154             0x000000FF,
 155             0x00000FFF,
 156             Integer.MAX_VALUE - 1,
 157             Integer.MAX_VALUE
 158         };
 159         for (int i = 0; i < a.length; i++) {
 160             doint(a[i]);
 161         }
 162     }
 163     static void doint(int x) throws Throwable {
 164         if (DEBUG)  System.out.println("int=" + x);
 165         MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(int.class, int.class));
 166         MethodHandle mh2 = mh1.asType(MethodType.methodType(int.class, Integer.class));
 167         int a = (int) mh1.invokeExact(x);
 168         int b = (int) mh2.invokeExact(Integer.valueOf(x));
 169         assert a == b : a + " != " + b;
 170     }
 171 
 172     public static boolean foo(boolean i) { return i; }
 173     public static byte    foo(byte    i) { return i; }
 174     public static char    foo(char    i) { return i; }
 175     public static short   foo(short   i) { return i; }
 176     public static int     foo(int     i) { return i; }
 177 }