jdk/test/java/lang/invoke/CountedLoopIterationCountsTest.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 41207 813a335bcb0c
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40418
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     1
/*
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     4
 *
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    10
 *
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    15
 * accompanied this code).
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    16
 *
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    20
 *
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    23
 * questions.
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    24
 */
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    25
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    26
/* @test
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    27
 * @bug 8164102
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    28
 * @run main/othervm -ea -esa test.java.lang.invoke.CountedLoopIterationCountsTest
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    29
 */
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    30
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    31
package test.java.lang.invoke;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    32
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    33
import java.lang.invoke.MethodHandle;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    34
import java.lang.invoke.MethodHandles;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    35
import java.lang.invoke.MethodType;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    36
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    37
public class CountedLoopIterationCountsTest {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    38
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    39
    public static void main(String[] args) throws Throwable {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    40
        run(1, -10, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    41
        run(1, 0, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    42
        run(Integer.MAX_VALUE - 1, Integer.MIN_VALUE + 10, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    43
        run(Integer.MIN_VALUE, Integer.MIN_VALUE + 4, 4);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    44
        run(Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1, 1);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    45
        run(Integer.MAX_VALUE - 1, 0, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    46
        run(Integer.MAX_VALUE - 1, 10, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    47
        run(Integer.MAX_VALUE - 1, -10, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    48
        run(Integer.MAX_VALUE, Integer.MIN_VALUE + 10, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    49
        run(Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 1);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    50
        run(Integer.MAX_VALUE, Integer.MAX_VALUE, 0);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    51
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    52
        if (failed) {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    53
            throw new AssertionError("one or more tests failed");
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    54
        }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    55
    }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    56
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    57
    static boolean failed = false;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    58
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    59
    private static void run(int start, int end, int expectedIterations) throws Throwable {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    60
        System.out.println("run from " + start + " to " + end);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    61
        MethodHandle loop = MethodHandles.countedLoop(
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    62
                MethodHandles.constant(int.class, start), // iterate from given start (inclusive) ...
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    63
                MethodHandles.constant(int.class, end),   // ... to given end (exclusive)
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    64
                MH_m1,                                    // initialise loop variable to -1
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    65
                MH_step);                                 // increment loop counter by one in each iteration
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    66
        // The loop variable's value, and hence the loop result, will be "number of iterations" minus one.
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    67
        int r = (int) loop.invoke();
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    68
        if (r + 1 != expectedIterations) {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    69
            System.out.println("expected " + expectedIterations + " iterations, but got " + r);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    70
            failed = true;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    71
        }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    72
    }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    73
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 40418
diff changeset
    74
    static int step(int stepCount, int counter) {
40418
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    75
        return stepCount + 1;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    76
    }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    77
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    78
    static final MethodHandle MH_m1;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    79
    static final MethodHandle MH_step;
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    80
    static {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    81
        try {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    82
            MH_m1 = MethodHandles.constant(int.class, -1);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    83
            MH_step = MethodHandles.lookup().findStatic(CountedLoopIterationCountsTest.class, "step",
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    84
                    MethodType.methodType(int.class, int.class, int.class));
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    85
        } catch (Throwable t) {
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    86
            throw new ExceptionInInitializerError(t);
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    87
        }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    88
    }
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    89
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents:
diff changeset
    90
}