< prev index next >

test/jdk/java/foreign/TestSlices.java

Print this page

 18  *
 19  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 import java.lang.foreign.Arena;
 26 import java.lang.foreign.MemoryLayout;
 27 import java.lang.foreign.MemorySegment;
 28 import java.lang.foreign.ValueLayout;
 29 import java.lang.invoke.VarHandle;
 30 import java.util.ArrayList;
 31 import java.util.List;
 32 
 33 import org.testng.annotations.*;
 34 import static org.testng.Assert.*;
 35 
 36 /*
 37  * @test
 38  * @enablePreview
 39  * @run testng/othervm -Xverify:all TestSlices
 40  */
 41 public class TestSlices {
 42 
 43     static MemoryLayout LAYOUT = MemoryLayout.sequenceLayout(2,
 44             MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_INT));
 45 
 46     static VarHandle VH_ALL = LAYOUT.varHandle(
 47             MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.sequenceElement());
 48 
 49     @Test(dataProvider = "slices")
 50     public void testSlices(VarHandle handle, int lo, int hi, int[] values) {
 51         try (Arena arena = Arena.ofConfined()) {
 52             MemorySegment segment = arena.allocate(LAYOUT);;
 53             //init
 54             for (long i = 0 ; i < 2 ; i++) {
 55                 for (long j = 0 ; j < 5 ; j++) {
 56                     VH_ALL.set(segment, i, j, (int)j + 1 + ((int)i * 5));
 57                 }
 58             }
 59 
 60             checkSlice(segment, handle, lo, hi, values);
 61         }
 62     }
 63 
 64     @Test(dataProvider = "slices")
 65     public void testSliceBadIndex(VarHandle handle, int lo, int hi, int[] values) {
 66         try (Arena arena = Arena.ofConfined()) {
 67             MemorySegment segment = arena.allocate(LAYOUT);;
 68             assertThrows(() -> handle.get(segment, lo, 0));
 69             assertThrows(() -> handle.get(segment, 0, hi));
 70         }
 71     }
 72 
 73     static void checkSlice(MemorySegment segment, VarHandle handle, long i_max, long j_max, int... values) {
 74         int index = 0;
 75         for (long i = 0 ; i < i_max ; i++) {
 76             for (long j = 0 ; j < j_max ; j++) {
 77                 int x = (int) handle.get(segment, i, j);
 78                 assertEquals(x, values[index++]);
 79             }
 80         }
 81         assertEquals(index, values.length);
 82     }
 83 
 84     @Test(expectedExceptions = IndexOutOfBoundsException.class)
 85     public void testSliceNegativeOffset() {
 86         MemorySegment.ofArray(new byte[100]).asSlice(-1);
 87     }
 88 
 89     @Test(expectedExceptions = IndexOutOfBoundsException.class)
 90     public void testSliceNegativeOffsetGoodSize() {
 91         MemorySegment.ofArray(new byte[100]).asSlice(-1, 10);
 92     }
 93 
 94     @Test(expectedExceptions = IndexOutOfBoundsException.class)
 95     public void testSliceGoodOffsetNegativeSize() {
 96         MemorySegment.ofArray(new byte[100]).asSlice(10, -1);
 97     }

 18  *
 19  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 import java.lang.foreign.Arena;
 26 import java.lang.foreign.MemoryLayout;
 27 import java.lang.foreign.MemorySegment;
 28 import java.lang.foreign.ValueLayout;
 29 import java.lang.invoke.VarHandle;
 30 import java.util.ArrayList;
 31 import java.util.List;
 32 
 33 import org.testng.annotations.*;
 34 import static org.testng.Assert.*;
 35 
 36 /*
 37  * @test

 38  * @run testng/othervm -Xverify:all TestSlices
 39  */
 40 public class TestSlices {
 41 
 42     static MemoryLayout LAYOUT = MemoryLayout.sequenceLayout(2,
 43             MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_INT));
 44 
 45     static VarHandle VH_ALL = LAYOUT.varHandle(
 46             MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.sequenceElement());
 47 
 48     @Test(dataProvider = "slices")
 49     public void testSlices(VarHandle handle, int lo, int hi, int[] values) {
 50         try (Arena arena = Arena.ofConfined()) {
 51             MemorySegment segment = arena.allocate(LAYOUT);;
 52             //init
 53             for (long i = 0 ; i < 2 ; i++) {
 54                 for (long j = 0 ; j < 5 ; j++) {
 55                     VH_ALL.set(segment, 0L, i, j, (int)j + 1 + ((int)i * 5));
 56                 }
 57             }
 58 
 59             checkSlice(segment, handle, lo, hi, values);
 60         }
 61     }
 62 
 63     @Test(dataProvider = "slices")
 64     public void testSliceBadIndex(VarHandle handle, int lo, int hi, int[] values) {
 65         try (Arena arena = Arena.ofConfined()) {
 66             MemorySegment segment = arena.allocate(LAYOUT);;
 67             assertThrows(() -> handle.get(segment, 0L, lo, 0));
 68             assertThrows(() -> handle.get(segment, 0L, 0, hi));
 69         }
 70     }
 71 
 72     static void checkSlice(MemorySegment segment, VarHandle handle, long i_max, long j_max, int... values) {
 73         int index = 0;
 74         for (long i = 0 ; i < i_max ; i++) {
 75             for (long j = 0 ; j < j_max ; j++) {
 76                 int x = (int) handle.get(segment, 0L, i, j);
 77                 assertEquals(x, values[index++]);
 78             }
 79         }
 80         assertEquals(index, values.length);
 81     }
 82 
 83     @Test(expectedExceptions = IndexOutOfBoundsException.class)
 84     public void testSliceNegativeOffset() {
 85         MemorySegment.ofArray(new byte[100]).asSlice(-1);
 86     }
 87 
 88     @Test(expectedExceptions = IndexOutOfBoundsException.class)
 89     public void testSliceNegativeOffsetGoodSize() {
 90         MemorySegment.ofArray(new byte[100]).asSlice(-1, 10);
 91     }
 92 
 93     @Test(expectedExceptions = IndexOutOfBoundsException.class)
 94     public void testSliceGoodOffsetNegativeSize() {
 95         MemorySegment.ofArray(new byte[100]).asSlice(10, -1);
 96     }
< prev index next >