Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/invoke/util/ValueConversionsTest.java
38839 views
1
/*
2
* Copyright (c) 2009, 2013, 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
package test.sun.invoke.util;
25
26
import sun.invoke.util.ValueConversions;
27
import sun.invoke.util.Wrapper;
28
import java.lang.invoke.MethodHandles;
29
import java.lang.invoke.MethodType;
30
import java.lang.invoke.MethodHandle;
31
import java.io.Serializable;
32
import java.util.Arrays;
33
import org.junit.Test;
34
import static org.junit.Assert.*;
35
36
/* @test
37
* @summary unit tests for value-type conversion utilities
38
* @compile -XDignore.symbol.file ValueConversionsTest.java
39
* @run junit/othervm test.sun.invoke.util.ValueConversionsTest
40
*/
41
42
/**
43
*
44
* @author jrose
45
*/
46
public class ValueConversionsTest {
47
@Test
48
public void testUnbox() throws Throwable {
49
testUnbox(false);
50
}
51
52
@Test
53
public void testUnboxCast() throws Throwable {
54
testUnbox(true);
55
}
56
57
private void testUnbox(boolean doCast) throws Throwable {
58
for (Wrapper dst : Wrapper.values()) {
59
for (Wrapper src : Wrapper.values()) {
60
testUnbox(doCast, dst, src);
61
}
62
}
63
}
64
65
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
66
boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
67
if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims
68
if (dst == Wrapper.VOID || src == Wrapper.VOID ) return; // must have values
69
if (dst == Wrapper.OBJECT)
70
expectThrow = false; // everything (even VOID==null here) converts to OBJECT
71
try {
72
for (int n = -5; n < 10; n++) {
73
Object box = src.wrap(n);
74
switch (src) {
75
case VOID: assertEquals(box, null); break;
76
case OBJECT: box = box.toString(); break;
77
case SHORT: assertEquals(box.getClass(), Short.class); break;
78
default: assertEquals(box.getClass(), src.wrapperType()); break;
79
}
80
MethodHandle unboxer;
81
if (doCast)
82
unboxer = ValueConversions.unboxCast(dst);
83
else
84
unboxer = ValueConversions.unboxWiden(dst);
85
Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
86
Object result = null;
87
switch (dst) {
88
case INT: result = (int) unboxer.invokeExact(box); break;
89
case LONG: result = (long) unboxer.invokeExact(box); break;
90
case FLOAT: result = (float) unboxer.invokeExact(box); break;
91
case DOUBLE: result = (double) unboxer.invokeExact(box); break;
92
case CHAR: result = (char) unboxer.invokeExact(box); break;
93
case BYTE: result = (byte) unboxer.invokeExact(box); break;
94
case SHORT: result = (short) unboxer.invokeExact(box); break;
95
case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
96
}
97
if (expectThrow) {
98
expResult = "(need an exception)";
99
}
100
assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
101
expResult, result);
102
}
103
} catch (RuntimeException ex) {
104
if (expectThrow) return;
105
System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
106
throw ex;
107
}
108
}
109
110
@Test
111
public void testBox() throws Throwable {
112
for (Wrapper w : Wrapper.values()) {
113
if (w == Wrapper.VOID) continue; // skip this; no unboxed form
114
if (w == Wrapper.OBJECT) continue; // skip this; already unboxed
115
for (int n = -5; n < 10; n++) {
116
Object box = w.wrap(n);
117
MethodHandle boxer = ValueConversions.boxExact(w);
118
Object expResult = box;
119
Object result = null;
120
switch (w) {
121
case INT: result = (Integer) boxer.invokeExact(/*int*/n); break;
122
case LONG: result = (Long) boxer.invokeExact((long)n); break;
123
case FLOAT: result = (Float) boxer.invokeExact((float)n); break;
124
case DOUBLE: result = (Double) boxer.invokeExact((double)n); break;
125
case CHAR: result = (Character) boxer.invokeExact((char)n); break;
126
case BYTE: result = (Byte) boxer.invokeExact((byte)n); break;
127
case SHORT: result = (Short) boxer.invokeExact((short)n); break;
128
case BOOLEAN: result = (Boolean) boxer.invokeExact((n & 1) != 0); break;
129
}
130
assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
131
expResult, result);
132
}
133
}
134
}
135
136
@Test
137
public void testCast() throws Throwable {
138
Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
139
Object[] objects = { new Object(), Boolean.FALSE, "hello", (Long)12L, (Integer)6 };
140
for (Class<?> dst : types) {
141
MethodHandle caster = ValueConversions.cast().bindTo(dst);
142
assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
143
for (Object obj : objects) {
144
Class<?> src = obj.getClass();
145
boolean canCast = dst.isAssignableFrom(src);
146
try {
147
Object result = caster.invokeExact(obj);
148
if (canCast)
149
assertEquals(obj, result);
150
else
151
assertEquals("cast should not have succeeded", dst, obj);
152
} catch (ClassCastException ex) {
153
if (canCast)
154
throw ex;
155
}
156
}
157
}
158
}
159
160
@Test
161
public void testConvert() throws Throwable {
162
for (long tval = 0, ctr = 0;;) {
163
if (++ctr > 99999) throw new AssertionError("too many test values");
164
// prints 3776 test patterns (3776 = 8*59*8)
165
tval = nextTestValue(tval);
166
if (tval == 0) {
167
break; // repeat
168
}
169
}
170
for (Wrapper src : Wrapper.values()) {
171
for (Wrapper dst : Wrapper.values()) {
172
testConvert(src, dst, 0);
173
}
174
}
175
}
176
static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
177
if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims
178
if (dst == Wrapper.VOID || src == Wrapper.VOID ) return; // must have values
179
boolean testSingleCase = (tval != 0);
180
final long tvalInit = tval;
181
MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
182
MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
183
assertEquals(convType, conv.type());
184
MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
185
for (;;) {
186
long n = tval;
187
Object testValue = src.wrap(n);
188
Object expResult = dst.cast(testValue, dst.primitiveType());
189
Object result;
190
switch (src) {
191
case INT: result = converter.invokeExact((int)n); break;
192
case LONG: result = converter.invokeExact(/*long*/n); break;
193
case FLOAT: result = converter.invokeExact((float)n); break;
194
case DOUBLE: result = converter.invokeExact((double)n); break;
195
case CHAR: result = converter.invokeExact((char)n); break;
196
case BYTE: result = converter.invokeExact((byte)n); break;
197
case SHORT: result = converter.invokeExact((short)n); break;
198
case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;
199
default: throw new AssertionError();
200
}
201
assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),
202
expResult, result);
203
if (testSingleCase) break;
204
// next test value:
205
tval = nextTestValue(tval);
206
if (tval == tvalInit) break; // repeat
207
}
208
}
209
static long tweakSign(long x) {
210
// Assuming that x is mostly zeroes, make those zeroes follow bit #62 (just below the sign).
211
// This function is self-inverse.
212
final long MID_SIGN_BIT = 62;
213
long sign = -((x >>> MID_SIGN_BIT) & 1); // all ones or all zeroes
214
long flip = (sign >>> -MID_SIGN_BIT); // apply the sign below the mid-bit
215
return x ^ flip;
216
}
217
static long nextTestValue(long x) {
218
// Produce 64 bits with three component bitfields: [ high:3 | mid:58 | low:3 ].
219
// The high and low fields vary through all possible bit patterns.
220
// The middle field is either all zero or has a single bit set.
221
// For better coverage of the neighborhood of zero, an internal sign bit is xored downward also.
222
long ux = tweakSign(x); // unsign the middle field
223
final long LOW_BITS = 3, LOW_BITS_MASK = (1L << LOW_BITS)-1;
224
final long HIGH_BITS = 3, HIGH_BITS_MASK = ~(-1L >>> HIGH_BITS);
225
if ((ux & LOW_BITS_MASK) != LOW_BITS_MASK) {
226
++ux;
227
} else {
228
ux &= ~LOW_BITS_MASK;
229
long midBit = (ux & ~HIGH_BITS_MASK);
230
if (midBit == 0)
231
midBit = (1L<<LOW_BITS); // introduce a low bit
232
ux += midBit;
233
}
234
return tweakSign(ux);
235
}
236
}
237
238