1 /* 2 * Copyright (c) 2024, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @run testng TestBreakContinue 27 */ 28 29 import org.testng.Assert; 30 import org.testng.annotations.Test; 31 32 import java.lang.reflect.code.OpTransformer; 33 import java.lang.reflect.code.op.CoreOp; 34 import java.lang.reflect.code.Op; 35 import java.lang.reflect.code.interpreter.Interpreter; 36 import java.lang.reflect.Method; 37 import java.lang.runtime.CodeReflection; 38 import java.util.BitSet; 39 import java.util.Optional; 40 import java.util.function.IntUnaryOperator; 41 import java.util.stream.Stream; 42 43 public class TestBreakContinue { 44 45 @CodeReflection 46 public static BitSet forLoopBreakContinue(IntUnaryOperator f) { 47 BitSet b = new BitSet(); 48 for (int i = 0; i < 8; i++) { 49 b.set(i); 50 int r = f.applyAsInt(i); 51 if (r == 0) { 52 continue; 53 } else if (r == 1) { 54 break; 55 } 56 b.set(i * 2); 57 } 58 return b; 59 } 60 61 @Test 62 public void testForLoopBreakContinue() { 63 CoreOp.FuncOp f = getFuncOp("forLoopBreakContinue"); 64 65 f.writeTo(System.out); 66 67 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 68 69 lf.writeTo(System.out); 70 71 IntUnaryOperator o = i -> { 72 if (i <= 3) return -1; 73 if (i <= 5) return 0; 74 return 1; 75 }; 76 Assert.assertEquals(Interpreter.invoke(lf, o), forLoopBreakContinue(o)); 77 } 78 79 @CodeReflection 80 public static BitSet nestedForLoopBreakContinue(IntUnaryOperator f) { 81 BitSet b = new BitSet(); 82 for (int j = 0; j < 8; j++) { 83 b.set(j); 84 int r = f.applyAsInt(j); 85 if (r == 0) { 86 continue; 87 } else if (r == 1) { 88 break; 89 } 90 for (int i = 8; i < 16; i++) { 91 b.set(i); 92 r = f.applyAsInt(i); 93 if (r == 2) { 94 continue; 95 } else if (r == 3) { 96 break; 97 } 98 b.set(i * 2); 99 } 100 b.set(j * 2); 101 } 102 return b; 103 } 104 105 @Test 106 public void testNestedForLoopBreakContinue() { 107 CoreOp.FuncOp f = getFuncOp("nestedForLoopBreakContinue"); 108 109 f.writeTo(System.out); 110 111 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 112 113 lf.writeTo(System.out); 114 115 for (int r = -1; r < 4; r++) { 116 int fr = r; 117 IntUnaryOperator o = i -> fr; 118 Assert.assertEquals(Interpreter.invoke(lf, o), nestedForLoopBreakContinue(o)); 119 } 120 } 121 122 123 @CodeReflection 124 public static BitSet forLoopLabeledBreakContinue(IntUnaryOperator f) { 125 BitSet b = new BitSet(); 126 outer: for (int j = 0; j < 8; j++) { 127 b.set(j); 128 int r = f.applyAsInt(j); 129 if (r == 0) { 130 continue outer; 131 } else if (r == 1) { 132 break outer; 133 } 134 inner: for (int i = 8; i < 16; i++) { 135 b.set(i); 136 r = f.applyAsInt(i); 137 if (r == 2) { 138 continue inner; 139 } else if (r == 3) { 140 break inner; 141 } else if (r == 4) { 142 continue outer; 143 } else if (r == 5) { 144 break outer; 145 } 146 b.set(i * 2); 147 } 148 b.set(j * 2); 149 } 150 return b; 151 } 152 153 @Test 154 public void testForLoopLabeledBreakContinue() { 155 CoreOp.FuncOp f = getFuncOp("forLoopLabeledBreakContinue"); 156 157 f.writeTo(System.out); 158 159 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 160 161 lf.writeTo(System.out); 162 163 for (int r = -1; r < 6; r++) { 164 int fr = r; 165 IntUnaryOperator o = i -> fr; 166 Assert.assertEquals(Interpreter.invoke(lf, o), forLoopLabeledBreakContinue(o)); 167 } 168 } 169 170 @CodeReflection 171 public static BitSet blockBreak(IntUnaryOperator f) { 172 BitSet b = new BitSet(); 173 a: b: { 174 b.set(1); 175 if (f.applyAsInt(1) != 0) { 176 break a; 177 } 178 b.set(2); 179 if (f.applyAsInt(2) != 0) { 180 break b; 181 } 182 b.set(3); 183 c: { 184 b.set(4); 185 if (f.applyAsInt(4) != 0) { 186 break a; 187 } 188 b.set(5); 189 if (f.applyAsInt(5) != 0) { 190 break b; 191 } 192 b.set(6); 193 if (f.applyAsInt(6) != 0) { 194 break c; 195 } 196 b.set(7); 197 } 198 b.set(8); 199 } 200 return b; 201 } 202 203 @Test 204 public void testBlockBreak() { 205 CoreOp.FuncOp f = getFuncOp("blockBreak"); 206 207 f.writeTo(System.out); 208 209 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 210 211 lf.writeTo(System.out); 212 213 for (int i = 0; i < 7; i++) { 214 int fi = i; 215 IntUnaryOperator o = v -> v == fi ? 1 : 0; 216 Assert.assertEquals(Interpreter.invoke(lf, o), blockBreak(o)); 217 } 218 } 219 220 221 static CoreOp.FuncOp getFuncOp(String name) { 222 Optional<Method> om = Stream.of(TestBreakContinue.class.getDeclaredMethods()) 223 .filter(m -> m.getName().equals(name)) 224 .findFirst(); 225 226 Method m = om.get(); 227 return m.getCodeModel().get(); 228 } 229 }