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 jdk.incubator.code.OpTransformer; 28 import jdk.incubator.code.dialect.core.CoreOp; 29 import jdk.incubator.code.Op; 30 import jdk.incubator.code.interpreter.Interpreter; 31 32 import java.lang.invoke.MethodHandles; 33 import java.lang.reflect.Method; 34 import jdk.incubator.code.CodeReflection; 35 import java.util.ArrayList; 36 import java.util.List; 37 import java.util.Optional; 38 import java.util.stream.Stream; 39 40 /* 41 * @test 42 * @modules jdk.incubator.code 43 * @run testng TestConditionalOp 44 */ 45 46 public class TestConditionalOp { 47 48 @CodeReflection 49 static boolean f(boolean a, boolean b, boolean c, List<String> l) { 50 return F.a(a, l) || (F.b(b, l) && F.c(c, l)); 51 } 52 53 static class F { 54 static boolean a(boolean a, List<String> l) { 55 l.add("a"); 56 return a; 57 } 58 59 static boolean b(boolean b, List<String> l) { 60 l.add("b"); 61 return b; 62 } 63 64 static boolean c(boolean c, List<String> l) { 65 l.add("c"); 66 return c; 67 } 68 } 69 70 static CoreOp.FuncOp getFuncOp(String name) { 71 Optional<Method> om = Stream.of(TestConditionalOp.class.getDeclaredMethods()) 72 .filter(m -> m.getName().equals(name)) 73 .findFirst(); 74 75 Method m = om.get(); 76 return Op.ofMethod(m).get(); 77 } 78 79 @Test 80 public void testf() { 81 CoreOp.FuncOp f = getFuncOp("f"); 82 83 System.out.println(f.toText()); 84 85 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 86 87 System.out.println(lf.toText()); 88 89 for (int i = 0; i < 8; i++) { 90 boolean a = (i & 1) != 0; 91 boolean b = (i & 2) != 0; 92 boolean c = (i & 4) != 0; 93 List<String> la = new ArrayList<>(); 94 boolean ra = (boolean) Interpreter.invoke(MethodHandles.lookup(), lf, a, b, c, la); 95 96 List<String> le = new ArrayList<>(); 97 boolean re = f(a, b, c, le); 98 99 Assert.assertEquals(ra, re); 100 Assert.assertEquals(la, le); 101 } 102 } 103 }