< prev index next >

test/jdk/java/foreign/TestLayoutPaths.java

Print this page
*** 26,28 ***
   * @test
   * @run testng TestLayoutPaths
   */
  
  import jdk.incubator.foreign.GroupLayout;
- import jdk.incubator.foreign.MemoryLayouts;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemoryLayout.PathElement;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import jdk.incubator.foreign.SequenceLayout;
  
  import org.testng.SkipException;
  import org.testng.annotations.*;
  
  import java.lang.invoke.MethodHandle;
- import java.nio.ByteOrder;
  import java.util.ArrayList;
  import java.util.List;
  
  import static jdk.incubator.foreign.MemoryLayout.PathElement.groupElement;
  import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
! import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
  import static org.testng.Assert.*;
  
  public class TestLayoutPaths {
  
      @Test(expectedExceptions = IllegalArgumentException.class)
--- 26,27 ---
   * @test
   * @run testng TestLayoutPaths
   */
  
  import jdk.incubator.foreign.GroupLayout;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemoryLayout.PathElement;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import jdk.incubator.foreign.SequenceLayout;
  
+ import jdk.incubator.foreign.ValueLayout;
  import org.testng.SkipException;
  import org.testng.annotations.*;
  
  import java.lang.invoke.MethodHandle;
  import java.util.ArrayList;
  import java.util.List;
  
  import static jdk.incubator.foreign.MemoryLayout.PathElement.groupElement;
  import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
! import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
  import static org.testng.Assert.*;
  
  public class TestLayoutPaths {
  
      @Test(expectedExceptions = IllegalArgumentException.class)

*** 151,11 ***
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testIncompleteAccess() {
          SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
!         seq.varHandle(int.class, sequenceElement());
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBitOffsetHandleBadRange() {
          SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
--- 150,11 ---
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testIncompleteAccess() {
          SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
!         seq.varHandle(sequenceElement());
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBitOffsetHandleBadRange() {
          SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));

*** 219,30 ***
              g.byteOffset(groupElement("foo"));
          } catch (Throwable ex) {
              throw new AssertionError(ex); // should be ok!
          }
          try {
!             g.varHandle(int.class, groupElement("foo")); //ok
              assertTrue(false); //should fail!
          } catch (UnsupportedOperationException ex) {
              //ok
          } catch (Throwable ex) {
              throw new AssertionError(ex); //should fail!
          }
      }
  
      @Test
      public void testBadAlignOffset() {
!         GroupLayout g = MemoryLayout.structLayout(MemoryLayouts.PAD_8, JAVA_INT.withBitAlignment(16).withName("foo"));
          try {
              g.bitOffset(groupElement("foo"));
              g.byteOffset(groupElement("foo"));
          } catch (Throwable ex) {
              throw new AssertionError(ex); // should be ok!
          }
          try {
!             g.varHandle(int.class, groupElement("foo")); //ok
              assertTrue(false); //should fail!
          } catch (UnsupportedOperationException ex) {
              //ok
          } catch (Throwable ex) {
              throw new AssertionError(ex); //should fail!
--- 218,30 ---
              g.byteOffset(groupElement("foo"));
          } catch (Throwable ex) {
              throw new AssertionError(ex); // should be ok!
          }
          try {
!             g.varHandle(groupElement("foo")); //ok
              assertTrue(false); //should fail!
          } catch (UnsupportedOperationException ex) {
              //ok
          } catch (Throwable ex) {
              throw new AssertionError(ex); //should fail!
          }
      }
  
      @Test
      public void testBadAlignOffset() {
!         GroupLayout g = MemoryLayout.structLayout(MemoryLayout.paddingLayout(8), JAVA_INT.withBitAlignment(16).withName("foo"));
          try {
              g.bitOffset(groupElement("foo"));
              g.byteOffset(groupElement("foo"));
          } catch (Throwable ex) {
              throw new AssertionError(ex); // should be ok!
          }
          try {
!             g.varHandle(groupElement("foo")); //ok
              assertTrue(false); //should fail!
          } catch (UnsupportedOperationException ex) {
              //ok
          } catch (Throwable ex) {
              throw new AssertionError(ex); //should fail!

*** 297,14 ***
  
      @Test
      public void testStructPaths() {
          long[] offsets = { 0, 8, 24, 56 };
          GroupLayout g = MemoryLayout.structLayout(
!                 MemoryLayouts.JAVA_BYTE.withName("1"),
!                 MemoryLayouts.JAVA_CHAR.withName("2"),
!                 MemoryLayouts.JAVA_FLOAT.withName("3"),
!                 MemoryLayouts.JAVA_LONG.withName("4")
          );
  
          // test select
  
          for (int i = 1 ; i <= 4 ; i++) {
--- 296,14 ---
  
      @Test
      public void testStructPaths() {
          long[] offsets = { 0, 8, 24, 56 };
          GroupLayout g = MemoryLayout.structLayout(
!                 ValueLayout.JAVA_BYTE.withName("1"),
!                 ValueLayout.JAVA_CHAR.withName("2"),
!                 ValueLayout.JAVA_FLOAT.withName("3"),
!                 ValueLayout.JAVA_LONG.withName("4")
          );
  
          // test select
  
          for (int i = 1 ; i <= 4 ; i++) {

*** 322,15 ***
          }
  
          // test map
  
          for (int i = 1 ; i <= 4 ; i++) {
!             GroupLayout g2 = (GroupLayout)g.map(l -> MemoryLayouts.JAVA_DOUBLE, groupElement(String.valueOf(i)));
              assertTrue(g2.isStruct());
              for (int j = 0 ; j < 4 ; j++) {
                  if (j == i - 1) {
!                     assertEquals(g2.memberLayouts().get(j), MemoryLayouts.JAVA_DOUBLE);
                  } else {
                      assertEquals(g2.memberLayouts().get(j), g.memberLayouts().get(j));
                  }
              }
          }
--- 321,15 ---
          }
  
          // test map
  
          for (int i = 1 ; i <= 4 ; i++) {
!             GroupLayout g2 = (GroupLayout)g.map(l -> ValueLayout.JAVA_DOUBLE, groupElement(String.valueOf(i)));
              assertTrue(g2.isStruct());
              for (int j = 0 ; j < 4 ; j++) {
                  if (j == i - 1) {
!                     assertEquals(g2.memberLayouts().get(j), ValueLayout.JAVA_DOUBLE);
                  } else {
                      assertEquals(g2.memberLayouts().get(j), g.memberLayouts().get(j));
                  }
              }
          }

*** 338,14 ***
  
      @Test
      public void testUnionPaths() {
          long[] offsets = { 0, 0, 0, 0 };
          GroupLayout g = MemoryLayout.unionLayout(
!                 MemoryLayouts.JAVA_BYTE.withName("1"),
!                 MemoryLayouts.JAVA_CHAR.withName("2"),
!                 MemoryLayouts.JAVA_FLOAT.withName("3"),
!                 MemoryLayouts.JAVA_LONG.withName("4")
          );
  
          // test select
  
          for (int i = 1 ; i <= 4 ; i++) {
--- 337,14 ---
  
      @Test
      public void testUnionPaths() {
          long[] offsets = { 0, 0, 0, 0 };
          GroupLayout g = MemoryLayout.unionLayout(
!                 ValueLayout.JAVA_BYTE.withName("1"),
!                 ValueLayout.JAVA_CHAR.withName("2"),
!                 ValueLayout.JAVA_FLOAT.withName("3"),
!                 ValueLayout.JAVA_LONG.withName("4")
          );
  
          // test select
  
          for (int i = 1 ; i <= 4 ; i++) {

*** 363,31 ***
          }
  
          // test map
  
          for (int i = 1 ; i <= 4 ; i++) {
!             GroupLayout g2 = (GroupLayout)g.map(l -> MemoryLayouts.JAVA_DOUBLE, groupElement(String.valueOf(i)));
              assertTrue(g2.isUnion());
              for (int j = 0 ; j < 4 ; j++) {
                  if (j == i - 1) {
!                     assertEquals(g2.memberLayouts().get(j), MemoryLayouts.JAVA_DOUBLE);
                  } else {
                      assertEquals(g2.memberLayouts().get(j), g.memberLayouts().get(j));
                  }
              }
          }
      }
  
      @Test
      public void testSequencePaths() {
          long[] offsets = { 0, 8, 16, 24 };
!         SequenceLayout g = MemoryLayout.sequenceLayout(4, MemoryLayouts.JAVA_BYTE);
  
          // test select
  
          MemoryLayout selected = g.select(sequenceElement());
!         assertTrue(selected == MemoryLayouts.JAVA_BYTE);
  
          // test offset
  
          for (int i = 0 ; i < 4 ; i++) {
              long bitOffset = g.bitOffset(sequenceElement(i));
--- 362,31 ---
          }
  
          // test map
  
          for (int i = 1 ; i <= 4 ; i++) {
!             GroupLayout g2 = (GroupLayout)g.map(l -> ValueLayout.JAVA_DOUBLE, groupElement(String.valueOf(i)));
              assertTrue(g2.isUnion());
              for (int j = 0 ; j < 4 ; j++) {
                  if (j == i - 1) {
!                     assertEquals(g2.memberLayouts().get(j), ValueLayout.JAVA_DOUBLE);
                  } else {
                      assertEquals(g2.memberLayouts().get(j), g.memberLayouts().get(j));
                  }
              }
          }
      }
  
      @Test
      public void testSequencePaths() {
          long[] offsets = { 0, 8, 16, 24 };
!         SequenceLayout g = MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_BYTE);
  
          // test select
  
          MemoryLayout selected = g.select(sequenceElement());
!         assertTrue(selected == ValueLayout.JAVA_BYTE);
  
          // test offset
  
          for (int i = 0 ; i < 4 ; i++) {
              long bitOffset = g.bitOffset(sequenceElement(i));

*** 396,12 ***
              assertEquals((offsets[i]) >>> 3, byteOffset);
          }
  
          // test map
  
!         SequenceLayout seq2 = (SequenceLayout)g.map(l -> MemoryLayouts.JAVA_DOUBLE, sequenceElement());
!         assertTrue(seq2.elementLayout() == MemoryLayouts.JAVA_DOUBLE);
      }
  
      @Test(dataProvider = "testLayouts")
      public void testOffsetHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
                                   long expectedBitOffset) throws Throwable {
--- 395,12 ---
              assertEquals((offsets[i]) >>> 3, byteOffset);
          }
  
          // test map
  
!         SequenceLayout seq2 = (SequenceLayout)g.map(l -> ValueLayout.JAVA_DOUBLE, sequenceElement());
!         assertTrue(seq2.elementLayout() == ValueLayout.JAVA_DOUBLE);
      }
  
      @Test(dataProvider = "testLayouts")
      public void testOffsetHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
                                   long expectedBitOffset) throws Throwable {

*** 504,41 ***
          sliceHandle = sliceHandle.asSpreader(long[].class, indexes.length);
  
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(layout, scope);
              MemorySegment slice = (MemorySegment) sliceHandle.invokeExact(segment, indexes);
!             assertEquals(slice.address().segmentOffset(segment), expectedBitOffset / 8);
              assertEquals(slice.byteSize(), selected.byteSize());
          }
      }
  
-     @Test(expectedExceptions = UnsupportedOperationException.class)
-     public void testSliceHandleUOEInvalidSize() {
-         MemoryLayout layout = MemoryLayout.structLayout(
-             MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()).withName("x"),
-             MemoryLayout.valueLayout(31, ByteOrder.nativeOrder()).withName("y") // size not a multiple of 8
-         );
- 
-         layout.sliceHandle(groupElement("y")); // should throw
-     }
- 
      @Test(expectedExceptions = UnsupportedOperationException.class)
      public void testSliceHandleUOEInvalidOffsetEager() throws Throwable {
          MemoryLayout layout = MemoryLayout.structLayout(
              MemoryLayout.paddingLayout(5),
!             MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()).withName("y") // offset not a multiple of 8
          );
  
          layout.sliceHandle(groupElement("y")); // should throw
      }
  
      @Test(expectedExceptions = UnsupportedOperationException.class)
      public void testSliceHandleUOEInvalidOffsetLate() throws Throwable {
          MemoryLayout layout = MemoryLayout.sequenceLayout(3,
              MemoryLayout.structLayout(
                  MemoryLayout.paddingLayout(4),
!                 MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()).withName("y") // offset not a multiple of 8
              )
          );
  
          MethodHandle sliceHandle;
          try {
--- 503,31 ---
          sliceHandle = sliceHandle.asSpreader(long[].class, indexes.length);
  
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(layout, scope);
              MemorySegment slice = (MemorySegment) sliceHandle.invokeExact(segment, indexes);
!             assertEquals(slice.address().toRawLongValue() - segment.address().toRawLongValue(), expectedBitOffset / 8);
              assertEquals(slice.byteSize(), selected.byteSize());
          }
      }
  
      @Test(expectedExceptions = UnsupportedOperationException.class)
      public void testSliceHandleUOEInvalidOffsetEager() throws Throwable {
          MemoryLayout layout = MemoryLayout.structLayout(
              MemoryLayout.paddingLayout(5),
!             JAVA_INT.withName("y") // offset not a multiple of 8
          );
  
          layout.sliceHandle(groupElement("y")); // should throw
      }
  
      @Test(expectedExceptions = UnsupportedOperationException.class)
      public void testSliceHandleUOEInvalidOffsetLate() throws Throwable {
          MemoryLayout layout = MemoryLayout.sequenceLayout(3,
              MemoryLayout.structLayout(
                  MemoryLayout.paddingLayout(4),
!                     JAVA_INT.withName("y") // offset not a multiple of 8
              )
          );
  
          MethodHandle sliceHandle;
          try {
< prev index next >