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  * @modules jdk.incubator.code
 27  * @run testng TestBinops
 28  */
 29 
 30 import jdk.incubator.code.Op;
 31 import org.testng.Assert;
 32 import org.testng.annotations.Test;
 33 
 34 import jdk.incubator.code.op.CoreOp;
 35 import jdk.incubator.code.interpreter.Interpreter;
 36 
 37 import java.lang.invoke.MethodHandles;
 38 import java.lang.reflect.Method;
 39 import jdk.incubator.code.CodeReflection;
 40 import java.util.Optional;
 41 import java.util.stream.Stream;
 42 
 43 public class TestBinops {
 44 
 45     @CodeReflection
 46     public static boolean not(boolean b) {
 47         return !b;
 48     }
 49 
 50     @Test
 51     public void testNot() {
 52         CoreOp.FuncOp f = getFuncOp("not");
 53 
 54         f.writeTo(System.out);
 55 
 56         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, true), not(true));
 57         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, false), not(false));
 58     }
 59 
 60     @CodeReflection
 61     public static int neg(int a) {
 62         return -a;
 63     }
 64 
 65     @Test
 66     public void testNeg() {
 67         CoreOp.FuncOp f = getFuncOp("neg");
 68 
 69         f.writeTo(System.out);
 70 
 71         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 42), neg(42));
 72     }
 73 
 74     @CodeReflection
 75     public static int compl(int a) {
 76         return ~a;
 77     }
 78 
 79     @Test
 80     public void testCompl() {
 81         CoreOp.FuncOp f = getFuncOp("compl");
 82 
 83         f.writeTo(System.out);
 84 
 85         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 42), compl(42));
 86     }
 87 
 88     @CodeReflection
 89     public static int mod(int a, int b) {
 90         return a % b;
 91     }
 92 
 93     @Test
 94     public void testMod() {
 95         CoreOp.FuncOp f = getFuncOp("mod");
 96 
 97         f.writeTo(System.out);
 98 
 99         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 10, 3), mod(10, 3));
100     }
101 
102     @CodeReflection
103     public static int bitand(int a, int b) {
104         return a & b;
105     }
106 
107     @Test
108     public void testBitand() {
109         CoreOp.FuncOp f = getFuncOp("bitand");
110 
111         f.writeTo(System.out);
112 
113         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 10, 3), bitand(10, 3));
114     }
115 
116     @CodeReflection
117     public static int bitor(int a, int b) {
118         return a | b;
119     }
120 
121     @Test
122     public void testBitor() {
123         CoreOp.FuncOp f = getFuncOp("bitor");
124 
125         f.writeTo(System.out);
126 
127         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 10, 3), bitor(10, 3));
128     }
129 
130     @CodeReflection
131     public static int bitxor(int a, int b) {
132         return a ^ b;
133     }
134 
135     @Test
136     public void testBitxor() {
137         CoreOp.FuncOp f = getFuncOp("bitxor");
138 
139         f.writeTo(System.out);
140 
141         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 10, 3), bitxor(10, 3));
142     }
143 
144     @CodeReflection
145     public static boolean booland(boolean a, boolean b) {
146         return a & b;
147     }
148 
149     @Test
150     public void testBooland() {
151         CoreOp.FuncOp f = getFuncOp("booland");
152 
153         f.writeTo(System.out);
154 
155         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, true, false), booland(true, false));
156     }
157 
158     @CodeReflection
159     public static boolean boolor(boolean a, boolean b) {
160         return a | b;
161     }
162 
163     @Test
164     public void testBoolor() {
165         CoreOp.FuncOp f = getFuncOp("boolor");
166 
167         f.writeTo(System.out);
168 
169         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, false, true), boolor(false, true));
170     }
171 
172     @CodeReflection
173     public static boolean boolxor(boolean a, boolean b) {
174         return a ^ b;
175     }
176 
177     @Test
178     public void testBoolxor() {
179         CoreOp.FuncOp f = getFuncOp("boolxor");
180 
181         f.writeTo(System.out);
182 
183         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, true, true), boolxor(true, true));
184     }
185 
186     @CodeReflection
187     public static double doublemod(double a, double b) {
188         return a % b;
189     }
190 
191     @Test
192     public void testDoublemod() {
193         CoreOp.FuncOp f = getFuncOp("doublemod");
194 
195         f.writeTo(System.out);
196 
197         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, 15.6, 2.1), doublemod(15.6, 2.1));
198     }
199 
200     static CoreOp.FuncOp getFuncOp(String name) {
201         Optional<Method> om = Stream.of(TestBinops.class.getDeclaredMethods())
202                 .filter(m -> m.getName().equals(name))
203                 .findFirst();
204 
205         Method m = om.get();
206         return Op.ofMethod(m).get();
207     }
208 }