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