jdk/test/sun/invoke/util/ValueConversionsTest.java
author vlivanov
Wed, 10 Sep 2014 19:19:47 +0400
changeset 26466 3bbb6a284bd4
parent 23010 6dadb192ad81
child 26474 655d08549e43
permissions -rw-r--r--
8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke Reviewed-by: psandoz, iignatyev
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 19075
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     4
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     8
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    13
 * accompanied this code).
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    14
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    18
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    21
 * questions.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    22
 */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    23
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    24
package test.sun.invoke.util;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    25
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    26
import sun.invoke.util.ValueConversions;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    27
import sun.invoke.util.Wrapper;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 23010
diff changeset
    28
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    29
import java.lang.invoke.MethodType;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    30
import java.lang.invoke.MethodHandle;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    31
import java.io.Serializable;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    32
import java.util.Arrays;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    33
import org.junit.Test;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    34
import static org.junit.Assert.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    35
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    36
/* @test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    37
 * @summary unit tests for value-type conversion utilities
19075
63e635266eec 8006324: [TEST_BUG] sun/invoke/util/ValueConversionsTest.java should be modified
rbackman
parents: 14342
diff changeset
    38
 * @compile -XDignore.symbol.file ValueConversionsTest.java
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    39
 * @run junit/othervm test.sun.invoke.util.ValueConversionsTest
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    40
 */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    41
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    42
/**
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    43
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    44
 * @author jrose
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    45
 */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    46
public class ValueConversionsTest {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    47
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    48
    public void testUnbox() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    49
        testUnbox(false);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    50
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    51
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    52
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    53
    public void testUnboxCast() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    54
        testUnbox(true);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    55
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    56
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    57
    private void testUnbox(boolean doCast) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    58
        for (Wrapper dst : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    59
            for (Wrapper src : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    60
                testUnbox(doCast, dst, src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    61
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    62
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    63
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    64
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    65
    private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    66
        boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    67
        if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    68
        if (dst == Wrapper.OBJECT)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    69
            expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    70
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    71
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    72
                Object box = src.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    73
                switch (src) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    74
                    case VOID:   assertEquals(box, null); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    75
                    case OBJECT: box = box.toString(); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    76
                    case SHORT:  assertEquals(box.getClass(), Short.class); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    77
                    default:     assertEquals(box.getClass(), src.wrapperType()); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    78
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    79
                MethodHandle unboxer;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    80
                if (doCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    81
                    unboxer = ValueConversions.unboxCast(dst.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    82
                else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    83
                    unboxer = ValueConversions.unbox(dst.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    84
                Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    85
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    86
                switch (dst) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    87
                    case INT:     result = (int)     unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    88
                    case LONG:    result = (long)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    89
                    case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    90
                    case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    91
                    case CHAR:    result = (char)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    92
                    case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    93
                    case SHORT:   result = (short)   unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    94
                    case OBJECT:  result = (Object)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    95
                    case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    96
                    case VOID:    result = null;     unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    97
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    98
                if (expectThrow) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    99
                    expResult = "(need an exception)";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   100
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   101
                assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   102
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   103
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   104
        } catch (RuntimeException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   105
            if (expectThrow)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   106
            System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   107
            throw ex;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   108
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   109
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   110
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   111
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   112
    public void testBox() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   113
        for (Wrapper w : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   114
            if (w == Wrapper.VOID)  continue;  // skip this; no unboxed form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   115
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   116
                Object box = w.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   117
                MethodHandle boxer = ValueConversions.box(w.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   118
                Object expResult = box;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   119
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   120
                switch (w) {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 9646
diff changeset
   121
                    case INT:     result = boxer.invokeExact(/*int*/n); break;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   122
                    case LONG:    result = boxer.invokeExact((long)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   123
                    case FLOAT:   result = boxer.invokeExact((float)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   124
                    case DOUBLE:  result = boxer.invokeExact((double)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   125
                    case CHAR:    result = boxer.invokeExact((char)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   126
                    case BYTE:    result = boxer.invokeExact((byte)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   127
                    case SHORT:   result = boxer.invokeExact((short)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   128
                    case OBJECT:  result = boxer.invokeExact((Object)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   129
                    case BOOLEAN: result = boxer.invokeExact((n & 1) != 0); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   130
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   131
                assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   132
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   133
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   134
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   135
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   136
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   137
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   138
    public void testCast() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   139
        Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   140
        Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   141
        for (Class<?> dst : types) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   142
            MethodHandle caster = ValueConversions.cast(dst);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   143
            assertEquals(caster.type(), ValueConversions.identity().type());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   144
            for (Object obj : objects) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   145
                Class<?> src = obj.getClass();
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   146
                boolean canCast = dst.isAssignableFrom(src);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   147
                try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   148
                    Object result = caster.invokeExact(obj);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   149
                    if (canCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   150
                        assertEquals(obj, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   151
                    else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   152
                        assertEquals("cast should not have succeeded", dst, obj);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   153
                } catch (ClassCastException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   154
                    if (canCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   155
                        throw ex;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   156
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   157
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   158
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   159
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   160
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   161
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   162
    public void testIdentity() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   163
        MethodHandle id = ValueConversions.identity();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   164
        Object expResult = "foo";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   165
        Object result = id.invokeExact(expResult);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   166
        assertEquals(expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   167
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   168
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   169
    @Test
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   170
    public void testConvert() throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   171
        for (long tval = 0, ctr = 0;;) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   172
            if (++ctr > 99999)  throw new AssertionError("too many test values");
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 23010
diff changeset
   173
            // prints 3776 test patterns (3776 = 8*59*8)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   174
            tval = nextTestValue(tval);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   175
            if (tval == 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   176
                break;  // repeat
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   177
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   178
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   179
        for (Wrapper src : Wrapper.values()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   180
            for (Wrapper dst : Wrapper.values()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   181
                testConvert(src, dst, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   182
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   183
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   184
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   185
    static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   186
        boolean testSingleCase = (tval != 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   187
        final long tvalInit = tval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   188
        MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   189
        MethodType convType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   190
        if (src == Wrapper.VOID)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   191
            convType = MethodType.methodType(dst.primitiveType() /* , void */);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   192
        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   193
            convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   194
        assertEquals(convType, conv.type());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   195
        MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   196
        for (;;) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   197
            long n = tval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   198
            Object testValue = src.wrap(n);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   199
            Object expResult = dst.cast(testValue, dst.primitiveType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   200
            Object result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   201
            switch (src) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   202
                case INT:     result = converter.invokeExact((int)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   203
                case LONG:    result = converter.invokeExact(/*long*/n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   204
                case FLOAT:   result = converter.invokeExact((float)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   205
                case DOUBLE:  result = converter.invokeExact((double)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   206
                case CHAR:    result = converter.invokeExact((char)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   207
                case BYTE:    result = converter.invokeExact((byte)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   208
                case SHORT:   result = converter.invokeExact((short)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   209
                case OBJECT:  result = converter.invokeExact((Object)n); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   210
                case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   211
                case VOID:    result = converter.invokeExact(); break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   212
                default:  throw new AssertionError();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   213
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   214
            assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   215
                         expResult, result);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   216
            if (testSingleCase)  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   217
            // next test value:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   218
            tval = nextTestValue(tval);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   219
            if (tval == tvalInit)  break;  // repeat
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   220
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   221
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   222
    static long tweakSign(long x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   223
        // Assuming that x is mostly zeroes, make those zeroes follow bit #62 (just below the sign).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   224
        // This function is self-inverse.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   225
        final long MID_SIGN_BIT = 62;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   226
        long sign = -((x >>> MID_SIGN_BIT) & 1);  // all ones or all zeroes
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   227
        long flip = (sign >>> -MID_SIGN_BIT);  // apply the sign below the mid-bit
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   228
        return x ^ flip;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   229
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   230
    static long nextTestValue(long x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   231
        // Produce 64 bits with three component bitfields:  [ high:3 | mid:58 | low:3 ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   232
        // The high and low fields vary through all possible bit patterns.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   233
        // The middle field is either all zero or has a single bit set.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   234
        // For better coverage of the neighborhood of zero, an internal sign bit is xored downward also.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   235
        long ux = tweakSign(x);  // unsign the middle field
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   236
        final long LOW_BITS  = 3, LOW_BITS_MASK  = (1L << LOW_BITS)-1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   237
        final long HIGH_BITS = 3, HIGH_BITS_MASK = ~(-1L >>> HIGH_BITS);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   238
        if ((ux & LOW_BITS_MASK) != LOW_BITS_MASK) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   239
            ++ux;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   240
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   241
            ux &= ~LOW_BITS_MASK;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   242
            long midBit = (ux & ~HIGH_BITS_MASK);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   243
            if (midBit == 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   244
                midBit = (1L<<LOW_BITS);  // introduce a low bit
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   245
            ux += midBit;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   246
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   247
        return tweakSign(ux);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   248
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   249
}