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