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 TestPrimitiveCast
 28  */
 29 
 30 import jdk.incubator.code.Op;
 31 import org.testng.Assert;
 32 import org.testng.annotations.DataProvider;
 33 import org.testng.annotations.Test;
 34 
 35 import jdk.incubator.code.op.CoreOp;
 36 import jdk.incubator.code.interpreter.Interpreter;
 37 import java.lang.invoke.MethodHandles;
 38 import java.lang.reflect.Method;
 39 import jdk.incubator.code.CodeReflection;
 40 import java.util.Optional;
 41 import java.util.function.Function;
 42 import java.util.stream.Stream;
 43 
 44 import static java.util.stream.Collectors.joining;
 45 
 46 public class TestPrimitiveCast {
 47 
 48     static final Function<Object, String> FROM_DOUBLE = v -> fromDouble((double) v);
 49 
 50     @CodeReflection
 51     @SuppressWarnings("cast")
 52     static String fromDouble(double v) {
 53         double d = (double) v;
 54         float f = (float) v;
 55         long l = (long) v;
 56         int i = (int) v;
 57         short s = (short) v;
 58         char c = (char) v;
 59         byte b = (byte) v;
 60         return collect(d, f, l, i, s, c, b);
 61     }
 62 
 63     static final Function<Object, String> FROM_FLOAT = v -> fromFloat((float) v);
 64 
 65     @CodeReflection
 66     @SuppressWarnings("cast")
 67     static String fromFloat(float v) {
 68         double d = (double) v;
 69         float f = (float) v;
 70         long l = (long) v;
 71         int i = (int) v;
 72         short s = (short) v;
 73         char c = (char) v;
 74         byte b = (byte) v;
 75         return collect(d, f, l, i, s, c, b);
 76     }
 77 
 78     static final Function<Object, String> FROM_LONG = v -> fromLong((long) v);
 79 
 80     @CodeReflection
 81     @SuppressWarnings("cast")
 82     static String fromLong(long v) {
 83         double d = (double) v;
 84         float f = (float) v;
 85         long l = (long) v;
 86         int i = (int) v;
 87         short s = (short) v;
 88         char c = (char) v;
 89         byte b = (byte) v;
 90         return collect(d, f, l, i, s, c, b);
 91     }
 92 
 93     static final Function<Object, String> FROM_INT = v -> fromInt((int) v);
 94 
 95     @CodeReflection
 96     @SuppressWarnings("cast")
 97     static String fromInt(int v) {
 98         double d = (double) v;
 99         float f = (float) v;
100         long l = (long) v;
101         int i = (int) v;
102         short s = (short) v;
103         char c = (char) v;
104         byte b = (byte) v;
105         return collect(d, f, l, i, s, c, b);
106     }
107 
108     static final Function<Object, String> FROM_SHORT = v -> fromShort((short) v);
109 
110     @CodeReflection
111     @SuppressWarnings("cast")
112     static String fromShort(short v) {
113         double d = (double) v;
114         float f = (float) v;
115         long l = (long) v;
116         int i = (int) v;
117         short s = (short) v;
118         char c = (char) v;
119         byte b = (byte) v;
120         return collect(d, f, l, i, s, c, b);
121     }
122 
123     static final Function<Object, String> FROM_CHAR = v -> fromChar((char) v);
124 
125     @CodeReflection
126     @SuppressWarnings("cast")
127     static String fromChar(char v) {
128         double d = (double) v;
129         float f = (float) v;
130         long l = (long) v;
131         int i = (int) v;
132         short s = (short) v;
133         char c = (char) v;
134         byte b = (byte) v;
135         return collect(d, f, l, i, s, c, b);
136     }
137 
138     static final Function<Object, String> FROM_BYTE = v -> fromByte((byte) v);
139 
140     @CodeReflection
141     @SuppressWarnings("cast")
142     static String fromByte(byte v) {
143         double d = (double) v;
144         float f = (float) v;
145         long l = (long) v;
146         int i = (int) v;
147         short s = (short) v;
148         char c = (char) v;
149         byte b = (byte) v;
150         return collect(d, f, l, i, s, c, b);
151     }
152 
153     @DataProvider
154     static Object[][] fromMethods() {
155         return new Object[][] {
156                 { "fromDouble", Math.PI, FROM_DOUBLE},
157                 { "fromDouble", 65.1, FROM_DOUBLE},
158                 { "fromFloat", (float) Math.PI, FROM_FLOAT},
159                 { "fromFloat", 65.1f, FROM_FLOAT},
160                 { "fromLong", Long.MAX_VALUE, FROM_LONG},
161                 { "fromInt", Integer.MAX_VALUE, FROM_INT},
162                 { "fromShort", Short.MAX_VALUE, FROM_SHORT},
163                 { "fromChar", Character.MAX_VALUE, FROM_CHAR},
164                 { "fromByte", Byte.MAX_VALUE, FROM_BYTE},
165         };
166     };
167 
168     @Test(dataProvider = "fromMethods")
169     public void testFromDouble(String name, Object value, Function<Object, String> m) {
170         CoreOp.FuncOp f = getFuncOp(name);
171         Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), f, value), m.apply(value));
172     }
173 
174 
175     static String collect(Object... values) {
176         return Stream.of(values).map(Object::toString).collect(joining(" "));
177     }
178 
179     static CoreOp.FuncOp getFuncOp(String name) {
180         Optional<Method> om = Stream.of(TestPrimitiveCast.class.getDeclaredMethods())
181                 .filter(m -> m.getName().equals(name))
182                 .findFirst();
183 
184         Method m = om.get();
185         return Op.ofMethod(m).get();
186     }
187 }