< prev index next > test/jdk/java/foreign/TestVarHandleCombinators.java
Print this page
*
*/
/*
* @test
- * @enablePreview
* @run testng TestVarHandleCombinators
*/
import java.lang.foreign.Arena;
import java.lang.foreign.ValueLayout;
public class TestVarHandleCombinators {
@Test
public void testElementAccess() {
- VarHandle vh = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_BYTE);
+ VarHandle vh = ValueLayout.JAVA_BYTE.varHandle();
byte[] arr = { 0, 0, -1, 0 };
MemorySegment segment = MemorySegment.ofArray(arr);
assertEquals((byte) vh.get(segment, 2), (byte) -1);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testUnalignedElement() {
- VarHandle vh = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_BYTE.withByteAlignment(4));
+ VarHandle vh = ValueLayout.JAVA_BYTE.withByteAlignment(4).varHandle();
MemorySegment segment = MemorySegment.ofArray(new byte[4]);
vh.get(segment, 2L); //should throw
//FIXME: the VH only checks the alignment of the segment, which is fine if the VH is derived from layouts,
//FIXME: but not if the VH is just created from scratch - we need a VH variable to govern this property,
//FIXME: at least until the VM is fixed
}
@Test
public void testAlign() {
- VarHandle vh = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_BYTE.withByteAlignment(2));
+ VarHandle vh = ValueLayout.JAVA_BYTE.withByteAlignment(2).varHandle();
Arena scope = Arena.ofAuto();
MemorySegment segment = scope.allocate(1L, 2);
vh.set(segment, 0L, (byte) 10); // fine, memory region is aligned
assertEquals((byte) vh.get(segment, 0L), (byte) 10);
}
@Test
public void testByteOrderLE() {
- VarHandle vh = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_SHORT_UNALIGNED
- .withOrder(ByteOrder.LITTLE_ENDIAN));
+ VarHandle vh = ValueLayout.JAVA_SHORT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN).varHandle();
byte[] arr = new byte[2];
MemorySegment segment = MemorySegment.ofArray(arr);
vh.set(segment, 0L, (short) 0xFF);
assertEquals(arr[0], (byte) 0xFF);
assertEquals(arr[1], (byte) 0);
}
@Test
public void testByteOrderBE() {
- VarHandle vh = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_SHORT_UNALIGNED
- .withOrder(ByteOrder.BIG_ENDIAN));
+ VarHandle vh = ValueLayout.JAVA_SHORT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN).varHandle();
byte[] arr = new byte[2];
MemorySegment segment = MemorySegment.ofArray(arr);
vh.set(segment, 0L, (short) 0xFF);
assertEquals(arr[0], (byte) 0);
assertEquals(arr[1], (byte) 0xFF);
int outer_size = 10;
int inner_size = 5;
//[10 : [5 : [x32 i32]]]
- VarHandle vh = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_INT);
+ VarHandle vh = ValueLayout.JAVA_INT.varHandle();
int count = 0;
try (Arena arena = Arena.ofConfined()) {
MemorySegment segment = arena.allocate(inner_size * outer_size * 8, 4);
for (long i = 0; i < outer_size; i++) {
for (long j = 0; j < inner_size; j++) {
< prev index next >