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