< prev index next >

test/jdk/java/foreign/TestArrays.java

Print this page
rev 57826 : 8237521: Memory Access API fixes for 32-bit
Reviewed-by: mcimadamore, dholmes


  21  *  questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @run testng TestArrays
  28  */
  29 
  30 import jdk.incubator.foreign.MemoryAddress;
  31 import jdk.incubator.foreign.MemoryLayout;
  32 import jdk.incubator.foreign.MemoryLayout.PathElement;
  33 import jdk.incubator.foreign.MemoryLayouts;
  34 import jdk.incubator.foreign.MemorySegment;
  35 import jdk.incubator.foreign.SequenceLayout;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.util.function.BiConsumer;
  39 import java.util.function.Consumer;
  40 
  41 import org.testng.SkipException;
  42 import org.testng.annotations.*;
  43 import static org.testng.Assert.*;
  44 
  45 public class TestArrays {
  46 
  47     static SequenceLayout bytes = MemoryLayout.ofSequence(100,
  48             MemoryLayouts.JAVA_BYTE
  49     );
  50 
  51     static SequenceLayout chars = MemoryLayout.ofSequence(100,
  52             MemoryLayouts.JAVA_CHAR
  53     );
  54 
  55     static SequenceLayout shorts = MemoryLayout.ofSequence(100,
  56             MemoryLayouts.JAVA_SHORT
  57     );
  58 
  59     static SequenceLayout ints = MemoryLayout.ofSequence(100,
  60             MemoryLayouts.JAVA_INT
  61     );


  88 
  89     static void checkBytes(MemoryAddress base, SequenceLayout layout) {
  90         long nBytes = layout.elementCount().getAsLong() * layout.elementLayout().byteSize();
  91         byte[] arr = base.segment().toByteArray();
  92         for (long i = 0 ; i < nBytes ; i++) {
  93             byte expected = (byte)byteHandle.get(base, i);
  94             byte found = arr[(int)i];
  95             assertEquals(expected, found);
  96         }
  97     }
  98 
  99     @Test(dataProvider = "arrays")
 100     public void testArrays(Consumer<MemoryAddress> init, SequenceLayout layout) {
 101         try (MemorySegment segment = MemorySegment.allocateNative(layout)) {
 102             init.accept(segment.baseAddress());
 103             checkBytes(segment.baseAddress(), layout);
 104         }
 105     }
 106 
 107     @Test(expectedExceptions = { UnsupportedOperationException.class,
 108                                  OutOfMemoryError.class })
 109     public void testTooBigForArray() {
 110         if (System.getProperty("sun.arch.data.model").equals("32")) {
 111             throw new SkipException("32-bit Unsafe does not support this allocation size");
 112         }
 113 
 114         MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).toByteArray();
 115     }
 116 
 117     @Test(expectedExceptions = IllegalStateException.class)
 118     public void testArrayFromClosedSegment() {
 119         MemorySegment segment = MemorySegment.allocateNative(8);
 120         segment.close();
 121         segment.toByteArray();
 122     }
 123 
 124     @DataProvider(name = "arrays")
 125     public Object[][] nativeAccessOps() {
 126         Consumer<MemoryAddress> byteInitializer =
 127                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
 128         Consumer<MemoryAddress> charInitializer =
 129                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
 130         Consumer<MemoryAddress> shortInitializer =
 131                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
 132         Consumer<MemoryAddress> intInitializer =
 133                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));


  21  *  questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @run testng TestArrays
  28  */
  29 
  30 import jdk.incubator.foreign.MemoryAddress;
  31 import jdk.incubator.foreign.MemoryLayout;
  32 import jdk.incubator.foreign.MemoryLayout.PathElement;
  33 import jdk.incubator.foreign.MemoryLayouts;
  34 import jdk.incubator.foreign.MemorySegment;
  35 import jdk.incubator.foreign.SequenceLayout;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.util.function.BiConsumer;
  39 import java.util.function.Consumer;
  40 

  41 import org.testng.annotations.*;
  42 import static org.testng.Assert.*;
  43 
  44 public class TestArrays {
  45 
  46     static SequenceLayout bytes = MemoryLayout.ofSequence(100,
  47             MemoryLayouts.JAVA_BYTE
  48     );
  49 
  50     static SequenceLayout chars = MemoryLayout.ofSequence(100,
  51             MemoryLayouts.JAVA_CHAR
  52     );
  53 
  54     static SequenceLayout shorts = MemoryLayout.ofSequence(100,
  55             MemoryLayouts.JAVA_SHORT
  56     );
  57 
  58     static SequenceLayout ints = MemoryLayout.ofSequence(100,
  59             MemoryLayouts.JAVA_INT
  60     );


  87 
  88     static void checkBytes(MemoryAddress base, SequenceLayout layout) {
  89         long nBytes = layout.elementCount().getAsLong() * layout.elementLayout().byteSize();
  90         byte[] arr = base.segment().toByteArray();
  91         for (long i = 0 ; i < nBytes ; i++) {
  92             byte expected = (byte)byteHandle.get(base, i);
  93             byte found = arr[(int)i];
  94             assertEquals(expected, found);
  95         }
  96     }
  97 
  98     @Test(dataProvider = "arrays")
  99     public void testArrays(Consumer<MemoryAddress> init, SequenceLayout layout) {
 100         try (MemorySegment segment = MemorySegment.allocateNative(layout)) {
 101             init.accept(segment.baseAddress());
 102             checkBytes(segment.baseAddress(), layout);
 103         }
 104     }
 105 
 106     @Test(expectedExceptions = { UnsupportedOperationException.class,
 107                                  IllegalArgumentException.class })
 108     public void testTooBigForArray() {




 109         MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).toByteArray();
 110     }
 111 
 112     @Test(expectedExceptions = IllegalStateException.class)
 113     public void testArrayFromClosedSegment() {
 114         MemorySegment segment = MemorySegment.allocateNative(8);
 115         segment.close();
 116         segment.toByteArray();
 117     }
 118 
 119     @DataProvider(name = "arrays")
 120     public Object[][] nativeAccessOps() {
 121         Consumer<MemoryAddress> byteInitializer =
 122                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
 123         Consumer<MemoryAddress> charInitializer =
 124                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
 125         Consumer<MemoryAddress> shortInitializer =
 126                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
 127         Consumer<MemoryAddress> intInitializer =
 128                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));
< prev index next >