author | psandoz |
Tue, 17 May 2016 12:06:41 +0200 | |
changeset 38328 | 40435a469d25 |
parent 37719 | add11bc0e6e2 |
child 38368 | c8eb5d6812c5 |
permissions | -rw-r--r-- |
36934 | 1 |
/* |
2 |
* Copyright (c) 2015, 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=10 -Xint VarHandleTestAccessLong |
|
27 |
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessLong |
|
28 |
* @run testng/othervm -Diters=20000 VarHandleTestAccessLong |
|
29 |
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccessLong |
|
38328
40435a469d25
8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents:
37719
diff
changeset
|
30 |
* @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestAccessLong |
36934 | 31 |
*/ |
32 |
||
33 |
import org.testng.annotations.BeforeClass; |
|
34 |
import org.testng.annotations.DataProvider; |
|
35 |
import org.testng.annotations.Test; |
|
36 |
||
37 |
import java.lang.invoke.MethodHandles; |
|
38 |
import java.lang.invoke.VarHandle; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Arrays; |
|
41 |
import java.util.List; |
|
42 |
||
43 |
import static org.testng.Assert.*; |
|
44 |
||
45 |
public class VarHandleTestAccessLong extends VarHandleBaseTest { |
|
46 |
static final long static_final_v = 1L; |
|
47 |
||
48 |
static long static_v; |
|
49 |
||
50 |
final long final_v = 1L; |
|
51 |
||
52 |
long v; |
|
53 |
||
54 |
VarHandle vhFinalField; |
|
55 |
||
56 |
VarHandle vhField; |
|
57 |
||
58 |
VarHandle vhStaticField; |
|
59 |
||
60 |
VarHandle vhStaticFinalField; |
|
61 |
||
62 |
VarHandle vhArray; |
|
63 |
||
64 |
@BeforeClass |
|
65 |
public void setup() throws Exception { |
|
66 |
vhFinalField = MethodHandles.lookup().findVarHandle( |
|
67 |
VarHandleTestAccessLong.class, "final_v", long.class); |
|
68 |
||
69 |
vhField = MethodHandles.lookup().findVarHandle( |
|
70 |
VarHandleTestAccessLong.class, "v", long.class); |
|
71 |
||
72 |
vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
|
73 |
VarHandleTestAccessLong.class, "static_final_v", long.class); |
|
74 |
||
75 |
vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
|
76 |
VarHandleTestAccessLong.class, "static_v", long.class); |
|
77 |
||
78 |
vhArray = MethodHandles.arrayElementVarHandle(long[].class); |
|
79 |
} |
|
80 |
||
81 |
||
82 |
@DataProvider |
|
83 |
public Object[][] varHandlesProvider() throws Exception { |
|
84 |
List<VarHandle> vhs = new ArrayList<>(); |
|
85 |
vhs.add(vhField); |
|
86 |
vhs.add(vhStaticField); |
|
87 |
vhs.add(vhArray); |
|
88 |
||
89 |
return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new); |
|
90 |
} |
|
91 |
||
92 |
@Test(dataProvider = "varHandlesProvider") |
|
93 |
public void testIsAccessModeSupported(VarHandle vh) { |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
94 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
95 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
96 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
97 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
98 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
99 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
100 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
101 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE)); |
36934 | 102 |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
103 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
104 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
105 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
106 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
107 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); |
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
108 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE)); |
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
109 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
110 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
111 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); |
36934 | 112 |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
113 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
114 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET)); |
36934 | 115 |
} |
116 |
||
117 |
||
118 |
@DataProvider |
|
119 |
public Object[][] typesProvider() throws Exception { |
|
120 |
List<Object[]> types = new ArrayList<>(); |
|
121 |
types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessLong.class)}); |
|
122 |
types.add(new Object[] {vhStaticField, Arrays.asList()}); |
|
123 |
types.add(new Object[] {vhArray, Arrays.asList(long[].class, int.class)}); |
|
124 |
||
125 |
return types.stream().toArray(Object[][]::new); |
|
126 |
} |
|
127 |
||
128 |
@Test(dataProvider = "typesProvider") |
|
129 |
public void testTypes(VarHandle vh, List<Class<?>> pts) { |
|
130 |
assertEquals(vh.varType(), long.class); |
|
131 |
||
132 |
assertEquals(vh.coordinateTypes(), pts); |
|
133 |
||
134 |
testTypes(vh); |
|
135 |
} |
|
136 |
||
137 |
||
138 |
@Test |
|
139 |
public void testLookupInstanceToStatic() { |
|
140 |
checkIAE("Lookup of static final field to instance final field", () -> { |
|
141 |
MethodHandles.lookup().findStaticVarHandle( |
|
142 |
VarHandleTestAccessLong.class, "final_v", long.class); |
|
143 |
}); |
|
144 |
||
145 |
checkIAE("Lookup of static field to instance field", () -> { |
|
146 |
MethodHandles.lookup().findStaticVarHandle( |
|
147 |
VarHandleTestAccessLong.class, "v", long.class); |
|
148 |
}); |
|
149 |
} |
|
150 |
||
151 |
@Test |
|
152 |
public void testLookupStaticToInstance() { |
|
153 |
checkIAE("Lookup of instance final field to static final field", () -> { |
|
154 |
MethodHandles.lookup().findVarHandle( |
|
155 |
VarHandleTestAccessLong.class, "static_final_v", long.class); |
|
156 |
}); |
|
157 |
||
158 |
checkIAE("Lookup of instance field to static field", () -> { |
|
159 |
vhStaticField = MethodHandles.lookup().findVarHandle( |
|
160 |
VarHandleTestAccessLong.class, "static_v", long.class); |
|
161 |
}); |
|
162 |
} |
|
163 |
||
164 |
||
165 |
@DataProvider |
|
166 |
public Object[][] accessTestCaseProvider() throws Exception { |
|
167 |
List<AccessTestCase<?>> cases = new ArrayList<>(); |
|
168 |
||
169 |
cases.add(new VarHandleAccessTestCase("Instance final field", |
|
170 |
vhFinalField, vh -> testInstanceFinalField(this, vh))); |
|
171 |
cases.add(new VarHandleAccessTestCase("Instance final field unsupported", |
|
172 |
vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh), |
|
173 |
false)); |
|
174 |
||
175 |
cases.add(new VarHandleAccessTestCase("Static final field", |
|
176 |
vhStaticFinalField, VarHandleTestAccessLong::testStaticFinalField)); |
|
177 |
cases.add(new VarHandleAccessTestCase("Static final field unsupported", |
|
178 |
vhStaticFinalField, VarHandleTestAccessLong::testStaticFinalFieldUnsupported, |
|
179 |
false)); |
|
180 |
||
181 |
cases.add(new VarHandleAccessTestCase("Instance field", |
|
182 |
vhField, vh -> testInstanceField(this, vh))); |
|
183 |
cases.add(new VarHandleAccessTestCase("Instance field unsupported", |
|
184 |
vhField, vh -> testInstanceFieldUnsupported(this, vh), |
|
185 |
false)); |
|
186 |
||
187 |
cases.add(new VarHandleAccessTestCase("Static field", |
|
188 |
vhStaticField, VarHandleTestAccessLong::testStaticField)); |
|
189 |
cases.add(new VarHandleAccessTestCase("Static field unsupported", |
|
190 |
vhStaticField, VarHandleTestAccessLong::testStaticFieldUnsupported, |
|
191 |
false)); |
|
192 |
||
193 |
cases.add(new VarHandleAccessTestCase("Array", |
|
194 |
vhArray, VarHandleTestAccessLong::testArray)); |
|
195 |
cases.add(new VarHandleAccessTestCase("Array unsupported", |
|
196 |
vhArray, VarHandleTestAccessLong::testArrayUnsupported, |
|
197 |
false)); |
|
198 |
cases.add(new VarHandleAccessTestCase("Array index out of bounds", |
|
199 |
vhArray, VarHandleTestAccessLong::testArrayIndexOutOfBounds, |
|
200 |
false)); |
|
201 |
||
202 |
// Work around issue with jtreg summary reporting which truncates |
|
203 |
// the String result of Object.toString to 30 characters, hence |
|
204 |
// the first dummy argument |
|
205 |
return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
|
206 |
} |
|
207 |
||
208 |
@Test(dataProvider = "accessTestCaseProvider") |
|
209 |
public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
|
210 |
T t = atc.get(); |
|
211 |
int iters = atc.requiresLoop() ? ITERS : 1; |
|
212 |
for (int c = 0; c < iters; c++) { |
|
213 |
atc.testAccess(t); |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
||
218 |
||
219 |
||
220 |
static void testInstanceFinalField(VarHandleTestAccessLong recv, VarHandle vh) { |
|
221 |
// Plain |
|
222 |
{ |
|
223 |
long x = (long) vh.get(recv); |
|
224 |
assertEquals(x, 1L, "get long value"); |
|
225 |
} |
|
226 |
||
227 |
||
228 |
// Volatile |
|
229 |
{ |
|
230 |
long x = (long) vh.getVolatile(recv); |
|
231 |
assertEquals(x, 1L, "getVolatile long value"); |
|
232 |
} |
|
233 |
||
234 |
// Lazy |
|
235 |
{ |
|
236 |
long x = (long) vh.getAcquire(recv); |
|
237 |
assertEquals(x, 1L, "getRelease long value"); |
|
238 |
} |
|
239 |
||
240 |
// Opaque |
|
241 |
{ |
|
242 |
long x = (long) vh.getOpaque(recv); |
|
243 |
assertEquals(x, 1L, "getOpaque long value"); |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
static void testInstanceFinalFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) { |
|
248 |
checkUOE(() -> { |
|
249 |
vh.set(recv, 2L); |
|
250 |
}); |
|
251 |
||
252 |
checkUOE(() -> { |
|
253 |
vh.setVolatile(recv, 2L); |
|
254 |
}); |
|
255 |
||
256 |
checkUOE(() -> { |
|
257 |
vh.setRelease(recv, 2L); |
|
258 |
}); |
|
259 |
||
260 |
checkUOE(() -> { |
|
261 |
vh.setOpaque(recv, 2L); |
|
262 |
}); |
|
263 |
||
264 |
||
265 |
} |
|
266 |
||
267 |
||
268 |
static void testStaticFinalField(VarHandle vh) { |
|
269 |
// Plain |
|
270 |
{ |
|
271 |
long x = (long) vh.get(); |
|
272 |
assertEquals(x, 1L, "get long value"); |
|
273 |
} |
|
274 |
||
275 |
||
276 |
// Volatile |
|
277 |
{ |
|
278 |
long x = (long) vh.getVolatile(); |
|
279 |
assertEquals(x, 1L, "getVolatile long value"); |
|
280 |
} |
|
281 |
||
282 |
// Lazy |
|
283 |
{ |
|
284 |
long x = (long) vh.getAcquire(); |
|
285 |
assertEquals(x, 1L, "getRelease long value"); |
|
286 |
} |
|
287 |
||
288 |
// Opaque |
|
289 |
{ |
|
290 |
long x = (long) vh.getOpaque(); |
|
291 |
assertEquals(x, 1L, "getOpaque long value"); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
static void testStaticFinalFieldUnsupported(VarHandle vh) { |
|
296 |
checkUOE(() -> { |
|
297 |
vh.set(2L); |
|
298 |
}); |
|
299 |
||
300 |
checkUOE(() -> { |
|
301 |
vh.setVolatile(2L); |
|
302 |
}); |
|
303 |
||
304 |
checkUOE(() -> { |
|
305 |
vh.setRelease(2L); |
|
306 |
}); |
|
307 |
||
308 |
checkUOE(() -> { |
|
309 |
vh.setOpaque(2L); |
|
310 |
}); |
|
311 |
||
312 |
||
313 |
} |
|
314 |
||
315 |
||
316 |
static void testInstanceField(VarHandleTestAccessLong recv, VarHandle vh) { |
|
317 |
// Plain |
|
318 |
{ |
|
319 |
vh.set(recv, 1L); |
|
320 |
long x = (long) vh.get(recv); |
|
321 |
assertEquals(x, 1L, "set long value"); |
|
322 |
} |
|
323 |
||
324 |
||
325 |
// Volatile |
|
326 |
{ |
|
327 |
vh.setVolatile(recv, 2L); |
|
328 |
long x = (long) vh.getVolatile(recv); |
|
329 |
assertEquals(x, 2L, "setVolatile long value"); |
|
330 |
} |
|
331 |
||
332 |
// Lazy |
|
333 |
{ |
|
334 |
vh.setRelease(recv, 1L); |
|
335 |
long x = (long) vh.getAcquire(recv); |
|
336 |
assertEquals(x, 1L, "setRelease long value"); |
|
337 |
} |
|
338 |
||
339 |
// Opaque |
|
340 |
{ |
|
341 |
vh.setOpaque(recv, 2L); |
|
342 |
long x = (long) vh.getOpaque(recv); |
|
343 |
assertEquals(x, 2L, "setOpaque long value"); |
|
344 |
} |
|
345 |
||
346 |
vh.set(recv, 1L); |
|
347 |
||
348 |
// Compare |
|
349 |
{ |
|
350 |
boolean r = vh.compareAndSet(recv, 1L, 2L); |
|
351 |
assertEquals(r, true, "success compareAndSet long"); |
|
352 |
long x = (long) vh.get(recv); |
|
353 |
assertEquals(x, 2L, "success compareAndSet long value"); |
|
354 |
} |
|
355 |
||
356 |
{ |
|
357 |
boolean r = vh.compareAndSet(recv, 1L, 3L); |
|
358 |
assertEquals(r, false, "failing compareAndSet long"); |
|
359 |
long x = (long) vh.get(recv); |
|
360 |
assertEquals(x, 2L, "failing compareAndSet long value"); |
|
361 |
} |
|
362 |
||
363 |
{ |
|
364 |
long r = (long) vh.compareAndExchangeVolatile(recv, 2L, 1L); |
|
365 |
assertEquals(r, 2L, "success compareAndExchangeVolatile long"); |
|
366 |
long x = (long) vh.get(recv); |
|
367 |
assertEquals(x, 1L, "success compareAndExchangeVolatile long value"); |
|
368 |
} |
|
369 |
||
370 |
{ |
|
371 |
long r = (long) vh.compareAndExchangeVolatile(recv, 2L, 3L); |
|
372 |
assertEquals(r, 1L, "failing compareAndExchangeVolatile long"); |
|
373 |
long x = (long) vh.get(recv); |
|
374 |
assertEquals(x, 1L, "failing compareAndExchangeVolatile long value"); |
|
375 |
} |
|
376 |
||
377 |
{ |
|
378 |
long r = (long) vh.compareAndExchangeAcquire(recv, 1L, 2L); |
|
379 |
assertEquals(r, 1L, "success compareAndExchangeAcquire long"); |
|
380 |
long x = (long) vh.get(recv); |
|
381 |
assertEquals(x, 2L, "success compareAndExchangeAcquire long value"); |
|
382 |
} |
|
383 |
||
384 |
{ |
|
385 |
long r = (long) vh.compareAndExchangeAcquire(recv, 1L, 3L); |
|
386 |
assertEquals(r, 2L, "failing compareAndExchangeAcquire long"); |
|
387 |
long x = (long) vh.get(recv); |
|
388 |
assertEquals(x, 2L, "failing compareAndExchangeAcquire long value"); |
|
389 |
} |
|
390 |
||
391 |
{ |
|
392 |
long r = (long) vh.compareAndExchangeRelease(recv, 2L, 1L); |
|
393 |
assertEquals(r, 2L, "success compareAndExchangeRelease long"); |
|
394 |
long x = (long) vh.get(recv); |
|
395 |
assertEquals(x, 1L, "success compareAndExchangeRelease long value"); |
|
396 |
} |
|
397 |
||
398 |
{ |
|
399 |
long r = (long) vh.compareAndExchangeRelease(recv, 2L, 3L); |
|
400 |
assertEquals(r, 1L, "failing compareAndExchangeRelease long"); |
|
401 |
long x = (long) vh.get(recv); |
|
402 |
assertEquals(x, 1L, "failing compareAndExchangeRelease long value"); |
|
403 |
} |
|
404 |
||
405 |
{ |
|
406 |
boolean r = vh.weakCompareAndSet(recv, 1L, 2L); |
|
407 |
assertEquals(r, true, "weakCompareAndSet long"); |
|
408 |
long x = (long) vh.get(recv); |
|
409 |
assertEquals(x, 2L, "weakCompareAndSet long value"); |
|
410 |
} |
|
411 |
||
412 |
{ |
|
413 |
boolean r = vh.weakCompareAndSetAcquire(recv, 2L, 1L); |
|
414 |
assertEquals(r, true, "weakCompareAndSetAcquire long"); |
|
415 |
long x = (long) vh.get(recv); |
|
416 |
assertEquals(x, 1L, "weakCompareAndSetAcquire long"); |
|
417 |
} |
|
418 |
||
419 |
{ |
|
420 |
boolean r = vh.weakCompareAndSetRelease(recv, 1L, 2L); |
|
421 |
assertEquals(r, true, "weakCompareAndSetRelease long"); |
|
422 |
long x = (long) vh.get(recv); |
|
423 |
assertEquals(x, 2L, "weakCompareAndSetRelease long"); |
|
424 |
} |
|
425 |
||
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
426 |
{ |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
427 |
boolean r = vh.weakCompareAndSetVolatile(recv, 2L, 1L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
428 |
assertEquals(r, true, "weakCompareAndSetVolatile long"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
429 |
long x = (long) vh.get(recv); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
430 |
assertEquals(x, 1L, "weakCompareAndSetVolatile long value"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
431 |
} |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
432 |
|
36934 | 433 |
// Compare set and get |
434 |
{ |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
435 |
long o = (long) vh.getAndSet(recv, 2L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
436 |
assertEquals(o, 1L, "getAndSet long"); |
36934 | 437 |
long x = (long) vh.get(recv); |
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
438 |
assertEquals(x, 2L, "getAndSet long value"); |
36934 | 439 |
} |
440 |
||
441 |
vh.set(recv, 1L); |
|
442 |
||
443 |
// get and add, add and get |
|
444 |
{ |
|
445 |
long o = (long) vh.getAndAdd(recv, 3L); |
|
446 |
assertEquals(o, 1L, "getAndAdd long"); |
|
447 |
long c = (long) vh.addAndGet(recv, 3L); |
|
448 |
assertEquals(c, 1L + 3L + 3L, "getAndAdd long value"); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
static void testInstanceFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) { |
|
453 |
||
454 |
} |
|
455 |
||
456 |
||
457 |
static void testStaticField(VarHandle vh) { |
|
458 |
// Plain |
|
459 |
{ |
|
460 |
vh.set(1L); |
|
461 |
long x = (long) vh.get(); |
|
462 |
assertEquals(x, 1L, "set long value"); |
|
463 |
} |
|
464 |
||
465 |
||
466 |
// Volatile |
|
467 |
{ |
|
468 |
vh.setVolatile(2L); |
|
469 |
long x = (long) vh.getVolatile(); |
|
470 |
assertEquals(x, 2L, "setVolatile long value"); |
|
471 |
} |
|
472 |
||
473 |
// Lazy |
|
474 |
{ |
|
475 |
vh.setRelease(1L); |
|
476 |
long x = (long) vh.getAcquire(); |
|
477 |
assertEquals(x, 1L, "setRelease long value"); |
|
478 |
} |
|
479 |
||
480 |
// Opaque |
|
481 |
{ |
|
482 |
vh.setOpaque(2L); |
|
483 |
long x = (long) vh.getOpaque(); |
|
484 |
assertEquals(x, 2L, "setOpaque long value"); |
|
485 |
} |
|
486 |
||
487 |
vh.set(1L); |
|
488 |
||
489 |
// Compare |
|
490 |
{ |
|
491 |
boolean r = vh.compareAndSet(1L, 2L); |
|
492 |
assertEquals(r, true, "success compareAndSet long"); |
|
493 |
long x = (long) vh.get(); |
|
494 |
assertEquals(x, 2L, "success compareAndSet long value"); |
|
495 |
} |
|
496 |
||
497 |
{ |
|
498 |
boolean r = vh.compareAndSet(1L, 3L); |
|
499 |
assertEquals(r, false, "failing compareAndSet long"); |
|
500 |
long x = (long) vh.get(); |
|
501 |
assertEquals(x, 2L, "failing compareAndSet long value"); |
|
502 |
} |
|
503 |
||
504 |
{ |
|
505 |
long r = (long) vh.compareAndExchangeVolatile(2L, 1L); |
|
506 |
assertEquals(r, 2L, "success compareAndExchangeVolatile long"); |
|
507 |
long x = (long) vh.get(); |
|
508 |
assertEquals(x, 1L, "success compareAndExchangeVolatile long value"); |
|
509 |
} |
|
510 |
||
511 |
{ |
|
512 |
long r = (long) vh.compareAndExchangeVolatile(2L, 3L); |
|
513 |
assertEquals(r, 1L, "failing compareAndExchangeVolatile long"); |
|
514 |
long x = (long) vh.get(); |
|
515 |
assertEquals(x, 1L, "failing compareAndExchangeVolatile long value"); |
|
516 |
} |
|
517 |
||
518 |
{ |
|
519 |
long r = (long) vh.compareAndExchangeAcquire(1L, 2L); |
|
520 |
assertEquals(r, 1L, "success compareAndExchangeAcquire long"); |
|
521 |
long x = (long) vh.get(); |
|
522 |
assertEquals(x, 2L, "success compareAndExchangeAcquire long value"); |
|
523 |
} |
|
524 |
||
525 |
{ |
|
526 |
long r = (long) vh.compareAndExchangeAcquire(1L, 3L); |
|
527 |
assertEquals(r, 2L, "failing compareAndExchangeAcquire long"); |
|
528 |
long x = (long) vh.get(); |
|
529 |
assertEquals(x, 2L, "failing compareAndExchangeAcquire long value"); |
|
530 |
} |
|
531 |
||
532 |
{ |
|
533 |
long r = (long) vh.compareAndExchangeRelease(2L, 1L); |
|
534 |
assertEquals(r, 2L, "success compareAndExchangeRelease long"); |
|
535 |
long x = (long) vh.get(); |
|
536 |
assertEquals(x, 1L, "success compareAndExchangeRelease long value"); |
|
537 |
} |
|
538 |
||
539 |
{ |
|
540 |
long r = (long) vh.compareAndExchangeRelease(2L, 3L); |
|
541 |
assertEquals(r, 1L, "failing compareAndExchangeRelease long"); |
|
542 |
long x = (long) vh.get(); |
|
543 |
assertEquals(x, 1L, "failing compareAndExchangeRelease long value"); |
|
544 |
} |
|
545 |
||
546 |
{ |
|
547 |
boolean r = (boolean) vh.weakCompareAndSet(1L, 2L); |
|
548 |
assertEquals(r, true, "weakCompareAndSet long"); |
|
549 |
long x = (long) vh.get(); |
|
550 |
assertEquals(x, 2L, "weakCompareAndSet long value"); |
|
551 |
} |
|
552 |
||
553 |
{ |
|
554 |
boolean r = (boolean) vh.weakCompareAndSetAcquire(2L, 1L); |
|
555 |
assertEquals(r, true, "weakCompareAndSetAcquire long"); |
|
556 |
long x = (long) vh.get(); |
|
557 |
assertEquals(x, 1L, "weakCompareAndSetAcquire long"); |
|
558 |
} |
|
559 |
||
560 |
{ |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
561 |
boolean r = (boolean) vh.weakCompareAndSetRelease(1L, 2L); |
36934 | 562 |
assertEquals(r, true, "weakCompareAndSetRelease long"); |
563 |
long x = (long) vh.get(); |
|
564 |
assertEquals(x, 2L, "weakCompareAndSetRelease long"); |
|
565 |
} |
|
566 |
||
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
567 |
{ |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
568 |
boolean r = (boolean) vh.weakCompareAndSetVolatile(2L, 1L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
569 |
assertEquals(r, true, "weakCompareAndSetVolatile long"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
570 |
long x = (long) vh.get(); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
571 |
assertEquals(x, 1L, "weakCompareAndSetVolatile long value"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
572 |
} |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
573 |
|
36934 | 574 |
// Compare set and get |
575 |
{ |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
576 |
long o = (long) vh.getAndSet( 2L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
577 |
assertEquals(o, 1L, "getAndSet long"); |
36934 | 578 |
long x = (long) vh.get(); |
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
579 |
assertEquals(x, 2L, "getAndSet long value"); |
36934 | 580 |
} |
581 |
||
582 |
vh.set(1L); |
|
583 |
||
584 |
// get and add, add and get |
|
585 |
{ |
|
586 |
long o = (long) vh.getAndAdd( 3L); |
|
587 |
assertEquals(o, 1L, "getAndAdd long"); |
|
588 |
long c = (long) vh.addAndGet(3L); |
|
589 |
assertEquals(c, 1L + 3L + 3L, "getAndAdd long value"); |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
static void testStaticFieldUnsupported(VarHandle vh) { |
|
594 |
||
595 |
} |
|
596 |
||
597 |
||
598 |
static void testArray(VarHandle vh) { |
|
599 |
long[] array = new long[10]; |
|
600 |
||
601 |
for (int i = 0; i < array.length; i++) { |
|
602 |
// Plain |
|
603 |
{ |
|
604 |
vh.set(array, i, 1L); |
|
605 |
long x = (long) vh.get(array, i); |
|
606 |
assertEquals(x, 1L, "get long value"); |
|
607 |
} |
|
608 |
||
609 |
||
610 |
// Volatile |
|
611 |
{ |
|
612 |
vh.setVolatile(array, i, 2L); |
|
613 |
long x = (long) vh.getVolatile(array, i); |
|
614 |
assertEquals(x, 2L, "setVolatile long value"); |
|
615 |
} |
|
616 |
||
617 |
// Lazy |
|
618 |
{ |
|
619 |
vh.setRelease(array, i, 1L); |
|
620 |
long x = (long) vh.getAcquire(array, i); |
|
621 |
assertEquals(x, 1L, "setRelease long value"); |
|
622 |
} |
|
623 |
||
624 |
// Opaque |
|
625 |
{ |
|
626 |
vh.setOpaque(array, i, 2L); |
|
627 |
long x = (long) vh.getOpaque(array, i); |
|
628 |
assertEquals(x, 2L, "setOpaque long value"); |
|
629 |
} |
|
630 |
||
631 |
vh.set(array, i, 1L); |
|
632 |
||
633 |
// Compare |
|
634 |
{ |
|
635 |
boolean r = vh.compareAndSet(array, i, 1L, 2L); |
|
636 |
assertEquals(r, true, "success compareAndSet long"); |
|
637 |
long x = (long) vh.get(array, i); |
|
638 |
assertEquals(x, 2L, "success compareAndSet long value"); |
|
639 |
} |
|
640 |
||
641 |
{ |
|
642 |
boolean r = vh.compareAndSet(array, i, 1L, 3L); |
|
643 |
assertEquals(r, false, "failing compareAndSet long"); |
|
644 |
long x = (long) vh.get(array, i); |
|
645 |
assertEquals(x, 2L, "failing compareAndSet long value"); |
|
646 |
} |
|
647 |
||
648 |
{ |
|
649 |
long r = (long) vh.compareAndExchangeVolatile(array, i, 2L, 1L); |
|
650 |
assertEquals(r, 2L, "success compareAndExchangeVolatile long"); |
|
651 |
long x = (long) vh.get(array, i); |
|
652 |
assertEquals(x, 1L, "success compareAndExchangeVolatile long value"); |
|
653 |
} |
|
654 |
||
655 |
{ |
|
656 |
long r = (long) vh.compareAndExchangeVolatile(array, i, 2L, 3L); |
|
657 |
assertEquals(r, 1L, "failing compareAndExchangeVolatile long"); |
|
658 |
long x = (long) vh.get(array, i); |
|
659 |
assertEquals(x, 1L, "failing compareAndExchangeVolatile long value"); |
|
660 |
} |
|
661 |
||
662 |
{ |
|
663 |
long r = (long) vh.compareAndExchangeAcquire(array, i, 1L, 2L); |
|
664 |
assertEquals(r, 1L, "success compareAndExchangeAcquire long"); |
|
665 |
long x = (long) vh.get(array, i); |
|
666 |
assertEquals(x, 2L, "success compareAndExchangeAcquire long value"); |
|
667 |
} |
|
668 |
||
669 |
{ |
|
670 |
long r = (long) vh.compareAndExchangeAcquire(array, i, 1L, 3L); |
|
671 |
assertEquals(r, 2L, "failing compareAndExchangeAcquire long"); |
|
672 |
long x = (long) vh.get(array, i); |
|
673 |
assertEquals(x, 2L, "failing compareAndExchangeAcquire long value"); |
|
674 |
} |
|
675 |
||
676 |
{ |
|
677 |
long r = (long) vh.compareAndExchangeRelease(array, i, 2L, 1L); |
|
678 |
assertEquals(r, 2L, "success compareAndExchangeRelease long"); |
|
679 |
long x = (long) vh.get(array, i); |
|
680 |
assertEquals(x, 1L, "success compareAndExchangeRelease long value"); |
|
681 |
} |
|
682 |
||
683 |
{ |
|
684 |
long r = (long) vh.compareAndExchangeRelease(array, i, 2L, 3L); |
|
685 |
assertEquals(r, 1L, "failing compareAndExchangeRelease long"); |
|
686 |
long x = (long) vh.get(array, i); |
|
687 |
assertEquals(x, 1L, "failing compareAndExchangeRelease long value"); |
|
688 |
} |
|
689 |
||
690 |
{ |
|
691 |
boolean r = vh.weakCompareAndSet(array, i, 1L, 2L); |
|
692 |
assertEquals(r, true, "weakCompareAndSet long"); |
|
693 |
long x = (long) vh.get(array, i); |
|
694 |
assertEquals(x, 2L, "weakCompareAndSet long value"); |
|
695 |
} |
|
696 |
||
697 |
{ |
|
698 |
boolean r = vh.weakCompareAndSetAcquire(array, i, 2L, 1L); |
|
699 |
assertEquals(r, true, "weakCompareAndSetAcquire long"); |
|
700 |
long x = (long) vh.get(array, i); |
|
701 |
assertEquals(x, 1L, "weakCompareAndSetAcquire long"); |
|
702 |
} |
|
703 |
||
704 |
{ |
|
705 |
boolean r = vh.weakCompareAndSetRelease(array, i, 1L, 2L); |
|
706 |
assertEquals(r, true, "weakCompareAndSetRelease long"); |
|
707 |
long x = (long) vh.get(array, i); |
|
708 |
assertEquals(x, 2L, "weakCompareAndSetRelease long"); |
|
709 |
} |
|
710 |
||
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
711 |
{ |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
712 |
boolean r = vh.weakCompareAndSetVolatile(array, i, 2L, 1L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
713 |
assertEquals(r, true, "weakCompareAndSetVolatile long"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
714 |
long x = (long) vh.get(array, i); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
715 |
assertEquals(x, 1L, "weakCompareAndSetVolatile long value"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
716 |
} |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
717 |
|
36934 | 718 |
// Compare set and get |
719 |
{ |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
720 |
long o = (long) vh.getAndSet(array, i, 2L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
721 |
assertEquals(o, 1L, "getAndSet long"); |
36934 | 722 |
long x = (long) vh.get(array, i); |
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
723 |
assertEquals(x, 2L, "getAndSet long value"); |
36934 | 724 |
} |
725 |
||
726 |
vh.set(array, i, 1L); |
|
727 |
||
728 |
// get and add, add and get |
|
729 |
{ |
|
730 |
long o = (long) vh.getAndAdd(array, i, 3L); |
|
731 |
assertEquals(o, 1L, "getAndAdd long"); |
|
732 |
long c = (long) vh.addAndGet(array, i, 3L); |
|
733 |
assertEquals(c, 1L + 3L + 3L, "getAndAdd long value"); |
|
734 |
} |
|
735 |
} |
|
736 |
} |
|
737 |
||
738 |
static void testArrayUnsupported(VarHandle vh) { |
|
739 |
long[] array = new long[10]; |
|
740 |
||
741 |
int i = 0; |
|
742 |
||
743 |
} |
|
744 |
||
745 |
static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { |
|
746 |
long[] array = new long[10]; |
|
747 |
||
748 |
for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
|
749 |
final int ci = i; |
|
750 |
||
751 |
checkIOOBE(() -> { |
|
752 |
long x = (long) vh.get(array, ci); |
|
753 |
}); |
|
754 |
||
755 |
checkIOOBE(() -> { |
|
756 |
vh.set(array, ci, 1L); |
|
757 |
}); |
|
758 |
||
759 |
checkIOOBE(() -> { |
|
760 |
long x = (long) vh.getVolatile(array, ci); |
|
761 |
}); |
|
762 |
||
763 |
checkIOOBE(() -> { |
|
764 |
vh.setVolatile(array, ci, 1L); |
|
765 |
}); |
|
766 |
||
767 |
checkIOOBE(() -> { |
|
768 |
long x = (long) vh.getAcquire(array, ci); |
|
769 |
}); |
|
770 |
||
771 |
checkIOOBE(() -> { |
|
772 |
vh.setRelease(array, ci, 1L); |
|
773 |
}); |
|
774 |
||
775 |
checkIOOBE(() -> { |
|
776 |
long x = (long) vh.getOpaque(array, ci); |
|
777 |
}); |
|
778 |
||
779 |
checkIOOBE(() -> { |
|
780 |
vh.setOpaque(array, ci, 1L); |
|
781 |
}); |
|
782 |
||
783 |
checkIOOBE(() -> { |
|
784 |
boolean r = vh.compareAndSet(array, ci, 1L, 2L); |
|
785 |
}); |
|
786 |
||
787 |
checkIOOBE(() -> { |
|
788 |
long r = (long) vh.compareAndExchangeVolatile(array, ci, 2L, 1L); |
|
789 |
}); |
|
790 |
||
791 |
checkIOOBE(() -> { |
|
792 |
long r = (long) vh.compareAndExchangeAcquire(array, ci, 2L, 1L); |
|
793 |
}); |
|
794 |
||
795 |
checkIOOBE(() -> { |
|
796 |
long r = (long) vh.compareAndExchangeRelease(array, ci, 2L, 1L); |
|
797 |
}); |
|
798 |
||
799 |
checkIOOBE(() -> { |
|
800 |
boolean r = vh.weakCompareAndSet(array, ci, 1L, 2L); |
|
801 |
}); |
|
802 |
||
803 |
checkIOOBE(() -> { |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
804 |
boolean r = vh.weakCompareAndSetVolatile(array, ci, 1L, 2L); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
805 |
}); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
806 |
|
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
807 |
checkIOOBE(() -> { |
36934 | 808 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, 1L, 2L); |
809 |
}); |
|
810 |
||
811 |
checkIOOBE(() -> { |
|
812 |
boolean r = vh.weakCompareAndSetRelease(array, ci, 1L, 2L); |
|
813 |
}); |
|
814 |
||
815 |
checkIOOBE(() -> { |
|
816 |
long o = (long) vh.getAndSet(array, ci, 1L); |
|
817 |
}); |
|
818 |
||
819 |
checkIOOBE(() -> { |
|
820 |
long o = (long) vh.getAndAdd(array, ci, 3L); |
|
821 |
}); |
|
822 |
||
823 |
checkIOOBE(() -> { |
|
824 |
long o = (long) vh.addAndGet(array, ci, 3L); |
|
825 |
}); |
|
826 |
} |
|
827 |
} |
|
828 |
} |
|
829 |