1 /*
  2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessBoolean
 27  */
 28 
 29 import org.testng.annotations.BeforeClass;
 30 import org.testng.annotations.DataProvider;
 31 import org.testng.annotations.Test;
 32 
 33 import java.lang.invoke.MethodHandles;
 34 import java.lang.invoke.VarHandle;
 35 import java.util.ArrayList;
 36 import java.util.Arrays;
 37 import java.util.List;
 38 
 39 import static org.testng.Assert.*;
 40 
 41 public class VarHandleTestMethodHandleAccessBoolean extends VarHandleBaseTest {
 42     static final boolean static_final_v = true;
 43 
 44     static boolean static_v;
 45 
 46     final boolean final_v = true;
 47 
 48     boolean v;
 49 
 50     VarHandle vhFinalField;
 51 
 52     VarHandle vhField;
 53 
 54     VarHandle vhStaticField;
 55 
 56     VarHandle vhStaticFinalField;
 57 
 58     VarHandle vhArray;
 59 
 60     @BeforeClass
 61     public void setup() throws Exception {
 62         vhFinalField = MethodHandles.lookup().findVarHandle(
 63                 VarHandleTestMethodHandleAccessBoolean.class, "final_v", boolean.class);
 64 
 65         vhField = MethodHandles.lookup().findVarHandle(
 66                 VarHandleTestMethodHandleAccessBoolean.class, "v", boolean.class);
 67 
 68         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 69             VarHandleTestMethodHandleAccessBoolean.class, "static_final_v", boolean.class);
 70 
 71         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 72             VarHandleTestMethodHandleAccessBoolean.class, "static_v", boolean.class);
 73 
 74         vhArray = MethodHandles.arrayElementVarHandle(boolean[].class);
 75     }
 76 
 77 
 78     @DataProvider
 79     public Object[][] accessTestCaseProvider() throws Exception {
 80         List<AccessTestCase<?>> cases = new ArrayList<>();
 81 
 82         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
 83             cases.add(new MethodHandleAccessTestCase("Instance field",
 84                                                      vhField, f, hs -> testInstanceField(this, hs)));
 85             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
 86                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
 87                                                      false));
 88 
 89             cases.add(new MethodHandleAccessTestCase("Static field",
 90                                                      vhStaticField, f, VarHandleTestMethodHandleAccessBoolean::testStaticField));
 91             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
 92                                                      vhStaticField, f, VarHandleTestMethodHandleAccessBoolean::testStaticFieldUnsupported,
 93                                                      false));
 94 
 95             cases.add(new MethodHandleAccessTestCase("Array",
 96                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArray));
 97             cases.add(new MethodHandleAccessTestCase("Array unsupported",
 98                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArrayUnsupported,
 99                                                      false));
100             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
101                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArrayIndexOutOfBounds,
102                                                      false));
103         }
104 
105         // Work around issue with jtreg summary reporting which truncates
106         // the String result of Object.toString to 30 characters, hence
107         // the first dummy argument
108         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
109     }
110 
111     @Test(dataProvider = "accessTestCaseProvider")
112     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
113         T t = atc.get();
114         int iters = atc.requiresLoop() ? ITERS : 1;
115         for (int c = 0; c < iters; c++) {
116             atc.testAccess(t);
117         }
118     }
119 
120 
121     static void testInstanceField(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable {
122         // Plain
123         {
124             hs.get(TestAccessMode.SET).invokeExact(recv, true);
125             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
126             assertEquals(x, true, "set boolean value");
127         }
128 
129 
130         // Volatile
131         {
132             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, false);
133             boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
134             assertEquals(x, false, "setVolatile boolean value");
135         }
136 
137         // Lazy
138         {
139             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, true);
140             boolean x = (boolean) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
141             assertEquals(x, true, "setRelease boolean value");
142         }
143 
144         // Opaque
145         {
146             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, false);
147             boolean x = (boolean) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
148             assertEquals(x, false, "setOpaque boolean value");
149         }
150 
151         hs.get(TestAccessMode.SET).invokeExact(recv, true);
152 
153         // Compare
154         {
155             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, true, false);
156             assertEquals(r, true, "success compareAndSet boolean");
157             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
158             assertEquals(x, false, "success compareAndSet boolean value");
159         }
160 
161         {
162             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, true, false);
163             assertEquals(r, false, "failing compareAndSet boolean");
164             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
165             assertEquals(x, false, "failing compareAndSet boolean value");
166         }
167 
168         {
169             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, false, true);
170             assertEquals(r, false, "success compareAndExchange boolean");
171             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
172             assertEquals(x, true, "success compareAndExchange boolean value");
173         }
174 
175         {
176             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, false, false);
177             assertEquals(r, true, "failing compareAndExchange boolean");
178             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
179             assertEquals(x, true, "failing compareAndExchange boolean value");
180         }
181 
182         {
183             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, true, false);
184             assertEquals(r, true, "success compareAndExchangeAcquire boolean");
185             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
186             assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
187         }
188 
189         {
190             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, true, false);
191             assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
192             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
193             assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
194         }
195 
196         {
197             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, false, true);
198             assertEquals(r, false, "success compareAndExchangeRelease boolean");
199             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
200             assertEquals(x, true, "success compareAndExchangeRelease boolean value");
201         }
202 
203         {
204             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, false, false);
205             assertEquals(r, true, "failing compareAndExchangeRelease boolean");
206             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
207             assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
208         }
209 
210         {
211             boolean success = false;
212             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
213                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, true, false);
214             }
215             assertEquals(success, true, "weakCompareAndSetPlain boolean");
216             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
217             assertEquals(x, false, "weakCompareAndSetPlain boolean value");
218         }
219 
220         {
221             boolean success = false;
222             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
223                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, false, true);
224             }
225             assertEquals(success, true, "weakCompareAndSetAcquire boolean");
226             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
227             assertEquals(x, true, "weakCompareAndSetAcquire boolean");
228         }
229 
230         {
231             boolean success = false;
232             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
233                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, true, false);
234             }
235             assertEquals(success, true, "weakCompareAndSetRelease boolean");
236             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
237             assertEquals(x, false, "weakCompareAndSetRelease boolean");
238         }
239 
240         {
241             boolean success = false;
242             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
243                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, false, true);
244             }
245             assertEquals(success, true, "weakCompareAndSet boolean");
246             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
247             assertEquals(x, true, "weakCompareAndSet boolean");
248         }
249 
250         // Compare set and get
251         {
252             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, false);
253             assertEquals(o, true, "getAndSet boolean");
254             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
255             assertEquals(x, false, "getAndSet boolean value");
256         }
257 
258 
259         // get and bitwise or
260         {
261             hs.get(TestAccessMode.SET).invokeExact(recv, true);
262 
263             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(recv, false);
264             assertEquals(o, true, "getAndBitwiseOr boolean");
265             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
266             assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value");
267         }
268 
269         {
270             hs.get(TestAccessMode.SET).invokeExact(recv, true);
271 
272             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(recv, false);
273             assertEquals(o, true, "getAndBitwiseOrAcquire boolean");
274             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
275             assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value");
276         }
277 
278         {
279             hs.get(TestAccessMode.SET).invokeExact(recv, true);
280 
281             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(recv, false);
282             assertEquals(o, true, "getAndBitwiseOrRelease boolean");
283             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
284             assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value");
285         }
286 
287         // get and bitwise and
288         {
289             hs.get(TestAccessMode.SET).invokeExact(recv, true);
290 
291             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(recv, false);
292             assertEquals(o, true, "getAndBitwiseAnd boolean");
293             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
294             assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value");
295         }
296 
297         {
298             hs.get(TestAccessMode.SET).invokeExact(recv, true);
299 
300             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(recv, false);
301             assertEquals(o, true, "getAndBitwiseAndAcquire boolean");
302             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
303             assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value");
304         }
305 
306         {
307             hs.get(TestAccessMode.SET).invokeExact(recv, true);
308 
309             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(recv, false);
310             assertEquals(o, true, "getAndBitwiseAndRelease boolean");
311             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
312             assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value");
313         }
314 
315         // get and bitwise xor
316         {
317             hs.get(TestAccessMode.SET).invokeExact(recv, true);
318 
319             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(recv, false);
320             assertEquals(o, true, "getAndBitwiseXor boolean");
321             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
322             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value");
323         }
324 
325         {
326             hs.get(TestAccessMode.SET).invokeExact(recv, true);
327 
328             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(recv, false);
329             assertEquals(o, true, "getAndBitwiseXorAcquire boolean");
330             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
331             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value");
332         }
333 
334         {
335             hs.get(TestAccessMode.SET).invokeExact(recv, true);
336 
337             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, false);
338             assertEquals(o, true, "getAndBitwiseXorRelease boolean");
339             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
340             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value");
341         }
342     }
343 
344     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable {
345 
346         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
347             checkUOE(am, () -> {
348                 boolean r = (boolean) hs.get(am).invokeExact(recv, true);
349             });
350         }
351 
352     }
353 
354 
355     static void testStaticField(Handles hs) throws Throwable {
356         // Plain
357         {
358             hs.get(TestAccessMode.SET).invokeExact(true);
359             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
360             assertEquals(x, true, "set boolean value");
361         }
362 
363 
364         // Volatile
365         {
366             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(false);
367             boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
368             assertEquals(x, false, "setVolatile boolean value");
369         }
370 
371         // Lazy
372         {
373             hs.get(TestAccessMode.SET_RELEASE).invokeExact(true);
374             boolean x = (boolean) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
375             assertEquals(x, true, "setRelease boolean value");
376         }
377 
378         // Opaque
379         {
380             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(false);
381             boolean x = (boolean) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
382             assertEquals(x, false, "setOpaque boolean value");
383         }
384 
385         hs.get(TestAccessMode.SET).invokeExact(true);
386 
387         // Compare
388         {
389             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(true, false);
390             assertEquals(r, true, "success compareAndSet boolean");
391             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
392             assertEquals(x, false, "success compareAndSet boolean value");
393         }
394 
395         {
396             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(true, false);
397             assertEquals(r, false, "failing compareAndSet boolean");
398             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
399             assertEquals(x, false, "failing compareAndSet boolean value");
400         }
401 
402         {
403             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(false, true);
404             assertEquals(r, false, "success compareAndExchange boolean");
405             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
406             assertEquals(x, true, "success compareAndExchange boolean value");
407         }
408 
409         {
410             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(false, false);
411             assertEquals(r, true, "failing compareAndExchange boolean");
412             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
413             assertEquals(x, true, "failing compareAndExchange boolean value");
414         }
415 
416         {
417             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(true, false);
418             assertEquals(r, true, "success compareAndExchangeAcquire boolean");
419             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
420             assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
421         }
422 
423         {
424             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(true, false);
425             assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
426             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
427             assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
428         }
429 
430         {
431             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(false, true);
432             assertEquals(r, false, "success compareAndExchangeRelease boolean");
433             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
434             assertEquals(x, true, "success compareAndExchangeRelease boolean value");
435         }
436 
437         {
438             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(false, false);
439             assertEquals(r, true, "failing compareAndExchangeRelease boolean");
440             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
441             assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
442         }
443 
444         {
445             boolean success = false;
446             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
447                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(true, false);
448             }
449             assertEquals(success, true, "weakCompareAndSetPlain boolean");
450             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
451             assertEquals(x, false, "weakCompareAndSetPlain boolean value");
452         }
453 
454         {
455             boolean success = false;
456             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
457                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(false, true);
458             }
459             assertEquals(success, true, "weakCompareAndSetAcquire boolean");
460             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
461             assertEquals(x, true, "weakCompareAndSetAcquire boolean");
462         }
463 
464         {
465             boolean success = false;
466             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
467                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(true, false);
468             }
469             assertEquals(success, true, "weakCompareAndSetRelease boolean");
470             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
471             assertEquals(x, false, "weakCompareAndSetRelease boolean");
472         }
473 
474         {
475             boolean success = false;
476             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
477                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(false, true);
478             }
479             assertEquals(success, true, "weakCompareAndSet boolean");
480             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
481             assertEquals(x, true, "weakCompareAndSet boolean");
482         }
483 
484         // Compare set and get
485         {
486             hs.get(TestAccessMode.SET).invokeExact(true);
487 
488             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET).invokeExact(false);
489             assertEquals(o, true, "getAndSet boolean");
490             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
491             assertEquals(x, false, "getAndSet boolean value");
492         }
493 
494         // Compare set and get
495         {
496             hs.get(TestAccessMode.SET).invokeExact(true);
497 
498             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(false);
499             assertEquals(o, true, "getAndSetAcquire boolean");
500             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
501             assertEquals(x, false, "getAndSetAcquire boolean value");
502         }
503 
504         // Compare set and get
505         {
506             hs.get(TestAccessMode.SET).invokeExact(true);
507 
508             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(false);
509             assertEquals(o, true, "getAndSetRelease boolean");
510             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
511             assertEquals(x, false, "getAndSetRelease boolean value");
512         }
513 
514 
515         // get and bitwise or
516         {
517             hs.get(TestAccessMode.SET).invokeExact(true);
518 
519             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(false);
520             assertEquals(o, true, "getAndBitwiseOr boolean");
521             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
522             assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value");
523         }
524 
525         {
526             hs.get(TestAccessMode.SET).invokeExact(true);
527 
528             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(false);
529             assertEquals(o, true, "getAndBitwiseOrAcquire boolean");
530             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
531             assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value");
532         }
533 
534         {
535             hs.get(TestAccessMode.SET).invokeExact(true);
536 
537             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(false);
538             assertEquals(o, true, "getAndBitwiseOrRelease boolean");
539             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
540             assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value");
541         }
542 
543         // get and bitwise and
544         {
545             hs.get(TestAccessMode.SET).invokeExact(true);
546 
547             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(false);
548             assertEquals(o, true, "getAndBitwiseAnd boolean");
549             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
550             assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value");
551         }
552 
553         {
554             hs.get(TestAccessMode.SET).invokeExact(true);
555 
556             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(false);
557             assertEquals(o, true, "getAndBitwiseAndAcquire boolean");
558             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
559             assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value");
560         }
561 
562         {
563             hs.get(TestAccessMode.SET).invokeExact(true);
564 
565             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(false);
566             assertEquals(o, true, "getAndBitwiseAndRelease boolean");
567             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
568             assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value");
569         }
570 
571         // get and bitwise xor
572         {
573             hs.get(TestAccessMode.SET).invokeExact(true);
574 
575             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(false);
576             assertEquals(o, true, "getAndBitwiseXor boolean");
577             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
578             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value");
579         }
580 
581         {
582             hs.get(TestAccessMode.SET).invokeExact(true);
583 
584             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(false);
585             assertEquals(o, true, "getAndBitwiseXorAcquire boolean");
586             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
587             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value");
588         }
589 
590         {
591             hs.get(TestAccessMode.SET).invokeExact(true);
592 
593             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(false);
594             assertEquals(o, true, "getAndBitwiseXorRelease boolean");
595             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
596             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value");
597         }
598     }
599 
600     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
601 
602         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
603             checkUOE(am, () -> {
604                 boolean r = (boolean) hs.get(am).invokeExact(true);
605             });
606         }
607 
608     }
609 
610 
611     static void testArray(Handles hs) throws Throwable {
612         boolean[] array = new boolean[10];
613 
614         for (int i = 0; i < array.length; i++) {
615             // Plain
616             {
617                 hs.get(TestAccessMode.SET).invokeExact(array, i, true);
618                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
619                 assertEquals(x, true, "get boolean value");
620             }
621 
622 
623             // Volatile
624             {
625                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, false);
626                 boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
627                 assertEquals(x, false, "setVolatile boolean value");
628             }
629 
630             // Lazy
631             {
632                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, true);
633                 boolean x = (boolean) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
634                 assertEquals(x, true, "setRelease boolean value");
635             }
636 
637             // Opaque
638             {
639                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, false);
640                 boolean x = (boolean) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
641                 assertEquals(x, false, "setOpaque boolean value");
642             }
643 
644             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
645 
646             // Compare
647             {
648                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, true, false);
649                 assertEquals(r, true, "success compareAndSet boolean");
650                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
651                 assertEquals(x, false, "success compareAndSet boolean value");
652             }
653 
654             {
655                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, true, false);
656                 assertEquals(r, false, "failing compareAndSet boolean");
657                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
658                 assertEquals(x, false, "failing compareAndSet boolean value");
659             }
660 
661             {
662                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, false, true);
663                 assertEquals(r, false, "success compareAndExchange boolean");
664                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
665                 assertEquals(x, true, "success compareAndExchange boolean value");
666             }
667 
668             {
669                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, false, false);
670                 assertEquals(r, true, "failing compareAndExchange boolean");
671                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
672                 assertEquals(x, true, "failing compareAndExchange boolean value");
673             }
674 
675             {
676                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, true, false);
677                 assertEquals(r, true, "success compareAndExchangeAcquire boolean");
678                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
679                 assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
680             }
681 
682             {
683                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, true, false);
684                 assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
685                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
686                 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
687             }
688 
689             {
690                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, false, true);
691                 assertEquals(r, false, "success compareAndExchangeRelease boolean");
692                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
693                 assertEquals(x, true, "success compareAndExchangeRelease boolean value");
694             }
695 
696             {
697                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, false, false);
698                 assertEquals(r, true, "failing compareAndExchangeRelease boolean");
699                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
700                 assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
701             }
702 
703             {
704                 boolean success = false;
705                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
706                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, true, false);
707                 }
708                 assertEquals(success, true, "weakCompareAndSetPlain boolean");
709                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
710                 assertEquals(x, false, "weakCompareAndSetPlain boolean value");
711             }
712 
713             {
714                 boolean success = false;
715                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
716                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, false, true);
717                 }
718                 assertEquals(success, true, "weakCompareAndSetAcquire boolean");
719                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
720                 assertEquals(x, true, "weakCompareAndSetAcquire boolean");
721             }
722 
723             {
724                 boolean success = false;
725                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
726                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, true, false);
727                 }
728                 assertEquals(success, true, "weakCompareAndSetRelease boolean");
729                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
730                 assertEquals(x, false, "weakCompareAndSetRelease boolean");
731             }
732 
733             {
734                 boolean success = false;
735                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
736                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, false, true);
737                 }
738                 assertEquals(success, true, "weakCompareAndSet boolean");
739                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
740                 assertEquals(x, true, "weakCompareAndSet boolean");
741             }
742 
743             // Compare set and get
744             {
745                 hs.get(TestAccessMode.SET).invokeExact(array, i, true);
746 
747                 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, false);
748                 assertEquals(o, true, "getAndSet boolean");
749                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
750                 assertEquals(x, false, "getAndSet boolean value");
751             }
752 
753             {
754                 hs.get(TestAccessMode.SET).invokeExact(array, i, true);
755 
756                 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, false);
757                 assertEquals(o, true, "getAndSetAcquire boolean");
758                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
759                 assertEquals(x, false, "getAndSetAcquire boolean value");
760             }
761 
762             {
763                 hs.get(TestAccessMode.SET).invokeExact(array, i, true);
764 
765                 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, false);
766                 assertEquals(o, true, "getAndSetRelease boolean");
767                 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
768                 assertEquals(x, false, "getAndSetRelease boolean value");
769             }
770 
771 
772         // get and bitwise or
773         {
774             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
775 
776             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(array, i, false);
777             assertEquals(o, true, "getAndBitwiseOr boolean");
778             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
779             assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value");
780         }
781 
782         {
783             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
784 
785             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(array, i, false);
786             assertEquals(o, true, "getAndBitwiseOrAcquire boolean");
787             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
788             assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value");
789         }
790 
791         {
792             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
793 
794             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(array, i, false);
795             assertEquals(o, true, "getAndBitwiseOrRelease boolean");
796             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
797             assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value");
798         }
799 
800         // get and bitwise and
801         {
802             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
803 
804             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(array, i, false);
805             assertEquals(o, true, "getAndBitwiseAnd boolean");
806             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
807             assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value");
808         }
809 
810         {
811             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
812 
813             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(array, i, false);
814             assertEquals(o, true, "getAndBitwiseAndAcquire boolean");
815             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
816             assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value");
817         }
818 
819         {
820             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
821 
822             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(array, i, false);
823             assertEquals(o, true, "getAndBitwiseAndRelease boolean");
824             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
825             assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value");
826         }
827 
828         // get and bitwise xor
829         {
830             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
831 
832             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(array, i, false);
833             assertEquals(o, true, "getAndBitwiseXor boolean");
834             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
835             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value");
836         }
837 
838         {
839             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
840 
841             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(array, i, false);
842             assertEquals(o, true, "getAndBitwiseXorAcquire boolean");
843             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
844             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value");
845         }
846 
847         {
848             hs.get(TestAccessMode.SET).invokeExact(array, i, true);
849 
850             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(array, i, false);
851             assertEquals(o, true, "getAndBitwiseXorRelease boolean");
852             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i);
853             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value");
854         }
855         }
856     }
857 
858     static void testArrayUnsupported(Handles hs) throws Throwable {
859         boolean[] array = new boolean[10];
860 
861         final int i = 0;
862 
863         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
864             checkUOE(am, () -> {
865                 boolean o = (boolean) hs.get(am).invokeExact(array, i, true);
866             });
867         }
868 
869     }
870 
871     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
872         boolean[] array = new boolean[10];
873 
874         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
875             final int ci = i;
876 
877             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
878                 checkAIOOBE(am, () -> {
879                     boolean x = (boolean) hs.get(am).invokeExact(array, ci);
880                 });
881             }
882 
883             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
884                 checkAIOOBE(am, () -> {
885                     hs.get(am).invokeExact(array, ci, true);
886                 });
887             }
888 
889             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
890                 checkAIOOBE(am, () -> {
891                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, true, false);
892                 });
893             }
894 
895             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
896                 checkAIOOBE(am, () -> {
897                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, false, true);
898                 });
899             }
900 
901             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
902                 checkAIOOBE(am, () -> {
903                     boolean o = (boolean) hs.get(am).invokeExact(array, ci, true);
904                 });
905             }
906 
907 
908             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
909                 checkAIOOBE(am, () -> {
910                     boolean o = (boolean) hs.get(am).invokeExact(array, ci, false);
911                 });
912             }
913         }
914     }
915 }
916