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