< prev index next >

test/jdk/java/foreign/TestSegments.java

Print this page
*** 25,41 ***
   * @test
   * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
   * @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M TestSegments
   */
  
- import jdk.incubator.foreign.MemoryAccess;
  import jdk.incubator.foreign.MemoryLayout;
- import jdk.incubator.foreign.MemoryLayouts;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;
  
  import java.lang.invoke.VarHandle;
  import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
  import java.util.List;
  import java.util.concurrent.atomic.AtomicReference;
  import java.util.function.IntFunction;
- import java.util.function.LongFunction;
  import java.util.function.Supplier;
  
  import static org.testng.Assert.*;
  
  public class TestSegments {
  
      @Test(dataProvider = "badSizeAndAlignments", expectedExceptions = IllegalArgumentException.class)
      public void testBadAllocateAlign(long size, long align) {
          MemorySegment.allocateNative(size, align, ResourceScope.newImplicitScope());
      }
  
-     @Test(dataProvider = "badLayouts", expectedExceptions = UnsupportedOperationException.class)
-     public void testBadAllocateLayout(MemoryLayout layout) {
-         MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
-     }
- 
      @Test(expectedExceptions = { OutOfMemoryError.class,
                                   IllegalArgumentException.class })
      public void testAllocateTooBig() {
          MemorySegment.allocateNative(Long.MAX_VALUE, ResourceScope.newImplicitScope());
      }
--- 25,34 ---
   * @test
   * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
   * @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M TestSegments
   */
  
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
+ import jdk.incubator.foreign.ValueLayout;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;
  
  import java.lang.invoke.VarHandle;
  import java.nio.ByteBuffer;
  import java.util.List;
  import java.util.concurrent.atomic.AtomicReference;
  import java.util.function.IntFunction;
  import java.util.function.Supplier;
  
+ import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
  import static org.testng.Assert.*;
  
  public class TestSegments {
  
      @Test(dataProvider = "badSizeAndAlignments", expectedExceptions = IllegalArgumentException.class)
      public void testBadAllocateAlign(long size, long align) {
          MemorySegment.allocateNative(size, align, ResourceScope.newImplicitScope());
      }
  
      @Test(expectedExceptions = { OutOfMemoryError.class,
                                   IllegalArgumentException.class })
      public void testAllocateTooBig() {
          MemorySegment.allocateNative(Long.MAX_VALUE, ResourceScope.newImplicitScope());
      }

*** 69,24 ***
          MemorySegment segment = MemorySegment.allocateNative(1024 * 1024 * 8 * 2, ResourceScope.newImplicitScope()); // 2M
      }
  
      @Test
      public void testNativeSegmentIsZeroed() {
!         VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
!                 .varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(1000, 1, scope);
              for (long i = 0 ; i < segment.byteSize() ; i++) {
                  assertEquals(0, (byte)byteHandle.get(segment, i));
              }
          }
      }
  
      @Test
      public void testSlices() {
!         VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
!                 .varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(10, 1, scope);
              //init
              for (byte i = 0 ; i < segment.byteSize() ; i++) {
                  byteHandle.set(segment, (long)i, i);
--- 62,24 ---
          MemorySegment segment = MemorySegment.allocateNative(1024 * 1024 * 8 * 2, ResourceScope.newImplicitScope()); // 2M
      }
  
      @Test
      public void testNativeSegmentIsZeroed() {
!         VarHandle byteHandle = MemoryLayout.sequenceLayout(ValueLayout.JAVA_BYTE)
!                 .varHandle(MemoryLayout.PathElement.sequenceElement());
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(1000, 1, scope);
              for (long i = 0 ; i < segment.byteSize() ; i++) {
                  assertEquals(0, (byte)byteHandle.get(segment, i));
              }
          }
      }
  
      @Test
      public void testSlices() {
!         VarHandle byteHandle = MemoryLayout.sequenceLayout(ValueLayout.JAVA_BYTE)
!                 .varHandle(MemoryLayout.PathElement.sequenceElement());
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
              MemorySegment segment = MemorySegment.allocateNative(10, 1, scope);
              //init
              for (byte i = 0 ; i < segment.byteSize() ; i++) {
                  byteHandle.set(segment, (long)i, i);

*** 105,29 ***
  
      @Test(expectedExceptions = IndexOutOfBoundsException.class)
      public void testSmallSegmentMax() {
          long offset = (long)Integer.MAX_VALUE + (long)Integer.MAX_VALUE + 2L + 6L; // overflows to 6 when casted to int
          MemorySegment memorySegment = MemorySegment.allocateNative(10, ResourceScope.newImplicitScope());
!         MemoryAccess.getIntAtOffset(memorySegment, offset);
      }
  
      @Test(expectedExceptions = IndexOutOfBoundsException.class)
      public void testSmallSegmentMin() {
          long offset = ((long)Integer.MIN_VALUE * 2L) + 6L; // underflows to 6 when casted to int
          MemorySegment memorySegment = MemorySegment.allocateNative(10, ResourceScope.newImplicitScope());
!         MemoryAccess.getIntAtOffset(memorySegment, offset);
      }
  
      @Test(dataProvider = "segmentFactories")
      public void testAccessModesOfFactories(Supplier<MemorySegment> memorySegmentSupplier) {
          MemorySegment segment = memorySegmentSupplier.get();
          assertFalse(segment.isReadOnly());
          tryClose(segment);
      }
  
      static void tryClose(MemorySegment segment) {
!         if (!segment.scope().isImplicit()) {
              segment.scope().close();
          }
      }
  
      @DataProvider(name = "segmentFactories")
--- 98,29 ---
  
      @Test(expectedExceptions = IndexOutOfBoundsException.class)
      public void testSmallSegmentMax() {
          long offset = (long)Integer.MAX_VALUE + (long)Integer.MAX_VALUE + 2L + 6L; // overflows to 6 when casted to int
          MemorySegment memorySegment = MemorySegment.allocateNative(10, ResourceScope.newImplicitScope());
!         memorySegment.get(JAVA_INT, offset);
      }
  
      @Test(expectedExceptions = IndexOutOfBoundsException.class)
      public void testSmallSegmentMin() {
          long offset = ((long)Integer.MIN_VALUE * 2L) + 6L; // underflows to 6 when casted to int
          MemorySegment memorySegment = MemorySegment.allocateNative(10, ResourceScope.newImplicitScope());
!         memorySegment.get(JAVA_INT, offset);
      }
  
      @Test(dataProvider = "segmentFactories")
      public void testAccessModesOfFactories(Supplier<MemorySegment> memorySegmentSupplier) {
          MemorySegment segment = memorySegmentSupplier.get();
          assertFalse(segment.isReadOnly());
          tryClose(segment);
      }
  
      static void tryClose(MemorySegment segment) {
!         if (segment.scope() != ResourceScope.globalScope()) {
              segment.scope().close();
          }
      }
  
      @DataProvider(name = "segmentFactories")

*** 140,23 ***
                  () -> MemorySegment.ofArray(new int[] { 1, 2, 3, 4 }),
                  () -> MemorySegment.ofArray(new long[] { 1l, 2l, 3l, 4l } ),
                  () -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } ),
                  () -> MemorySegment.allocateNative(4, ResourceScope.newImplicitScope()),
                  () -> MemorySegment.allocateNative(4, 8, ResourceScope.newImplicitScope()),
!                 () -> MemorySegment.allocateNative(MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()), ResourceScope.newImplicitScope()),
!                 () -> MemorySegment.allocateNative(4, ResourceScope.newConfinedScope()),
!                 () -> MemorySegment.allocateNative(4, 8, ResourceScope.newConfinedScope()),
!                 () -> MemorySegment.allocateNative(MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()), ResourceScope.newConfinedScope())
  
          );
          return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
      }
  
      @Test(dataProvider = "segmentFactories")
      public void testFill(Supplier<MemorySegment> memorySegmentSupplier) {
!         VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
!                 .varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
  
          for (byte value : new byte[] {(byte) 0xFF, (byte) 0x00, (byte) 0x45}) {
              MemorySegment segment = memorySegmentSupplier.get();
              segment.fill(value);
              for (long l = 0; l < segment.byteSize(); l++) {
--- 133,23 ---
                  () -> MemorySegment.ofArray(new int[] { 1, 2, 3, 4 }),
                  () -> MemorySegment.ofArray(new long[] { 1l, 2l, 3l, 4l } ),
                  () -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } ),
                  () -> MemorySegment.allocateNative(4, ResourceScope.newImplicitScope()),
                  () -> MemorySegment.allocateNative(4, 8, ResourceScope.newImplicitScope()),
!                 () -> MemorySegment.allocateNative(JAVA_INT, ResourceScope.newImplicitScope()),
!                 () -> MemorySegment.allocateNative(4, ResourceScope.newImplicitScope()),
!                 () -> MemorySegment.allocateNative(4, 8, ResourceScope.newImplicitScope()),
!                 () -> MemorySegment.allocateNative(JAVA_INT, ResourceScope.newImplicitScope())
  
          );
          return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
      }
  
      @Test(dataProvider = "segmentFactories")
      public void testFill(Supplier<MemorySegment> memorySegmentSupplier) {
!         VarHandle byteHandle = MemoryLayout.sequenceLayout(ValueLayout.JAVA_BYTE)
!                 .varHandle(MemoryLayout.PathElement.sequenceElement());
  
          for (byte value : new byte[] {(byte) 0xFF, (byte) 0x00, (byte) 0x45}) {
              MemorySegment segment = memorySegmentSupplier.get();
              segment.fill(value);
              for (long l = 0; l < segment.byteSize(); l++) {

*** 191,19 ***
              }
          }
      }
  
      @Test(dataProvider = "segmentFactories")
!     public void testNativeSegments(Supplier<MemorySegment> memorySegmentSupplier) throws Exception {
          MemorySegment segment = memorySegmentSupplier.get();
          try {
!             segment.address().toRawLongValue();
              assertTrue(segment.isNative());
-             assertTrue(segment.address().isNative());
          } catch (UnsupportedOperationException exception) {
              assertFalse(segment.isNative());
-             assertFalse(segment.address().isNative());
          }
          tryClose(segment);
      }
  
      @Test(dataProvider = "segmentFactories", expectedExceptions = UnsupportedOperationException.class)
--- 184,17 ---
              }
          }
      }
  
      @Test(dataProvider = "segmentFactories")
!     public void testNativeSegments(Supplier<MemorySegment> memorySegmentSupplier) {
          MemorySegment segment = memorySegmentSupplier.get();
          try {
!             segment.address();
              assertTrue(segment.isNative());
          } catch (UnsupportedOperationException exception) {
              assertFalse(segment.isNative());
          }
          tryClose(segment);
      }
  
      @Test(dataProvider = "segmentFactories", expectedExceptions = UnsupportedOperationException.class)

*** 260,37 ***
                  { 1, 15 },
                  { 1, -15 }
          };
      }
  
-     @DataProvider(name = "badLayouts")
-     public Object[][] layouts() {
-         SizedLayoutFactory[] layoutFactories = SizedLayoutFactory.values();
-         Object[][] values = new Object[layoutFactories.length * 2][2];
-         for (int i = 0; i < layoutFactories.length ; i++) {
-             values[i * 2] = new Object[] { MemoryLayout.structLayout(layoutFactories[i].make(7), MemoryLayout.paddingLayout(9)) }; // good size, bad align
-             values[(i * 2) + 1] = new Object[] { layoutFactories[i].make(15).withBitAlignment(16) }; // bad size, good align
-         }
-         return values;
-     }
- 
-     enum SizedLayoutFactory {
-         VALUE_BE(size -> MemoryLayout.valueLayout(size, ByteOrder.BIG_ENDIAN)),
-         VALUE_LE(size -> MemoryLayout.valueLayout(size, ByteOrder.LITTLE_ENDIAN)),
-         PADDING(MemoryLayout::paddingLayout);
- 
-         private final LongFunction<MemoryLayout> factory;
- 
-         SizedLayoutFactory(LongFunction<MemoryLayout> factory) {
-             this.factory = factory;
-         }
- 
-         MemoryLayout make(long size) {
-             return factory.apply(size);
-         }
-     }
- 
      @DataProvider(name = "heapFactories")
      public Object[][] heapFactories() {
          return new Object[][] {
                  { (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new char[size]), 2 },
                  { (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new short[size]), 2 },
--- 251,10 ---
< prev index next >