blob: 9afb2de83208ef34bc2349d953015da4c8e3dbc7 [file] [log] [blame]
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001/*
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
Paul Sandoz44afe202016-05-17 12:06:41 +020030 * @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestAccessLong
Paul Sandoz9fb30a32016-03-24 11:21:21 +010031 */
32
33import org.testng.annotations.BeforeClass;
34import org.testng.annotations.DataProvider;
35import org.testng.annotations.Test;
36
37import java.lang.invoke.MethodHandles;
38import java.lang.invoke.VarHandle;
39import java.util.ArrayList;
40import java.util.Arrays;
41import java.util.List;
42
43import static org.testng.Assert.*;
44
45public 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) {
Paul Sandoza7aff442016-04-13 15:05:48 +020094 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
98 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
99 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
100 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
101 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100102
Paul Sandoza7aff442016-04-13 15:05:48 +0200103 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
104 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
105 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
106 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
107 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300108 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE));
Paul Sandoza7aff442016-04-13 15:05:48 +0200109 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
110 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
Paul Sandoza7aff442016-04-13 15:05:48 +0200111 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100112
Paul Sandoza7aff442016-04-13 15:05:48 +0200113 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
114 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100115 }
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 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300406 boolean success = false;
407 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
408 success = vh.weakCompareAndSet(recv, 1L, 2L);
409 }
410 assertEquals(success, true, "weakCompareAndSet long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100411 long x = (long) vh.get(recv);
412 assertEquals(x, 2L, "weakCompareAndSet long value");
413 }
414
415 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300416 boolean success = false;
417 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
418 success = vh.weakCompareAndSetAcquire(recv, 2L, 1L);
419 }
420 assertEquals(success, true, "weakCompareAndSetAcquire long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100421 long x = (long) vh.get(recv);
422 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
423 }
424
425 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300426 boolean success = false;
427 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
428 success = vh.weakCompareAndSetRelease(recv, 1L, 2L);
429 }
430 assertEquals(success, true, "weakCompareAndSetRelease long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100431 long x = (long) vh.get(recv);
432 assertEquals(x, 2L, "weakCompareAndSetRelease long");
433 }
434
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300435 {
436 boolean success = false;
437 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
438 success = vh.weakCompareAndSetVolatile(recv, 2L, 1L);
439 }
440 assertEquals(success, true, "weakCompareAndSetVolatile long");
441 long x = (long) vh.get(recv);
442 assertEquals(x, 1L, "weakCompareAndSetVolatile long value");
443 }
444
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100445 // Compare set and get
446 {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300447 long o = (long) vh.getAndSet(recv, 2L);
448 assertEquals(o, 1L, "getAndSet long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100449 long x = (long) vh.get(recv);
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300450 assertEquals(x, 2L, "getAndSet long value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100451 }
452
453 vh.set(recv, 1L);
454
455 // get and add, add and get
456 {
457 long o = (long) vh.getAndAdd(recv, 3L);
458 assertEquals(o, 1L, "getAndAdd long");
459 long c = (long) vh.addAndGet(recv, 3L);
460 assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
461 }
462 }
463
464 static void testInstanceFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
465
466 }
467
468
469 static void testStaticField(VarHandle vh) {
470 // Plain
471 {
472 vh.set(1L);
473 long x = (long) vh.get();
474 assertEquals(x, 1L, "set long value");
475 }
476
477
478 // Volatile
479 {
480 vh.setVolatile(2L);
481 long x = (long) vh.getVolatile();
482 assertEquals(x, 2L, "setVolatile long value");
483 }
484
485 // Lazy
486 {
487 vh.setRelease(1L);
488 long x = (long) vh.getAcquire();
489 assertEquals(x, 1L, "setRelease long value");
490 }
491
492 // Opaque
493 {
494 vh.setOpaque(2L);
495 long x = (long) vh.getOpaque();
496 assertEquals(x, 2L, "setOpaque long value");
497 }
498
499 vh.set(1L);
500
501 // Compare
502 {
503 boolean r = vh.compareAndSet(1L, 2L);
504 assertEquals(r, true, "success compareAndSet long");
505 long x = (long) vh.get();
506 assertEquals(x, 2L, "success compareAndSet long value");
507 }
508
509 {
510 boolean r = vh.compareAndSet(1L, 3L);
511 assertEquals(r, false, "failing compareAndSet long");
512 long x = (long) vh.get();
513 assertEquals(x, 2L, "failing compareAndSet long value");
514 }
515
516 {
517 long r = (long) vh.compareAndExchangeVolatile(2L, 1L);
518 assertEquals(r, 2L, "success compareAndExchangeVolatile long");
519 long x = (long) vh.get();
520 assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
521 }
522
523 {
524 long r = (long) vh.compareAndExchangeVolatile(2L, 3L);
525 assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
526 long x = (long) vh.get();
527 assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
528 }
529
530 {
531 long r = (long) vh.compareAndExchangeAcquire(1L, 2L);
532 assertEquals(r, 1L, "success compareAndExchangeAcquire long");
533 long x = (long) vh.get();
534 assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
535 }
536
537 {
538 long r = (long) vh.compareAndExchangeAcquire(1L, 3L);
539 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
540 long x = (long) vh.get();
541 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
542 }
543
544 {
545 long r = (long) vh.compareAndExchangeRelease(2L, 1L);
546 assertEquals(r, 2L, "success compareAndExchangeRelease long");
547 long x = (long) vh.get();
548 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
549 }
550
551 {
552 long r = (long) vh.compareAndExchangeRelease(2L, 3L);
553 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
554 long x = (long) vh.get();
555 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
556 }
557
558 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300559 boolean success = false;
560 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
561 success = vh.weakCompareAndSet(1L, 2L);
562 }
563 assertEquals(success, true, "weakCompareAndSet long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100564 long x = (long) vh.get();
565 assertEquals(x, 2L, "weakCompareAndSet long value");
566 }
567
568 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300569 boolean success = false;
570 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
571 success = vh.weakCompareAndSetAcquire(2L, 1L);
572 }
573 assertEquals(success, true, "weakCompareAndSetAcquire long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100574 long x = (long) vh.get();
575 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
576 }
577
578 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300579 boolean success = false;
580 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
581 success = vh.weakCompareAndSetRelease(1L, 2L);
582 }
583 assertEquals(success, true, "weakCompareAndSetRelease long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100584 long x = (long) vh.get();
585 assertEquals(x, 2L, "weakCompareAndSetRelease long");
586 }
587
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300588 {
589 boolean success = false;
590 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
591 success = vh.weakCompareAndSetRelease(2L, 1L);
592 }
593 assertEquals(success, true, "weakCompareAndSetVolatile long");
594 long x = (long) vh.get();
595 assertEquals(x, 1L, "weakCompareAndSetVolatile long");
596 }
597
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100598 // Compare set and get
599 {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300600 long o = (long) vh.getAndSet(2L);
601 assertEquals(o, 1L, "getAndSet long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100602 long x = (long) vh.get();
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300603 assertEquals(x, 2L, "getAndSet long value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100604 }
605
606 vh.set(1L);
607
608 // get and add, add and get
609 {
610 long o = (long) vh.getAndAdd( 3L);
611 assertEquals(o, 1L, "getAndAdd long");
612 long c = (long) vh.addAndGet(3L);
613 assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
614 }
615 }
616
617 static void testStaticFieldUnsupported(VarHandle vh) {
618
619 }
620
621
622 static void testArray(VarHandle vh) {
623 long[] array = new long[10];
624
625 for (int i = 0; i < array.length; i++) {
626 // Plain
627 {
628 vh.set(array, i, 1L);
629 long x = (long) vh.get(array, i);
630 assertEquals(x, 1L, "get long value");
631 }
632
633
634 // Volatile
635 {
636 vh.setVolatile(array, i, 2L);
637 long x = (long) vh.getVolatile(array, i);
638 assertEquals(x, 2L, "setVolatile long value");
639 }
640
641 // Lazy
642 {
643 vh.setRelease(array, i, 1L);
644 long x = (long) vh.getAcquire(array, i);
645 assertEquals(x, 1L, "setRelease long value");
646 }
647
648 // Opaque
649 {
650 vh.setOpaque(array, i, 2L);
651 long x = (long) vh.getOpaque(array, i);
652 assertEquals(x, 2L, "setOpaque long value");
653 }
654
655 vh.set(array, i, 1L);
656
657 // Compare
658 {
659 boolean r = vh.compareAndSet(array, i, 1L, 2L);
660 assertEquals(r, true, "success compareAndSet long");
661 long x = (long) vh.get(array, i);
662 assertEquals(x, 2L, "success compareAndSet long value");
663 }
664
665 {
666 boolean r = vh.compareAndSet(array, i, 1L, 3L);
667 assertEquals(r, false, "failing compareAndSet long");
668 long x = (long) vh.get(array, i);
669 assertEquals(x, 2L, "failing compareAndSet long value");
670 }
671
672 {
673 long r = (long) vh.compareAndExchangeVolatile(array, i, 2L, 1L);
674 assertEquals(r, 2L, "success compareAndExchangeVolatile long");
675 long x = (long) vh.get(array, i);
676 assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
677 }
678
679 {
680 long r = (long) vh.compareAndExchangeVolatile(array, i, 2L, 3L);
681 assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
682 long x = (long) vh.get(array, i);
683 assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
684 }
685
686 {
687 long r = (long) vh.compareAndExchangeAcquire(array, i, 1L, 2L);
688 assertEquals(r, 1L, "success compareAndExchangeAcquire long");
689 long x = (long) vh.get(array, i);
690 assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
691 }
692
693 {
694 long r = (long) vh.compareAndExchangeAcquire(array, i, 1L, 3L);
695 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
696 long x = (long) vh.get(array, i);
697 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
698 }
699
700 {
701 long r = (long) vh.compareAndExchangeRelease(array, i, 2L, 1L);
702 assertEquals(r, 2L, "success compareAndExchangeRelease long");
703 long x = (long) vh.get(array, i);
704 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
705 }
706
707 {
708 long r = (long) vh.compareAndExchangeRelease(array, i, 2L, 3L);
709 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
710 long x = (long) vh.get(array, i);
711 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
712 }
713
714 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300715 boolean success = false;
716 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
717 success = vh.weakCompareAndSet(array, i, 1L, 2L);
718 }
719 assertEquals(success, true, "weakCompareAndSet long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100720 long x = (long) vh.get(array, i);
721 assertEquals(x, 2L, "weakCompareAndSet long value");
722 }
723
724 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300725 boolean success = false;
726 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
727 success = vh.weakCompareAndSetAcquire(array, i, 2L, 1L);
728 }
729 assertEquals(success, true, "weakCompareAndSetAcquire long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100730 long x = (long) vh.get(array, i);
731 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
732 }
733
734 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300735 boolean success = false;
736 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
737 success = vh.weakCompareAndSetRelease(array, i, 1L, 2L);
738 }
739 assertEquals(success, true, "weakCompareAndSetRelease long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100740 long x = (long) vh.get(array, i);
741 assertEquals(x, 2L, "weakCompareAndSetRelease long");
742 }
743
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300744 {
745 boolean success = false;
746 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
747 success = vh.weakCompareAndSetVolatile(array, i, 2L, 1L);
748 }
749 assertEquals(success, true, "weakCompareAndSetVolatile long");
750 long x = (long) vh.get(array, i);
751 assertEquals(x, 1L, "weakCompareAndSetVolatile long");
752 }
753
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100754 // Compare set and get
755 {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300756 long o = (long) vh.getAndSet(array, i, 2L);
757 assertEquals(o, 1L, "getAndSet long");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100758 long x = (long) vh.get(array, i);
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300759 assertEquals(x, 2L, "getAndSet long value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100760 }
761
762 vh.set(array, i, 1L);
763
764 // get and add, add and get
765 {
766 long o = (long) vh.getAndAdd(array, i, 3L);
767 assertEquals(o, 1L, "getAndAdd long");
768 long c = (long) vh.addAndGet(array, i, 3L);
769 assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
770 }
771 }
772 }
773
774 static void testArrayUnsupported(VarHandle vh) {
775 long[] array = new long[10];
776
777 int i = 0;
778
779 }
780
781 static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
782 long[] array = new long[10];
783
784 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
785 final int ci = i;
786
787 checkIOOBE(() -> {
788 long x = (long) vh.get(array, ci);
789 });
790
791 checkIOOBE(() -> {
792 vh.set(array, ci, 1L);
793 });
794
795 checkIOOBE(() -> {
796 long x = (long) vh.getVolatile(array, ci);
797 });
798
799 checkIOOBE(() -> {
800 vh.setVolatile(array, ci, 1L);
801 });
802
803 checkIOOBE(() -> {
804 long x = (long) vh.getAcquire(array, ci);
805 });
806
807 checkIOOBE(() -> {
808 vh.setRelease(array, ci, 1L);
809 });
810
811 checkIOOBE(() -> {
812 long x = (long) vh.getOpaque(array, ci);
813 });
814
815 checkIOOBE(() -> {
816 vh.setOpaque(array, ci, 1L);
817 });
818
819 checkIOOBE(() -> {
820 boolean r = vh.compareAndSet(array, ci, 1L, 2L);
821 });
822
823 checkIOOBE(() -> {
824 long r = (long) vh.compareAndExchangeVolatile(array, ci, 2L, 1L);
825 });
826
827 checkIOOBE(() -> {
828 long r = (long) vh.compareAndExchangeAcquire(array, ci, 2L, 1L);
829 });
830
831 checkIOOBE(() -> {
832 long r = (long) vh.compareAndExchangeRelease(array, ci, 2L, 1L);
833 });
834
835 checkIOOBE(() -> {
836 boolean r = vh.weakCompareAndSet(array, ci, 1L, 2L);
837 });
838
839 checkIOOBE(() -> {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300840 boolean r = vh.weakCompareAndSetVolatile(array, ci, 1L, 2L);
841 });
842
843 checkIOOBE(() -> {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100844 boolean r = vh.weakCompareAndSetAcquire(array, ci, 1L, 2L);
845 });
846
847 checkIOOBE(() -> {
848 boolean r = vh.weakCompareAndSetRelease(array, ci, 1L, 2L);
849 });
850
851 checkIOOBE(() -> {
852 long o = (long) vh.getAndSet(array, ci, 1L);
853 });
854
855 checkIOOBE(() -> {
856 long o = (long) vh.getAndAdd(array, ci, 3L);
857 });
858
859 checkIOOBE(() -> {
860 long o = (long) vh.addAndGet(array, ci, 3L);
861 });
862 }
863 }
864}
865