jdk/test/sun/invoke/util/ValueConversionsTest.java
author jrose
Fri, 17 Aug 2012 13:42:25 -0700
changeset 13610 28122b96858e
parent 13423 17843fff200d
child 14342 8435a30053c1
permissions -rw-r--r--
7191102: nightly failures after JSR 292 lazy method handle update (round 3) Reviewed-by: twisti, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     1
/*
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
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;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    28
import java.lang.invoke.MethodType;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    29
import java.lang.invoke.MethodHandle;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    30
import java.io.Serializable;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    31
import java.util.Arrays;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    32
import java.util.Collections;
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
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    38
 * @ignore This test requires a special compilation environment to access sun.inovke.util.  Run by hand.
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
 * @run junit/othervm
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    41
 *          -DValueConversionsTest.MAX_ARITY=255 -DValueConversionsTest.START_ARITY=250
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    42
 *              test.sun.invoke.util.ValueConversionsTest
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
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    45
// This might take a while and burn lots of metadata:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    46
// @run junit/othervm -DValueConversionsTest.MAX_ARITY=255 -DValueConversionsTest.EXHAUSTIVE=true test.sun.invoke.util.ValueConversionsTest
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    47
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    48
/**
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    49
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    50
 * @author jrose
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
public class ValueConversionsTest {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 9646
diff changeset
    53
    private static final Class<?> CLASS = ValueConversionsTest.class;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    54
    private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    55
    private static final int START_ARITY = Integer.getInteger(CLASS.getSimpleName()+".START_ARITY", 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    56
    private static final boolean EXHAUSTIVE = Boolean.getBoolean(CLASS.getSimpleName()+".EXHAUSTIVE");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    57
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    58
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    59
    public void testUnbox() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    60
        testUnbox(false);
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
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    64
    public void testUnboxCast() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    65
        testUnbox(true);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    66
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    67
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    68
    private void testUnbox(boolean doCast) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    69
        //System.out.println("unbox");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    70
        for (Wrapper dst : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    71
            //System.out.println(dst);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    72
            for (Wrapper src : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    73
                testUnbox(doCast, dst, src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    74
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    75
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    76
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    77
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    78
    private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    79
        boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    80
        if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    81
        if (dst == Wrapper.OBJECT)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    82
            expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    83
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    84
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    85
                Object box = src.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    86
                switch (src) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    87
                    case VOID:   assertEquals(box, null); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    88
                    case OBJECT: box = box.toString(); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    89
                    case SHORT:  assertEquals(box.getClass(), Short.class); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    90
                    default:     assertEquals(box.getClass(), src.wrapperType()); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    91
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    92
                MethodHandle unboxer;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    93
                if (doCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    94
                    unboxer = ValueConversions.unboxCast(dst.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    95
                else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    96
                    unboxer = ValueConversions.unbox(dst.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    97
                Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    98
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    99
                switch (dst) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   100
                    case INT:     result = (int)     unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   101
                    case LONG:    result = (long)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   102
                    case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   103
                    case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   104
                    case CHAR:    result = (char)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   105
                    case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   106
                    case SHORT:   result = (short)   unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   107
                    case OBJECT:  result = (Object)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   108
                    case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   109
                    case VOID:    result = null;     unboxer.invokeExact(box); break;
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
                if (expectThrow) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   112
                    expResult = "(need an exception)";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   113
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   114
                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
   115
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   116
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   117
        } catch (RuntimeException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   118
            if (expectThrow)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   119
            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
   120
            throw ex;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   121
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   122
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   123
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   124
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   125
    public void testBox() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   126
        //System.out.println("box");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   127
        for (Wrapper w : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   128
            if (w == Wrapper.VOID)  continue;  // skip this; no unboxed form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   129
            //System.out.println(w);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   130
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   131
                Object box = w.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   132
                MethodHandle boxer = ValueConversions.box(w.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   133
                Object expResult = box;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   134
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   135
                switch (w) {
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 9646
diff changeset
   136
                    case INT:     result = boxer.invokeExact(/*int*/n); break;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   137
                    case LONG:    result = boxer.invokeExact((long)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   138
                    case FLOAT:   result = boxer.invokeExact((float)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   139
                    case DOUBLE:  result = boxer.invokeExact((double)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   140
                    case CHAR:    result = boxer.invokeExact((char)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   141
                    case BYTE:    result = boxer.invokeExact((byte)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   142
                    case SHORT:   result = boxer.invokeExact((short)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   143
                    case OBJECT:  result = boxer.invokeExact((Object)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   144
                    case BOOLEAN: result = boxer.invokeExact((n & 1) != 0); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   145
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   146
                assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   147
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   148
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   149
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   150
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   151
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   152
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   153
    public void testCast() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   154
        //System.out.println("cast");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   155
        Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   156
        Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   157
        for (Class<?> dst : types) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   158
            MethodHandle caster = ValueConversions.cast(dst);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   159
            assertEquals(caster.type(), ValueConversions.identity().type());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   160
            for (Object obj : objects) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   161
                Class<?> src = obj.getClass();
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   162
                boolean canCast = dst.isAssignableFrom(src);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   163
                //System.out.println("obj="+obj+" <: dst="+dst+(canCast ? " (OK)" : " (will fail)"));
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   164
                try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   165
                    Object result = caster.invokeExact(obj);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   166
                    if (canCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   167
                        assertEquals(obj, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   168
                    else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   169
                        assertEquals("cast should not have succeeded", dst, obj);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   170
                } catch (ClassCastException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   171
                    if (canCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   172
                        throw ex;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   173
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   174
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   175
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   176
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   177
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   178
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   179
    public void testIdentity() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   180
        //System.out.println("identity");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   181
        MethodHandle id = ValueConversions.identity();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   182
        Object expResult = "foo";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   183
        Object result = id.invokeExact(expResult);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   184
        // compiler bug:  ValueConversions.identity().invokeExact("bar");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   185
        assertEquals(expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   186
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   187
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   188
    @Test
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   189
    public void testConvert() throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   190
        //System.out.println("convert");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   191
        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
   192
            if (++ctr > 99999)  throw new AssertionError("too many test values");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   193
            // next test value:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   194
            //System.out.println(Long.toHexString(tval)); // prints 3776 test patterns
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   195
            tval = nextTestValue(tval);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   196
            if (tval == 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   197
                //System.out.println("test value count = "+ctr);  // 3776 = 8*59*8
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   198
                break;  // repeat
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   199
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   200
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   201
        for (Wrapper src : Wrapper.values()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   202
            for (Wrapper dst : Wrapper.values()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   203
                testConvert(src, dst, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   204
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   205
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   206
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   207
    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
   208
        //System.out.println(src+" => "+dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   209
        boolean testSingleCase = (tval != 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   210
        final long tvalInit = tval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   211
        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
   212
        MethodType convType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   213
        if (src == Wrapper.VOID)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   214
            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
   215
        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   216
            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
   217
        assertEquals(convType, conv.type());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   218
        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
   219
        for (;;) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   220
            long n = tval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   221
            Object testValue = src.wrap(n);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   222
            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
   223
            Object result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   224
            switch (src) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   225
                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
   226
                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
   227
                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
   228
                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
   229
                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
   230
                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
   231
                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
   232
                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
   233
                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
   234
                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
   235
                default:  throw new AssertionError();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   236
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   237
            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
   238
                         expResult, result);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   239
            if (testSingleCase)  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   240
            // next test value:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   241
            tval = nextTestValue(tval);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   242
            if (tval == tvalInit)  break;  // repeat
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   243
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   244
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   245
    static long tweakSign(long x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   246
        // 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
   247
        // This function is self-inverse.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   248
        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
   249
        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
   250
        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
   251
        return x ^ flip;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   252
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   253
    static long nextTestValue(long x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   254
        // 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
   255
        // 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
   256
        // 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
   257
        // 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
   258
        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
   259
        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
   260
        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
   261
        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
   262
            ++ux;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   263
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   264
            ux &= ~LOW_BITS_MASK;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   265
            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
   266
            if (midBit == 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   267
                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
   268
            ux += midBit;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   269
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   270
        return tweakSign(ux);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   271
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   272
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   273
    @Test
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   274
    public void testVarargsArray() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   275
        //System.out.println("varargsArray");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   276
        final int MIN = START_ARITY;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   277
        final int MAX = MAX_ARITY-2;  // 253+1 would cause parameter overflow with 'this' added
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   278
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   279
            MethodHandle target = ValueConversions.varargsArray(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   280
            Object[] args = new Object[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   281
            for (int i = 0; i < nargs; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   282
                args[i] = "#"+i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   283
            Object res = target.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   284
            assertArrayEquals(args, (Object[])res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   285
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   286
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   287
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   288
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   289
    public void testVarargsReferenceArray() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   290
        //System.out.println("varargsReferenceArray");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   291
        testTypedVarargsArray(Object[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   292
        testTypedVarargsArray(String[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   293
        testTypedVarargsArray(Number[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   294
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   295
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   296
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   297
    public void testVarargsPrimitiveArray() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   298
        //System.out.println("varargsPrimitiveArray");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   299
        testTypedVarargsArray(int[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   300
        testTypedVarargsArray(long[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   301
        testTypedVarargsArray(byte[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   302
        testTypedVarargsArray(boolean[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   303
        testTypedVarargsArray(short[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   304
        testTypedVarargsArray(char[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   305
        testTypedVarargsArray(float[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   306
        testTypedVarargsArray(double[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   307
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   308
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   309
    private static int nextArgCount(int nargs, int density, int MAX) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   310
        if (EXHAUSTIVE)  return nargs + 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   311
        if (nargs >= MAX)  return Integer.MAX_VALUE;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   312
        int BOT = 20, TOP = MAX-5;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   313
        if (density < 10) { BOT = 10; MAX = TOP-2; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   314
        if (nargs <= BOT || nargs >= TOP) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   315
            ++nargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   316
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   317
            int bump = Math.max(1, 100 / density);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   318
            nargs += bump;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   319
            if (nargs > TOP)  nargs = TOP;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   320
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   321
        return nargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   322
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   323
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   324
    private void testTypedVarargsArray(Class<?> arrayType) throws Throwable {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   325
        //System.out.println(arrayType.getSimpleName());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   326
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   327
        int MIN = START_ARITY;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   328
        int MAX = MAX_ARITY-2;  // 253+1 would cause parameter overflow with 'this' added
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   329
        int density = 3;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   330
        if (elemType == int.class || elemType == long.class)  density = 7;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   331
        if (elemType == long.class || elemType == double.class) { MAX /= 2; MIN /= 2; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   332
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, density, MAX)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   333
            Object[] args = makeTestArray(elemType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   334
            MethodHandle varargsArray = ValueConversions.varargsArray(arrayType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   335
            MethodType vaType = varargsArray.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   336
            assertEquals(arrayType, vaType.returnType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   337
            if (nargs != 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   338
                assertEquals(elemType, vaType.parameterType(0));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   339
                assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   340
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   341
            assertEquals(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   342
                         vaType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   343
            Object res = varargsArray.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   344
            String resString = toArrayString(res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   345
            assertEquals(Arrays.toString(args), resString);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   346
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   347
            MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   348
            MethodType stype = spreader.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   349
            assert(stype == MethodType.methodType(arrayType, arrayType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   350
            if (nargs <= 5) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   351
                // invoke target as a spreader also:
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 9646
diff changeset
   352
                @SuppressWarnings("cast")
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   353
                Object res2 = spreader.invokeWithArguments((Object)res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   354
                String res2String = toArrayString(res2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   355
                assertEquals(Arrays.toString(args), res2String);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   356
                // invoke the spreader on a generic Object[] array; check for error
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   357
                try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   358
                    Object res3 = spreader.invokeWithArguments((Object)args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   359
                    String res3String = toArrayString(res3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   360
                    assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   361
                    assertEquals(Arrays.toString(args), res3String);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   362
                } catch (ClassCastException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   363
                    assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   364
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   365
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   366
            if (nargs == 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   367
                // invoke spreader on null arglist
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   368
                Object res3 = spreader.invokeWithArguments((Object)null);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   369
                String res3String = toArrayString(res3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   370
                assertEquals(Arrays.toString(args), res3String);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   371
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   372
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   373
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   374
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   375
    private static Object[] makeTestArray(Class<?> elemType, int len) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   376
        Wrapper elem = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   377
        if (elemType.isPrimitive())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   378
            elem = Wrapper.forPrimitiveType(elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   379
        else if (Wrapper.isWrapperType(elemType))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   380
            elem = Wrapper.forWrapperType(elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   381
        Object[] args = new Object[len];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   382
        for (int i = 0; i < len; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   383
            Object arg = i * 100;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   384
            if (elem == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   385
                if (elemType == String.class)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   386
                    arg = "#"+arg;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   387
                arg = elemType.cast(arg);  // just to make sure
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   388
            } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   389
                switch (elem) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   390
                    case BOOLEAN: arg = (i % 3 == 0);           break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   391
                    case CHAR:    arg = 'a' + i;                break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   392
                    case LONG:    arg = (long)i * 1000_000_000; break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   393
                    case FLOAT:   arg = (float)i / 100;         break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   394
                    case DOUBLE:  arg = (double)i / 1000_000;   break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   395
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   396
                arg = elem.cast(arg, elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   397
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   398
            args[i] = arg;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   399
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   400
        //System.out.println(elemType.getName()+Arrays.toString(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   401
        return args;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   402
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   403
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   404
    private static String toArrayString(Object a) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   405
        if (a == null)  return "null";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   406
        Class<?> elemType = a.getClass().getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   407
        if (elemType == null)  return a.toString();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   408
        if (elemType.isPrimitive()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   409
            switch (Wrapper.forPrimitiveType(elemType)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   410
                case INT:      return Arrays.toString((int[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   411
                case BYTE:     return Arrays.toString((byte[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   412
                case BOOLEAN:  return Arrays.toString((boolean[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   413
                case SHORT:    return Arrays.toString((short[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   414
                case CHAR:     return Arrays.toString((char[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   415
                case FLOAT:    return Arrays.toString((float[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   416
                case LONG:     return Arrays.toString((long[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   417
                case DOUBLE:   return Arrays.toString((double[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   418
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   419
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   420
        return Arrays.toString((Object[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   421
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   422
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   423
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   424
    public void testVarargsList() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   425
        //System.out.println("varargsList");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   426
        final int MIN = START_ARITY;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   427
        final int MAX = MAX_ARITY-2;  // 253+1 would cause parameter overflow with 'this' added
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   428
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 7, MAX)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   429
            MethodHandle target = ValueConversions.varargsList(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   430
            Object[] args = new Object[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   431
            for (int i = 0; i < nargs; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   432
                args[i] = "#"+i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   433
            Object res = target.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   434
            assertEquals(Arrays.asList(args), res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   435
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   436
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   437
}