< prev index next >

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

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


  37 import java.nio.ByteBuffer;
  38 import java.nio.ByteOrder;
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.EnumSet;
  42 import java.util.List;
  43 
  44 import static org.testng.Assert.*;
  45 
  46 public class VarHandleTestByteArrayAsShort extends VarHandleBaseByteArrayTest {
  47     static final int SIZE = Short.BYTES;
  48 
  49     static final short VALUE_1 = (short)0x0102;
  50 
  51     static final short VALUE_2 = (short)0x1112;
  52 
  53     static final short VALUE_3 = (short)0xFFFE;
  54 
  55 
  56     @Override
  57     public void setupVarHandleSources() {
  58         // Combinations of VarHandle byte[] or ByteBuffer
  59         vhss = new ArrayList<>();
  60         for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) {
  61 
  62             ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
  63                     ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;








  64             VarHandleSource aeh = new VarHandleSource(
  65                     MethodHandles.byteArrayViewVarHandle(short[].class, bo),
  66                     endianess, MemoryMode.READ_WRITE);
  67             vhss.add(aeh);
  68 
  69             VarHandleSource bbh = new VarHandleSource(
  70                     MethodHandles.byteBufferViewVarHandle(short[].class, bo),
  71                     endianess, MemoryMode.READ_WRITE);
  72             vhss.add(bbh);
  73         }

  74     }
  75 



























  76 
  77     @Test(dataProvider = "varHandlesProvider")
  78     public void testIsAccessModeSupported(VarHandleSource vhs) {
  79         VarHandle vh = vhs.s;
  80 
  81         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  82         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  83 
  84         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  85         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
  86         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
  87         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
  88         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
  89         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
  90 
  91         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
  92         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
  93         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
  94         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
  95         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));




  37 import java.nio.ByteBuffer;
  38 import java.nio.ByteOrder;
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.EnumSet;
  42 import java.util.List;
  43 
  44 import static org.testng.Assert.*;
  45 
  46 public class VarHandleTestByteArrayAsShort extends VarHandleBaseByteArrayTest {
  47     static final int SIZE = Short.BYTES;
  48 
  49     static final short VALUE_1 = (short)0x0102;
  50 
  51     static final short VALUE_2 = (short)0x1112;
  52 
  53     static final short VALUE_3 = (short)0xFFFE;
  54 
  55 
  56     @Override
  57     public List<VarHandleSource> setupVarHandleSources(boolean same) {
  58         // Combinations of VarHandle byte[] or ByteBuffer
  59         List<VarHandleSource> vhss = new ArrayList<>();
  60         for (MemoryMode endianess : List.of(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) {
  61 
  62             ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
  63                     ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
  64 
  65             Class<?> arrayType;
  66             if (same) {
  67                 arrayType = short[].class;
  68             }
  69             else {
  70                 arrayType = int[].class;
  71             }
  72             VarHandleSource aeh = new VarHandleSource(
  73                     MethodHandles.byteArrayViewVarHandle(arrayType, bo),
  74                     endianess, MemoryMode.READ_WRITE);
  75             vhss.add(aeh);
  76 
  77             VarHandleSource bbh = new VarHandleSource(
  78                     MethodHandles.byteBufferViewVarHandle(arrayType, bo),
  79                     endianess, MemoryMode.READ_WRITE);
  80             vhss.add(bbh);
  81         }
  82         return vhss;
  83     }
  84 
  85     @Test
  86     public void testEqualsAndHashCode() {
  87         VarHandle[] vhs1 = setupVarHandleSources(true).stream().
  88             map(vhs -> vhs.s).toArray(VarHandle[]::new);
  89         VarHandle[] vhs2 = setupVarHandleSources(true).stream().
  90             map(vhs -> vhs.s).toArray(VarHandle[]::new);
  91 
  92         for (int i = 0; i < vhs1.length; i++) {
  93             for (int j = 0; j < vhs1.length; j++) {
  94                 if (i == j) {
  95                     assertEquals(vhs1[i], vhs1[i]);
  96                     assertEquals(vhs1[i], vhs2[i]);
  97                     assertEquals(vhs1[i].hashCode(), vhs2[i].hashCode());
  98                 }
  99                 else {
 100                     assertNotEquals(vhs1[i], vhs1[j]);
 101                     assertNotEquals(vhs1[i], vhs2[j]);
 102                 }
 103             }
 104         }
 105 
 106         VarHandle[] vhs3 = setupVarHandleSources(false).stream().
 107             map(vhs -> vhs.s).toArray(VarHandle[]::new);
 108         for (int i = 0; i < vhs1.length; i++) {
 109             assertNotEquals(vhs1[i], vhs3[i]);
 110         }
 111     }
 112 
 113     @Test(dataProvider = "varHandlesProvider")
 114     public void testIsAccessModeSupported(VarHandleSource vhs) {
 115         VarHandle vh = vhs.s;
 116 
 117         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 118         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 119 
 120         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 121         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 122         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 123         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 124         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 125         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 126 
 127         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 128         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 129         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 130         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 131         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));


< prev index next >