< prev index next >

test/jdk/java/foreign/TestLayouts.java

Print this page
@@ -32,33 +32,33 @@
  import java.nio.ByteOrder;
  import java.util.function.LongFunction;
  import java.util.stream.Stream;
  
  import org.testng.annotations.*;
+ 
+ import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
+ import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
+ import static jdk.incubator.foreign.ValueLayout.JAVA_LONG;
+ import static jdk.incubator.foreign.ValueLayout.JAVA_SHORT;
  import static org.testng.Assert.*;
  
  public class TestLayouts {
  
-     @Test(dataProvider = "badLayoutSizes", expectedExceptions = IllegalArgumentException.class)
-     public void testBadLayoutSize(SizedLayoutFactory factory, long size) {
-         factory.make(size);
-     }
- 
      @Test(dataProvider = "badAlignments", expectedExceptions = IllegalArgumentException.class)
      public void testBadLayoutAlignment(MemoryLayout layout, long alignment) {
          layout.withBitAlignment(alignment);
      }
  
      @Test
      public void testVLAInStruct() {
          MemoryLayout layout = MemoryLayout.structLayout(
-                 MemoryLayouts.JAVA_INT.withName("size"),
+                 ValueLayout.JAVA_INT.withName("size"),
                  MemoryLayout.paddingLayout(32),
-                 MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_DOUBLE).withName("arr"));
+                 MemoryLayout.sequenceLayout(ValueLayout.JAVA_DOUBLE).withName("arr"));
          assertFalse(layout.hasSize());
-         VarHandle size_handle = layout.varHandle(int.class, MemoryLayout.PathElement.groupElement("size"));
-         VarHandle array_elem_handle = layout.varHandle(double.class,
+         VarHandle size_handle = layout.varHandle(MemoryLayout.PathElement.groupElement("size"));
+         VarHandle array_elem_handle = layout.varHandle(
                  MemoryLayout.PathElement.groupElement("arr"),
                  MemoryLayout.PathElement.sequenceElement());
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(
                      layout.map(l -> ((SequenceLayout)l).withElementCount(4), MemoryLayout.PathElement.groupElement("arr")), scope);

@@ -75,16 +75,16 @@
      }
  
      @Test
      public void testVLAInSequence() {
          MemoryLayout layout = MemoryLayout.structLayout(
-                 MemoryLayouts.JAVA_INT.withName("size"),
+                 ValueLayout.JAVA_INT.withName("size"),
                  MemoryLayout.paddingLayout(32),
-                 MemoryLayout.sequenceLayout(1, MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_DOUBLE)).withName("arr"));
+                 MemoryLayout.sequenceLayout(1, MemoryLayout.sequenceLayout(ValueLayout.JAVA_DOUBLE)).withName("arr"));
          assertFalse(layout.hasSize());
-         VarHandle size_handle = layout.varHandle(int.class, MemoryLayout.PathElement.groupElement("size"));
-         VarHandle array_elem_handle = layout.varHandle(double.class,
+         VarHandle size_handle = layout.varHandle(MemoryLayout.PathElement.groupElement("size"));
+         VarHandle array_elem_handle = layout.varHandle(
                  MemoryLayout.PathElement.groupElement("arr"),
                  MemoryLayout.PathElement.sequenceElement(0),
                  MemoryLayout.PathElement.sequenceElement());
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(

@@ -101,21 +101,21 @@
          }
      }
  
      @Test
      public void testIndexedSequencePath() {
-         MemoryLayout seq = MemoryLayout.sequenceLayout(10, MemoryLayouts.JAVA_INT);
+         MemoryLayout seq = MemoryLayout.sequenceLayout(10, ValueLayout.JAVA_INT);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(seq, scope);
-             VarHandle indexHandle = seq.varHandle(int.class, MemoryLayout.PathElement.sequenceElement());
+             VarHandle indexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement());
              // init segment
              for (int i = 0 ; i < 10 ; i++) {
                  indexHandle.set(segment, (long)i, i);
              }
              //check statically indexed handles
              for (int i = 0 ; i < 10 ; i++) {
-                 VarHandle preindexHandle = seq.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(i));
+                 VarHandle preindexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement(i));
                  int expected = (int)indexHandle.get(segment, (long)i);
                  int found = (int)preindexHandle.get(segment);
                  assertEquals(expected, found);
              }
          }

@@ -141,17 +141,17 @@
          layout.hashCode();
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadUnboundSequenceLayoutResize() {
-         SequenceLayout seq = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT);
+         SequenceLayout seq = MemoryLayout.sequenceLayout(ValueLayout.JAVA_INT);
          seq.withElementCount(-1);
      }
  
      @Test(expectedExceptions = IllegalArgumentException.class)
      public void testBadBoundSequenceLayoutResize() {
-         SequenceLayout seq = MemoryLayout.sequenceLayout(10, MemoryLayouts.JAVA_INT);
+         SequenceLayout seq = MemoryLayout.sequenceLayout(10, ValueLayout.JAVA_INT);
          seq.withElementCount(-1);
      }
  
      @Test
      public void testEmptyGroup() {

@@ -166,17 +166,17 @@
  
      @Test
      public void testStructSizeAndAlign() {
          MemoryLayout struct = MemoryLayout.structLayout(
                  MemoryLayout.paddingLayout(8),
-                 MemoryLayouts.JAVA_BYTE,
-                 MemoryLayouts.JAVA_CHAR,
-                 MemoryLayouts.JAVA_INT,
-                 MemoryLayouts.JAVA_LONG
+                 ValueLayout.JAVA_BYTE,
+                 ValueLayout.JAVA_CHAR,
+                 ValueLayout.JAVA_INT,
+                 ValueLayout.JAVA_LONG
          );
          assertEquals(struct.byteSize(), 1 + 1 + 2 + 4 + 8);
-         assertEquals(struct.byteAlignment(), MemoryLayouts.ADDRESS.byteAlignment());
+         assertEquals(struct.byteAlignment(), ValueLayout.ADDRESS.byteAlignment());
      }
  
      @Test(dataProvider="basicLayouts")
      public void testPaddingNoAlign(MemoryLayout layout) {
          assertEquals(MemoryLayout.paddingLayout(layout.bitSize()).bitAlignment(), 1);

@@ -197,17 +197,17 @@
      }
  
      @Test
      public void testUnionSizeAndAlign() {
          MemoryLayout struct = MemoryLayout.unionLayout(
-                 MemoryLayouts.JAVA_BYTE,
-                 MemoryLayouts.JAVA_CHAR,
-                 MemoryLayouts.JAVA_INT,
-                 MemoryLayouts.JAVA_LONG
+                 ValueLayout.JAVA_BYTE,
+                 ValueLayout.JAVA_CHAR,
+                 ValueLayout.JAVA_INT,
+                 ValueLayout.JAVA_LONG
          );
          assertEquals(struct.byteSize(), 8);
-         assertEquals(struct.byteAlignment(), MemoryLayouts.ADDRESS.byteAlignment());
+         assertEquals(struct.byteAlignment(), ValueLayout.ADDRESS.byteAlignment());
      }
  
      @Test(dataProvider = "layoutKinds")
      public void testPadding(LayoutKind kind) {
          assertEquals(kind == LayoutKind.PADDING, kind.layout.isPadding());

@@ -222,35 +222,23 @@
                  assertEquals(layout.withBitAlignment(a).toString().contains("%"), a != bitAlign);
              }
          }
      }
  
-     @DataProvider(name = "badLayoutSizes")
-     public Object[][] factoriesAndSizes() {
-         return new Object[][] {
-                 { SizedLayoutFactory.VALUE_BE, 0 },
-                 { SizedLayoutFactory.VALUE_BE, -1 },
-                 { SizedLayoutFactory.VALUE_LE, 0 },
-                 { SizedLayoutFactory.VALUE_LE, -1 },
-                 { SizedLayoutFactory.PADDING, 0 },
-                 { SizedLayoutFactory.PADDING, -1 },
-                 { SizedLayoutFactory.SEQUENCE, -1 }
-         };
-     }
- 
      @DataProvider(name = "unboundLayouts")
      public Object[][] unboundLayouts() {
+         ValueLayout alignedInt = JAVA_INT.withBitAlignment(32);
          return new Object[][] {
-                 { MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT), 32 },
-                 { MemoryLayout.sequenceLayout(MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)), 32 },
-                 { MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)), 32 },
-                 { MemoryLayout.structLayout(MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)), 32 },
-                 { MemoryLayout.structLayout(MemoryLayout.sequenceLayout(MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT))), 32 },
-                 { MemoryLayout.structLayout(MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT))), 32 },
-                 { MemoryLayout.unionLayout(MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT)), 32 },
-                 { MemoryLayout.unionLayout(MemoryLayout.sequenceLayout(MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT))), 32 },
-                 { MemoryLayout.unionLayout(MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT))), 32 },
+                 { MemoryLayout.sequenceLayout(alignedInt), 32 },
+                 { MemoryLayout.sequenceLayout(MemoryLayout.sequenceLayout(alignedInt)), 32 },
+                 { MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(alignedInt)), 32 },
+                 { MemoryLayout.structLayout(MemoryLayout.sequenceLayout(alignedInt)), 32 },
+                 { MemoryLayout.structLayout(MemoryLayout.sequenceLayout(MemoryLayout.sequenceLayout(alignedInt))), 32 },
+                 { MemoryLayout.structLayout(MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(alignedInt))), 32 },
+                 { MemoryLayout.unionLayout(MemoryLayout.sequenceLayout(alignedInt)), 32 },
+                 { MemoryLayout.unionLayout(MemoryLayout.sequenceLayout(MemoryLayout.sequenceLayout(alignedInt))), 32 },
+                 { MemoryLayout.unionLayout(MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(alignedInt))), 32 },
          };
      }
  
      @DataProvider(name = "badAlignments")
      public Object[][] layoutsAndBadAlignments() {

@@ -269,14 +257,14 @@
                  .map(lk -> new Object[] { lk })
                  .toArray(Object[][]::new);
      }
  
      enum SizedLayoutFactory {
-         VALUE_LE(size -> MemoryLayout.valueLayout(size, ByteOrder.LITTLE_ENDIAN)),
-         VALUE_BE(size -> MemoryLayout.valueLayout(size, ByteOrder.BIG_ENDIAN)),
+         VALUE_LE(size -> valueLayoutForSize((int)size).withOrder(ByteOrder.LITTLE_ENDIAN)),
+         VALUE_BE(size -> valueLayoutForSize((int)size).withOrder(ByteOrder.BIG_ENDIAN)),
          PADDING(MemoryLayout::paddingLayout),
-         SEQUENCE(size -> MemoryLayout.sequenceLayout(size, MemoryLayouts.PAD_8));
+         SEQUENCE(size -> MemoryLayout.sequenceLayout(size, MemoryLayout.paddingLayout(8)));
  
          private final LongFunction<MemoryLayout> factory;
  
          SizedLayoutFactory(LongFunction<MemoryLayout> factory) {
              this.factory = factory;

@@ -285,17 +273,26 @@
          MemoryLayout make(long size) {
              return factory.apply(size);
          }
      }
  
+     static ValueLayout valueLayoutForSize(int size) {
+         return switch (size) {
+             case 1 -> JAVA_BYTE;
+             case 2 -> JAVA_SHORT;
+             case 4 -> JAVA_INT;
+             case 8 -> JAVA_LONG;
+             default -> throw new UnsupportedOperationException();
+         };
+     }
+ 
      enum LayoutKind {
-         VALUE_LE(MemoryLayouts.BITS_8_LE),
-         VALUE_BE(MemoryLayouts.BITS_8_BE),
-         PADDING(MemoryLayouts.PAD_8),
-         SEQUENCE(MemoryLayout.sequenceLayout(1, MemoryLayouts.PAD_8)),
-         STRUCT(MemoryLayout.structLayout(MemoryLayouts.PAD_8, MemoryLayouts.PAD_8)),
-         UNION(MemoryLayout.unionLayout(MemoryLayouts.PAD_8, MemoryLayouts.PAD_8));
+         VALUE(ValueLayout.JAVA_BYTE),
+         PADDING(MemoryLayout.paddingLayout(8)),
+         SEQUENCE(MemoryLayout.sequenceLayout(1, MemoryLayout.paddingLayout(8))),
+         STRUCT(MemoryLayout.structLayout(MemoryLayout.paddingLayout(8), MemoryLayout.paddingLayout(8))),
+         UNION(MemoryLayout.unionLayout(MemoryLayout.paddingLayout(8), MemoryLayout.paddingLayout(8)));
  
          final MemoryLayout layout;
  
          LayoutKind(MemoryLayout layout) {
              this.layout = layout;

@@ -331,14 +328,14 @@
          }
          return layoutsAndAlignments;
      }
  
      static MemoryLayout[] basicLayouts = {
-             MemoryLayouts.JAVA_BYTE,
-             MemoryLayouts.JAVA_CHAR,
-             MemoryLayouts.JAVA_SHORT,
-             MemoryLayouts.JAVA_INT,
-             MemoryLayouts.JAVA_FLOAT,
-             MemoryLayouts.JAVA_LONG,
-             MemoryLayouts.JAVA_DOUBLE,
+             ValueLayout.JAVA_BYTE,
+             ValueLayout.JAVA_CHAR,
+             ValueLayout.JAVA_SHORT,
+             ValueLayout.JAVA_INT,
+             ValueLayout.JAVA_FLOAT,
+             ValueLayout.JAVA_LONG,
+             ValueLayout.JAVA_DOUBLE,
      };
  }
< prev index next >