Path: blob/master/test/jdk/sun/invoke/util/WrapperTest.java
41149 views
/*1* Copyright (c) 2015, 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*/2223package test.sun.invoke.util;2425import sun.invoke.util.ValueConversions;26import sun.invoke.util.Wrapper;27import java.lang.invoke.MethodHandles;28import java.lang.invoke.MethodType;29import java.lang.invoke.MethodHandle;30import java.io.Serializable;31import java.util.Arrays;32import org.junit.Test;33import static org.junit.Assert.*;3435/* @test36* @summary unit tests to assert Wrapper zero identities and conversion behave correctly37* @modules java.base/sun.invoke.util38* @compile -XDignore.symbol.file WrapperTest.java39* @run junit test.sun.invoke.util.WrapperTest40*/41public class WrapperTest {4243@Test44public void testShortZeroConversion() throws Throwable {45MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);46MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 4247MethodHandle h3 = h2.asType(MethodType.methodType(short.class)); // add 048MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box4950Object x = h4.invokeExact();51assertEquals(x, (short)0);52assertTrue(x == Short.valueOf((short)0));53assertTrue(x == Wrapper.SHORT.zero());54}5556@Test57public void testIntZeroConversion() throws Throwable {58MethodHandle h1 = MethodHandles.constant(Integer.class, 42);59MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 4260MethodHandle h3 = h2.asType(MethodType.methodType(int.class)); // add 061MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box6263Object x = h4.invokeExact();64assertEquals(x, 0);65assertTrue(x == Integer.valueOf(0));66assertTrue(x == Wrapper.INT.zero());67}6869@Test70public void testLongZeroConversion() throws Throwable {71MethodHandle h1 = MethodHandles.constant(Long.class, 42L);72MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 4273MethodHandle h3 = h2.asType(MethodType.methodType(long.class)); // add 074MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box7576Object x = h4.invokeExact();77assertEquals(x, 0L);78assertTrue(x == Long.valueOf(0));79assertTrue(x == Wrapper.LONG.zero());80}8182@Test83public void testByteZeroConversion() throws Throwable {84MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42);85MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 4286MethodHandle h3 = h2.asType(MethodType.methodType(byte.class)); // add 087MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box8889Object x = h4.invokeExact();90assertEquals(x, (byte)0);91assertTrue(x == Byte.valueOf((byte)0));92assertTrue(x == Wrapper.BYTE.zero());93}9495@Test96public void testCharacterZeroConversion() throws Throwable {97MethodHandle h1 = MethodHandles.constant(Character.class, (char)42);98MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 4299MethodHandle h3 = h2.asType(MethodType.methodType(char.class)); // add 0100MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box101102Object x = h4.invokeExact();103assertEquals(x, (char)0);104assertTrue(x == Character.valueOf((char)0));105assertTrue(x == Wrapper.CHAR.zero());106}107}108109110