|
| 1 | +package com.baeldung.java14.foreign.api; |
| 2 | + |
| 3 | +import jdk.incubator.foreign.*; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import static org.hamcrest.core.Is.is; |
| 7 | +import static org.junit.Assert.assertThat; |
| 8 | + |
| 9 | +import java.lang.invoke.VarHandle; |
| 10 | +import java.nio.ByteOrder; |
| 11 | + |
| 12 | +public class ForeignMemoryUnitTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void whenAValueIsSet_thenAccessTheValue() { |
| 16 | + long value = 10; |
| 17 | + MemoryAddress memoryAddress = |
| 18 | + MemorySegment.allocateNative(8).baseAddress(); |
| 19 | + VarHandle varHandle = MemoryHandles.varHandle(long.class, |
| 20 | + ByteOrder.nativeOrder()); |
| 21 | + varHandle.set(memoryAddress, value); |
| 22 | + assertThat(varHandle.get(memoryAddress), is(value)); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void whenMultipleValuesAreSet_thenAccessAll() { |
| 27 | + VarHandle varHandle = MemoryHandles.varHandle(int.class, |
| 28 | + ByteOrder.nativeOrder()); |
| 29 | + |
| 30 | + try(MemorySegment memorySegment = |
| 31 | + MemorySegment.allocateNative(100)) { |
| 32 | + MemoryAddress base = memorySegment.baseAddress(); |
| 33 | + for(int i=0; i<25; i++) { |
| 34 | + varHandle.set(base.addOffset((i*4)), i); |
| 35 | + } |
| 36 | + for(int i=0; i<25; i++) { |
| 37 | + assertThat(varHandle.get(base.addOffset((i*4))), is(i)); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void whenSetValuesWithMemoryLayout_thenTheyCanBeRetrieved() { |
| 44 | + SequenceLayout sequenceLayout = |
| 45 | + MemoryLayout.ofSequence(25, |
| 46 | + MemoryLayout.ofValueBits(64, ByteOrder.nativeOrder())); |
| 47 | + VarHandle varHandle = |
| 48 | + sequenceLayout.varHandle(long.class, |
| 49 | + MemoryLayout.PathElement.sequenceElement()); |
| 50 | + |
| 51 | + try(MemorySegment memorySegment = |
| 52 | + MemorySegment.allocateNative(sequenceLayout)) { |
| 53 | + MemoryAddress base = memorySegment.baseAddress(); |
| 54 | + for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) { |
| 55 | + varHandle.set(base, i, i); |
| 56 | + } |
| 57 | + for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) { |
| 58 | + assertThat(varHandle.get(base, i), is(i)); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void whenSlicingMemorySegment_thenTheyCanBeAccessedIndividually() { |
| 65 | + MemoryAddress memoryAddress = |
| 66 | + MemorySegment.allocateNative(12).baseAddress(); |
| 67 | + MemoryAddress memoryAddress1 = |
| 68 | + memoryAddress.segment().asSlice(0,4).baseAddress(); |
| 69 | + MemoryAddress memoryAddress2 = |
| 70 | + memoryAddress.segment().asSlice(4,4).baseAddress(); |
| 71 | + MemoryAddress memoryAddress3 = |
| 72 | + memoryAddress.segment().asSlice(8,4).baseAddress(); |
| 73 | + |
| 74 | + VarHandle intHandle = |
| 75 | + MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder()); |
| 76 | + intHandle.set(memoryAddress1, Integer.MIN_VALUE); |
| 77 | + intHandle.set(memoryAddress2, 0); |
| 78 | + intHandle.set(memoryAddress3, Integer.MAX_VALUE); |
| 79 | + |
| 80 | + assertThat(intHandle.get(memoryAddress1), is(Integer.MIN_VALUE)); |
| 81 | + assertThat(intHandle.get(memoryAddress2), is(0)); |
| 82 | + assertThat(intHandle.get(memoryAddress3), is(Integer.MAX_VALUE)); |
| 83 | + } |
| 84 | +} |
0 commit comments