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 TestConstants
 28  */
 29 
 30 import org.testng.Assert;
 31 import org.testng.annotations.DataProvider;
 32 import org.testng.annotations.Test;
 33 
 34 import java.lang.invoke.MethodHandles;
 35 import jdk.incubator.code.OpTransformer;
 36 import jdk.incubator.code.op.CoreOp;
 37 import jdk.incubator.code.Op;
 38 import jdk.incubator.code.interpreter.Interpreter;
 39 import java.lang.reflect.Method;
 40 import jdk.incubator.code.CodeReflection;
 41 import java.util.List;
 42 import java.util.Optional;
 43 import java.util.stream.Stream;
 44 
 45 public class TestConstants {
 46 
 47     @CodeReflection
 48     public static boolean c_boolean() {
 49         return true;
 50     }
 51 
 52     @CodeReflection
 53     public static boolean c_boolean_f() {
 54         return false;
 55     }
 56 
 57     @CodeReflection
 58     public static byte c_byte() {
 59         return 42;
 60     }
 61 
 62     @CodeReflection
 63     public static byte c_byte_neg() {
 64         return -42;
 65     }
 66 
 67     @CodeReflection
 68     public static short c_short() {
 69         return 42;
 70     }
 71 
 72     @CodeReflection
 73     public static short c_short_neg() {
 74         return -42;
 75     }
 76 
 77     @CodeReflection
 78     public static char c_char() {
 79         return 'A';
 80     }
 81 
 82     @CodeReflection
 83     public static int c_int() {
 84         return 42;
 85     }
 86 
 87     @CodeReflection
 88     public static int c_int_neg() {
 89         return -42;
 90     }
 91 
 92     @CodeReflection
 93     public static long c_long() {
 94         return 42L;
 95     }
 96 
 97     @CodeReflection
 98     public static long c_long_neg() {
 99         return -42L;
100     }
101 
102     @CodeReflection
103     public static float c_float() {
104         return 1.0f;
105     }
106 
107     @CodeReflection
108     public static float c_float_neg() {
109         return -1.0f;
110     }
111 
112     @CodeReflection
113     public static double c_double() {
114         return 1.0;
115     }
116 
117     @CodeReflection
118     public static double c_double_neg() {
119         return -1.0;
120     }
121 
122     @CodeReflection
123     public static String c_String() {
124         String s = "42";
125         s = null;
126         return s;
127     }
128 
129     @CodeReflection
130     public static Class<?> c_Class() {
131         return String.class;
132     }
133 
134     @CodeReflection
135     public static Class<?> c_Class_primitive() {
136         return float.class;
137     }
138 
139     @DataProvider
140     static Object[][] provider() {
141         return new Object[][] {
142                 { boolean.class },
143                 { byte.class },
144                 { short.class },
145                 { char.class },
146                 { int.class },
147                 { long.class },
148                 { float.class },
149                 { double.class },
150                 { String.class },
151                 { Class.class }
152         };
153     }
154 
155     @Test(dataProvider = "provider")
156     public void testString(Class<?> c) throws Exception {
157         String name = "c_" + c.getSimpleName();
158         List<Method> ms = Stream.of(TestConstants.class.getDeclaredMethods())
159                 .filter(m -> m.getName().startsWith(name))
160                 .toList();
161 
162         for (Method m : ms) {
163             CoreOp.FuncOp f = Op.ofMethod(m).get();
164 
165             f.writeTo(System.out);
166 
167             Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f), m.invoke(null));
168         }
169     }
170 
171     @CodeReflection
172     public static String compareNull(String s) {
173         if (s == null) {
174             return "NULL";
175         } else {
176             return s;
177         }
178     }
179 
180     @Test
181     public void testCompareNull() {
182         CoreOp.FuncOp f = getFuncOp("compareNull");
183 
184         f.writeTo(System.out);
185 
186         CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER);
187 
188         lf.writeTo(System.out);
189 
190         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), lf, (Object) null), compareNull(null));
191     }
192 
193     static CoreOp.FuncOp getFuncOp(String name) {
194         Optional<Method> om = Stream.of(TestConstants.class.getDeclaredMethods())
195                 .filter(m -> m.getName().equals(name))
196                 .findFirst();
197 
198         Method m = om.get();
199         return Op.ofMethod(m).get();
200     }
201 }