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