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