1 /*
2  * Copyright (c) 2015, 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 package test.sun.invoke.util;
25 
26 // Android-changed: Remove unused import.
27 // import sun.invoke.util.ValueConversions;
28 import sun.invoke.util.Wrapper;
29 import java.lang.invoke.MethodHandles;
30 import java.lang.invoke.MethodType;
31 import java.lang.invoke.MethodHandle;
32 // Android-changed: Remove unused imports.
33 // import java.io.Serializable;
34 // import java.util.Arrays;
35 import org.junit.Test;
36 import static org.junit.Assert.*;
37 
38 /* @test
39  * @summary unit tests to assert Wrapper zero identities and conversion behave correctly
40  * @modules java.base/sun.invoke.util
41  * @compile -XDignore.symbol.file WrapperTest.java
42  * @run junit test.sun.invoke.util.WrapperTest
43  */
44 public class WrapperTest {
45 
46     @Test
testShortZeroConversion()47     public void testShortZeroConversion() throws Throwable {
48         MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);
49         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
50         MethodHandle h3 = h2.asType(MethodType.methodType(short.class));  // add 0
51         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
52 
53         Object x = h4.invokeExact();
54         assertEquals(x, (short)0);
55         assertTrue(x == Short.valueOf((short)0));
56         assertTrue(x == Wrapper.SHORT.zero());
57     }
58 
59     @Test
testIntZeroConversion()60     public void testIntZeroConversion() throws Throwable {
61         MethodHandle h1 = MethodHandles.constant(Integer.class, 42);
62         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
63         MethodHandle h3 = h2.asType(MethodType.methodType(int.class));  // add 0
64         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
65 
66         Object x = h4.invokeExact();
67         assertEquals(x, 0);
68         assertTrue(x == Integer.valueOf(0));
69         assertTrue(x == Wrapper.INT.zero());
70     }
71 
72     @Test
testLongZeroConversion()73     public void testLongZeroConversion() throws Throwable {
74         MethodHandle h1 = MethodHandles.constant(Long.class, 42L);
75         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
76         MethodHandle h3 = h2.asType(MethodType.methodType(long.class));  // add 0
77         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
78 
79         Object x = h4.invokeExact();
80         assertEquals(x, 0L);
81         assertTrue(x == Long.valueOf(0));
82         assertTrue(x == Wrapper.LONG.zero());
83     }
84 
85     @Test
testByteZeroConversion()86     public void testByteZeroConversion() throws Throwable {
87         MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42);
88         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
89         MethodHandle h3 = h2.asType(MethodType.methodType(byte.class));  // add 0
90         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
91 
92         Object x = h4.invokeExact();
93         assertEquals(x, (byte)0);
94         assertTrue(x == Byte.valueOf((byte)0));
95         assertTrue(x == Wrapper.BYTE.zero());
96     }
97 
98     @Test
testCharacterZeroConversion()99     public void testCharacterZeroConversion() throws Throwable {
100         MethodHandle h1 = MethodHandles.constant(Character.class, (char)42);
101         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
102         MethodHandle h3 = h2.asType(MethodType.methodType(char.class));  // add 0
103         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
104 
105         Object x = h4.invokeExact();
106         assertEquals(x, (char)0);
107         assertTrue(x == Character.valueOf((char)0));
108         assertTrue(x == Wrapper.CHAR.zero());
109     }
110 }
111