< prev index next >

test/jdk/java/foreign/TestSegments.java

Print this page
M TestSegments.java


  26  * @run testng TestSegments
  27  */
  28 
  29 import jdk.incubator.foreign.MemoryAddress;
  30 import jdk.incubator.foreign.MemoryLayout;
  31 import jdk.incubator.foreign.MemoryLayouts;
  32 import jdk.incubator.foreign.MemorySegment;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.VarHandle;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Modifier;
  39 import java.nio.ByteOrder;
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import java.util.Spliterator;
  43 import java.util.concurrent.atomic.AtomicBoolean;
  44 import java.util.concurrent.atomic.AtomicReference;
  45 import java.util.function.LongFunction;

  46 import java.util.stream.Stream;
  47 
  48 import static org.testng.Assert.*;
  49 
  50 public class TestSegments {
  51 
  52     @Test(dataProvider = "badSizeAndAlignments", expectedExceptions = IllegalArgumentException.class)
  53     public void testBadAllocateAlign(long size, long align) {
  54         MemorySegment.allocateNative(size, align);
  55     }
  56 
  57     @Test(dataProvider = "badLayouts", expectedExceptions = UnsupportedOperationException.class)
  58     public void testBadAllocateLayout(MemoryLayout layout) {
  59         MemorySegment.allocateNative(layout);
  60     }
  61 
  62     @Test(expectedExceptions = { OutOfMemoryError.class,
  63                                  IllegalArgumentException.class })
  64     public void testAllocateTooBig() {
  65         MemorySegment.allocateNative(Long.MAX_VALUE);
  66     }
  67 


 127             for (byte i = 0 ; i < segment.byteSize() ; i++) {
 128                 byteHandle.set(segment.baseAddress(), (long)i, i);
 129             }
 130             long start = 0;
 131             MemoryAddress base = segment.baseAddress();
 132             MemoryAddress last = base.addOffset(10);
 133             while (!base.equals(last)) {
 134                 MemorySegment slice = segment.asSlice(base.segmentOffset(), 10 - start);
 135                 for (long i = start ; i < 10 ; i++) {
 136                     assertEquals(
 137                             byteHandle.get(segment.baseAddress(), i),
 138                             byteHandle.get(slice.baseAddress(), i - start)
 139                     );
 140                 }
 141                 base = base.addOffset(1);
 142                 start++;
 143             }
 144         }
 145     }
 146 



























 147     @Test(dataProvider = "accessModes")
 148     public void testAccessModes(int accessModes) {
 149         int[] arr = new int[1];
 150         for (AccessActions action : AccessActions.values()) {
 151             MemorySegment segment = MemorySegment.ofArray(arr);
 152             MemorySegment restrictedSegment = segment.withAccessModes(accessModes);
 153             assertEquals(restrictedSegment.accessModes(), accessModes);
 154             boolean shouldFail = !restrictedSegment.hasAccessModes(action.accessMode);
 155             try {
 156                 action.run(restrictedSegment);
 157                 assertFalse(shouldFail);
 158             } catch (UnsupportedOperationException ex) {
 159                 assertTrue(shouldFail);
 160             }
 161         }
 162     }
 163 
 164     @Test(expectedExceptions = IllegalArgumentException.class)
 165     public void testBadWithAccessModes() {
 166         int[] arr = new int[1];




  26  * @run testng TestSegments
  27  */
  28 
  29 import jdk.incubator.foreign.MemoryAddress;
  30 import jdk.incubator.foreign.MemoryLayout;
  31 import jdk.incubator.foreign.MemoryLayouts;
  32 import jdk.incubator.foreign.MemorySegment;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.VarHandle;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Modifier;
  39 import java.nio.ByteOrder;
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import java.util.Spliterator;
  43 import java.util.concurrent.atomic.AtomicBoolean;
  44 import java.util.concurrent.atomic.AtomicReference;
  45 import java.util.function.LongFunction;
  46 import java.util.function.Supplier;
  47 import java.util.stream.Stream;
  48 import static jdk.incubator.foreign.MemorySegment.*;
  49 import static org.testng.Assert.*;
  50 
  51 public class TestSegments {
  52 
  53     @Test(dataProvider = "badSizeAndAlignments", expectedExceptions = IllegalArgumentException.class)
  54     public void testBadAllocateAlign(long size, long align) {
  55         MemorySegment.allocateNative(size, align);
  56     }
  57 
  58     @Test(dataProvider = "badLayouts", expectedExceptions = UnsupportedOperationException.class)
  59     public void testBadAllocateLayout(MemoryLayout layout) {
  60         MemorySegment.allocateNative(layout);
  61     }
  62 
  63     @Test(expectedExceptions = { OutOfMemoryError.class,
  64                                  IllegalArgumentException.class })
  65     public void testAllocateTooBig() {
  66         MemorySegment.allocateNative(Long.MAX_VALUE);
  67     }
  68 


 128             for (byte i = 0 ; i < segment.byteSize() ; i++) {
 129                 byteHandle.set(segment.baseAddress(), (long)i, i);
 130             }
 131             long start = 0;
 132             MemoryAddress base = segment.baseAddress();
 133             MemoryAddress last = base.addOffset(10);
 134             while (!base.equals(last)) {
 135                 MemorySegment slice = segment.asSlice(base.segmentOffset(), 10 - start);
 136                 for (long i = start ; i < 10 ; i++) {
 137                     assertEquals(
 138                             byteHandle.get(segment.baseAddress(), i),
 139                             byteHandle.get(slice.baseAddress(), i - start)
 140                     );
 141                 }
 142                 base = base.addOffset(1);
 143                 start++;
 144             }
 145         }
 146     }
 147 
 148     static final int ALL_ACCESS_MODES = READ | WRITE | CLOSE | ACQUIRE | HANDOFF;
 149 
 150     @DataProvider(name = "segmentFactories")
 151     public Object[][] segmentFactories() {
 152         List<Supplier<MemorySegment>> l = List.of(
 153                 () -> MemorySegment.ofArray(new byte[1]),
 154                 () -> MemorySegment.ofArray(new char[1]),
 155                 () -> MemorySegment.ofArray(new double[1]),
 156                 () -> MemorySegment.ofArray(new float[1]),
 157                 () -> MemorySegment.ofArray(new int[1]),
 158                 () -> MemorySegment.ofArray(new long[1]),
 159                 () -> MemorySegment.ofArray(new short[1]),
 160                 () -> MemorySegment.ofArray(new int[1]),
 161                 () -> MemorySegment.allocateNative(1),
 162                 () -> MemorySegment.allocateNative(1, 2),
 163                 () -> MemorySegment.allocateNative(MemoryLayout.ofValueBits(8, ByteOrder.LITTLE_ENDIAN))
 164         );
 165         return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
 166     }
 167     @Test(dataProvider = "segmentFactories")
 168     public void testAccessModesOfFactories(Supplier<MemorySegment> memorySegmentSupplier) {
 169         try (MemorySegment segment = memorySegmentSupplier.get()) {
 170             assertTrue(segment.hasAccessModes(ALL_ACCESS_MODES));
 171             assertEquals(segment.accessModes(), ALL_ACCESS_MODES);
 172         }
 173     }
 174 
 175     @Test(dataProvider = "accessModes")
 176     public void testAccessModes(int accessModes) {
 177         int[] arr = new int[1];
 178         for (AccessActions action : AccessActions.values()) {
 179             MemorySegment segment = MemorySegment.ofArray(arr);
 180             MemorySegment restrictedSegment = segment.withAccessModes(accessModes);
 181             assertEquals(restrictedSegment.accessModes(), accessModes);
 182             boolean shouldFail = !restrictedSegment.hasAccessModes(action.accessMode);
 183             try {
 184                 action.run(restrictedSegment);
 185                 assertFalse(shouldFail);
 186             } catch (UnsupportedOperationException ex) {
 187                 assertTrue(shouldFail);
 188             }
 189         }
 190     }
 191 
 192     @Test(expectedExceptions = IllegalArgumentException.class)
 193     public void testBadWithAccessModes() {
 194         int[] arr = new int[1];


< prev index next >