# HG changeset patch # User redestad # Date 1447111305 -3600 # Tue Nov 10 00:21:45 2015 +0100 # Node ID bee49d439785b025c49724255496fd31c2801d94 # Parent 6cfb4df38b6d76c15584fc46ca9a70b017f664dc imported patch wrappertest diff --git a/test/sun/invoke/util/WrapperTest.java b/test/sun/invoke/util/WrapperTest.java new file mode 100644 --- /dev/null +++ b/test/sun/invoke/util/WrapperTest.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package test.sun.invoke.util; + +import sun.invoke.util.ValueConversions; +import sun.invoke.util.Wrapper; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.invoke.MethodHandle; +import java.io.Serializable; +import java.util.Arrays; +import org.junit.Test; +import static org.junit.Assert.*; + +/* @test + * @summary unit tests for making sure Wrapper.zero doesn't leak + * @modules java.base/sun.invoke.util + * @compile -XDignore.symbol.file WrapperTest.java + * @run junit/othervm test.sun.invoke.util.WrapperTest + */ + +public class WrapperTest { + + @Test + public void testShortZeroConversion() throws Throwable { + MethodHandle h1 = MethodHandles.constant(Short.class, (short)42); + MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42 + MethodHandle h3 = h2.asType(MethodType.methodType(short.class)); // add 0 + MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box + + Object x = h4.invokeExact(); + assertEquals(x, (short)0); + assertTrue(x == Short.valueOf((short)0)); + assertFalse(x == Wrapper.SHORT.zero()); + } + + @Test + public void testIntZeroConversion() throws Throwable { + MethodHandle h1 = MethodHandles.constant(Integer.class, 42); + MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42 + MethodHandle h3 = h2.asType(MethodType.methodType(int.class)); // add 0 + MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box + + Object x = h4.invokeExact(); + assertEquals(x, 0); + assertTrue(x == Integer.valueOf(0)); + assertFalse(x == Wrapper.INT.zero()); + } + + @Test + public void testLongZeroConversion() throws Throwable { + MethodHandle h1 = MethodHandles.constant(Long.class, 42L); + MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42 + MethodHandle h3 = h2.asType(MethodType.methodType(long.class)); // add 0 + MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box + + Object x = h4.invokeExact(); + assertEquals(x, 0L); + assertTrue(x == Long.valueOf(0)); + assertFalse(x == Wrapper.LONG.zero()); + } + + @Test + public void testByteZeroConversion() throws Throwable { + MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42); + MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42 + MethodHandle h3 = h2.asType(MethodType.methodType(Byte.class)); // add 0 + MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box + + Object x = h4.invokeExact(); + assertEquals(x, null); + } + + @Test + public void testCharacterZeroConversion() throws Throwable { + MethodHandle h1 = MethodHandles.constant(Character.class, (char)42); + MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42 + MethodHandle h3 = h2.asType(MethodType.methodType(Character.class)); // add 0 + MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box + + Object x = h4.invokeExact(); + assertEquals(x, null); + } +}