jdk/test/java/lang/invoke/RicochetTest.java
author jrose
Thu, 12 May 2011 19:27:49 -0700
changeset 9646 5ebbe5ab084f
child 9781 ce852878da20
permissions -rw-r--r--
6939861: JVM should handle more conversion operations Summary: Integrate JDK code with JVM-supplied ricochet frames. Reviewed-by: never, twisti
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) 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.  Oracle designates this
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    10
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    11
 * 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
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    14
 * 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
    15
 * accompanied this code).
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    16
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    17
 * 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
    18
 * 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
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    20
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    22
 * 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
    23
 * questions.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    24
 */
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
/* @test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    27
 * @summary unit tests for recursive method handles
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    28
 * @run junit/othervm -DRicochetTest.MAX_ARITY=255 test.java.lang.invoke.RicochetTest
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    29
 */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    30
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    31
package test.java.lang.invoke;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    32
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    33
import java.lang.invoke.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    34
import java.util.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    35
import org.junit.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    36
import static java.lang.invoke.MethodType.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    37
import static java.lang.invoke.MethodHandles.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    38
import static org.junit.Assert.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    39
import static org.junit.Assume.*;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    40
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    41
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    42
/**
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    43
 *
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    44
 * @author jrose
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    45
 */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    46
public class RicochetTest {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    47
    private static final Class CLASS = RicochetTest.class;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    48
    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
    49
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    50
    public static void main(String... av) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    51
        RicochetTest test = new RicochetTest();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    52
        if (av.length > 0)  test.testOnly = Arrays.asList(av).toString();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    53
        if (REPEAT == 1 || test.testOnly != null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    54
            test.testAll();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    55
            if (test.testOnlyTests == null)  throw new RuntimeException("no matching test: "+test.testOnly);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    56
        } else if (REPEAT == 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    57
            org.junit.runner.JUnitCore.runClasses(RicochetTest.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    58
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    59
            verbose(1, "REPEAT="+REPEAT);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    60
            for (int i = 0; i < REPEAT; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    61
                test.testRepetition = (i+1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    62
                verbose(0, "[#"+test.testRepetition+"]");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    63
                test.testAll();
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
        }
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
    int testRepetition;
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
    public void testAll() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    70
        testNull();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    71
        testBoxInteger();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    72
        testFilterReturnValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    73
        testFilterObject();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    74
        testBoxLong();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    75
        testFilterInteger();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    76
        testIntSpreads();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    77
        testByteSpreads();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    78
        testLongSpreads();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    79
        testIntCollects();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    80
        testReturns();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    81
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    82
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    83
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    84
    public void testNull() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    85
        if (testRepetition > (1+REPEAT/100))  return;  // trivial test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    86
        if (!startTest("testNull"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    87
        assertEquals(opI(37), opI.invokeWithArguments(37));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    88
        assertEqualFunction(opI, opI);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    89
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    90
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    91
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    92
    public void testBoxInteger() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    93
        if (!startTest("testBoxInteger"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    94
        assertEqualFunction(opI, opI.asType(opL_I.type()).asType(opI.type()));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    95
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    96
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    97
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    98
    public void testFilterReturnValue() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
    99
        if (!startTest("testFilterReturnValue"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   100
        int[] ints = { 12, 23, 34, 45, 56, 67, 78, 89 };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   101
        Object res = list8ints.invokeExact(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   102
        assertEquals(Arrays.toString(ints), res.toString());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   103
        MethodHandle idreturn = filterReturnValue(list8ints, identity(Object.class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   104
        res = idreturn.invokeExact(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   105
        assertEquals(Arrays.toString(ints), res.toString());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   106
        MethodHandle add0 = addL.bindTo(0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   107
        assertEqualFunction(filterReturnValue(opL2, add0), opL2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   108
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   109
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   110
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   111
    public void testFilterObject() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   112
        if (!startTest("testFilterObject"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   113
        MethodHandle add0 = addL.bindTo(0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   114
        assertEqualFunction(sequence(opL2, add0), opL2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   115
        int bump13 = -13;  // value near 20 works as long as test values are near [-80..80]
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   116
        MethodHandle add13   = addL.bindTo(bump13);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   117
        MethodHandle add13_0 = addL.bindTo(opI2(bump13, 0));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   118
        MethodHandle add13_1 = addL.bindTo(opI2(0, bump13));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   119
        assertEqualFunction(sequence(opL2, add13_0),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   120
                            filterArguments(opL2, 0, add13));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   121
        assertEqualFunction(sequence(opL2, add13_1),
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   122
                            filterArguments(opL2, 1, add13));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   123
        System.out.println("[testFilterObject done]");
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 testBoxLong() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   128
        if (!startTest("testBoxLong"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   129
        assertEqualFunction(opJ, opJ.asType(opL_J.type()).asType(opJ.type()));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   130
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   131
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   132
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   133
    public void testFilterInteger() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   134
        if (!startTest("testFilterInteger"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   135
        assertEqualFunction(opI, sequence(convI_L, opL_I));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   136
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   137
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   138
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   139
    public void testIntSpreads() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   140
        if (!startTest("testIntSpreads"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   141
        MethodHandle id = identity(int[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   142
        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
   143
        for (int nargs = 0; nargs <= MAX; nargs++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   144
            if (nargs > 30 && nargs < MAX-20)  nargs += 10;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   145
            int[] args = new int[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   146
            for (int j = 0; j < args.length; j++)  args[j] = (int)(j + 11);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   147
            //System.out.println("testIntSpreads "+Arrays.toString(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   148
            int[] args1 = (int[]) id.invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   149
            assertArrayEquals(args, args1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   150
            MethodHandle coll = id.asCollector(int[].class, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   151
            int[] args2 = args;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   152
            switch (nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   153
                case 0:  args2 = (int[]) coll.invokeExact(); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   154
                case 1:  args2 = (int[]) coll.invokeExact(args[0]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   155
                case 2:  args2 = (int[]) coll.invokeExact(args[0], args[1]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   156
                case 3:  args2 = (int[]) coll.invokeExact(args[0], args[1], args[2]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   157
                case 4:  args2 = (int[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   158
                case 5:  args2 = (int[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   159
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   160
            assertArrayEquals(args, args2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   161
            MethodHandle mh = coll.asSpreader(int[].class, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   162
            int[] args3 = (int[]) mh.invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   163
            assertArrayEquals(args, args3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   164
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   165
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   166
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   167
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   168
    public void testByteSpreads() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   169
        if (!startTest("testByteSpreads"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   170
        MethodHandle id = identity(byte[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   171
        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
   172
        for (int nargs = 0; nargs <= MAX; nargs++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   173
            if (nargs > 30 && nargs < MAX-20)  nargs += 10;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   174
            byte[] args = new byte[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   175
            for (int j = 0; j < args.length; j++)  args[j] = (byte)(j + 11);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   176
            //System.out.println("testByteSpreads "+Arrays.toString(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   177
            byte[] args1 = (byte[]) id.invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   178
            assertArrayEquals(args, args1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   179
            MethodHandle coll = id.asCollector(byte[].class, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   180
            byte[] args2 = args;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   181
            switch (nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   182
                case 0:  args2 = (byte[]) coll.invokeExact(); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   183
                case 1:  args2 = (byte[]) coll.invokeExact(args[0]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   184
                case 2:  args2 = (byte[]) coll.invokeExact(args[0], args[1]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   185
                case 3:  args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   186
                case 4:  args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   187
                case 5:  args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   188
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   189
            assertArrayEquals(args, args2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   190
            MethodHandle mh = coll.asSpreader(byte[].class, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   191
            byte[] args3 = (byte[]) mh.invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   192
            assertArrayEquals(args, args3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   193
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   194
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   195
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   196
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   197
    public void testLongSpreads() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   198
        if (!startTest("testLongSpreads"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   199
        MethodHandle id = identity(long[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   200
        final int MAX = (MAX_ARITY - 2) / 2;  // 253/2+1 would cause parameter overflow with 'this' added
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   201
        for (int nargs = 0; nargs <= MAX; nargs++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   202
            if (nargs > 30 && nargs < MAX-20)  nargs += 10;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   203
            long[] args = new long[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   204
            for (int j = 0; j < args.length; j++)  args[j] = (long)(j + 11);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   205
            //System.out.println("testLongSpreads "+Arrays.toString(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   206
            long[] args1 = (long[]) id.invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   207
            assertArrayEquals(args, args1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   208
            MethodHandle coll = id.asCollector(long[].class, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   209
            long[] args2 = args;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   210
            switch (nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   211
                case 0:  args2 = (long[]) coll.invokeExact(); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   212
                case 1:  args2 = (long[]) coll.invokeExact(args[0]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   213
                case 2:  args2 = (long[]) coll.invokeExact(args[0], args[1]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   214
                case 3:  args2 = (long[]) coll.invokeExact(args[0], args[1], args[2]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   215
                case 4:  args2 = (long[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   216
                case 5:  args2 = (long[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   217
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   218
            assertArrayEquals(args, args2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   219
            MethodHandle mh = coll.asSpreader(long[].class, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   220
            long[] args3 = (long[]) mh.invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   221
            assertArrayEquals(args, args3);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   222
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   223
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   224
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   225
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   226
    public void testIntCollects() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   227
        if (!startTest("testIntCollects"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   228
        for (MethodHandle lister : INT_LISTERS) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   229
            int outputs = lister.type().parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   230
            for (int collects = 0; collects <= Math.min(outputs, INT_COLLECTORS.length-1); collects++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   231
                int inputs = outputs - 1 + collects;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   232
                if (inputs < 0)  continue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   233
                for (int pos = 0; pos + collects <= inputs; pos++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   234
                    MethodHandle collector = INT_COLLECTORS[collects];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   235
                    int[] args = new int[inputs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   236
                    int ap = 0, arg = 31;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   237
                    for (int i = 0; i < pos; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   238
                        args[ap++] = arg++ + 0;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   239
                    for (int i = 0; i < collects; i++)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   240
                        args[ap++] = arg++ + 10;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   241
                    while (ap < args.length)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   242
                        args[ap++] = arg++ + 20;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   243
                    // calculate piecemeal:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   244
                    //System.out.println("testIntCollects "+Arrays.asList(lister, pos, collector)+" on "+Arrays.toString(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   245
                    int[] collargs = Arrays.copyOfRange(args, pos, pos+collects);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   246
                    int coll = (int) collector.asSpreader(int[].class, collargs.length).invokeExact(collargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   247
                    int[] listargs = Arrays.copyOfRange(args, 0, outputs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   248
                    System.arraycopy(args, pos+collects, listargs, pos+1, outputs - (pos+1));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   249
                    listargs[pos] = coll;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   250
                    //System.out.println("  coll="+coll+" listargs="+Arrays.toString(listargs));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   251
                    Object expect = lister.asSpreader(int[].class, listargs.length).invokeExact(listargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   252
                    //System.out.println("  expect="+expect);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   253
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   254
                    // now use the combined MH, and test the output:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   255
                    MethodHandle mh = collectArguments(lister, pos, INT_COLLECTORS[collects]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   256
                    if (mh == null)  continue;  // no infix collection, yet
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   257
                    assert(mh.type().parameterCount() == inputs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   258
                    Object observe = mh.asSpreader(int[].class, args.length).invokeExact(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   259
                    assertEquals(expect, observe);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   260
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   261
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   262
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   263
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   264
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   265
    private static MethodHandle collectArguments(MethodHandle lister, int pos, MethodHandle collector) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   266
        int collects = collector.type().parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   267
        int outputs = lister.type().parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   268
        if (pos == outputs - 1)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   269
            return MethodHandles.filterArguments(lister, pos,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   270
                        collector.asSpreader(int[].class, collects))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   271
                            .asCollector(int[].class, collects);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   272
        //return MethodHandles.collectArguments(lister, pos, collector); //no such animal
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   273
        return null;
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
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   276
    private static final Class<?>[] RETURN_TYPES = {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   277
        Object.class, String.class, Integer.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   278
        int.class, long.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   279
        boolean.class, byte.class, char.class, short.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   280
        float.class, double.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   281
        void.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   282
    };
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
    @Test
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   285
    public void testReturns() throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   286
        if (!startTest("testReturns"))  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   287
        // fault injection:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   288
        int faultCount = 0;  // total of 1296 tests
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   289
        faultCount = Integer.getInteger("testReturns.faultCount", 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   290
        for (Class<?> ret : RETURN_TYPES) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   291
            // make a complicated identity function and pass something through it
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   292
            System.out.println(ret.getSimpleName());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   293
            Class<?> vret = (ret == void.class) ? Void.class : ret;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   294
            MethodHandle id = // (vret)->ret
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   295
                identity(vret).asType(methodType(ret, vret));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   296
            final int LENGTH = 4;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   297
            int[] index = {0};
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   298
            Object vals = java.lang.reflect.Array.newInstance(vret, LENGTH);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   299
            MethodHandle indexGetter =  //()->int
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   300
                insertArguments(arrayElementGetter(index.getClass()), 0, index, 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   301
            MethodHandle valSelector =  // (int)->vret
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   302
                arrayElementGetter(vals.getClass()).bindTo(vals);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   303
            MethodHandle valGetter =  // ()->vret
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   304
                foldArguments(valSelector, indexGetter);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   305
            if (ret != void.class) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   306
                for (int i = 0; i < LENGTH; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   307
                    Object val = (i + 50);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   308
                    if (ret == boolean.class)  val = (i % 3 == 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   309
                    if (ret == String.class)   val = "#"+i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   310
                    if (ret == char.class)     val = (char)('a'+i);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   311
                    if (ret == byte.class)     val = (byte)~i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   312
                    if (ret == short.class)    val = (short)(1<<i);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   313
                    java.lang.reflect.Array.set(vals, i, val);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   314
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   315
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   316
            for (int i = 0; i < LENGTH; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   317
                Object val = java.lang.reflect.Array.get(vals, i);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   318
                System.out.println(i+" => "+val);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   319
                index[0] = i;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   320
                if (--faultCount == 0)  index[0] ^= 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   321
                Object x = valGetter.invokeWithArguments();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   322
                assertEquals(val, x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   323
                // make a return-filter call:  x = id(valGetter())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   324
                if (--faultCount == 0)  index[0] ^= 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   325
                x = filterReturnValue(valGetter, id).invokeWithArguments();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   326
                assertEquals(val, x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   327
                // make a filter call:  x = id(*,valGetter(),*)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   328
                for (int len = 1; len <= 4; len++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   329
                    for (int pos = 0; pos < len; pos++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   330
                        MethodHandle proj = id;  // lambda(..., vret x,...){x}
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   331
                        for (int j = 0; j < len; j++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   332
                            if (j == pos)  continue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   333
                            proj = dropArguments(proj, j, Object.class);
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
                        assert(proj.type().parameterCount() == len);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   336
                        // proj: (Object*, pos: vret, Object*)->ret
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   337
                        assertEquals(vret, proj.type().parameterType(pos));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   338
                        MethodHandle vgFilter = dropArguments(valGetter, 0, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   339
                        if (--faultCount == 0)  index[0] ^= 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   340
                        x = filterArguments(proj, pos, vgFilter).invokeWithArguments(new Object[len]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   341
                        assertEquals(val, x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   342
                    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   343
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   344
                // make a fold call:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   345
                for (int len = 0; len <= 4; len++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   346
                    for (int fold = 0; fold <= len; fold++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   347
                        MethodHandle proj = id;  // lambda(ret x, ...){x}
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   348
                        if (ret == void.class)  proj = constant(Object.class, null);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   349
                        int arg0 = (ret == void.class ? 0 : 1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   350
                        for (int j = 0; j < len; j++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   351
                            proj = dropArguments(proj, arg0, Object.class);
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
                        assert(proj.type().parameterCount() == arg0 + len);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   354
                        // proj: (Object*, pos: vret, Object*)->ret
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   355
                        if (arg0 != 0)  assertEquals(vret, proj.type().parameterType(0));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   356
                        MethodHandle vgFilter = valGetter.asType(methodType(ret));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   357
                        for (int j = 0; j < fold; j++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   358
                            vgFilter = dropArguments(vgFilter, j, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   359
                        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   360
                        x = foldArguments(proj, vgFilter).invokeWithArguments(new Object[len]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   361
                        if (--faultCount == 0)  index[0] ^= 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   362
                        assertEquals(val, x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   363
                    }
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
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   367
        //System.out.println("faultCount="+faultCount);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   368
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   369
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   370
    private static MethodHandle sequence(MethodHandle mh1, MethodHandle... mhs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   371
        MethodHandle res = mh1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   372
        for (MethodHandle mh2 : mhs)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   373
            res = filterReturnValue(res, mh2);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   374
        return res;
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
    private static void assertEqualFunction(MethodHandle x, MethodHandle y) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   377
        assertEquals(x.type(), y.type()); //??
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   378
        MethodType t = x.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   379
        if (t.parameterCount() == 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   380
            assertEqualFunctionAt(null, x, y);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   381
            return;
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
        Class<?> ptype = t.parameterType(0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   384
        if (ptype == long.class || ptype == Long.class) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   385
            for (long i = -10; i <= 10; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   386
                assertEqualFunctionAt(i, x, y);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   387
            }
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
            for (int i = -10; i <= 10; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   390
                assertEqualFunctionAt(i, x, y);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   391
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   392
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   393
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   394
    private static void assertEqualFunctionAt(Object v, MethodHandle x, MethodHandle y) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   395
        Object[] args = new Object[x.type().parameterCount()];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   396
        Arrays.fill(args, v);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   397
        Object xval = invokeWithCatch(x, args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   398
        Object yval = invokeWithCatch(y, args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   399
        String msg = "ok";
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   400
        if (!Objects.equals(xval, yval)) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   401
            msg = ("applying "+x+" & "+y+" to "+v);
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
        assertEquals(msg, xval, yval);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   404
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   405
    private static Object invokeWithCatch(MethodHandle mh, Object... args) throws Throwable {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   406
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   407
            return mh.invokeWithArguments(args);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   408
        } catch (Throwable ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   409
            System.out.println("threw: "+mh+Arrays.asList(args));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   410
            ex.printStackTrace();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   411
            return ex;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   412
        }
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 final Lookup LOOKUP = lookup();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   416
    private static MethodHandle findStatic(String name,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   417
                                           Class<?> rtype,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   418
                                           Class<?>... ptypes) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   419
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   420
            return LOOKUP.findStatic(LOOKUP.lookupClass(), name, methodType(rtype, ptypes));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   421
        } catch (ReflectiveOperationException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   422
            throw new RuntimeException(ex);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   423
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   424
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   425
    private static MethodHandle findStatic(String name,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   426
                                           Class<?> rtype,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   427
                                           List<?> ptypes) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   428
        return findStatic(name, rtype, ptypes.toArray(new Class<?>[ptypes.size()]));
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
    static int getProperty(String name, int dflt) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   431
        String qual = LOOKUP.lookupClass().getName();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   432
        String prop = System.getProperty(qual+"."+name);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   433
        if (prop == null)  prop = System.getProperty(name);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   434
        if (prop == null)  return dflt;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   435
        return Integer.parseInt(prop);
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
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   438
    private static int opI(int... xs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   439
        stress();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   440
        int base = 100;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   441
        int z = 0;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   442
        for (int x : xs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   443
            z = (z * base) + (x % base);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   444
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   445
        verbose("opI", xs.length, xs, z);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   446
        return z;
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
    private static int opI2(int x, int y) { return opI(x, y); }  // x*100 + y%100
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   449
    private static int opI3(int x, int y, int z) { return opI(x, y, z); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   450
    private static int opI4(int w, int x, int y, int z) { return opI(w, x, y, z); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   451
    private static int opI(int x) { return opI2(x, 37); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   452
    private static Object opI_L(int x) { return (Object) opI(x); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   453
    private static long opJ3(long x, long y, long z) { return (long) opI3((int)x, (int)y, (int)z); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   454
    private static long opJ2(long x, long y) { return (long) opI2((int)x, (int)y); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   455
    private static long opJ(long x) { return (long) opI((int)x); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   456
    private static Object opL2(Object x, Object y) { return (Object) opI2((int)x, (int)y); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   457
    private static Object opL(Object x) { return (Object) opI((int)x); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   458
    private static int opL2_I(Object x, Object y) { return (int) opI2((int)x, (int)y); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   459
    private static int opL_I(Object x) { return (int) opI((int)x); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   460
    private static long opL_J(Object x) { return (long) opI((int)x); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   461
    private static final MethodHandle opI, opI2, opI3, opI4, opI_L, opJ, opJ2, opJ3, opL2, opL, opL2_I, opL_I, opL_J;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   462
    static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   463
        opI4 = findStatic("opI4", int.class, int.class, int.class, int.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   464
        opI3 = findStatic("opI3", int.class, int.class, int.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   465
        opI2 = findStatic("opI2", int.class, int.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   466
        opI = findStatic("opI", int.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   467
        opI_L = findStatic("opI_L", Object.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   468
        opJ = findStatic("opJ", long.class, long.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   469
        opJ2 = findStatic("opJ2", long.class, long.class, long.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   470
        opJ3 = findStatic("opJ3", long.class, long.class, long.class, long.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   471
        opL2 = findStatic("opL2", Object.class, Object.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   472
        opL = findStatic("opL", Object.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   473
        opL2_I = findStatic("opL2_I", int.class, Object.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   474
        opL_I = findStatic("opL_I", int.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   475
        opL_J = findStatic("opL_J", long.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   476
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   477
    private static final MethodHandle[] INT_COLLECTORS = {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   478
        constant(int.class, 42), opI, opI2, opI3, opI4
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   479
    };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   480
    private static final MethodHandle[] LONG_COLLECTORS = {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   481
        constant(long.class, 42), opJ, opJ2, opJ3
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   482
    };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   483
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   484
    private static int addI(int x, int y) { stress(); return x+y; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   485
    private static Object addL(Object x, Object y) { return addI((int)x, (int)y); }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   486
    private static final MethodHandle addI, addL;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   487
    static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   488
        addI = findStatic("addI", int.class, int.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   489
        addL = findStatic("addL", Object.class, Object.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   490
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   491
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   492
    private static Object list8ints(int a, int b, int c, int d, int e, int f, int g, int h) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   493
        return Arrays.asList(a, b, c, d, e, f, g, h);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   494
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   495
    private static Object list8longs(long a, long b, long c, long d, long e, long f, long g, long h) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   496
        return Arrays.asList(a, b, c, d, e, f, g, h);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   497
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   498
    private static final MethodHandle list8ints = findStatic("list8ints", Object.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   499
                                                             Collections.nCopies(8, int.class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   500
    private static final MethodHandle list8longs = findStatic("list8longs", Object.class,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   501
                                                              Collections.nCopies(8, long.class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   502
    private static final MethodHandle[] INT_LISTERS, LONG_LISTERS;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   503
    static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   504
        int listerCount = list8ints.type().parameterCount() + 1;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   505
        INT_LISTERS  = new MethodHandle[listerCount];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   506
        LONG_LISTERS = new MethodHandle[listerCount];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   507
        MethodHandle lister = list8ints;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   508
        MethodHandle llister = list8longs;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   509
        for (int i = listerCount - 1; ; i--) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   510
            INT_LISTERS[i] = lister;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   511
            LONG_LISTERS[i] = llister;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   512
            if (i == 0)  break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   513
            lister  = insertArguments(lister,  i-1, (int)0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   514
            llister = insertArguments(llister, i-1, (long)0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   515
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   516
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   517
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   518
    private static Object  convI_L(int     x) { stress(); return (Object)  x; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   519
    private static int     convL_I(Object  x) { stress(); return (int)     x; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   520
    private static Object  convJ_L(long    x) { stress(); return (Object)  x; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   521
    private static long    convL_J(Object  x) { stress(); return (long)    x; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   522
    private static int     convJ_I(long    x) { stress(); return (int)     x; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   523
    private static long    convI_J(int     x) { stress(); return (long)    x; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   524
    private static final MethodHandle convI_L, convL_I, convJ_L, convL_J, convJ_I, convI_J;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   525
    static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   526
        convI_L = findStatic("convI_L", Object.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   527
        convL_I = findStatic("convL_I", int.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   528
        convJ_L = findStatic("convJ_L", Object.class, long.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   529
        convL_J = findStatic("convL_J", long.class, Object.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   530
        convJ_I = findStatic("convJ_I", int.class, long.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   531
        convI_J = findStatic("convI_J", long.class, int.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   532
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   533
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   534
    // stress modes:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   535
    private static final int REPEAT = getProperty("REPEAT", 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   536
    private static final int STRESS = getProperty("STRESS", 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   537
    private static /*v*/ int STRESS_COUNT;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   538
    private static final Object[] SINK = new Object[4];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   539
    private static void stress() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   540
        if (STRESS <= 0) return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   541
        int count = STRESS + (STRESS_COUNT++ & 0x1);  // non-constant value
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   542
        for (int i = 0; i < count; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   543
            SINK[i % SINK.length] = new Object[STRESS + i % (SINK.length + 1)];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   544
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   545
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   546
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   547
    // verbosity:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   548
    private static final int VERBOSITY = getProperty("VERBOSITY", 0) + (REPEAT == 0 ? 0 : -1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   549
    private static void verbose(Object a, Object b, Object c, Object d) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   550
        if (VERBOSITY <= 0)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   551
        verbose(1, a, b, c, d);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   552
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   553
    private static void verbose(Object a, Object b, Object c) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   554
        if (VERBOSITY <= 0)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   555
        verbose(1, a, b, c);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   556
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   557
    private static void verbose(int level, Object a, Object... bcd) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   558
        if (level > VERBOSITY)  return;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   559
        String m = a.toString();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   560
        if (bcd != null && bcd.length > 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   561
            List<Object> l = new ArrayList<>(bcd.length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   562
            for (Object x : bcd) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   563
                if (x instanceof Object[])  x = Arrays.asList((Object[])x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   564
                if (x instanceof int[])     x = Arrays.toString((int[])x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   565
                if (x instanceof long[])    x = Arrays.toString((long[])x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   566
                l.add(x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   567
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   568
            m = m+Arrays.asList(bcd);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   569
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   570
        System.out.println(m);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   571
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   572
    String testOnly;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   573
    String testOnlyTests;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   574
    private boolean startTest(String name) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   575
        if (testOnly != null && !testOnly.contains(name))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   576
            return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   577
        verbose(0, "["+name+"]");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   578
        testOnlyTests = (testOnlyTests == null) ? name : testOnlyTests+" "+name;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   579
        return true;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   580
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   581
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents:
diff changeset
   582
}