< prev index next >

test/java/lang/invoke/VarHandles/VarHandleTestMethodHandleAccessByte.java

Print this page




  22  */
  23 
  24 /*
  25  * @test
  26  * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessByte
  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 VarHandleTestMethodHandleAccessByte extends VarHandleBaseTest {
  42     static final byte static_final_v = (byte)1;
  43 
  44     static byte static_v;
  45 
  46     final byte final_v = (byte)1;
  47 
  48     byte 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                 VarHandleTestMethodHandleAccessByte.class, "final_v", byte.class);
  64 
  65         vhField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestMethodHandleAccessByte.class, "v", byte.class);


 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(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
 122         // Plain
 123         {
 124             hs.get(TestAccessMode.SET).invokeExact(recv, (byte)1);
 125             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 126             assertEquals(x, (byte)1, "set byte value");
 127         }
 128 
 129 
 130         // Volatile
 131         {
 132             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, (byte)2);
 133             byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
 134             assertEquals(x, (byte)2, "setVolatile byte value");
 135         }
 136 
 137         // Lazy
 138         {
 139             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, (byte)1);
 140             byte x = (byte) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
 141             assertEquals(x, (byte)1, "setRelease byte value");
 142         }
 143 
 144         // Opaque
 145         {
 146             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, (byte)2);
 147             byte x = (byte) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
 148             assertEquals(x, (byte)2, "setOpaque byte value");
 149         }
 150 

 151 






 152     }
 153 
 154     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
 155         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 156             checkUOE(am, () -> {
 157                 boolean r = (boolean) hs.get(am).invokeExact(recv, (byte)1, (byte)2);
 158             });
 159         }
 160 
 161         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 162             checkUOE(am, () -> {
 163                 byte r = (byte) hs.get(am).invokeExact(recv, (byte)1, (byte)2);
 164             });

 165         }
 166 
 167         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 168             checkUOE(am, () -> {
 169                 byte r = (byte) hs.get(am).invokeExact(recv, (byte)1);
 170             });

 171         }
 172 
 173         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 174             checkUOE(am, () -> {
 175                 byte r = (byte) hs.get(am).invokeExact(recv, (byte)1);
 176             });






































































 177         }
 178     }
 179 




 180 
 181     static void testStaticField(Handles hs) throws Throwable {
 182         // Plain
 183         {
 184             hs.get(TestAccessMode.SET).invokeExact((byte)1);
 185             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 186             assertEquals(x, (byte)1, "set byte value");
 187         }
 188 
 189 
 190         // Volatile
 191         {
 192             hs.get(TestAccessMode.SET_VOLATILE).invokeExact((byte)2);
 193             byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 194             assertEquals(x, (byte)2, "setVolatile byte value");
 195         }
 196 
 197         // Lazy
 198         {
 199             hs.get(TestAccessMode.SET_RELEASE).invokeExact((byte)1);
 200             byte x = (byte) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
 201             assertEquals(x, (byte)1, "setRelease byte value");
 202         }
 203 
 204         // Opaque
 205         {
 206             hs.get(TestAccessMode.SET_OPAQUE).invokeExact((byte)2);
 207             byte x = (byte) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
 208             assertEquals(x, (byte)2, "setOpaque byte value");
 209         }
 210 

 211 






 212     }
 213 
 214     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
 215         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 216             checkUOE(am, () -> {
 217                 boolean r = (boolean) hs.get(am).invokeExact((byte)1, (byte)2);
 218             });
 219         }
 220 
 221         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 222             checkUOE(am, () -> {
 223                 byte r = (byte) hs.get(am).invokeExact((byte)1, (byte)2);
 224             });

 225         }
 226 
 227         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 228             checkUOE(am, () -> {
 229                 byte r = (byte) hs.get(am).invokeExact((byte)1);
 230             });

 231         }
 232 
 233         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 234             checkUOE(am, () -> {
 235                 byte r = (byte) hs.get(am).invokeExact((byte)1);
 236             });










































 237         }
































 238     }
 239 
 240 
 241     static void testArray(Handles hs) throws Throwable {
 242         byte[] array = new byte[10];
 243 
 244         for (int i = 0; i < array.length; i++) {
 245             // Plain
 246             {
 247                 hs.get(TestAccessMode.SET).invokeExact(array, i, (byte)1);
 248                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 249                 assertEquals(x, (byte)1, "get byte value");
 250             }
 251 
 252 
 253             // Volatile
 254             {
 255                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, (byte)2);
 256                 byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
 257                 assertEquals(x, (byte)2, "setVolatile byte value");
 258             }
 259 
 260             // Lazy
 261             {
 262                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, (byte)1);
 263                 byte x = (byte) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
 264                 assertEquals(x, (byte)1, "setRelease byte value");
 265             }
 266 
 267             // Opaque
 268             {
 269                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, (byte)2);
 270                 byte x = (byte) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
 271                 assertEquals(x, (byte)2, "setOpaque byte value");
 272             }
 273 

 274 






 275         }






 276     }
 277 
 278     static void testArrayUnsupported(Handles hs) throws Throwable {
 279         byte[] array = new byte[10];




 280 
 281         final int i = 0;
 282         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 283             checkUOE(am, () -> {
 284                 boolean r = (boolean) hs.get(am).invokeExact(array, i, (byte)1, (byte)2);
 285             });
 286         }
 287 
 288         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 289             checkUOE(am, () -> {
 290                 byte r = (byte) hs.get(am).invokeExact(array, i, (byte)1, (byte)2);
 291             });

 292         }
 293 
 294         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 295             checkUOE(am, () -> {
 296                 byte r = (byte) hs.get(am).invokeExact(array, i, (byte)1);
 297             });

 298         }
 299 
 300         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 301             checkUOE(am, () -> {
 302                 byte o = (byte) hs.get(am).invokeExact(array, i, (byte)1);
 303             });








 304         }
























































 305     }
 306 
 307     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
 308         byte[] array = new byte[10];
 309 
 310         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 311             final int ci = i;
 312 
 313             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
 314                 checkIOOBE(am, () -> {
 315                     byte x = (byte) hs.get(am).invokeExact(array, ci);
 316                 });
 317             }
 318 
 319             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 320                 checkIOOBE(am, () -> {
 321                     hs.get(am).invokeExact(array, ci, (byte)1);












 322                 });
 323             }
 324 





 325 





 326         }
 327     }
 328 }
 329 


  22  */
  23 
  24 /*
  25  * @test
  26  * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessByte
  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 VarHandleTestMethodHandleAccessByte extends VarHandleBaseTest {
  42     static final byte static_final_v = (byte)0x01;
  43 
  44     static byte static_v;
  45 
  46     final byte final_v = (byte)0x01;
  47 
  48     byte 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                 VarHandleTestMethodHandleAccessByte.class, "final_v", byte.class);
  64 
  65         vhField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestMethodHandleAccessByte.class, "v", byte.class);


 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(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
 122         // Plain
 123         {
 124             hs.get(TestAccessMode.SET).invokeExact(recv, (byte)0x01);
 125             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 126             assertEquals(x, (byte)0x01, "set byte value");
 127         }
 128 
 129 
 130         // Volatile
 131         {
 132             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, (byte)0x23);
 133             byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
 134             assertEquals(x, (byte)0x23, "setVolatile byte value");
 135         }
 136 
 137         // Lazy
 138         {
 139             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, (byte)0x01);
 140             byte x = (byte) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
 141             assertEquals(x, (byte)0x01, "setRelease byte value");
 142         }
 143 
 144         // Opaque
 145         {
 146             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, (byte)0x23);
 147             byte x = (byte) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
 148             assertEquals(x, (byte)0x23, "setOpaque byte value");
 149         }
 150 
 151         hs.get(TestAccessMode.SET).invokeExact(recv, (byte)0x01);
 152 
 153         // Compare
 154         {
 155             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, (byte)0x01, (byte)0x23);
 156             assertEquals(r, true, "success compareAndSet byte");
 157             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 158             assertEquals(x, (byte)0x23, "success compareAndSet byte value");
 159         }
 160 
 161         {
 162             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, (byte)0x01, (byte)0x45);
 163             assertEquals(r, false, "failing compareAndSet byte");
 164             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 165             assertEquals(x, (byte)0x23, "failing compareAndSet byte value");
 166         }
 167 
 168         {
 169             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, (byte)0x23, (byte)0x01);
 170             assertEquals(r, (byte)0x23, "success compareAndExchangeVolatile byte");
 171             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 172             assertEquals(x, (byte)0x01, "success compareAndExchangeVolatile byte value");
 173         }
 174 
 175         {
 176             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, (byte)0x23, (byte)0x45);
 177             assertEquals(r, (byte)0x01, "failing compareAndExchangeVolatile byte");
 178             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 179             assertEquals(x, (byte)0x01, "failing compareAndExchangeVolatile byte value");
 180         }
 181 
 182         {
 183             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, (byte)0x01, (byte)0x23);
 184             assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
 185             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 186             assertEquals(x, (byte)0x23, "success compareAndExchangeAcquire byte value");
 187         }
 188 
 189         {
 190             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, (byte)0x01, (byte)0x45);
 191             assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
 192             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 193             assertEquals(x, (byte)0x23, "failing compareAndExchangeAcquire byte value");
 194         }
 195 
 196         {
 197             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, (byte)0x23, (byte)0x01);
 198             assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
 199             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 200             assertEquals(x, (byte)0x01, "success compareAndExchangeRelease byte value");
 201         }
 202 
 203         {
 204             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, (byte)0x23, (byte)0x45);
 205             assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
 206             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 207             assertEquals(x, (byte)0x01, "failing compareAndExchangeRelease byte 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).invokeExact(recv, (byte)0x01, (byte)0x23);
 214             }
 215             assertEquals(success, true, "weakCompareAndSet byte");
 216             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 217             assertEquals(x, (byte)0x23, "weakCompareAndSet byte 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, (byte)0x23, (byte)0x01);
 224             }
 225             assertEquals(success, true, "weakCompareAndSetAcquire byte");
 226             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 227             assertEquals(x, (byte)0x01, "weakCompareAndSetAcquire byte");
 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, (byte)0x01, (byte)0x23);
 234             }
 235             assertEquals(success, true, "weakCompareAndSetRelease byte");
 236             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 237             assertEquals(x, (byte)0x23, "weakCompareAndSetRelease byte");
 238         }
 239 
 240         // Compare set and get
 241         {
 242             byte o = (byte) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, (byte)0x01);
 243             assertEquals(o, (byte)0x23, "getAndSet byte");
 244             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
 245             assertEquals(x, (byte)0x01, "getAndSet byte value");
 246         }
 247 
 248         hs.get(TestAccessMode.SET).invokeExact(recv, (byte)0x01);
 249 
 250         // get and add, add and get
 251         {
 252             byte o = (byte) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, (byte)0x45);
 253             assertEquals(o, (byte)0x01, "getAndAdd byte");
 254             byte c = (byte) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(recv, (byte)0x45);
 255             assertEquals(c, (byte)((byte)0x01 + (byte)0x45 + (byte)0x45), "getAndAdd byte value");
 256         }
 257     }
 258 
 259     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
 260 
 261     }
 262 
 263 
 264     static void testStaticField(Handles hs) throws Throwable {
 265         // Plain
 266         {
 267             hs.get(TestAccessMode.SET).invokeExact((byte)0x01);
 268             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 269             assertEquals(x, (byte)0x01, "set byte value");
 270         }
 271 
 272 
 273         // Volatile
 274         {
 275             hs.get(TestAccessMode.SET_VOLATILE).invokeExact((byte)0x23);
 276             byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 277             assertEquals(x, (byte)0x23, "setVolatile byte value");
 278         }
 279 
 280         // Lazy
 281         {
 282             hs.get(TestAccessMode.SET_RELEASE).invokeExact((byte)0x01);
 283             byte x = (byte) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
 284             assertEquals(x, (byte)0x01, "setRelease byte value");
 285         }
 286 
 287         // Opaque
 288         {
 289             hs.get(TestAccessMode.SET_OPAQUE).invokeExact((byte)0x23);
 290             byte x = (byte) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
 291             assertEquals(x, (byte)0x23, "setOpaque byte value");
 292         }
 293 
 294         hs.get(TestAccessMode.SET).invokeExact((byte)0x01);
 295 
 296         // Compare
 297         {
 298             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact((byte)0x01, (byte)0x23);
 299             assertEquals(r, true, "success compareAndSet byte");
 300             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 301             assertEquals(x, (byte)0x23, "success compareAndSet byte value");
 302         }
 303 
 304         {
 305             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact((byte)0x01, (byte)0x45);
 306             assertEquals(r, false, "failing compareAndSet byte");
 307             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 308             assertEquals(x, (byte)0x23, "failing compareAndSet byte value");
 309         }
 310 
 311         {
 312             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact((byte)0x23, (byte)0x01);
 313             assertEquals(r, (byte)0x23, "success compareAndExchangeVolatile byte");
 314             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 315             assertEquals(x, (byte)0x01, "success compareAndExchangeVolatile byte value");
 316         }
 317 
 318         {
 319             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact((byte)0x23, (byte)0x45);
 320             assertEquals(r, (byte)0x01, "failing compareAndExchangeVolatile byte");
 321             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 322             assertEquals(x, (byte)0x01, "failing compareAndExchangeVolatile byte value");
 323         }
 324 
 325         {
 326             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact((byte)0x01, (byte)0x23);
 327             assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
 328             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 329             assertEquals(x, (byte)0x23, "success compareAndExchangeAcquire byte value");
 330         }
 331 
 332         {
 333             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact((byte)0x01, (byte)0x45);
 334             assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
 335             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 336             assertEquals(x, (byte)0x23, "failing compareAndExchangeAcquire byte value");
 337         }
 338 
 339         {
 340             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact((byte)0x23, (byte)0x01);
 341             assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
 342             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 343             assertEquals(x, (byte)0x01, "success compareAndExchangeRelease byte value");
 344         }
 345 
 346         {
 347             byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact((byte)0x23, (byte)0x45);
 348             assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
 349             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 350             assertEquals(x, (byte)0x01, "failing compareAndExchangeRelease byte value");
 351         }
 352 
 353         {
 354             boolean success = false;
 355             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 356                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact((byte)0x01, (byte)0x23);
 357             }
 358             assertEquals(success, true, "weakCompareAndSet byte");
 359             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 360             assertEquals(x, (byte)0x23, "weakCompareAndSet byte value");
 361         }
 362 
 363         {
 364             boolean success = false;
 365             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 366                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact((byte)0x23, (byte)0x01);
 367             }
 368             assertEquals(success, true, "weakCompareAndSetAcquire byte");
 369             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 370             assertEquals(x, (byte)0x01, "weakCompareAndSetAcquire byte");
 371         }
 372 
 373         {
 374             boolean success = false;
 375             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 376                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact((byte)0x01, (byte)0x23);
 377             }
 378             assertEquals(success, true, "weakCompareAndSetRelease byte");
 379             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 380             assertEquals(x, (byte)0x23, "weakCompareAndSetRelease byte");
 381         }
 382 
 383         // Compare set and get
 384         {
 385             byte o = (byte) hs.get(TestAccessMode.GET_AND_SET).invokeExact( (byte)0x01);
 386             assertEquals(o, (byte)0x23, "getAndSet byte");
 387             byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
 388             assertEquals(x, (byte)0x01, "getAndSet byte value");
 389         }
 390 
 391         hs.get(TestAccessMode.SET).invokeExact((byte)0x01);
 392 
 393         // get and add, add and get
 394         {
 395             byte o = (byte) hs.get(TestAccessMode.GET_AND_ADD).invokeExact( (byte)0x45);
 396             assertEquals(o, (byte)0x01, "getAndAdd byte");
 397             byte c = (byte) hs.get(TestAccessMode.ADD_AND_GET).invokeExact((byte)0x45);
 398             assertEquals(c, (byte)((byte)0x01 + (byte)0x45 + (byte)0x45), "getAndAdd byte value");
 399         }
 400     }
 401 
 402     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
 403 
 404     }
 405 
 406 
 407     static void testArray(Handles hs) throws Throwable {
 408         byte[] array = new byte[10];
 409 
 410         for (int i = 0; i < array.length; i++) {
 411             // Plain
 412             {
 413                 hs.get(TestAccessMode.SET).invokeExact(array, i, (byte)0x01);
 414                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 415                 assertEquals(x, (byte)0x01, "get byte value");
 416             }
 417 
 418 
 419             // Volatile
 420             {
 421                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, (byte)0x23);
 422                 byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
 423                 assertEquals(x, (byte)0x23, "setVolatile byte value");
 424             }
 425 
 426             // Lazy
 427             {
 428                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, (byte)0x01);
 429                 byte x = (byte) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
 430                 assertEquals(x, (byte)0x01, "setRelease byte value");
 431             }
 432 
 433             // Opaque
 434             {
 435                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, (byte)0x23);
 436                 byte x = (byte) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
 437                 assertEquals(x, (byte)0x23, "setOpaque byte value");
 438             }
 439 
 440             hs.get(TestAccessMode.SET).invokeExact(array, i, (byte)0x01);
 441 
 442             // Compare
 443             {
 444                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, (byte)0x01, (byte)0x23);
 445                 assertEquals(r, true, "success compareAndSet byte");
 446                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 447                 assertEquals(x, (byte)0x23, "success compareAndSet byte value");
 448             }
 449 
 450             {
 451                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, (byte)0x01, (byte)0x45);
 452                 assertEquals(r, false, "failing compareAndSet byte");
 453                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 454                 assertEquals(x, (byte)0x23, "failing compareAndSet byte value");
 455             }
 456 
 457             {
 458                 byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, (byte)0x23, (byte)0x01);
 459                 assertEquals(r, (byte)0x23, "success compareAndExchangeVolatile byte");
 460                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 461                 assertEquals(x, (byte)0x01, "success compareAndExchangeVolatile byte value");
 462             }
 463 
 464             {
 465                 byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, (byte)0x23, (byte)0x45);
 466                 assertEquals(r, (byte)0x01, "failing compareAndExchangeVolatile byte");
 467                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 468                 assertEquals(x, (byte)0x01, "failing compareAndExchangeVolatile byte value");
 469             }
 470 
 471             {
 472                 byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, (byte)0x01, (byte)0x23);
 473                 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
 474                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 475                 assertEquals(x, (byte)0x23, "success compareAndExchangeAcquire byte value");
 476             }
 477 
 478             {
 479                 byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, (byte)0x01, (byte)0x45);
 480                 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
 481                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 482                 assertEquals(x, (byte)0x23, "failing compareAndExchangeAcquire byte value");
 483             }
 484 
 485             {
 486                 byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, (byte)0x23, (byte)0x01);
 487                 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
 488                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 489                 assertEquals(x, (byte)0x01, "success compareAndExchangeRelease byte value");
 490             }
 491 
 492             {
 493                 byte r = (byte) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, (byte)0x23, (byte)0x45);
 494                 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
 495                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 496                 assertEquals(x, (byte)0x01, "failing compareAndExchangeRelease byte value");
 497             }
 498 
 499             {
 500                 boolean success = false;
 501                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 502                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, (byte)0x01, (byte)0x23);
 503                 }
 504                 assertEquals(success, true, "weakCompareAndSet byte");
 505                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 506                 assertEquals(x, (byte)0x23, "weakCompareAndSet byte value");
 507             }
 508 
 509             {
 510                 boolean success = false;
 511                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 512                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, (byte)0x23, (byte)0x01);
 513                 }
 514                 assertEquals(success, true, "weakCompareAndSetAcquire byte");
 515                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 516                 assertEquals(x, (byte)0x01, "weakCompareAndSetAcquire byte");
 517             }
 518 
 519             {
 520                 boolean success = false;
 521                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 522                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, (byte)0x01, (byte)0x23);
 523                 }
 524                 assertEquals(success, true, "weakCompareAndSetRelease byte");
 525                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 526                 assertEquals(x, (byte)0x23, "weakCompareAndSetRelease byte");
 527             }
 528 
 529             // Compare set and get
 530             {
 531                 byte o = (byte) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, (byte)0x01);
 532                 assertEquals(o, (byte)0x23, "getAndSet byte");
 533                 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(array, i);
 534                 assertEquals(x, (byte)0x01, "getAndSet byte value");
 535             }
 536 
 537             hs.get(TestAccessMode.SET).invokeExact(array, i, (byte)0x01);
 538 
 539             // get and add, add and get
 540             {
 541                 byte o = (byte) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, (byte)0x45);
 542                 assertEquals(o, (byte)0x01, "getAndAdd byte");
 543                 byte c = (byte) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(array, i, (byte)0x45);
 544                 assertEquals(c, (byte)((byte)0x01 + (byte)0x45 + (byte)0x45), "getAndAdd byte value");
 545             }
 546         }
 547     }
 548 
 549     static void testArrayUnsupported(Handles hs) throws Throwable {
 550         byte[] array = new byte[10];
 551 
 552         final int i = 0;
 553 
 554     }
 555 
 556     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
 557         byte[] array = new byte[10];
 558 
 559         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 560             final int ci = i;
 561 
 562             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
 563                 checkIOOBE(am, () -> {
 564                     byte x = (byte) hs.get(am).invokeExact(array, ci);
 565                 });
 566             }
 567 
 568             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 569                 checkIOOBE(am, () -> {
 570                     hs.get(am).invokeExact(array, ci, (byte)0x01);
 571                 });
 572             }
 573 
 574             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 575                 checkIOOBE(am, () -> {
 576                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, (byte)0x01, (byte)0x23);
 577                 });
 578             }
 579 
 580             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 581                 checkIOOBE(am, () -> {
 582                     byte r = (byte) hs.get(am).invokeExact(array, ci, (byte)0x23, (byte)0x01);
 583                 });
 584             }
 585 
 586             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 587                 checkIOOBE(am, () -> {
 588                     byte o = (byte) hs.get(am).invokeExact(array, ci, (byte)0x01);
 589                 });
 590             }
 591 
 592             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 593                 checkIOOBE(am, () -> {
 594                     byte o = (byte) hs.get(am).invokeExact(array, ci, (byte)0x45);
 595                 });
 596             }
 597         }
 598     }
 599 }
 600 
< prev index next >