1 /*
2 * Copyright (c) 2015, 2025, 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 junit/othervm -Diters=10 -Xint VarHandleTestAccessByte
27 *
28 * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
29 * to hit compilation thresholds
30 *
31 * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccessByte
32 * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestAccessByte
33 * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation VarHandleTestAccessByte
34 */
35
36 import java.lang.invoke.MethodHandles;
37 import java.lang.invoke.VarHandle;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41
42 import static org.junit.jupiter.api.Assertions.*;
43 import org.junit.jupiter.api.BeforeAll;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.TestInstance;
46 import org.junit.jupiter.params.ParameterizedTest;
47 import org.junit.jupiter.params.provider.MethodSource;
48
49 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
50 public class VarHandleTestAccessByte extends VarHandleBaseTest {
51 static final byte static_final_v = (byte)0x01;
52
53 static byte static_v;
54
55 final byte final_v = (byte)0x01;
56
57 byte v;
58
59 static final byte static_final_v2 = (byte)0x01;
60
61 static byte static_v2;
62
63 final byte final_v2 = (byte)0x01;
64
65 byte v2;
66
67 VarHandle vhFinalField;
68
69 VarHandle vhField;
70
71 VarHandle vhStaticField;
72
73 VarHandle vhStaticFinalField;
74
75 VarHandle vhArray;
76
77
78 VarHandle[] allocate(boolean same) {
79 List<VarHandle> vhs = new ArrayList<>();
80
81 String postfix = same ? "" : "2";
82 VarHandle vh;
83 try {
84 vh = MethodHandles.lookup().findVarHandle(
85 VarHandleTestAccessByte.class, "final_v" + postfix, byte.class);
86 vhs.add(vh);
87
88 vh = MethodHandles.lookup().findVarHandle(
89 VarHandleTestAccessByte.class, "v" + postfix, byte.class);
90 vhs.add(vh);
91
92 vh = MethodHandles.lookup().findStaticVarHandle(
93 VarHandleTestAccessByte.class, "static_final_v" + postfix, byte.class);
94 vhs.add(vh);
95
96 vh = MethodHandles.lookup().findStaticVarHandle(
97 VarHandleTestAccessByte.class, "static_v" + postfix, byte.class);
98 vhs.add(vh);
99
100 if (same) {
101 vh = MethodHandles.arrayElementVarHandle(byte[].class);
102 }
103 else {
104 vh = MethodHandles.arrayElementVarHandle(String[].class);
105 }
106 vhs.add(vh);
107 } catch (Exception e) {
108 throw new InternalError(e);
109 }
110 return vhs.toArray(new VarHandle[0]);
111 }
112
113 @BeforeAll
114 public void setup() throws Exception {
115 vhFinalField = MethodHandles.lookup().findVarHandle(
116 VarHandleTestAccessByte.class, "final_v", byte.class);
117
118 vhField = MethodHandles.lookup().findVarHandle(
119 VarHandleTestAccessByte.class, "v", byte.class);
120
121 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
122 VarHandleTestAccessByte.class, "static_final_v", byte.class);
123
124 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
125 VarHandleTestAccessByte.class, "static_v", byte.class);
126
127 vhArray = MethodHandles.arrayElementVarHandle(byte[].class);
128 }
129
130 public Object[][] varHandlesProvider() throws Exception {
131 List<VarHandle> vhs = new ArrayList<>();
132 vhs.add(vhField);
133 vhs.add(vhStaticField);
134 vhs.add(vhArray);
135
136 return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
137 }
138
139 @Test
140 public void testEquals() {
141 VarHandle[] vhs1 = allocate(true);
142 VarHandle[] vhs2 = allocate(true);
143
144 for (int i = 0; i < vhs1.length; i++) {
145 for (int j = 0; j < vhs1.length; j++) {
146 if (i != j) {
147 assertNotEquals(vhs1[i], vhs1[j]);
148 assertNotEquals(vhs1[i], vhs2[j]);
149 }
150 }
151 }
152
153 VarHandle[] vhs3 = allocate(false);
154 for (int i = 0; i < vhs1.length; i++) {
155 assertNotEquals(vhs1[i], vhs3[i]);
156 }
157 }
158
159 @ParameterizedTest
160 @MethodSource("varHandlesProvider")
161 public void testIsAccessModeSupported(VarHandle vh) {
162 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
163 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
164 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
165 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
166 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
167 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
168 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
169 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
170
171 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
172 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
173 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
174 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
175 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
176 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
177 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
178 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
179 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
180 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
181 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
182
183 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
184 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
185 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
186
187 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
188 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
189 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
190 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
191 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
192 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
193 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
194 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
195 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
196 }
197
198 public Object[][] typesProvider() throws Exception {
199 List<Object[]> types = new ArrayList<>();
200 types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessByte.class)});
201 types.add(new Object[] {vhStaticField, Arrays.asList()});
202 types.add(new Object[] {vhArray, Arrays.asList(byte[].class, int.class)});
203
204 return types.stream().toArray(Object[][]::new);
205 }
206
207 @ParameterizedTest
208 @MethodSource("typesProvider")
209 public void testTypes(VarHandle vh, List<Class<?>> pts) {
210 assertEquals(byte.class, vh.varType());
211
212 assertEquals(pts, vh.coordinateTypes());
213
214 testTypes(vh);
215 }
216
217 @Test
218 public void testLookupInstanceToStatic() {
219 checkIAE("Lookup of static final field to instance final field", () -> {
220 MethodHandles.lookup().findStaticVarHandle(
221 VarHandleTestAccessByte.class, "final_v", byte.class);
222 });
223
224 checkIAE("Lookup of static field to instance field", () -> {
225 MethodHandles.lookup().findStaticVarHandle(
226 VarHandleTestAccessByte.class, "v", byte.class);
227 });
228 }
229
230 @Test
231 public void testLookupStaticToInstance() {
232 checkIAE("Lookup of instance final field to static final field", () -> {
233 MethodHandles.lookup().findVarHandle(
234 VarHandleTestAccessByte.class, "static_final_v", byte.class);
235 });
236
237 checkIAE("Lookup of instance field to static field", () -> {
238 vhStaticField = MethodHandles.lookup().findVarHandle(
239 VarHandleTestAccessByte.class, "static_v", byte.class);
240 });
241 }
242
243 public Object[][] accessTestCaseProvider() throws Exception {
244 List<AccessTestCase<?>> cases = new ArrayList<>();
245
246 cases.add(new VarHandleAccessTestCase("Instance final field",
247 vhFinalField, vh -> testInstanceFinalField(this, vh)));
248 cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
249 vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
250 false));
251
252 cases.add(new VarHandleAccessTestCase("Static final field",
253 vhStaticFinalField, VarHandleTestAccessByte::testStaticFinalField));
254 cases.add(new VarHandleAccessTestCase("Static final field unsupported",
255 vhStaticFinalField, VarHandleTestAccessByte::testStaticFinalFieldUnsupported,
256 false));
257
258 cases.add(new VarHandleAccessTestCase("Instance field",
259 vhField, vh -> testInstanceField(this, vh)));
260 cases.add(new VarHandleAccessTestCase("Instance field unsupported",
261 vhField, vh -> testInstanceFieldUnsupported(this, vh),
262 false));
263
264 cases.add(new VarHandleAccessTestCase("Static field",
265 vhStaticField, VarHandleTestAccessByte::testStaticField));
266 cases.add(new VarHandleAccessTestCase("Static field unsupported",
267 vhStaticField, VarHandleTestAccessByte::testStaticFieldUnsupported,
268 false));
269
270 cases.add(new VarHandleAccessTestCase("Array",
271 vhArray, VarHandleTestAccessByte::testArray));
272 cases.add(new VarHandleAccessTestCase("Array unsupported",
273 vhArray, VarHandleTestAccessByte::testArrayUnsupported,
274 false));
275 cases.add(new VarHandleAccessTestCase("Array index out of bounds",
276 vhArray, VarHandleTestAccessByte::testArrayIndexOutOfBounds,
277 false));
278 // Work around issue with jtreg summary reporting which truncates
279 // the String result of Object.toString to 30 characters, hence
280 // the first dummy argument
281 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
282 }
283
284 @ParameterizedTest
285 @MethodSource("accessTestCaseProvider")
286 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
287 T t = atc.get();
288 int iters = atc.requiresLoop() ? ITERS : 1;
289 for (int c = 0; c < iters; c++) {
290 atc.testAccess(t);
291 }
292 }
293
294 static void testInstanceFinalField(VarHandleTestAccessByte recv, VarHandle vh) {
295 // Plain
296 {
297 byte x = (byte) vh.get(recv);
298 assertEquals((byte)0x01, x, "get byte value");
299 }
300
301
302 // Volatile
303 {
304 byte x = (byte) vh.getVolatile(recv);
305 assertEquals((byte)0x01, x, "getVolatile byte value");
306 }
307
308 // Lazy
309 {
310 byte x = (byte) vh.getAcquire(recv);
311 assertEquals((byte)0x01, x, "getRelease byte value");
312 }
313
314 // Opaque
315 {
316 byte x = (byte) vh.getOpaque(recv);
317 assertEquals((byte)0x01, x, "getOpaque byte value");
318 }
319 }
320
321 static void testInstanceFinalFieldUnsupported(VarHandleTestAccessByte recv, VarHandle vh) {
322 checkUOE(() -> {
323 vh.set(recv, (byte)0x23);
324 });
325
326 checkUOE(() -> {
327 vh.setVolatile(recv, (byte)0x23);
328 });
329
330 checkUOE(() -> {
331 vh.setRelease(recv, (byte)0x23);
332 });
333
334 checkUOE(() -> {
335 vh.setOpaque(recv, (byte)0x23);
336 });
337
338
339
340 }
341
342
343 static void testStaticFinalField(VarHandle vh) {
344 // Plain
345 {
346 byte x = (byte) vh.get();
347 assertEquals((byte)0x01, x, "get byte value");
348 }
349
350
351 // Volatile
352 {
353 byte x = (byte) vh.getVolatile();
354 assertEquals((byte)0x01, x, "getVolatile byte value");
355 }
356
357 // Lazy
358 {
359 byte x = (byte) vh.getAcquire();
360 assertEquals((byte)0x01, x, "getRelease byte value");
361 }
362
363 // Opaque
364 {
365 byte x = (byte) vh.getOpaque();
366 assertEquals((byte)0x01, x, "getOpaque byte value");
367 }
368 }
369
370 static void testStaticFinalFieldUnsupported(VarHandle vh) {
371 checkUOE(() -> {
372 vh.set((byte)0x23);
373 });
374
375 checkUOE(() -> {
376 vh.setVolatile((byte)0x23);
377 });
378
379 checkUOE(() -> {
380 vh.setRelease((byte)0x23);
381 });
382
383 checkUOE(() -> {
384 vh.setOpaque((byte)0x23);
385 });
386
387
388
389 }
390
391
392 static void testInstanceField(VarHandleTestAccessByte recv, VarHandle vh) {
393 // Plain
394 {
395 vh.set(recv, (byte)0x01);
396 byte x = (byte) vh.get(recv);
397 assertEquals((byte)0x01, x, "set byte value");
398 }
399
400
401 // Volatile
402 {
403 vh.setVolatile(recv, (byte)0x23);
404 byte x = (byte) vh.getVolatile(recv);
405 assertEquals((byte)0x23, x, "setVolatile byte value");
406 }
407
408 // Lazy
409 {
410 vh.setRelease(recv, (byte)0x01);
411 byte x = (byte) vh.getAcquire(recv);
412 assertEquals((byte)0x01, x, "setRelease byte value");
413 }
414
415 // Opaque
416 {
417 vh.setOpaque(recv, (byte)0x23);
418 byte x = (byte) vh.getOpaque(recv);
419 assertEquals((byte)0x23, x, "setOpaque byte value");
420 }
421
422 vh.set(recv, (byte)0x01);
423
424 // Compare
425 {
426 boolean r = vh.compareAndSet(recv, (byte)0x01, (byte)0x23);
427 assertEquals(r, true, "success compareAndSet byte");
428 byte x = (byte) vh.get(recv);
429 assertEquals((byte)0x23, x, "success compareAndSet byte value");
430 }
431
432 {
433 boolean r = vh.compareAndSet(recv, (byte)0x01, (byte)0x45);
434 assertEquals(r, false, "failing compareAndSet byte");
435 byte x = (byte) vh.get(recv);
436 assertEquals((byte)0x23, x, "failing compareAndSet byte value");
437 }
438
439 {
440 byte r = (byte) vh.compareAndExchange(recv, (byte)0x23, (byte)0x01);
441 assertEquals(r, (byte)0x23, "success compareAndExchange byte");
442 byte x = (byte) vh.get(recv);
443 assertEquals((byte)0x01, x, "success compareAndExchange byte value");
444 }
445
446 {
447 byte r = (byte) vh.compareAndExchange(recv, (byte)0x23, (byte)0x45);
448 assertEquals(r, (byte)0x01, "failing compareAndExchange byte");
449 byte x = (byte) vh.get(recv);
450 assertEquals((byte)0x01, x, "failing compareAndExchange byte value");
451 }
452
453 {
454 byte r = (byte) vh.compareAndExchangeAcquire(recv, (byte)0x01, (byte)0x23);
455 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
456 byte x = (byte) vh.get(recv);
457 assertEquals((byte)0x23, x, "success compareAndExchangeAcquire byte value");
458 }
459
460 {
461 byte r = (byte) vh.compareAndExchangeAcquire(recv, (byte)0x01, (byte)0x45);
462 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
463 byte x = (byte) vh.get(recv);
464 assertEquals((byte)0x23, x, "failing compareAndExchangeAcquire byte value");
465 }
466
467 {
468 byte r = (byte) vh.compareAndExchangeRelease(recv, (byte)0x23, (byte)0x01);
469 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
470 byte x = (byte) vh.get(recv);
471 assertEquals((byte)0x01, x, "success compareAndExchangeRelease byte value");
472 }
473
474 {
475 byte r = (byte) vh.compareAndExchangeRelease(recv, (byte)0x23, (byte)0x45);
476 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
477 byte x = (byte) vh.get(recv);
478 assertEquals((byte)0x01, x, "failing compareAndExchangeRelease byte value");
479 }
480
481 {
482 boolean success = false;
483 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
484 success = vh.weakCompareAndSetPlain(recv, (byte)0x01, (byte)0x23);
485 if (!success) weakDelay();
486 }
487 assertEquals(success, true, "success weakCompareAndSetPlain byte");
488 byte x = (byte) vh.get(recv);
489 assertEquals((byte)0x23, x, "success weakCompareAndSetPlain byte value");
490 }
491
492 {
493 boolean success = vh.weakCompareAndSetPlain(recv, (byte)0x01, (byte)0x45);
494 assertEquals(success, false, "failing weakCompareAndSetPlain byte");
495 byte x = (byte) vh.get(recv);
496 assertEquals((byte)0x23, x, "failing weakCompareAndSetPlain byte value");
497 }
498
499 {
500 boolean success = false;
501 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
502 success = vh.weakCompareAndSetAcquire(recv, (byte)0x23, (byte)0x01);
503 if (!success) weakDelay();
504 }
505 assertEquals(success, true, "success weakCompareAndSetAcquire byte");
506 byte x = (byte) vh.get(recv);
507 assertEquals((byte)0x01, x, "success weakCompareAndSetAcquire byte");
508 }
509
510 {
511 boolean success = vh.weakCompareAndSetAcquire(recv, (byte)0x23, (byte)0x45);
512 assertEquals(success, false, "failing weakCompareAndSetAcquire byte");
513 byte x = (byte) vh.get(recv);
514 assertEquals((byte)0x01, x, "failing weakCompareAndSetAcquire byte value");
515 }
516
517 {
518 boolean success = false;
519 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
520 success = vh.weakCompareAndSetRelease(recv, (byte)0x01, (byte)0x23);
521 if (!success) weakDelay();
522 }
523 assertEquals(success, true, "success weakCompareAndSetRelease byte");
524 byte x = (byte) vh.get(recv);
525 assertEquals((byte)0x23, x, "success weakCompareAndSetRelease byte");
526 }
527
528 {
529 boolean success = vh.weakCompareAndSetRelease(recv, (byte)0x01, (byte)0x45);
530 assertEquals(success, false, "failing weakCompareAndSetRelease byte");
531 byte x = (byte) vh.get(recv);
532 assertEquals((byte)0x23, x, "failing weakCompareAndSetRelease byte value");
533 }
534
535 {
536 boolean success = false;
537 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
538 success = vh.weakCompareAndSet(recv, (byte)0x23, (byte)0x01);
539 if (!success) weakDelay();
540 }
541 assertEquals(success, true, "success weakCompareAndSet byte");
542 byte x = (byte) vh.get(recv);
543 assertEquals((byte)0x01, x, "success weakCompareAndSet byte value");
544 }
545
546 {
547 boolean success = vh.weakCompareAndSet(recv, (byte)0x23, (byte)0x45);
548 assertEquals(success, false, "failing weakCompareAndSet byte");
549 byte x = (byte) vh.get(recv);
550 assertEquals((byte)0x01, x, "failing weakCompareAndSet byte value");
551 }
552
553 // Compare set and get
554 {
555 vh.set(recv, (byte)0x01);
556
557 byte o = (byte) vh.getAndSet(recv, (byte)0x23);
558 assertEquals((byte)0x01, o, "getAndSet byte");
559 byte x = (byte) vh.get(recv);
560 assertEquals((byte)0x23, x, "getAndSet byte value");
561 }
562
563 {
564 vh.set(recv, (byte)0x01);
565
566 byte o = (byte) vh.getAndSetAcquire(recv, (byte)0x23);
567 assertEquals((byte)0x01, o, "getAndSetAcquire byte");
568 byte x = (byte) vh.get(recv);
569 assertEquals((byte)0x23, x, "getAndSetAcquire byte value");
570 }
571
572 {
573 vh.set(recv, (byte)0x01);
574
575 byte o = (byte) vh.getAndSetRelease(recv, (byte)0x23);
576 assertEquals((byte)0x01, o, "getAndSetRelease byte");
577 byte x = (byte) vh.get(recv);
578 assertEquals((byte)0x23, x, "getAndSetRelease byte value");
579 }
580
581 // get and add, add and get
582 {
583 vh.set(recv, (byte)0x01);
584
585 byte o = (byte) vh.getAndAdd(recv, (byte)0x23);
586 assertEquals((byte)0x01, o, "getAndAdd byte");
587 byte x = (byte) vh.get(recv);
588 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAdd byte value");
589 }
590
591 {
592 vh.set(recv, (byte)0x01);
593
594 byte o = (byte) vh.getAndAddAcquire(recv, (byte)0x23);
595 assertEquals((byte)0x01, o, "getAndAddAcquire byte");
596 byte x = (byte) vh.get(recv);
597 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddAcquire byte value");
598 }
599
600 {
601 vh.set(recv, (byte)0x01);
602
603 byte o = (byte) vh.getAndAddRelease(recv, (byte)0x23);
604 assertEquals((byte)0x01, o, "getAndAddReleasebyte");
605 byte x = (byte) vh.get(recv);
606 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddRelease byte value");
607 }
608
609 // get and bitwise or
610 {
611 vh.set(recv, (byte)0x01);
612
613 byte o = (byte) vh.getAndBitwiseOr(recv, (byte)0x23);
614 assertEquals((byte)0x01, o, "getAndBitwiseOr byte");
615 byte x = (byte) vh.get(recv);
616 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOr byte value");
617 }
618
619 {
620 vh.set(recv, (byte)0x01);
621
622 byte o = (byte) vh.getAndBitwiseOrAcquire(recv, (byte)0x23);
623 assertEquals((byte)0x01, o, "getAndBitwiseOrAcquire byte");
624 byte x = (byte) vh.get(recv);
625 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrAcquire byte value");
626 }
627
628 {
629 vh.set(recv, (byte)0x01);
630
631 byte o = (byte) vh.getAndBitwiseOrRelease(recv, (byte)0x23);
632 assertEquals((byte)0x01, o, "getAndBitwiseOrRelease byte");
633 byte x = (byte) vh.get(recv);
634 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrRelease byte value");
635 }
636
637 // get and bitwise and
638 {
639 vh.set(recv, (byte)0x01);
640
641 byte o = (byte) vh.getAndBitwiseAnd(recv, (byte)0x23);
642 assertEquals((byte)0x01, o, "getAndBitwiseAnd byte");
643 byte x = (byte) vh.get(recv);
644 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAnd byte value");
645 }
646
647 {
648 vh.set(recv, (byte)0x01);
649
650 byte o = (byte) vh.getAndBitwiseAndAcquire(recv, (byte)0x23);
651 assertEquals((byte)0x01, o, "getAndBitwiseAndAcquire byte");
652 byte x = (byte) vh.get(recv);
653 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndAcquire byte value");
654 }
655
656 {
657 vh.set(recv, (byte)0x01);
658
659 byte o = (byte) vh.getAndBitwiseAndRelease(recv, (byte)0x23);
660 assertEquals((byte)0x01, o, "getAndBitwiseAndRelease byte");
661 byte x = (byte) vh.get(recv);
662 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndRelease byte value");
663 }
664
665 // get and bitwise xor
666 {
667 vh.set(recv, (byte)0x01);
668
669 byte o = (byte) vh.getAndBitwiseXor(recv, (byte)0x23);
670 assertEquals((byte)0x01, o, "getAndBitwiseXor byte");
671 byte x = (byte) vh.get(recv);
672 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXor byte value");
673 }
674
675 {
676 vh.set(recv, (byte)0x01);
677
678 byte o = (byte) vh.getAndBitwiseXorAcquire(recv, (byte)0x23);
679 assertEquals((byte)0x01, o, "getAndBitwiseXorAcquire byte");
680 byte x = (byte) vh.get(recv);
681 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorAcquire byte value");
682 }
683
684 {
685 vh.set(recv, (byte)0x01);
686
687 byte o = (byte) vh.getAndBitwiseXorRelease(recv, (byte)0x23);
688 assertEquals((byte)0x01, o, "getAndBitwiseXorRelease byte");
689 byte x = (byte) vh.get(recv);
690 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorRelease byte value");
691 }
692 }
693
694 static void testInstanceFieldUnsupported(VarHandleTestAccessByte recv, VarHandle vh) {
695
696
697 }
698
699
700 static void testStaticField(VarHandle vh) {
701 // Plain
702 {
703 vh.set((byte)0x01);
704 byte x = (byte) vh.get();
705 assertEquals((byte)0x01, x, "set byte value");
706 }
707
708
709 // Volatile
710 {
711 vh.setVolatile((byte)0x23);
712 byte x = (byte) vh.getVolatile();
713 assertEquals((byte)0x23, x, "setVolatile byte value");
714 }
715
716 // Lazy
717 {
718 vh.setRelease((byte)0x01);
719 byte x = (byte) vh.getAcquire();
720 assertEquals((byte)0x01, x, "setRelease byte value");
721 }
722
723 // Opaque
724 {
725 vh.setOpaque((byte)0x23);
726 byte x = (byte) vh.getOpaque();
727 assertEquals((byte)0x23, x, "setOpaque byte value");
728 }
729
730 vh.set((byte)0x01);
731
732 // Compare
733 {
734 boolean r = vh.compareAndSet((byte)0x01, (byte)0x23);
735 assertEquals(r, true, "success compareAndSet byte");
736 byte x = (byte) vh.get();
737 assertEquals((byte)0x23, x, "success compareAndSet byte value");
738 }
739
740 {
741 boolean r = vh.compareAndSet((byte)0x01, (byte)0x45);
742 assertEquals(r, false, "failing compareAndSet byte");
743 byte x = (byte) vh.get();
744 assertEquals((byte)0x23, x, "failing compareAndSet byte value");
745 }
746
747 {
748 byte r = (byte) vh.compareAndExchange((byte)0x23, (byte)0x01);
749 assertEquals(r, (byte)0x23, "success compareAndExchange byte");
750 byte x = (byte) vh.get();
751 assertEquals((byte)0x01, x, "success compareAndExchange byte value");
752 }
753
754 {
755 byte r = (byte) vh.compareAndExchange((byte)0x23, (byte)0x45);
756 assertEquals(r, (byte)0x01, "failing compareAndExchange byte");
757 byte x = (byte) vh.get();
758 assertEquals((byte)0x01, x, "failing compareAndExchange byte value");
759 }
760
761 {
762 byte r = (byte) vh.compareAndExchangeAcquire((byte)0x01, (byte)0x23);
763 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
764 byte x = (byte) vh.get();
765 assertEquals((byte)0x23, x, "success compareAndExchangeAcquire byte value");
766 }
767
768 {
769 byte r = (byte) vh.compareAndExchangeAcquire((byte)0x01, (byte)0x45);
770 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
771 byte x = (byte) vh.get();
772 assertEquals((byte)0x23, x, "failing compareAndExchangeAcquire byte value");
773 }
774
775 {
776 byte r = (byte) vh.compareAndExchangeRelease((byte)0x23, (byte)0x01);
777 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
778 byte x = (byte) vh.get();
779 assertEquals((byte)0x01, x, "success compareAndExchangeRelease byte value");
780 }
781
782 {
783 byte r = (byte) vh.compareAndExchangeRelease((byte)0x23, (byte)0x45);
784 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
785 byte x = (byte) vh.get();
786 assertEquals((byte)0x01, x, "failing compareAndExchangeRelease byte value");
787 }
788
789 {
790 boolean success = false;
791 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
792 success = vh.weakCompareAndSetPlain((byte)0x01, (byte)0x23);
793 if (!success) weakDelay();
794 }
795 assertEquals(success, true, "success weakCompareAndSetPlain byte");
796 byte x = (byte) vh.get();
797 assertEquals((byte)0x23, x, "success weakCompareAndSetPlain byte value");
798 }
799
800 {
801 boolean success = vh.weakCompareAndSetPlain((byte)0x01, (byte)0x45);
802 assertEquals(success, false, "failing weakCompareAndSetPlain byte");
803 byte x = (byte) vh.get();
804 assertEquals((byte)0x23, x, "failing weakCompareAndSetPlain byte value");
805 }
806
807 {
808 boolean success = false;
809 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
810 success = vh.weakCompareAndSetAcquire((byte)0x23, (byte)0x01);
811 if (!success) weakDelay();
812 }
813 assertEquals(success, true, "success weakCompareAndSetAcquire byte");
814 byte x = (byte) vh.get();
815 assertEquals((byte)0x01, x, "success weakCompareAndSetAcquire byte");
816 }
817
818 {
819 boolean success = vh.weakCompareAndSetAcquire((byte)0x23, (byte)0x45);
820 assertEquals(success, false, "failing weakCompareAndSetAcquire byte");
821 byte x = (byte) vh.get();
822 assertEquals((byte)0x01, x, "failing weakCompareAndSetAcquire byte value");
823 }
824
825 {
826 boolean success = false;
827 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
828 success = vh.weakCompareAndSetRelease((byte)0x01, (byte)0x23);
829 if (!success) weakDelay();
830 }
831 assertEquals(success, true, "success weakCompareAndSetRelease byte");
832 byte x = (byte) vh.get();
833 assertEquals((byte)0x23, x, "success weakCompareAndSetRelease byte");
834 }
835
836 {
837 boolean success = vh.weakCompareAndSetRelease((byte)0x01, (byte)0x45);
838 assertEquals(success, false, "failing weakCompareAndSetRelease byte");
839 byte x = (byte) vh.get();
840 assertEquals((byte)0x23, x, "failing weakCompareAndSetRelease byte value");
841 }
842
843 {
844 boolean success = false;
845 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
846 success = vh.weakCompareAndSet((byte)0x23, (byte)0x01);
847 if (!success) weakDelay();
848 }
849 assertEquals(success, true, "success weakCompareAndSet byte");
850 byte x = (byte) vh.get();
851 assertEquals((byte)0x01, x, "success weakCompareAndSet byte");
852 }
853
854 {
855 boolean success = vh.weakCompareAndSet((byte)0x23, (byte)0x45);
856 assertEquals(success, false, "failing weakCompareAndSet byte");
857 byte x = (byte) vh.get();
858 assertEquals((byte)0x01, x, "failing weakCompareAndSet byte value");
859 }
860
861 // Compare set and get
862 {
863 vh.set((byte)0x01);
864
865 byte o = (byte) vh.getAndSet((byte)0x23);
866 assertEquals((byte)0x01, o, "getAndSet byte");
867 byte x = (byte) vh.get();
868 assertEquals((byte)0x23, x, "getAndSet byte value");
869 }
870
871 {
872 vh.set((byte)0x01);
873
874 byte o = (byte) vh.getAndSetAcquire((byte)0x23);
875 assertEquals((byte)0x01, o, "getAndSetAcquire byte");
876 byte x = (byte) vh.get();
877 assertEquals((byte)0x23, x, "getAndSetAcquire byte value");
878 }
879
880 {
881 vh.set((byte)0x01);
882
883 byte o = (byte) vh.getAndSetRelease((byte)0x23);
884 assertEquals((byte)0x01, o, "getAndSetRelease byte");
885 byte x = (byte) vh.get();
886 assertEquals((byte)0x23, x, "getAndSetRelease byte value");
887 }
888
889 // get and add, add and get
890 {
891 vh.set((byte)0x01);
892
893 byte o = (byte) vh.getAndAdd((byte)0x23);
894 assertEquals((byte)0x01, o, "getAndAdd byte");
895 byte x = (byte) vh.get();
896 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAdd byte value");
897 }
898
899 {
900 vh.set((byte)0x01);
901
902 byte o = (byte) vh.getAndAddAcquire((byte)0x23);
903 assertEquals((byte)0x01, o, "getAndAddAcquire byte");
904 byte x = (byte) vh.get();
905 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddAcquire byte value");
906 }
907
908 {
909 vh.set((byte)0x01);
910
911 byte o = (byte) vh.getAndAddRelease((byte)0x23);
912 assertEquals((byte)0x01, o, "getAndAddReleasebyte");
913 byte x = (byte) vh.get();
914 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddRelease byte value");
915 }
916
917 // get and bitwise or
918 {
919 vh.set((byte)0x01);
920
921 byte o = (byte) vh.getAndBitwiseOr((byte)0x23);
922 assertEquals((byte)0x01, o, "getAndBitwiseOr byte");
923 byte x = (byte) vh.get();
924 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOr byte value");
925 }
926
927 {
928 vh.set((byte)0x01);
929
930 byte o = (byte) vh.getAndBitwiseOrAcquire((byte)0x23);
931 assertEquals((byte)0x01, o, "getAndBitwiseOrAcquire byte");
932 byte x = (byte) vh.get();
933 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrAcquire byte value");
934 }
935
936 {
937 vh.set((byte)0x01);
938
939 byte o = (byte) vh.getAndBitwiseOrRelease((byte)0x23);
940 assertEquals((byte)0x01, o, "getAndBitwiseOrRelease byte");
941 byte x = (byte) vh.get();
942 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrRelease byte value");
943 }
944
945 // get and bitwise and
946 {
947 vh.set((byte)0x01);
948
949 byte o = (byte) vh.getAndBitwiseAnd((byte)0x23);
950 assertEquals((byte)0x01, o, "getAndBitwiseAnd byte");
951 byte x = (byte) vh.get();
952 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAnd byte value");
953 }
954
955 {
956 vh.set((byte)0x01);
957
958 byte o = (byte) vh.getAndBitwiseAndAcquire((byte)0x23);
959 assertEquals((byte)0x01, o, "getAndBitwiseAndAcquire byte");
960 byte x = (byte) vh.get();
961 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndAcquire byte value");
962 }
963
964 {
965 vh.set((byte)0x01);
966
967 byte o = (byte) vh.getAndBitwiseAndRelease((byte)0x23);
968 assertEquals((byte)0x01, o, "getAndBitwiseAndRelease byte");
969 byte x = (byte) vh.get();
970 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndRelease byte value");
971 }
972
973 // get and bitwise xor
974 {
975 vh.set((byte)0x01);
976
977 byte o = (byte) vh.getAndBitwiseXor((byte)0x23);
978 assertEquals((byte)0x01, o, "getAndBitwiseXor byte");
979 byte x = (byte) vh.get();
980 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXor byte value");
981 }
982
983 {
984 vh.set((byte)0x01);
985
986 byte o = (byte) vh.getAndBitwiseXorAcquire((byte)0x23);
987 assertEquals((byte)0x01, o, "getAndBitwiseXorAcquire byte");
988 byte x = (byte) vh.get();
989 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorAcquire byte value");
990 }
991
992 {
993 vh.set((byte)0x01);
994
995 byte o = (byte) vh.getAndBitwiseXorRelease((byte)0x23);
996 assertEquals((byte)0x01, o, "getAndBitwiseXorRelease byte");
997 byte x = (byte) vh.get();
998 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorRelease byte value");
999 }
1000 }
1001
1002 static void testStaticFieldUnsupported(VarHandle vh) {
1003
1004
1005 }
1006
1007
1008 static void testArray(VarHandle vh) {
1009 byte[] array = new byte[10];
1010
1011 for (int i = 0; i < array.length; i++) {
1012 // Plain
1013 {
1014 vh.set(array, i, (byte)0x01);
1015 byte x = (byte) vh.get(array, i);
1016 assertEquals((byte)0x01, x, "get byte value");
1017 }
1018
1019
1020 // Volatile
1021 {
1022 vh.setVolatile(array, i, (byte)0x23);
1023 byte x = (byte) vh.getVolatile(array, i);
1024 assertEquals((byte)0x23, x, "setVolatile byte value");
1025 }
1026
1027 // Lazy
1028 {
1029 vh.setRelease(array, i, (byte)0x01);
1030 byte x = (byte) vh.getAcquire(array, i);
1031 assertEquals((byte)0x01, x, "setRelease byte value");
1032 }
1033
1034 // Opaque
1035 {
1036 vh.setOpaque(array, i, (byte)0x23);
1037 byte x = (byte) vh.getOpaque(array, i);
1038 assertEquals((byte)0x23, x, "setOpaque byte value");
1039 }
1040
1041 vh.set(array, i, (byte)0x01);
1042
1043 // Compare
1044 {
1045 boolean r = vh.compareAndSet(array, i, (byte)0x01, (byte)0x23);
1046 assertEquals(r, true, "success compareAndSet byte");
1047 byte x = (byte) vh.get(array, i);
1048 assertEquals((byte)0x23, x, "success compareAndSet byte value");
1049 }
1050
1051 {
1052 boolean r = vh.compareAndSet(array, i, (byte)0x01, (byte)0x45);
1053 assertEquals(r, false, "failing compareAndSet byte");
1054 byte x = (byte) vh.get(array, i);
1055 assertEquals((byte)0x23, x, "failing compareAndSet byte value");
1056 }
1057
1058 {
1059 byte r = (byte) vh.compareAndExchange(array, i, (byte)0x23, (byte)0x01);
1060 assertEquals(r, (byte)0x23, "success compareAndExchange byte");
1061 byte x = (byte) vh.get(array, i);
1062 assertEquals((byte)0x01, x, "success compareAndExchange byte value");
1063 }
1064
1065 {
1066 byte r = (byte) vh.compareAndExchange(array, i, (byte)0x23, (byte)0x45);
1067 assertEquals(r, (byte)0x01, "failing compareAndExchange byte");
1068 byte x = (byte) vh.get(array, i);
1069 assertEquals((byte)0x01, x, "failing compareAndExchange byte value");
1070 }
1071
1072 {
1073 byte r = (byte) vh.compareAndExchangeAcquire(array, i, (byte)0x01, (byte)0x23);
1074 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
1075 byte x = (byte) vh.get(array, i);
1076 assertEquals((byte)0x23, x, "success compareAndExchangeAcquire byte value");
1077 }
1078
1079 {
1080 byte r = (byte) vh.compareAndExchangeAcquire(array, i, (byte)0x01, (byte)0x45);
1081 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
1082 byte x = (byte) vh.get(array, i);
1083 assertEquals((byte)0x23, x, "failing compareAndExchangeAcquire byte value");
1084 }
1085
1086 {
1087 byte r = (byte) vh.compareAndExchangeRelease(array, i, (byte)0x23, (byte)0x01);
1088 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
1089 byte x = (byte) vh.get(array, i);
1090 assertEquals((byte)0x01, x, "success compareAndExchangeRelease byte value");
1091 }
1092
1093 {
1094 byte r = (byte) vh.compareAndExchangeRelease(array, i, (byte)0x23, (byte)0x45);
1095 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
1096 byte x = (byte) vh.get(array, i);
1097 assertEquals((byte)0x01, x, "failing compareAndExchangeRelease byte value");
1098 }
1099
1100 {
1101 boolean success = false;
1102 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1103 success = vh.weakCompareAndSetPlain(array, i, (byte)0x01, (byte)0x23);
1104 if (!success) weakDelay();
1105 }
1106 assertEquals(success, true, "success weakCompareAndSetPlain byte");
1107 byte x = (byte) vh.get(array, i);
1108 assertEquals((byte)0x23, x, "success weakCompareAndSetPlain byte value");
1109 }
1110
1111 {
1112 boolean success = vh.weakCompareAndSetPlain(array, i, (byte)0x01, (byte)0x45);
1113 assertEquals(success, false, "failing weakCompareAndSetPlain byte");
1114 byte x = (byte) vh.get(array, i);
1115 assertEquals((byte)0x23, x, "failing weakCompareAndSetPlain byte value");
1116 }
1117
1118 {
1119 boolean success = false;
1120 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1121 success = vh.weakCompareAndSetAcquire(array, i, (byte)0x23, (byte)0x01);
1122 if (!success) weakDelay();
1123 }
1124 assertEquals(success, true, "success weakCompareAndSetAcquire byte");
1125 byte x = (byte) vh.get(array, i);
1126 assertEquals((byte)0x01, x, "success weakCompareAndSetAcquire byte");
1127 }
1128
1129 {
1130 boolean success = vh.weakCompareAndSetAcquire(array, i, (byte)0x23, (byte)0x45);
1131 assertEquals(success, false, "failing weakCompareAndSetAcquire byte");
1132 byte x = (byte) vh.get(array, i);
1133 assertEquals((byte)0x01, x, "failing weakCompareAndSetAcquire byte value");
1134 }
1135
1136 {
1137 boolean success = false;
1138 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1139 success = vh.weakCompareAndSetRelease(array, i, (byte)0x01, (byte)0x23);
1140 if (!success) weakDelay();
1141 }
1142 assertEquals(success, true, "success weakCompareAndSetRelease byte");
1143 byte x = (byte) vh.get(array, i);
1144 assertEquals((byte)0x23, x, "success weakCompareAndSetRelease byte");
1145 }
1146
1147 {
1148 boolean success = vh.weakCompareAndSetRelease(array, i, (byte)0x01, (byte)0x45);
1149 assertEquals(success, false, "failing weakCompareAndSetRelease byte");
1150 byte x = (byte) vh.get(array, i);
1151 assertEquals((byte)0x23, x, "failing weakCompareAndSetRelease byte value");
1152 }
1153
1154 {
1155 boolean success = false;
1156 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1157 success = vh.weakCompareAndSet(array, i, (byte)0x23, (byte)0x01);
1158 if (!success) weakDelay();
1159 }
1160 assertEquals(success, true, "success weakCompareAndSet byte");
1161 byte x = (byte) vh.get(array, i);
1162 assertEquals((byte)0x01, x, "success weakCompareAndSet byte");
1163 }
1164
1165 {
1166 boolean success = vh.weakCompareAndSet(array, i, (byte)0x23, (byte)0x45);
1167 assertEquals(success, false, "failing weakCompareAndSet byte");
1168 byte x = (byte) vh.get(array, i);
1169 assertEquals((byte)0x01, x, "failing weakCompareAndSet byte value");
1170 }
1171
1172 // Compare set and get
1173 {
1174 vh.set(array, i, (byte)0x01);
1175
1176 byte o = (byte) vh.getAndSet(array, i, (byte)0x23);
1177 assertEquals((byte)0x01, o, "getAndSet byte");
1178 byte x = (byte) vh.get(array, i);
1179 assertEquals((byte)0x23, x, "getAndSet byte value");
1180 }
1181
1182 {
1183 vh.set(array, i, (byte)0x01);
1184
1185 byte o = (byte) vh.getAndSetAcquire(array, i, (byte)0x23);
1186 assertEquals((byte)0x01, o, "getAndSetAcquire byte");
1187 byte x = (byte) vh.get(array, i);
1188 assertEquals((byte)0x23, x, "getAndSetAcquire byte value");
1189 }
1190
1191 {
1192 vh.set(array, i, (byte)0x01);
1193
1194 byte o = (byte) vh.getAndSetRelease(array, i, (byte)0x23);
1195 assertEquals((byte)0x01, o, "getAndSetRelease byte");
1196 byte x = (byte) vh.get(array, i);
1197 assertEquals((byte)0x23, x, "getAndSetRelease byte value");
1198 }
1199
1200 // get and add, add and get
1201 {
1202 vh.set(array, i, (byte)0x01);
1203
1204 byte o = (byte) vh.getAndAdd(array, i, (byte)0x23);
1205 assertEquals((byte)0x01, o, "getAndAdd byte");
1206 byte x = (byte) vh.get(array, i);
1207 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAdd byte value");
1208 }
1209
1210 {
1211 vh.set(array, i, (byte)0x01);
1212
1213 byte o = (byte) vh.getAndAddAcquire(array, i, (byte)0x23);
1214 assertEquals((byte)0x01, o, "getAndAddAcquire byte");
1215 byte x = (byte) vh.get(array, i);
1216 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddAcquire byte value");
1217 }
1218
1219 {
1220 vh.set(array, i, (byte)0x01);
1221
1222 byte o = (byte) vh.getAndAddRelease(array, i, (byte)0x23);
1223 assertEquals((byte)0x01, o, "getAndAddReleasebyte");
1224 byte x = (byte) vh.get(array, i);
1225 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddRelease byte value");
1226 }
1227
1228 // get and bitwise or
1229 {
1230 vh.set(array, i, (byte)0x01);
1231
1232 byte o = (byte) vh.getAndBitwiseOr(array, i, (byte)0x23);
1233 assertEquals((byte)0x01, o, "getAndBitwiseOr byte");
1234 byte x = (byte) vh.get(array, i);
1235 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOr byte value");
1236 }
1237
1238 {
1239 vh.set(array, i, (byte)0x01);
1240
1241 byte o = (byte) vh.getAndBitwiseOrAcquire(array, i, (byte)0x23);
1242 assertEquals((byte)0x01, o, "getAndBitwiseOrAcquire byte");
1243 byte x = (byte) vh.get(array, i);
1244 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrAcquire byte value");
1245 }
1246
1247 {
1248 vh.set(array, i, (byte)0x01);
1249
1250 byte o = (byte) vh.getAndBitwiseOrRelease(array, i, (byte)0x23);
1251 assertEquals((byte)0x01, o, "getAndBitwiseOrRelease byte");
1252 byte x = (byte) vh.get(array, i);
1253 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrRelease byte value");
1254 }
1255
1256 // get and bitwise and
1257 {
1258 vh.set(array, i, (byte)0x01);
1259
1260 byte o = (byte) vh.getAndBitwiseAnd(array, i, (byte)0x23);
1261 assertEquals((byte)0x01, o, "getAndBitwiseAnd byte");
1262 byte x = (byte) vh.get(array, i);
1263 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAnd byte value");
1264 }
1265
1266 {
1267 vh.set(array, i, (byte)0x01);
1268
1269 byte o = (byte) vh.getAndBitwiseAndAcquire(array, i, (byte)0x23);
1270 assertEquals((byte)0x01, o, "getAndBitwiseAndAcquire byte");
1271 byte x = (byte) vh.get(array, i);
1272 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndAcquire byte value");
1273 }
1274
1275 {
1276 vh.set(array, i, (byte)0x01);
1277
1278 byte o = (byte) vh.getAndBitwiseAndRelease(array, i, (byte)0x23);
1279 assertEquals((byte)0x01, o, "getAndBitwiseAndRelease byte");
1280 byte x = (byte) vh.get(array, i);
1281 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndRelease byte value");
1282 }
1283
1284 // get and bitwise xor
1285 {
1286 vh.set(array, i, (byte)0x01);
1287
1288 byte o = (byte) vh.getAndBitwiseXor(array, i, (byte)0x23);
1289 assertEquals((byte)0x01, o, "getAndBitwiseXor byte");
1290 byte x = (byte) vh.get(array, i);
1291 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXor byte value");
1292 }
1293
1294 {
1295 vh.set(array, i, (byte)0x01);
1296
1297 byte o = (byte) vh.getAndBitwiseXorAcquire(array, i, (byte)0x23);
1298 assertEquals((byte)0x01, o, "getAndBitwiseXorAcquire byte");
1299 byte x = (byte) vh.get(array, i);
1300 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorAcquire byte value");
1301 }
1302
1303 {
1304 vh.set(array, i, (byte)0x01);
1305
1306 byte o = (byte) vh.getAndBitwiseXorRelease(array, i, (byte)0x23);
1307 assertEquals((byte)0x01, o, "getAndBitwiseXorRelease byte");
1308 byte x = (byte) vh.get(array, i);
1309 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorRelease byte value");
1310 }
1311 }
1312 }
1313
1314 static void testArrayUnsupported(VarHandle vh) {
1315 byte[] array = new byte[10];
1316
1317 int i = 0;
1318
1319
1320 }
1321
1322 static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1323 byte[] array = new byte[10];
1324
1325 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1326 final int ci = i;
1327
1328 checkAIOOBE(() -> {
1329 byte x = (byte) vh.get(array, ci);
1330 });
1331
1332 checkAIOOBE(() -> {
1333 vh.set(array, ci, (byte)0x01);
1334 });
1335
1336 checkAIOOBE(() -> {
1337 byte x = (byte) vh.getVolatile(array, ci);
1338 });
1339
1340 checkAIOOBE(() -> {
1341 vh.setVolatile(array, ci, (byte)0x01);
1342 });
1343
1344 checkAIOOBE(() -> {
1345 byte x = (byte) vh.getAcquire(array, ci);
1346 });
1347
1348 checkAIOOBE(() -> {
1349 vh.setRelease(array, ci, (byte)0x01);
1350 });
1351
1352 checkAIOOBE(() -> {
1353 byte x = (byte) vh.getOpaque(array, ci);
1354 });
1355
1356 checkAIOOBE(() -> {
1357 vh.setOpaque(array, ci, (byte)0x01);
1358 });
1359
1360 checkAIOOBE(() -> {
1361 boolean r = vh.compareAndSet(array, ci, (byte)0x01, (byte)0x23);
1362 });
1363
1364 checkAIOOBE(() -> {
1365 byte r = (byte) vh.compareAndExchange(array, ci, (byte)0x23, (byte)0x01);
1366 });
1367
1368 checkAIOOBE(() -> {
1369 byte r = (byte) vh.compareAndExchangeAcquire(array, ci, (byte)0x23, (byte)0x01);
1370 });
1371
1372 checkAIOOBE(() -> {
1373 byte r = (byte) vh.compareAndExchangeRelease(array, ci, (byte)0x23, (byte)0x01);
1374 });
1375
1376 checkAIOOBE(() -> {
1377 boolean r = vh.weakCompareAndSetPlain(array, ci, (byte)0x01, (byte)0x23);
1378 });
1379
1380 checkAIOOBE(() -> {
1381 boolean r = vh.weakCompareAndSet(array, ci, (byte)0x01, (byte)0x23);
1382 });
1383
1384 checkAIOOBE(() -> {
1385 boolean r = vh.weakCompareAndSetAcquire(array, ci, (byte)0x01, (byte)0x23);
1386 });
1387
1388 checkAIOOBE(() -> {
1389 boolean r = vh.weakCompareAndSetRelease(array, ci, (byte)0x01, (byte)0x23);
1390 });
1391
1392 checkAIOOBE(() -> {
1393 byte o = (byte) vh.getAndSet(array, ci, (byte)0x01);
1394 });
1395
1396 checkAIOOBE(() -> {
1397 byte o = (byte) vh.getAndSetAcquire(array, ci, (byte)0x01);
1398 });
1399
1400 checkAIOOBE(() -> {
1401 byte o = (byte) vh.getAndSetRelease(array, ci, (byte)0x01);
1402 });
1403
1404 checkAIOOBE(() -> {
1405 byte o = (byte) vh.getAndAdd(array, ci, (byte)0x01);
1406 });
1407
1408 checkAIOOBE(() -> {
1409 byte o = (byte) vh.getAndAddAcquire(array, ci, (byte)0x01);
1410 });
1411
1412 checkAIOOBE(() -> {
1413 byte o = (byte) vh.getAndAddRelease(array, ci, (byte)0x01);
1414 });
1415
1416 checkAIOOBE(() -> {
1417 byte o = (byte) vh.getAndBitwiseOr(array, ci, (byte)0x01);
1418 });
1419
1420 checkAIOOBE(() -> {
1421 byte o = (byte) vh.getAndBitwiseOrAcquire(array, ci, (byte)0x01);
1422 });
1423
1424 checkAIOOBE(() -> {
1425 byte o = (byte) vh.getAndBitwiseOrRelease(array, ci, (byte)0x01);
1426 });
1427
1428 checkAIOOBE(() -> {
1429 byte o = (byte) vh.getAndBitwiseAnd(array, ci, (byte)0x01);
1430 });
1431
1432 checkAIOOBE(() -> {
1433 byte o = (byte) vh.getAndBitwiseAndAcquire(array, ci, (byte)0x01);
1434 });
1435
1436 checkAIOOBE(() -> {
1437 byte o = (byte) vh.getAndBitwiseAndRelease(array, ci, (byte)0x01);
1438 });
1439
1440 checkAIOOBE(() -> {
1441 byte o = (byte) vh.getAndBitwiseXor(array, ci, (byte)0x01);
1442 });
1443
1444 checkAIOOBE(() -> {
1445 byte o = (byte) vh.getAndBitwiseXorAcquire(array, ci, (byte)0x01);
1446 });
1447
1448 checkAIOOBE(() -> {
1449 byte o = (byte) vh.getAndBitwiseXorRelease(array, ci, (byte)0x01);
1450 });
1451 }
1452 }
1453
1454 }
1455