< prev index next >

test/jdk/java/foreign/TestAdaptVarHandles.java

Print this page
*** 30,11 ***
   * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
   */
  
  import jdk.incubator.foreign.MemoryHandles;
  import jdk.incubator.foreign.MemoryLayout;
- import jdk.incubator.foreign.MemoryLayouts;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import jdk.incubator.foreign.ValueLayout;
  import org.testng.annotations.*;
  import static org.testng.Assert.*;
--- 30,10 ---

*** 84,22 ***
          } catch (Throwable ex) {
              throw new ExceptionInInitializerError();
          }
      }
  
!     static final VarHandle intHandleIndexed = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)
!             .varHandle(int.class, MemoryLayout.PathElement.sequenceElement());
  
!     static final VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
  
!     static final VarHandle floatHandle = MemoryLayouts.JAVA_FLOAT.varHandle(float.class);
  
      @Test
      public void testFilterValue() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
!         VarHandle intHandle = layout.varHandle(int.class);
          VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, S2I, I2S);
          i2SHandle.set(segment, "1");
          String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
          assertEquals(oldValue, "1");
          String value = (String)i2SHandle.get(segment);
--- 83,22 ---
          } catch (Throwable ex) {
              throw new ExceptionInInitializerError();
          }
      }
  
!     static final VarHandle intHandleIndexed = MemoryLayout.sequenceLayout(ValueLayout.JAVA_INT)
!             .varHandle(MemoryLayout.PathElement.sequenceElement());
  
!     static final VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
  
!     static final VarHandle floatHandle = ValueLayout.JAVA_FLOAT.varHandle();
  
      @Test
      public void testFilterValue() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
!         VarHandle intHandle = layout.varHandle();
          VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, S2I, I2S);
          i2SHandle.set(segment, "1");
          String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
          assertEquals(oldValue, "1");
          String value = (String)i2SHandle.get(segment);

*** 112,13 ***
          assertEquals(value, "42");
      }
  
      @Test
      public void testFilterValueComposite() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
!         VarHandle intHandle = layout.varHandle(int.class);
          MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
          VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
          i2SHandle = MemoryHandles.insertCoordinates(i2SHandle, 1, "a", "b");
          i2SHandle.set(segment, "1");
          String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
--- 111,13 ---
          assertEquals(value, "42");
      }
  
      @Test
      public void testFilterValueComposite() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
!         VarHandle intHandle = layout.varHandle();
          MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
          VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
          i2SHandle = MemoryHandles.insertCoordinates(i2SHandle, 1, "a", "b");
          i2SHandle.set(segment, "1");
          String oldValue = (String)i2SHandle.getAndAdd(segment, "42");

*** 133,13 ***
          assertEquals(value, "ab42");
      }
  
      @Test
      public void testFilterValueLoose() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
!         VarHandle intHandle = layout.varHandle(int.class);
          VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, O2I, I2O);
          i2SHandle.set(segment, "1");
          String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
          assertEquals(oldValue, "1");
          String value = (String)i2SHandle.get(segment);
--- 132,13 ---
          assertEquals(value, "ab42");
      }
  
      @Test
      public void testFilterValueLoose() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
!         VarHandle intHandle = layout.varHandle();
          VarHandle i2SHandle = MemoryHandles.filterValue(intHandle, O2I, I2O);
          i2SHandle.set(segment, "1");
          String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
          assertEquals(oldValue, "1");
          String value = (String)i2SHandle.get(segment);

*** 157,55 ***
          MemoryHandles.filterValue(floatHandle, S2I, I2S);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterUnboxArity() {
!         VarHandle floatHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
          MemoryHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxArity() {
!         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
          MemoryHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxPrefixCoordinates() {
!         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
          MemoryHandles.filterValue(intHandle,
                  MethodHandles.dropArguments(S2I, 1, int.class),
                  MethodHandles.dropArguments(I2S, 1, long.class));
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxException() {
!         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
          MemoryHandles.filterValue(intHandle, I2S, S2L_EX);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterUnboxException() {
!         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
          MemoryHandles.filterValue(intHandle, S2L_EX, I2S);
      }
  
!     @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxHandleException() {
!         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
!         MemoryHandles.filterValue(intHandle, S2I, I2S_EX);
      }
  
!     @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterUnboxHandleException() {
!         VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);
!         MemoryHandles.filterValue(intHandle, S2I_EX, I2S);
      }
  
      @Test
      public void testFilterCoordinates() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_longIndex = MemoryHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
          intHandle_longIndex.set(segment, "0", 1);
          int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
          assertEquals(oldValue, 1);
--- 156,64 ---
          MemoryHandles.filterValue(floatHandle, S2I, I2S);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterUnboxArity() {
!         VarHandle floatHandle = ValueLayout.JAVA_INT.varHandle();
          MemoryHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxArity() {
!         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
          MemoryHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxPrefixCoordinates() {
!         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
          MemoryHandles.filterValue(intHandle,
                  MethodHandles.dropArguments(S2I, 1, int.class),
                  MethodHandles.dropArguments(I2S, 1, long.class));
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterBoxException() {
!         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
          MemoryHandles.filterValue(intHandle, I2S, S2L_EX);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadFilterUnboxException() {
!         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
          MemoryHandles.filterValue(intHandle, S2L_EX, I2S);
      }
  
!     @Test(expectedExceptions = IllegalStateException.class)
      public void testBadFilterBoxHandleException() {
!         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
!         VarHandle vh = MemoryHandles.filterValue(intHandle, S2I, I2S_EX);
+         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+             MemorySegment seg = MemorySegment.allocateNative(ValueLayout.JAVA_INT, scope);
+             vh.set(seg, "42");
+             String x = (String) vh.get(seg); // should throw
+         }
      }
  
!     @Test(expectedExceptions = IllegalStateException.class)
      public void testBadFilterUnboxHandleException() {
!         VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
!         VarHandle vh = MemoryHandles.filterValue(intHandle, S2I_EX, I2S);
+         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+             MemorySegment seg = MemorySegment.allocateNative(ValueLayout.JAVA_INT, scope);
+             vh.set(seg, "42"); // should throw
+         }
      }
  
      @Test
      public void testFilterCoordinates() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_longIndex = MemoryHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
          intHandle_longIndex.set(segment, "0", 1);
          int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
          assertEquals(oldValue, 1);

*** 244,11 ***
          MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L, S2L);
      }
  
      @Test
      public void testInsertCoordinates() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_longIndex = MemoryHandles.insertCoordinates(intHandleIndexed, 0, segment, 0L);
          intHandle_longIndex.set(1);
          int oldValue = (int)intHandle_longIndex.getAndAdd(42);
          assertEquals(oldValue, 1);
--- 252,11 ---
          MemoryHandles.filterCoordinates(intHandleIndexed, 1, S2L, S2L);
      }
  
      @Test
      public void testInsertCoordinates() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_longIndex = MemoryHandles.insertCoordinates(intHandleIndexed, 0, segment, 0L);
          intHandle_longIndex.set(1);
          int oldValue = (int)intHandle_longIndex.getAndAdd(42);
          assertEquals(oldValue, 1);

*** 282,11 ***
          MemoryHandles.insertCoordinates(intHandleIndexed, 1, 0L, 0L);
      }
  
      @Test
      public void testPermuteCoordinates() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_swap = MemoryHandles.permuteCoordinates(intHandleIndexed,
                  List.of(long.class, MemorySegment.class), 1, 0);
          intHandle_swap.set(0L, segment, 1);
          int oldValue = (int)intHandle_swap.getAndAdd(0L, segment, 42);
--- 290,11 ---
          MemoryHandles.insertCoordinates(intHandleIndexed, 1, 0L, 0L);
      }
  
      @Test
      public void testPermuteCoordinates() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_swap = MemoryHandles.permuteCoordinates(intHandleIndexed,
                  List.of(long.class, MemorySegment.class), 1, 0);
          intHandle_swap.set(0L, segment, 1);
          int oldValue = (int)intHandle_swap.getAndAdd(0L, segment, 42);

*** 321,11 ***
          MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), -1);
      }
  
      @Test
      public void testCollectCoordinates() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_sum = MemoryHandles.collectCoordinates(intHandleIndexed, 1, SUM_OFFSETS);
          intHandle_sum.set(segment, -2L, 2L, 1);
          int oldValue = (int)intHandle_sum.getAndAdd(segment, -2L, 2L, 42);
          assertEquals(oldValue, 1);
--- 329,11 ---
          MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), -1);
      }
  
      @Test
      public void testCollectCoordinates() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_sum = MemoryHandles.collectCoordinates(intHandleIndexed, 1, SUM_OFFSETS);
          intHandle_sum.set(segment, -2L, 2L, 1);
          int oldValue = (int)intHandle_sum.getAndAdd(segment, -2L, 2L, 42);
          assertEquals(oldValue, 1);

*** 364,11 ***
          MemoryHandles.collectCoordinates(intHandle, 0, S2L_EX);
      }
  
      @Test
      public void testDropCoordinates() throws Throwable {
!         ValueLayout layout = MemoryLayouts.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_dummy = MemoryHandles.dropCoordinates(intHandleIndexed, 1, float.class, String.class);
          intHandle_dummy.set(segment, 1f, "hello", 0L, 1);
          int oldValue = (int)intHandle_dummy.getAndAdd(segment, 1f, "hello", 0L, 42);
          assertEquals(oldValue, 1);
--- 372,11 ---
          MemoryHandles.collectCoordinates(intHandle, 0, S2L_EX);
      }
  
      @Test
      public void testDropCoordinates() throws Throwable {
!         ValueLayout layout = ValueLayout.JAVA_INT;
          MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
          VarHandle intHandle_dummy = MemoryHandles.dropCoordinates(intHandleIndexed, 1, float.class, String.class);
          intHandle_dummy.set(segment, 1f, "hello", 0L, 1);
          int oldValue = (int)intHandle_dummy.getAndAdd(segment, 1f, "hello", 0L, 42);
          assertEquals(oldValue, 1);
< prev index next >