< prev index next >

test/jdk/java/foreign/TestVarHandleCombinators.java

Print this page

 12  *  version 2 for more details (a copy is included in the LICENSE file that
 13  *  accompanied this code).
 14  *
 15  *  You should have received a copy of the GNU General Public License version
 16  *  2 along with this work; if not, write to the Free Software Foundation,
 17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 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 /*
 26  * @test
 27  * @run testng TestVarHandleCombinators
 28  */
 29 
 30 import jdk.incubator.foreign.MemoryHandles;
 31 import jdk.incubator.foreign.ResourceScope;

 32 import org.testng.annotations.DataProvider;
 33 import org.testng.annotations.Test;
 34 
 35 import jdk.incubator.foreign.MemorySegment;
 36 
 37 import java.lang.invoke.VarHandle;
 38 import java.nio.ByteOrder;
 39 
 40 import static org.testng.Assert.assertEquals;
 41 
 42 public class TestVarHandleCombinators {
 43 
 44     @Test
 45     public void testElementAccess() {
 46         VarHandle vh = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
 47 
 48         byte[] arr = { 0, 0, -1, 0 };
 49         MemorySegment segment = MemorySegment.ofArray(arr);
 50         assertEquals((byte) vh.get(segment, 2), (byte) -1);
 51     }
 52 
 53     @Test(expectedExceptions = IllegalStateException.class)
 54     public void testUnalignedElement() {
 55         VarHandle vh = MemoryHandles.varHandle(byte.class, 4, ByteOrder.nativeOrder());
 56         MemorySegment segment = MemorySegment.ofArray(new byte[4]);
 57         vh.get(segment, 2L); //should throw
 58         //FIXME: the VH only checks the alignment of the segment, which is fine if the VH is derived from layouts,
 59         //FIXME: but not if the VH is just created from scratch - we need a VH variable to govern this property,
 60         //FIXME: at least until the VM is fixed
 61     }
 62 
 63     @Test(expectedExceptions = IllegalArgumentException.class)
 64     public void testAlignNotPowerOf2() {
 65         VarHandle vh = MemoryHandles.varHandle(byte.class, 3, ByteOrder.nativeOrder());
 66     }
 67 
 68     @Test(expectedExceptions = IllegalArgumentException.class)
 69     public void testAlignNegative() {
 70         VarHandle vh = MemoryHandles.varHandle(byte.class, -1, ByteOrder.nativeOrder());
 71     }
 72 
 73     @Test
 74     public void testAlign() {
 75         VarHandle vh = MemoryHandles.varHandle(byte.class, 2, ByteOrder.nativeOrder());
 76 
 77         MemorySegment segment = MemorySegment.allocateNative(1, 2, ResourceScope.newImplicitScope());
 78         vh.set(segment, 0L, (byte) 10); // fine, memory region is aligned
 79         assertEquals((byte) vh.get(segment, 0L), (byte) 10);
 80     }
 81 
 82     @Test
 83     public void testByteOrderLE() {
 84         VarHandle vh = MemoryHandles.varHandle(short.class, 2, ByteOrder.LITTLE_ENDIAN);
 85         byte[] arr = new byte[2];
 86         MemorySegment segment = MemorySegment.ofArray(arr);
 87         vh.set(segment, 0L, (short) 0xFF);
 88         assertEquals(arr[0], (byte) 0xFF);
 89         assertEquals(arr[1], (byte) 0);
 90     }
 91 
 92     @Test
 93     public void testByteOrderBE() {
 94         VarHandle vh = MemoryHandles.varHandle(short.class, 2, ByteOrder.BIG_ENDIAN);
 95         byte[] arr = new byte[2];
 96         MemorySegment segment = MemorySegment.ofArray(arr);
 97         vh.set(segment, 0L, (short) 0xFF);
 98         assertEquals(arr[0], (byte) 0);
 99         assertEquals(arr[1], (byte) 0xFF);
100     }
101 
102     @Test
103     public void testNestedSequenceAccess() {
104         int outer_size = 10;
105         int inner_size = 5;
106 
107         //[10 : [5 : [x32 i32]]]
108 
109         VarHandle vh = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
110         int count = 0;
111         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
112             MemorySegment segment = MemorySegment.allocateNative(inner_size * outer_size * 8, 4, scope);
113             for (long i = 0; i < outer_size; i++) {
114                 for (long j = 0; j < inner_size; j++) {
115                     vh.set(segment, i * 40 + j * 8, count);
116                     assertEquals(
117                             (int)vh.get(segment.asSlice(i * inner_size * 8), j * 8),
118                             count);
119                     count++;
120                 }
121             }
122         }
123     }
124 
125     @Test(dataProvider = "badCarriers", expectedExceptions = IllegalArgumentException.class)
126     public void testBadCarrier(Class<?> carrier) {
127         MemoryHandles.varHandle(carrier, ByteOrder.nativeOrder());
128     }
129 
130     @DataProvider(name = "badCarriers")
131     public Object[][] createBadCarriers() {
132         return new Object[][] {
133                 { void.class },
134                 { boolean.class },
135                 { Object.class },
136                 { int[].class },
137                 { MemorySegment.class }
138         };
139     }
140 
141 }

 12  *  version 2 for more details (a copy is included in the LICENSE file that
 13  *  accompanied this code).
 14  *
 15  *  You should have received a copy of the GNU General Public License version
 16  *  2 along with this work; if not, write to the Free Software Foundation,
 17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 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 /*
 26  * @test
 27  * @run testng TestVarHandleCombinators
 28  */
 29 
 30 import jdk.incubator.foreign.MemoryHandles;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.incubator.foreign.ValueLayout;
 33 import org.testng.annotations.DataProvider;
 34 import org.testng.annotations.Test;
 35 
 36 import jdk.incubator.foreign.MemorySegment;
 37 
 38 import java.lang.invoke.VarHandle;
 39 import java.nio.ByteOrder;
 40 
 41 import static org.testng.Assert.assertEquals;
 42 
 43 public class TestVarHandleCombinators {
 44 
 45     @Test
 46     public void testElementAccess() {
 47         VarHandle vh = MemoryHandles.varHandle(ValueLayout.JAVA_BYTE);
 48 
 49         byte[] arr = { 0, 0, -1, 0 };
 50         MemorySegment segment = MemorySegment.ofArray(arr);
 51         assertEquals((byte) vh.get(segment, 2), (byte) -1);
 52     }
 53 
 54     @Test(expectedExceptions = IllegalStateException.class)
 55     public void testUnalignedElement() {
 56         VarHandle vh = MemoryHandles.varHandle(ValueLayout.JAVA_BYTE.withBitAlignment(32));
 57         MemorySegment segment = MemorySegment.ofArray(new byte[4]);
 58         vh.get(segment, 2L); //should throw
 59         //FIXME: the VH only checks the alignment of the segment, which is fine if the VH is derived from layouts,
 60         //FIXME: but not if the VH is just created from scratch - we need a VH variable to govern this property,
 61         //FIXME: at least until the VM is fixed
 62     }
 63 










 64     @Test
 65     public void testAlign() {
 66         VarHandle vh = MemoryHandles.varHandle(ValueLayout.JAVA_BYTE.withBitAlignment(16));
 67 
 68         MemorySegment segment = MemorySegment.allocateNative(1, 2, ResourceScope.newImplicitScope());
 69         vh.set(segment, 0L, (byte) 10); // fine, memory region is aligned
 70         assertEquals((byte) vh.get(segment, 0L), (byte) 10);
 71     }
 72 
 73     @Test
 74     public void testByteOrderLE() {
 75         VarHandle vh = MemoryHandles.varHandle(ValueLayout.JAVA_SHORT.withOrder(ByteOrder.LITTLE_ENDIAN));
 76         byte[] arr = new byte[2];
 77         MemorySegment segment = MemorySegment.ofArray(arr);
 78         vh.set(segment, 0L, (short) 0xFF);
 79         assertEquals(arr[0], (byte) 0xFF);
 80         assertEquals(arr[1], (byte) 0);
 81     }
 82 
 83     @Test
 84     public void testByteOrderBE() {
 85         VarHandle vh = MemoryHandles.varHandle(ValueLayout.JAVA_SHORT.withOrder(ByteOrder.BIG_ENDIAN));
 86         byte[] arr = new byte[2];
 87         MemorySegment segment = MemorySegment.ofArray(arr);
 88         vh.set(segment, 0L, (short) 0xFF);
 89         assertEquals(arr[0], (byte) 0);
 90         assertEquals(arr[1], (byte) 0xFF);
 91     }
 92 
 93     @Test
 94     public void testNestedSequenceAccess() {
 95         int outer_size = 10;
 96         int inner_size = 5;
 97 
 98         //[10 : [5 : [x32 i32]]]
 99 
100         VarHandle vh = MemoryHandles.varHandle(ValueLayout.JAVA_INT.withBitAlignment(32));
101         int count = 0;
102         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
103             MemorySegment segment = MemorySegment.allocateNative(inner_size * outer_size * 8, 4, scope);
104             for (long i = 0; i < outer_size; i++) {
105                 for (long j = 0; j < inner_size; j++) {
106                     vh.set(segment, i * 40 + j * 8, count);
107                     assertEquals(
108                             (int)vh.get(segment.asSlice(i * inner_size * 8), j * 8),
109                             count);
110                     count++;
111                 }
112             }
113         }
114     }

















115 }
< prev index next >