Path: blob/master/test/jdk/sun/invoke/util/ValueConversionsTest.java
41149 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* @modules java.base/sun.invoke.util38* @compile -XDignore.symbol.file ValueConversionsTest.java39* @run junit/othervm test.sun.invoke.util.ValueConversionsTest40*/4142/**43*44* @author jrose45*/46public class ValueConversionsTest {47@Test48public void testUnbox() throws Throwable {49testUnbox(false);50}5152@Test53public void testUnboxCast() throws Throwable {54testUnbox(true);55}5657private void testUnbox(boolean doCast) throws Throwable {58for (Wrapper dst : Wrapper.values()) {59for (Wrapper src : Wrapper.values()) {60testUnbox(doCast, dst, src);61}62}63}6465private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {66boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);67if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims68if (dst == Wrapper.VOID || src == Wrapper.VOID ) return; // must have values69if (dst == Wrapper.OBJECT)70expectThrow = false; // everything (even VOID==null here) converts to OBJECT71try {72for (int n = -5; n < 10; n++) {73Object box = src.wrap(n);74switch (src) {75case VOID: assertEquals(box, null); break;76case OBJECT: box = box.toString(); break;77case SHORT: assertEquals(box.getClass(), Short.class); break;78default: assertEquals(box.getClass(), src.wrapperType()); break;79}80MethodHandle unboxer;81if (doCast)82unboxer = ValueConversions.unboxCast(dst);83else84unboxer = ValueConversions.unboxWiden(dst);85Object expResult = (box == null) ? dst.zero() : dst.wrap(box);86Object result = null;87switch (dst) {88case INT: result = (int) unboxer.invokeExact(box); break;89case LONG: result = (long) unboxer.invokeExact(box); break;90case FLOAT: result = (float) unboxer.invokeExact(box); break;91case DOUBLE: result = (double) unboxer.invokeExact(box); break;92case CHAR: result = (char) unboxer.invokeExact(box); break;93case BYTE: result = (byte) unboxer.invokeExact(box); break;94case SHORT: result = (short) unboxer.invokeExact(box); break;95case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;96}97if (expectThrow) {98expResult = "(need an exception)";99}100assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),101expResult, result);102}103} catch (RuntimeException ex) {104if (expectThrow) return;105System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));106throw ex;107}108}109110@Test111public void testBox() throws Throwable {112for (Wrapper w : Wrapper.values()) {113if (w == Wrapper.VOID) continue; // skip this; no unboxed form114if (w == Wrapper.OBJECT) continue; // skip this; already unboxed115for (int n = -5; n < 10; n++) {116Object box = w.wrap(n);117MethodHandle boxer = ValueConversions.boxExact(w);118Object expResult = box;119Object result = null;120switch (w) {121case INT: result = (Integer) boxer.invokeExact(/*int*/n); break;122case LONG: result = (Long) boxer.invokeExact((long)n); break;123case FLOAT: result = (Float) boxer.invokeExact((float)n); break;124case DOUBLE: result = (Double) boxer.invokeExact((double)n); break;125case CHAR: result = (Character) boxer.invokeExact((char)n); break;126case BYTE: result = (Byte) boxer.invokeExact((byte)n); break;127case SHORT: result = (Short) boxer.invokeExact((short)n); break;128case BOOLEAN: result = (Boolean) boxer.invokeExact((n & 1) != 0); break;129}130assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),131expResult, result);132}133}134}135136@Test137public void testCast() throws Throwable {138Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };139Object[] objects = { new Object(), Boolean.FALSE, "hello", (Long)12L, (Integer)6 };140for (Class<?> dst : types) {141MethodHandle caster = ValueConversions.cast().bindTo(dst);142assertEquals(caster.type(), MethodHandles.identity(Object.class).type());143for (Object obj : objects) {144Class<?> src = obj.getClass();145boolean canCast = dst.isAssignableFrom(src);146try {147Object result = caster.invokeExact(obj);148if (canCast)149assertEquals(obj, result);150else151assertEquals("cast should not have succeeded", dst, obj);152} catch (ClassCastException ex) {153if (canCast)154throw ex;155}156}157}158}159160@Test161public void testConvert() throws Throwable {162for (long tval = 0, ctr = 0;;) {163if (++ctr > 99999) throw new AssertionError("too many test values");164// prints 3776 test patterns (3776 = 8*59*8)165tval = nextTestValue(tval);166if (tval == 0) {167break; // repeat168}169}170for (Wrapper src : Wrapper.values()) {171for (Wrapper dst : Wrapper.values()) {172testConvert(src, dst, 0);173}174}175}176static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {177if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims178if (dst == Wrapper.VOID || src == Wrapper.VOID ) return; // must have values179boolean testSingleCase = (tval != 0);180final long tvalInit = tval;181MethodHandle conv = ValueConversions.convertPrimitive(src, dst);182MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());183assertEquals(convType, conv.type());184MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));185for (;;) {186long n = tval;187Object testValue = src.wrap(n);188Object expResult = dst.cast(testValue, dst.primitiveType());189Object result;190switch (src) {191case INT: result = converter.invokeExact((int)n); break;192case LONG: result = converter.invokeExact(/*long*/n); break;193case FLOAT: result = converter.invokeExact((float)n); break;194case DOUBLE: result = converter.invokeExact((double)n); break;195case CHAR: result = converter.invokeExact((char)n); break;196case BYTE: result = converter.invokeExact((byte)n); break;197case SHORT: result = converter.invokeExact((short)n); break;198case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;199default: throw new AssertionError();200}201assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),202expResult, result);203if (testSingleCase) break;204// next test value:205tval = nextTestValue(tval);206if (tval == tvalInit) break; // repeat207}208}209static long tweakSign(long x) {210// Assuming that x is mostly zeroes, make those zeroes follow bit #62 (just below the sign).211// This function is self-inverse.212final long MID_SIGN_BIT = 62;213long sign = -((x >>> MID_SIGN_BIT) & 1); // all ones or all zeroes214long flip = (sign >>> -MID_SIGN_BIT); // apply the sign below the mid-bit215return x ^ flip;216}217static long nextTestValue(long x) {218// Produce 64 bits with three component bitfields: [ high:3 | mid:58 | low:3 ].219// The high and low fields vary through all possible bit patterns.220// The middle field is either all zero or has a single bit set.221// For better coverage of the neighborhood of zero, an internal sign bit is xored downward also.222long ux = tweakSign(x); // unsign the middle field223final long LOW_BITS = 3, LOW_BITS_MASK = (1L << LOW_BITS)-1;224final long HIGH_BITS = 3, HIGH_BITS_MASK = ~(-1L >>> HIGH_BITS);225if ((ux & LOW_BITS_MASK) != LOW_BITS_MASK) {226++ux;227} else {228ux &= ~LOW_BITS_MASK;229long midBit = (ux & ~HIGH_BITS_MASK);230if (midBit == 0)231midBit = (1L<<LOW_BITS); // introduce a low bit232ux += midBit;233}234return tweakSign(ux);235}236}237238239