author | psandoz |
Fri, 29 Apr 2016 13:46:19 -0700 | |
changeset 37719 | add11bc0e6e2 |
parent 37343 | 35a2231828a7 |
child 38328 | 40435a469d25 |
permissions | -rw-r--r-- |
36934 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, 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/othervm -Diters=20000 VarHandleTestMethodHandleAccessDouble |
|
27 |
*/ |
|
28 |
||
29 |
import org.testng.annotations.BeforeClass; |
|
30 |
import org.testng.annotations.DataProvider; |
|
31 |
import org.testng.annotations.Test; |
|
32 |
||
33 |
import java.lang.invoke.MethodHandles; |
|
34 |
import java.lang.invoke.VarHandle; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.List; |
|
38 |
||
39 |
import static org.testng.Assert.*; |
|
40 |
||
41 |
public class VarHandleTestMethodHandleAccessDouble extends VarHandleBaseTest { |
|
42 |
static final double static_final_v = 1.0d; |
|
43 |
||
44 |
static double static_v; |
|
45 |
||
46 |
final double final_v = 1.0d; |
|
47 |
||
48 |
double v; |
|
49 |
||
50 |
VarHandle vhFinalField; |
|
51 |
||
52 |
VarHandle vhField; |
|
53 |
||
54 |
VarHandle vhStaticField; |
|
55 |
||
56 |
VarHandle vhStaticFinalField; |
|
57 |
||
58 |
VarHandle vhArray; |
|
59 |
||
60 |
@BeforeClass |
|
61 |
public void setup() throws Exception { |
|
62 |
vhFinalField = MethodHandles.lookup().findVarHandle( |
|
63 |
VarHandleTestMethodHandleAccessDouble.class, "final_v", double.class); |
|
64 |
||
65 |
vhField = MethodHandles.lookup().findVarHandle( |
|
66 |
VarHandleTestMethodHandleAccessDouble.class, "v", double.class); |
|
67 |
||
68 |
vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
|
69 |
VarHandleTestMethodHandleAccessDouble.class, "static_final_v", double.class); |
|
70 |
||
71 |
vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
|
72 |
VarHandleTestMethodHandleAccessDouble.class, "static_v", double.class); |
|
73 |
||
74 |
vhArray = MethodHandles.arrayElementVarHandle(double[].class); |
|
75 |
} |
|
76 |
||
77 |
||
78 |
@DataProvider |
|
79 |
public Object[][] accessTestCaseProvider() throws Exception { |
|
80 |
List<AccessTestCase<?>> cases = new ArrayList<>(); |
|
81 |
||
82 |
for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) { |
|
83 |
cases.add(new MethodHandleAccessTestCase("Instance field", |
|
84 |
vhField, f, hs -> testInstanceField(this, hs))); |
|
85 |
cases.add(new MethodHandleAccessTestCase("Instance field unsupported", |
|
86 |
vhField, f, hs -> testInstanceFieldUnsupported(this, hs), |
|
87 |
false)); |
|
88 |
||
89 |
cases.add(new MethodHandleAccessTestCase("Static field", |
|
90 |
vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticField)); |
|
91 |
cases.add(new MethodHandleAccessTestCase("Static field unsupported", |
|
92 |
vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticFieldUnsupported, |
|
93 |
false)); |
|
94 |
||
95 |
cases.add(new MethodHandleAccessTestCase("Array", |
|
96 |
vhArray, f, VarHandleTestMethodHandleAccessDouble::testArray)); |
|
97 |
cases.add(new MethodHandleAccessTestCase("Array unsupported", |
|
98 |
vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayUnsupported, |
|
99 |
false)); |
|
100 |
cases.add(new MethodHandleAccessTestCase("Array index out of bounds", |
|
101 |
vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayIndexOutOfBounds, |
|
102 |
false)); |
|
103 |
} |
|
104 |
||
105 |
// Work around issue with jtreg summary reporting which truncates |
|
106 |
// the String result of Object.toString to 30 characters, hence |
|
107 |
// the first dummy argument |
|
108 |
return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
|
109 |
} |
|
110 |
||
111 |
@Test(dataProvider = "accessTestCaseProvider") |
|
112 |
public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
|
113 |
T t = atc.get(); |
|
114 |
int iters = atc.requiresLoop() ? ITERS : 1; |
|
115 |
for (int c = 0; c < iters; c++) { |
|
116 |
atc.testAccess(t); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
||
121 |
static void testInstanceField(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable { |
|
122 |
// Plain |
|
123 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
124 |
hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
125 |
double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv); |
36934 | 126 |
assertEquals(x, 1.0d, "set double value"); |
127 |
} |
|
128 |
||
129 |
||
130 |
// Volatile |
|
131 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
132 |
hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
133 |
double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); |
36934 | 134 |
assertEquals(x, 2.0d, "setVolatile double value"); |
135 |
} |
|
136 |
||
137 |
// Lazy |
|
138 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
139 |
hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
140 |
double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); |
36934 | 141 |
assertEquals(x, 1.0d, "setRelease double value"); |
142 |
} |
|
143 |
||
144 |
// Opaque |
|
145 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
146 |
hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
147 |
double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); |
36934 | 148 |
assertEquals(x, 2.0d, "setOpaque double value"); |
149 |
} |
|
150 |
||
151 |
||
152 |
} |
|
153 |
||
154 |
static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable { |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
155 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
36934 | 156 |
checkUOE(am, () -> { |
157 |
boolean r = (boolean) hs.get(am).invokeExact(recv, 1.0d, 2.0d); |
|
158 |
}); |
|
159 |
} |
|
160 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
161 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
36934 | 162 |
checkUOE(am, () -> { |
163 |
double r = (double) hs.get(am).invokeExact(recv, 1.0d, 2.0d); |
|
164 |
}); |
|
165 |
} |
|
166 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
167 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
36934 | 168 |
checkUOE(am, () -> { |
169 |
double r = (double) hs.get(am).invokeExact(recv, 1.0d); |
|
170 |
}); |
|
171 |
} |
|
172 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
173 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
36934 | 174 |
checkUOE(am, () -> { |
175 |
double r = (double) hs.get(am).invokeExact(recv, 1.0d); |
|
176 |
}); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
||
181 |
static void testStaticField(Handles hs) throws Throwable { |
|
182 |
// Plain |
|
183 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
184 |
hs.get(TestAccessMode.SET).invokeExact(1.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
185 |
double x = (double) hs.get(TestAccessMode.GET).invokeExact(); |
36934 | 186 |
assertEquals(x, 1.0d, "set double value"); |
187 |
} |
|
188 |
||
189 |
||
190 |
// Volatile |
|
191 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
192 |
hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
193 |
double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); |
36934 | 194 |
assertEquals(x, 2.0d, "setVolatile double value"); |
195 |
} |
|
196 |
||
197 |
// Lazy |
|
198 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
199 |
hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
200 |
double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); |
36934 | 201 |
assertEquals(x, 1.0d, "setRelease double value"); |
202 |
} |
|
203 |
||
204 |
// Opaque |
|
205 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
206 |
hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
207 |
double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); |
36934 | 208 |
assertEquals(x, 2.0d, "setOpaque double value"); |
209 |
} |
|
210 |
||
211 |
||
212 |
} |
|
213 |
||
214 |
static void testStaticFieldUnsupported(Handles hs) throws Throwable { |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
215 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
36934 | 216 |
checkUOE(am, () -> { |
217 |
boolean r = (boolean) hs.get(am).invokeExact(1.0d, 2.0d); |
|
218 |
}); |
|
219 |
} |
|
220 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
221 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
36934 | 222 |
checkUOE(am, () -> { |
223 |
double r = (double) hs.get(am).invokeExact(1.0d, 2.0d); |
|
224 |
}); |
|
225 |
} |
|
226 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
227 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
36934 | 228 |
checkUOE(am, () -> { |
229 |
double r = (double) hs.get(am).invokeExact(1.0d); |
|
230 |
}); |
|
231 |
} |
|
232 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
233 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
36934 | 234 |
checkUOE(am, () -> { |
235 |
double r = (double) hs.get(am).invokeExact(1.0d); |
|
236 |
}); |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
||
241 |
static void testArray(Handles hs) throws Throwable { |
|
242 |
double[] array = new double[10]; |
|
243 |
||
244 |
for (int i = 0; i < array.length; i++) { |
|
245 |
// Plain |
|
246 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
247 |
hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
248 |
double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i); |
36934 | 249 |
assertEquals(x, 1.0d, "get double value"); |
250 |
} |
|
251 |
||
252 |
||
253 |
// Volatile |
|
254 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
255 |
hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
256 |
double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); |
36934 | 257 |
assertEquals(x, 2.0d, "setVolatile double value"); |
258 |
} |
|
259 |
||
260 |
// Lazy |
|
261 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
262 |
hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
263 |
double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); |
36934 | 264 |
assertEquals(x, 1.0d, "setRelease double value"); |
265 |
} |
|
266 |
||
267 |
// Opaque |
|
268 |
{ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
269 |
hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0d); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
270 |
double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); |
36934 | 271 |
assertEquals(x, 2.0d, "setOpaque double value"); |
272 |
} |
|
273 |
||
274 |
||
275 |
} |
|
276 |
} |
|
277 |
||
278 |
static void testArrayUnsupported(Handles hs) throws Throwable { |
|
279 |
double[] array = new double[10]; |
|
280 |
||
281 |
final int i = 0; |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
282 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
36934 | 283 |
checkUOE(am, () -> { |
284 |
boolean r = (boolean) hs.get(am).invokeExact(array, i, 1.0d, 2.0d); |
|
285 |
}); |
|
286 |
} |
|
287 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
288 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
36934 | 289 |
checkUOE(am, () -> { |
290 |
double r = (double) hs.get(am).invokeExact(array, i, 1.0d, 2.0d); |
|
291 |
}); |
|
292 |
} |
|
293 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
294 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
36934 | 295 |
checkUOE(am, () -> { |
296 |
double r = (double) hs.get(am).invokeExact(array, i, 1.0d); |
|
297 |
}); |
|
298 |
} |
|
299 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
300 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
36934 | 301 |
checkUOE(am, () -> { |
302 |
double o = (double) hs.get(am).invokeExact(array, i, 1.0d); |
|
303 |
}); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { |
|
308 |
double[] array = new double[10]; |
|
309 |
||
310 |
for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
|
311 |
final int ci = i; |
|
312 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
313 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { |
36934 | 314 |
checkIOOBE(am, () -> { |
315 |
double x = (double) hs.get(am).invokeExact(array, ci); |
|
316 |
}); |
|
317 |
} |
|
318 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
319 |
for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { |
36934 | 320 |
checkIOOBE(am, () -> { |
321 |
hs.get(am).invokeExact(array, ci, 1.0d); |
|
322 |
}); |
|
323 |
} |
|
324 |
||
325 |
||
326 |
} |
|
327 |
} |
|
328 |
} |
|
329 |