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