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 import org.testng.Assert; 25 import org.testng.annotations.Test; 26 27 import java.lang.reflect.code.OpTransformer; 28 import java.lang.reflect.code.op.CoreOp; 29 import java.lang.reflect.code.Op; 30 import java.lang.reflect.code.interpreter.Interpreter; 31 import java.lang.reflect.Method; 32 import java.lang.runtime.CodeReflection; 33 import java.util.Optional; 34 import java.util.stream.Stream; 35 36 /* 37 * @test 38 * @run testng TestIfOp 39 */ 40 41 public class TestIfOp { 42 43 @CodeReflection 44 public static String f(int i) { 45 String s; 46 if (i <= 1) { 47 s = "<=ONE"; 48 } else if (i == 2) { 49 s = "TWO"; 50 } else if (i == 3) { 51 s = "THREE"; 52 } else if (i == 4) { 53 s = "FOUR"; 54 } else { 55 s = ">=FIVE"; 56 } 57 return s; 58 } 59 60 static CoreOp.FuncOp getFuncOp(String name) { 61 Optional<Method> om = Stream.of(TestIfOp.class.getDeclaredMethods()) 62 .filter(m -> m.getName().equals(name)) 63 .findFirst(); 64 65 Method m = om.get(); 66 return m.getCodeModel().get(); 67 } 68 69 @Test 70 public void testf() { 71 CoreOp.FuncOp f = getFuncOp("f"); 72 73 f.writeTo(System.out); 74 75 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 76 77 lf.writeTo(System.out); 78 79 for (int i = 0; i < 6; i++) { 80 Assert.assertEquals(Interpreter.invoke(lf, i), f(i)); 81 } 82 } 83 }