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