jdk/test/sun/invoke/util/ValueConversionsTest.java
author zgu
Tue, 16 Aug 2011 09:18:47 -0400
changeset 10248 62296fcbd7cb
parent 9646 5ebbe5ab084f
child 11534 9949ffb8eb3a
permissions -rw-r--r--
Merge
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.lang.invoke.MethodHandles;
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 java.util.Collections;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    34
import org.junit.Ignore;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    35
import org.junit.Test;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    36
import static org.junit.Assert.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    37
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    38
/* @test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    39
 * @summary unit tests for value-type conversion utilities
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    40
 * @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
    41
 * @run junit/othervm test.sun.invoke.util.ValueConversionsTest
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    42
 * @run junit/othervm
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    43
 *          -DValueConversionsTest.MAX_ARITY=255 -DValueConversionsTest.START_ARITY=250
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    44
 *              test.sun.invoke.util.ValueConversionsTest
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
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    47
// This might take a while and burn lots of metadata:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    48
// @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
    49
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
 * @author jrose
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    53
 */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    54
public class ValueConversionsTest {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    55
    private static final Class CLASS = ValueConversionsTest.class;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    56
    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
    57
    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
    58
    private static final boolean EXHAUSTIVE = Boolean.getBoolean(CLASS.getSimpleName()+".EXHAUSTIVE");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    59
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    60
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    61
    public void testUnbox() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    62
        testUnbox(false);
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
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    66
    public void testUnboxCast() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    67
        testUnbox(true);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    68
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    69
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    70
    private void testUnbox(boolean doCast) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    71
        //System.out.println("unbox");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    72
        for (Wrapper dst : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    73
            //System.out.println(dst);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    74
            for (Wrapper src : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    75
                testUnbox(doCast, dst, src);
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
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    79
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    80
    private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    81
        boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    82
        if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    83
        if (dst == Wrapper.OBJECT)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    84
            expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    85
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    86
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    87
                Object box = src.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    88
                switch (src) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    89
                    case VOID:   assertEquals(box, null); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    90
                    case OBJECT: box = box.toString(); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    91
                    case SHORT:  assertEquals(box.getClass(), Short.class); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    92
                    default:     assertEquals(box.getClass(), src.wrapperType()); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    93
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    94
                MethodHandle unboxer;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    95
                if (doCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    96
                    unboxer = ValueConversions.unboxCast(dst.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    97
                else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    98
                    unboxer = ValueConversions.unbox(dst.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    99
                Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   100
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   101
                switch (dst) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   102
                    case INT:     result = (int)     unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   103
                    case LONG:    result = (long)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   104
                    case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   105
                    case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   106
                    case CHAR:    result = (char)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   107
                    case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   108
                    case SHORT:   result = (short)   unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   109
                    case OBJECT:  result = (Object)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   110
                    case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   111
                    case VOID:    result = null;     unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   112
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   113
                if (expectThrow) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   114
                    expResult = "(need an exception)";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   115
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   116
                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
   117
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   118
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   119
        } catch (RuntimeException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   120
            if (expectThrow)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   121
            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
   122
            throw ex;
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
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   125
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   126
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   127
    public void testUnboxRaw() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   128
        //System.out.println("unboxRaw");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   129
        for (Wrapper w : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   130
            if (w == Wrapper.OBJECT)  continue;  // skip this; no raw form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   131
            //System.out.println(w);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   132
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   133
                Object box = w.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   134
                long expResult = w.unwrapRaw(box);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   135
                Object box2 = w.wrapRaw(expResult);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   136
                assertEquals(box, box2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   137
                MethodHandle unboxer = ValueConversions.unboxRaw(w.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   138
                long result = -1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   139
                switch (w) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   140
                    case INT:     result = (int)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   141
                    case LONG:    result = (long) unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   142
                    case FLOAT:   result = (int)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   143
                    case DOUBLE:  result = (long) unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   144
                    case CHAR:    result = (int)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   145
                    case BYTE:    result = (int)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   146
                    case SHORT:   result = (int)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   147
                    case BOOLEAN: result = (int)  unboxer.invokeExact(box); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   148
                    case VOID:    result = (int)  unboxer.invokeExact(box); break;
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
                assertEquals("(w,n,box)="+Arrays.asList(w,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   151
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   152
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   153
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   154
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   155
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   156
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   157
    public void testBox() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   158
        //System.out.println("box");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   159
        for (Wrapper w : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   160
            if (w == Wrapper.VOID)  continue;  // skip this; no unboxed form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   161
            //System.out.println(w);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   162
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   163
                Object box = w.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   164
                MethodHandle boxer = ValueConversions.box(w.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   165
                Object expResult = box;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   166
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   167
                switch (w) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   168
                    case INT:     result = boxer.invokeExact((int)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   169
                    case LONG:    result = boxer.invokeExact((long)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   170
                    case FLOAT:   result = boxer.invokeExact((float)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   171
                    case DOUBLE:  result = boxer.invokeExact((double)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   172
                    case CHAR:    result = boxer.invokeExact((char)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   173
                    case BYTE:    result = boxer.invokeExact((byte)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   174
                    case SHORT:   result = boxer.invokeExact((short)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   175
                    case OBJECT:  result = boxer.invokeExact((Object)n); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   176
                    case BOOLEAN: result = boxer.invokeExact((n & 1) != 0); break;
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
                assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   179
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   180
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   181
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   182
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   183
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   184
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   185
    public void testBoxRaw() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   186
        //System.out.println("boxRaw");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   187
        for (Wrapper w : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   188
            if (w == Wrapper.VOID)  continue;  // skip this; no unboxed form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   189
            if (w == Wrapper.OBJECT)  continue;  // skip this; no raw form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   190
            //System.out.println(w);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   191
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   192
                Object box = w.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   193
                long   raw = w.unwrapRaw(box);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   194
                Object expResult = box;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   195
                MethodHandle boxer = ValueConversions.boxRaw(w.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   196
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   197
                switch (w) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   198
                case INT:     result = boxer.invokeExact((int)raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   199
                case LONG:    result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   200
                case FLOAT:   result = boxer.invokeExact((int)raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   201
                case DOUBLE:  result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   202
                case CHAR:    result = boxer.invokeExact((int)raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   203
                case BYTE:    result = boxer.invokeExact((int)raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   204
                case SHORT:   result = boxer.invokeExact((int)raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   205
                case BOOLEAN: result = boxer.invokeExact((int)raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   206
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   207
                assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   208
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   209
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   210
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   211
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   212
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   213
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   214
    public void testReboxRaw() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   215
        //System.out.println("reboxRaw");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   216
        for (Wrapper w : Wrapper.values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   217
            Wrapper pw = Wrapper.forPrimitiveType(w.rawPrimitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   218
            if (w == Wrapper.VOID)  continue;  // skip this; no unboxed form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   219
            if (w == Wrapper.OBJECT)  continue;  // skip this; no raw form
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   220
            //System.out.println(w);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   221
            for (int n = -5; n < 10; n++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   222
                Object box = w.wrap(n);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   223
                Object raw = pw.wrap(w.unwrapRaw(box));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   224
                Object expResult = box;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   225
                MethodHandle boxer = ValueConversions.rebox(w.primitiveType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   226
                Object result = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   227
                switch (w) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   228
                case INT:     result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   229
                case LONG:    result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   230
                case FLOAT:   result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   231
                case DOUBLE:  result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   232
                case CHAR:    result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   233
                case BYTE:    result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   234
                case SHORT:   result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   235
                case BOOLEAN: result = boxer.invokeExact(raw); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   236
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   237
                assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   238
                             expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   239
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   240
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   241
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   242
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   243
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   244
    public void testCast() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   245
        //System.out.println("cast");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   246
        Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   247
        Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   248
        for (Class<?> dst : types) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   249
            MethodHandle caster = ValueConversions.cast(dst);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   250
            assertEquals(caster.type(), ValueConversions.identity().type());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   251
            for (Object obj : objects) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   252
                Class<?> src = obj.getClass();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   253
                boolean canCast;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   254
                if (dst.isInterface()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   255
                    canCast = true;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   256
                } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   257
                    canCast = dst.isAssignableFrom(src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   258
                    assertEquals(canCast, dst.isInstance(obj));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   259
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   260
                //System.out.println("obj="+obj+" <: dst="+dst);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   261
                try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   262
                    Object result = caster.invokeExact(obj);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   263
                    if (canCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   264
                        assertEquals(obj, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   265
                    else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   266
                        assertEquals("cast should not have succeeded", dst, obj);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   267
                } catch (ClassCastException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   268
                    if (canCast)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   269
                        throw ex;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   270
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   271
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   272
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   273
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   274
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   275
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   276
    public void testIdentity() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   277
        //System.out.println("identity");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   278
        MethodHandle id = ValueConversions.identity();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   279
        Object expResult = "foo";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   280
        Object result = id.invokeExact(expResult);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   281
        // compiler bug:  ValueConversions.identity().invokeExact("bar");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   282
        assertEquals(expResult, result);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   283
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   284
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   285
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   286
    public void testVarargsArray() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   287
        //System.out.println("varargsArray");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   288
        final int MIN = START_ARITY;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   289
        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
   290
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   291
            MethodHandle target = ValueConversions.varargsArray(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   292
            Object[] args = new Object[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   293
            for (int i = 0; i < nargs; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   294
                args[i] = "#"+i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   295
            Object res = target.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   296
            assertArrayEquals(args, (Object[])res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   297
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   298
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   299
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   300
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   301
    public void testVarargsReferenceArray() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   302
        //System.out.println("varargsReferenceArray");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   303
        testTypedVarargsArray(Object[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   304
        testTypedVarargsArray(String[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   305
        testTypedVarargsArray(Number[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   306
    }
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
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   309
    public void testVarargsPrimitiveArray() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   310
        //System.out.println("varargsPrimitiveArray");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   311
        testTypedVarargsArray(int[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   312
        testTypedVarargsArray(long[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   313
        testTypedVarargsArray(byte[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   314
        testTypedVarargsArray(boolean[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   315
        testTypedVarargsArray(short[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   316
        testTypedVarargsArray(char[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   317
        testTypedVarargsArray(float[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   318
        testTypedVarargsArray(double[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   319
    }
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
    private static int nextArgCount(int nargs, int density, int MAX) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   322
        if (EXHAUSTIVE)  return nargs + 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   323
        if (nargs >= MAX)  return Integer.MAX_VALUE;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   324
        int BOT = 20, TOP = MAX-5;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   325
        if (density < 10) { BOT = 10; MAX = TOP-2; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   326
        if (nargs <= BOT || nargs >= TOP) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   327
            ++nargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   328
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   329
            int bump = Math.max(1, 100 / density);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   330
            nargs += bump;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   331
            if (nargs > TOP)  nargs = TOP;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   332
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   333
        return nargs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   334
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   335
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   336
    private void testTypedVarargsArray(Class<?> arrayType) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   337
        System.out.println(arrayType.getSimpleName());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   338
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   339
        int MIN = START_ARITY;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   340
        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
   341
        int density = 3;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   342
        if (elemType == int.class || elemType == long.class)  density = 7;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   343
        if (elemType == long.class || elemType == double.class) { MAX /= 2; MIN /= 2; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   344
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, density, MAX)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   345
            Object[] args = makeTestArray(elemType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   346
            MethodHandle varargsArray = ValueConversions.varargsArray(arrayType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   347
            MethodType vaType = varargsArray.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   348
            assertEquals(arrayType, vaType.returnType());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   349
            if (nargs != 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   350
                assertEquals(elemType, vaType.parameterType(0));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   351
                assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   352
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   353
            assertEquals(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   354
                         vaType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   355
            Object res = varargsArray.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   356
            String resString = toArrayString(res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   357
            assertEquals(Arrays.toString(args), resString);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   358
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   359
            MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   360
            MethodType stype = spreader.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   361
            assert(stype == MethodType.methodType(arrayType, arrayType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   362
            if (nargs <= 5) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   363
                // invoke target as a spreader also:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   364
                Object res2 = spreader.invokeWithArguments((Object)res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   365
                String res2String = toArrayString(res2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   366
                assertEquals(Arrays.toString(args), res2String);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   367
                // invoke the spreader on a generic Object[] array; check for error
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   368
                try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   369
                    Object res3 = spreader.invokeWithArguments((Object)args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   370
                    String res3String = toArrayString(res3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   371
                    assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   372
                    assertEquals(Arrays.toString(args), res3String);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   373
                } catch (ClassCastException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   374
                    assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   375
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   376
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   377
            if (nargs == 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   378
                // invoke spreader on null arglist
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   379
                Object res3 = spreader.invokeWithArguments((Object)null);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   380
                String res3String = toArrayString(res3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   381
                assertEquals(Arrays.toString(args), res3String);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   382
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   383
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   384
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   385
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   386
    private static Object[] makeTestArray(Class<?> elemType, int len) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   387
        Wrapper elem = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   388
        if (elemType.isPrimitive())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   389
            elem = Wrapper.forPrimitiveType(elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   390
        else if (Wrapper.isWrapperType(elemType))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   391
            elem = Wrapper.forWrapperType(elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   392
        Object[] args = new Object[len];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   393
        for (int i = 0; i < len; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   394
            Object arg = i * 100;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   395
            if (elem == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   396
                if (elemType == String.class)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   397
                    arg = "#"+arg;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   398
                arg = elemType.cast(arg);  // just to make sure
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   399
            } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   400
                switch (elem) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   401
                    case BOOLEAN: arg = (i % 3 == 0);           break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   402
                    case CHAR:    arg = 'a' + i;                break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   403
                    case LONG:    arg = (long)i * 1000_000_000; break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   404
                    case FLOAT:   arg = (float)i / 100;         break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   405
                    case DOUBLE:  arg = (double)i / 1000_000;   break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   406
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   407
                arg = elem.cast(arg, elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   408
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   409
            args[i] = arg;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   410
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   411
        //System.out.println(elemType.getName()+Arrays.toString(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   412
        return args;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   413
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   414
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   415
    private static String toArrayString(Object a) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   416
        if (a == null)  return "null";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   417
        Class<?> elemType = a.getClass().getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   418
        if (elemType == null)  return a.toString();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   419
        if (elemType.isPrimitive()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   420
            switch (Wrapper.forPrimitiveType(elemType)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   421
                case INT:      return Arrays.toString((int[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   422
                case BYTE:     return Arrays.toString((byte[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   423
                case BOOLEAN:  return Arrays.toString((boolean[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   424
                case SHORT:    return Arrays.toString((short[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   425
                case CHAR:     return Arrays.toString((char[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   426
                case FLOAT:    return Arrays.toString((float[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   427
                case LONG:     return Arrays.toString((long[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   428
                case DOUBLE:   return Arrays.toString((double[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   429
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   430
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   431
        return Arrays.toString((Object[])a);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   432
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   433
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   434
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   435
    public void testVarargsList() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   436
        //System.out.println("varargsList");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   437
        final int MIN = START_ARITY;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   438
        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
   439
        for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 7, MAX)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   440
            MethodHandle target = ValueConversions.varargsList(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   441
            Object[] args = new Object[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   442
            for (int i = 0; i < nargs; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   443
                args[i] = "#"+i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   444
            Object res = target.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   445
            assertEquals(Arrays.asList(args), res);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   446
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   447
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   448
}