Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/invoke/util/ValueConversionsTest.java
38839 views
/*1* Copyright (c) 2009, 2013, 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 for value-type conversion utilities37* @compile -XDignore.symbol.file ValueConversionsTest.java38* @run junit/othervm test.sun.invoke.util.ValueConversionsTest39*/4041/**42*43* @author jrose44*/45public class ValueConversionsTest {46@Test47public void testUnbox() throws Throwable {48testUnbox(false);49}5051@Test52public void testUnboxCast() throws Throwable {53testUnbox(true);54}5556private void testUnbox(boolean doCast) throws Throwable {57for (Wrapper dst : Wrapper.values()) {58for (Wrapper src : Wrapper.values()) {59testUnbox(doCast, dst, src);60}61}62}6364private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {65boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);66if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims67if (dst == Wrapper.VOID || src == Wrapper.VOID ) return; // must have values68if (dst == Wrapper.OBJECT)69expectThrow = false; // everything (even VOID==null here) converts to OBJECT70try {71for (int n = -5; n < 10; n++) {72Object box = src.wrap(n);73switch (src) {74case VOID: assertEquals(box, null); break;75case OBJECT: box = box.toString(); break;76case SHORT: assertEquals(box.getClass(), Short.class); break;77default: assertEquals(box.getClass(), src.wrapperType()); break;78}79MethodHandle unboxer;80if (doCast)81unboxer = ValueConversions.unboxCast(dst);82else83unboxer = ValueConversions.unboxWiden(dst);84Object expResult = (box == null) ? dst.zero() : dst.wrap(box);85Object result = null;86switch (dst) {87case INT: result = (int) unboxer.invokeExact(box); break;88case LONG: result = (long) unboxer.invokeExact(box); break;89case FLOAT: result = (float) unboxer.invokeExact(box); break;90case DOUBLE: result = (double) unboxer.invokeExact(box); break;91case CHAR: result = (char) unboxer.invokeExact(box); break;92case BYTE: result = (byte) unboxer.invokeExact(box); break;93case SHORT: result = (short) unboxer.invokeExact(box); break;94case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;95}96if (expectThrow) {97expResult = "(need an exception)";98}99assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),100expResult, result);101}102} catch (RuntimeException ex) {103if (expectThrow) return;104System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));105throw ex;106}107}108109@Test110public void testBox() throws Throwable {111for (Wrapper w : Wrapper.values()) {112if (w == Wrapper.VOID) continue; // skip this; no unboxed form113if (w == Wrapper.OBJECT) continue; // skip this; already unboxed114for (int n = -5; n < 10; n++) {115Object box = w.wrap(n);116MethodHandle boxer = ValueConversions.boxExact(w);117Object expResult = box;118Object result = null;119switch (w) {120case INT: result = (Integer) boxer.invokeExact(/*int*/n); break;121case LONG: result = (Long) boxer.invokeExact((long)n); break;122case FLOAT: result = (Float) boxer.invokeExact((float)n); break;123case DOUBLE: result = (Double) boxer.invokeExact((double)n); break;124case CHAR: result = (Character) boxer.invokeExact((char)n); break;125case BYTE: result = (Byte) boxer.invokeExact((byte)n); break;126case SHORT: result = (Short) boxer.invokeExact((short)n); break;127case BOOLEAN: result = (Boolean) boxer.invokeExact((n & 1) != 0); break;128}129assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),130expResult, result);131}132}133}134135@Test136public void testCast() throws Throwable {137Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };138Object[] objects = { new Object(), Boolean.FALSE, "hello", (Long)12L, (Integer)6 };139for (Class<?> dst : types) {140MethodHandle caster = ValueConversions.cast().bindTo(dst);141assertEquals(caster.type(), MethodHandles.identity(Object.class).type());142for (Object obj : objects) {143Class<?> src = obj.getClass();144boolean canCast = dst.isAssignableFrom(src);145try {146Object result = caster.invokeExact(obj);147if (canCast)148assertEquals(obj, result);149else150assertEquals("cast should not have succeeded", dst, obj);151} catch (ClassCastException ex) {152if (canCast)153throw ex;154}155}156}157}158159@Test160public void testConvert() throws Throwable {161for (long tval = 0, ctr = 0;;) {162if (++ctr > 99999) throw new AssertionError("too many test values");163// prints 3776 test patterns (3776 = 8*59*8)164tval = nextTestValue(tval);165if (tval == 0) {166break; // repeat167}168}169for (Wrapper src : Wrapper.values()) {170for (Wrapper dst : Wrapper.values()) {171testConvert(src, dst, 0);172}173}174}175static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {176if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims177if (dst == Wrapper.VOID || src == Wrapper.VOID ) return; // must have values178boolean testSingleCase = (tval != 0);179final long tvalInit = tval;180MethodHandle conv = ValueConversions.convertPrimitive(src, dst);181MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());182assertEquals(convType, conv.type());183MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));184for (;;) {185long n = tval;186Object testValue = src.wrap(n);187Object expResult = dst.cast(testValue, dst.primitiveType());188Object result;189switch (src) {190case INT: result = converter.invokeExact((int)n); break;191case LONG: result = converter.invokeExact(/*long*/n); break;192case FLOAT: result = converter.invokeExact((float)n); break;193case DOUBLE: result = converter.invokeExact((double)n); break;194case CHAR: result = converter.invokeExact((char)n); break;195case BYTE: result = converter.invokeExact((byte)n); break;196case SHORT: result = converter.invokeExact((short)n); break;197case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;198default: throw new AssertionError();199}200assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),201expResult, result);202if (testSingleCase) break;203// next test value:204tval = nextTestValue(tval);205if (tval == tvalInit) break; // repeat206}207}208static long tweakSign(long x) {209// Assuming that x is mostly zeroes, make those zeroes follow bit #62 (just below the sign).210// This function is self-inverse.211final long MID_SIGN_BIT = 62;212long sign = -((x >>> MID_SIGN_BIT) & 1); // all ones or all zeroes213long flip = (sign >>> -MID_SIGN_BIT); // apply the sign below the mid-bit214return x ^ flip;215}216static long nextTestValue(long x) {217// Produce 64 bits with three component bitfields: [ high:3 | mid:58 | low:3 ].218// The high and low fields vary through all possible bit patterns.219// The middle field is either all zero or has a single bit set.220// For better coverage of the neighborhood of zero, an internal sign bit is xored downward also.221long ux = tweakSign(x); // unsign the middle field222final long LOW_BITS = 3, LOW_BITS_MASK = (1L << LOW_BITS)-1;223final long HIGH_BITS = 3, HIGH_BITS_MASK = ~(-1L >>> HIGH_BITS);224if ((ux & LOW_BITS_MASK) != LOW_BITS_MASK) {225++ux;226} else {227ux &= ~LOW_BITS_MASK;228long midBit = (ux & ~HIGH_BITS_MASK);229if (midBit == 0)230midBit = (1L<<LOW_BITS); // introduce a low bit231ux += midBit;232}233return tweakSign(ux);234}235}236237238