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