< prev index next >

test/jdk/java/foreign/TestLayouts.java

Print this page
@@ -21,11 +21,10 @@
   * questions.
   */
  
  /*
   * @test
-  * @enablePreview
   * @run testng TestLayouts
   */
  
  import java.lang.foreign.*;
  

@@ -96,17 +95,17 @@
          try (Arena arena = Arena.ofConfined()) {
              MemorySegment segment = arena.allocate(seq);;
              VarHandle indexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement());
              // init segment
              for (int i = 0 ; i < 10 ; i++) {
-                 indexHandle.set(segment, (long)i, i);
+                 indexHandle.set(segment, 0L, (long)i, i);
              }
              //check statically indexed handles
              for (int i = 0 ; i < 10 ; i++) {
                  VarHandle preindexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement(i));
-                 int expected = (int)indexHandle.get(segment, (long)i);
-                 int found = (int)preindexHandle.get(segment);
+                 int expected = (int)indexHandle.get(segment, 0L, (long)i);
+                 int found = (int)preindexHandle.get(segment, 0L);
                  assertEquals(expected, found);
              }
          }
      }
  

@@ -199,16 +198,11 @@
      public void testSequenceBadCount() {
          assertThrows(IllegalArgumentException.class, // negative
                  () -> MemoryLayout.sequenceLayout(-2, JAVA_SHORT));
      }
  
-     @Test(dataProvider = "basicLayouts")
-     public void testSequenceInferredCount(MemoryLayout layout) {
-         assertEquals(MemoryLayout.sequenceLayout(layout),
-                      MemoryLayout.sequenceLayout(Long.MAX_VALUE / layout.byteSize(), layout));
-     }
- 
+     @Test
      public void testSequenceNegativeElementCount() {
          assertThrows(IllegalArgumentException.class, // negative
                  () -> MemoryLayout.sequenceLayout(-1, JAVA_SHORT));
      }
  

@@ -298,18 +292,18 @@
      }
  
      @Test(dataProvider="layoutsAndAlignments", expectedExceptions = IllegalArgumentException.class)
      public void testBadSequenceElementAlignmentTooBig(MemoryLayout layout, long byteAlign) {
          layout = layout.withByteAlignment(layout.byteSize() * 2); // hyper-align
-         MemoryLayout.sequenceLayout(layout);
+         MemoryLayout.sequenceLayout(1, layout);
      }
  
      @Test(dataProvider="layoutsAndAlignments")
      public void testBadSequenceElementSizeNotMultipleOfAlignment(MemoryLayout layout, long byteAlign) {
          boolean shouldFail = layout.byteSize() % layout.byteAlignment() != 0;
          try {
-             MemoryLayout.sequenceLayout(layout);
+             MemoryLayout.sequenceLayout(1, layout);
              assertFalse(shouldFail);
          } catch (IllegalArgumentException ex) {
              assertTrue(shouldFail);
          }
      }

@@ -336,18 +330,10 @@
          } catch (IllegalArgumentException ex) {
              assertTrue(shouldFail);
          }
      }
  
-     @Test(dataProvider="layoutsAndAlignments")
-     public void testArrayElementVarHandleBadAlignment(MemoryLayout layout, long byteAlign) {
-         if (layout instanceof ValueLayout) {
-             assertThrows(UnsupportedOperationException.class, () ->
-                     ((ValueLayout) layout).withByteAlignment(byteAlign * 2).arrayElementVarHandle());
-         }
-     }
- 
      @Test(dataProvider="layoutsAndAlignments", expectedExceptions = IllegalArgumentException.class)
      public void testBadStruct(MemoryLayout layout, long byteAlign) {
          layout = layout.withByteAlignment(layout.byteSize() * 2); // hyper-align
          MemoryLayout.structLayout(layout, layout);
      }

@@ -357,10 +343,41 @@
          SequenceLayout layout = MemoryLayout.sequenceLayout(10, JAVA_INT);
          // Step must be != 0
          PathElement.sequenceElement(3, 0);
      }
  
+     @Test
+     public void testVarHandleCaching() {
+         assertSame(JAVA_INT.varHandle(), JAVA_INT.varHandle());
+         assertSame(JAVA_INT.withName("foo").varHandle(), JAVA_INT.varHandle());
+ 
+         assertNotSame(JAVA_INT_UNALIGNED.varHandle(), JAVA_INT.varHandle());
+         assertNotSame(ADDRESS.withTargetLayout(JAVA_INT).varHandle(), ADDRESS.varHandle());
+     }
+ 
+     @Test(expectedExceptions=IllegalArgumentException.class,
+         expectedExceptionsMessageRegExp=".*Negative offset.*")
+     public void testScaleNegativeOffset() {
+         JAVA_INT.scale(-1, 0);
+     }
+ 
+     @Test(expectedExceptions=IllegalArgumentException.class,
+         expectedExceptionsMessageRegExp=".*Negative index.*")
+     public void testScaleNegativeIndex() {
+         JAVA_INT.scale(0, -1);
+     }
+ 
+     @Test(expectedExceptions=ArithmeticException.class)
+     public void testScaleAddOverflow() {
+         JAVA_INT.scale(Long.MAX_VALUE, 1);
+     }
+ 
+     @Test(expectedExceptions=ArithmeticException.class)
+     public void testScaleMultiplyOverflow() {
+         JAVA_INT.scale(0, Long.MAX_VALUE);
+     }
+ 
      @DataProvider(name = "badAlignments")
      public Object[][] layoutsAndBadAlignments() {
          LayoutKind[] layoutKinds = LayoutKind.values();
          Object[][] values = new Object[layoutKinds.length * 2][2];
          for (int i = 0; i < layoutKinds.length ; i++) {

@@ -494,11 +511,10 @@
      }
  
      static Stream<MemoryLayout> groupLayoutStream() {
          return Stream.of(
                  MemoryLayout.sequenceLayout(10, JAVA_INT),
-                 MemoryLayout.sequenceLayout(JAVA_INT),
                  MemoryLayout.structLayout(JAVA_INT, MemoryLayout.paddingLayout(4), JAVA_LONG),
                  MemoryLayout.unionLayout(JAVA_LONG, JAVA_DOUBLE)
          );
      }
  
< prev index next >