Path: blob/master/test/jdk/java/lang/invoke/CountedLoopIterationCountsTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* @test26* @bug 816410227* @run main/othervm -ea -esa test.java.lang.invoke.CountedLoopIterationCountsTest28*/2930package test.java.lang.invoke;3132import java.lang.invoke.MethodHandle;33import java.lang.invoke.MethodHandles;34import java.lang.invoke.MethodType;3536public class CountedLoopIterationCountsTest {3738public static void main(String[] args) throws Throwable {39run(1, -10, 0);40run(1, 0, 0);41run(Integer.MAX_VALUE - 1, Integer.MIN_VALUE + 10, 0);42run(Integer.MIN_VALUE, Integer.MIN_VALUE + 4, 4);43run(Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1, 1);44run(Integer.MAX_VALUE - 1, 0, 0);45run(Integer.MAX_VALUE - 1, 10, 0);46run(Integer.MAX_VALUE - 1, -10, 0);47run(Integer.MAX_VALUE, Integer.MIN_VALUE + 10, 0);48run(Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 1);49run(Integer.MAX_VALUE, Integer.MAX_VALUE, 0);5051if (failed) {52throw new AssertionError("one or more tests failed");53}54}5556static boolean failed = false;5758private static void run(int start, int end, int expectedIterations) throws Throwable {59System.out.println("run from " + start + " to " + end);60MethodHandle loop = MethodHandles.countedLoop(61MethodHandles.constant(int.class, start), // iterate from given start (inclusive) ...62MethodHandles.constant(int.class, end), // ... to given end (exclusive)63MH_m1, // initialise loop variable to -164MH_step); // increment loop counter by one in each iteration65// The loop variable's value, and hence the loop result, will be "number of iterations" minus one.66int r = (int) loop.invoke();67if (r + 1 != expectedIterations) {68System.out.println("expected " + expectedIterations + " iterations, but got " + r);69failed = true;70}71}7273static int step(int stepCount, int counter) {74return stepCount + 1;75}7677static final MethodHandle MH_m1;78static final MethodHandle MH_step;79static {80try {81MH_m1 = MethodHandles.constant(int.class, -1);82MH_step = MethodHandles.lookup().findStatic(CountedLoopIterationCountsTest.class, "step",83MethodType.methodType(int.class, int.class, int.class));84} catch (Throwable t) {85throw new ExceptionInInitializerError(t);86}87}8889}909192