< prev index next >

test/jdk/java/lang/invoke/VarHandles/VarHandleTestAccessLong.java

Print this page
rev 52786 : 8210031: implementation for JVM Constants API


  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class VarHandleTestAccessLong extends VarHandleBaseTest {
  45     static final long static_final_v = 0x0123456789ABCDEFL;
  46 
  47     static long static_v;
  48 
  49     final long final_v = 0x0123456789ABCDEFL;
  50 
  51     long v;
  52 








  53     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63 



































  64     @BeforeClass
  65     public void setup() throws Exception {
  66         vhFinalField = MethodHandles.lookup().findVarHandle(
  67                 VarHandleTestAccessLong.class, "final_v", long.class);
  68 
  69         vhField = MethodHandles.lookup().findVarHandle(
  70                 VarHandleTestAccessLong.class, "v", long.class);
  71 
  72         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  73             VarHandleTestAccessLong.class, "static_final_v", long.class);
  74 
  75         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  76             VarHandleTestAccessLong.class, "static_v", long.class);
  77 
  78         vhArray = MethodHandles.arrayElementVarHandle(long[].class);
  79     }
  80 
  81 
  82     @DataProvider
  83     public Object[][] varHandlesProvider() throws Exception {
  84         List<VarHandle> vhs = new ArrayList<>();
  85         vhs.add(vhField);
  86         vhs.add(vhStaticField);
  87         vhs.add(vhArray);
  88 
  89         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);

























  90     }
  91 
  92     @Test(dataProvider = "varHandlesProvider")
  93     public void testIsAccessModeSupported(VarHandle vh) {
  94         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  95         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  96         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  97         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
  98         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
  99         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 101         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 102 
 103         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 104         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 105         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 106         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 107         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 108         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 109         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));




  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class VarHandleTestAccessLong extends VarHandleBaseTest {
  45     static final long static_final_v = 0x0123456789ABCDEFL;
  46 
  47     static long static_v;
  48 
  49     final long final_v = 0x0123456789ABCDEFL;
  50 
  51     long v;
  52 
  53     static final long static_final_v2 = 0x0123456789ABCDEFL;
  54 
  55     static long static_v2;
  56 
  57     final long final_v2 = 0x0123456789ABCDEFL;
  58 
  59     long v2;
  60 
  61     VarHandle vhFinalField;
  62 
  63     VarHandle vhField;
  64 
  65     VarHandle vhStaticField;
  66 
  67     VarHandle vhStaticFinalField;
  68 
  69     VarHandle vhArray;
  70 
  71 
  72     VarHandle[] allocate(boolean same) {
  73         List<VarHandle> vhs = new ArrayList<>();
  74 
  75         String postfix = same ? "" : "2";
  76         VarHandle vh;
  77         try {
  78             vh = MethodHandles.lookup().findVarHandle(
  79                     VarHandleTestAccessLong.class, "final_v" + postfix, long.class);
  80             vhs.add(vh);
  81 
  82             vh = MethodHandles.lookup().findVarHandle(
  83                     VarHandleTestAccessLong.class, "v" + postfix, long.class);
  84             vhs.add(vh);
  85 
  86             vh = MethodHandles.lookup().findStaticVarHandle(
  87                 VarHandleTestAccessLong.class, "static_final_v" + postfix, long.class);
  88             vhs.add(vh);
  89 
  90             vh = MethodHandles.lookup().findStaticVarHandle(
  91                 VarHandleTestAccessLong.class, "static_v" + postfix, long.class);
  92             vhs.add(vh);
  93 
  94             if (same) {
  95                 vh = MethodHandles.arrayElementVarHandle(long[].class);
  96             }
  97             else {
  98                 vh = MethodHandles.arrayElementVarHandle(String[].class);
  99             }
 100             vhs.add(vh);
 101         } catch (Exception e) {
 102             throw new InternalError(e);
 103         }
 104         return vhs.toArray(new VarHandle[0]);
 105     }
 106 
 107     @BeforeClass
 108     public void setup() throws Exception {
 109         vhFinalField = MethodHandles.lookup().findVarHandle(
 110                 VarHandleTestAccessLong.class, "final_v", long.class);
 111 
 112         vhField = MethodHandles.lookup().findVarHandle(
 113                 VarHandleTestAccessLong.class, "v", long.class);
 114 
 115         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 116             VarHandleTestAccessLong.class, "static_final_v", long.class);
 117 
 118         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 119             VarHandleTestAccessLong.class, "static_v", long.class);
 120 
 121         vhArray = MethodHandles.arrayElementVarHandle(long[].class);
 122     }
 123 
 124 
 125     @DataProvider
 126     public Object[][] varHandlesProvider() throws Exception {
 127         List<VarHandle> vhs = new ArrayList<>();
 128         vhs.add(vhField);
 129         vhs.add(vhStaticField);
 130         vhs.add(vhArray);
 131 
 132         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 133     }
 134 
 135     @Test
 136     public void testEqualsAndHashCode() {
 137         VarHandle[] vhs1 = allocate(true);
 138         VarHandle[] vhs2 = allocate(true);
 139 
 140         for (int i = 0; i < vhs1.length; i++) {
 141             for (int j = 0; j < vhs1.length; j++) {
 142                 if (i == j) {
 143                     assertEquals(vhs1[i], vhs1[i]);
 144                     assertEquals(vhs1[i], vhs2[i]);
 145                     assertEquals(vhs1[i].hashCode(), vhs2[i].hashCode());
 146                 }
 147                 else {
 148                     assertNotEquals(vhs1[i], vhs1[j]);
 149                     assertNotEquals(vhs1[i], vhs2[j]);
 150                 }
 151             }
 152         }
 153 
 154         VarHandle[] vhs3 = allocate(false);
 155         for (int i = 0; i < vhs1.length; i++) {
 156             assertNotEquals(vhs1[i], vhs3[i]);
 157         }
 158     }
 159 
 160     @Test(dataProvider = "varHandlesProvider")
 161     public void testIsAccessModeSupported(VarHandle vh) {
 162         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 163         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 164         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 165         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 166         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 167         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 168         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 169         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 170 
 171         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 173         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 175         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 176         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));


< prev index next >