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