< prev index next >

test/jdk/java/foreign/TestSegments.java

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 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  * @test
 26  * @enablePreview
 27  * @requires jdk.foreign.linker != "UNSUPPORTED"
 28  * @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M --enable-native-access=ALL-UNNAMED TestSegments
 29  */
 30 
 31 import java.lang.foreign.*;
 32 
 33 import org.testng.annotations.DataProvider;
 34 import org.testng.annotations.Test;
 35 
 36 import java.lang.invoke.VarHandle;
 37 import java.nio.ByteBuffer;
 38 import java.util.List;
 39 import java.util.concurrent.atomic.AtomicInteger;
 40 import java.util.concurrent.atomic.AtomicReference;
 41 import java.util.function.IntFunction;
 42 import java.util.function.Supplier;
 43 
 44 import static java.lang.foreign.ValueLayout.JAVA_INT;
 45 import static org.testng.Assert.*;
 46 
 47 public class TestSegments {

 67             MemorySegment rawAddress = MemorySegment.ofAddress(segment.address());
 68             assertEquals(rawAddress.byteSize(), 0);
 69             assertEquals(rawAddress.address() % 4, 0);
 70         }
 71     }
 72 
 73     @Test(expectedExceptions = { OutOfMemoryError.class,
 74                                  IllegalArgumentException.class })
 75     public void testAllocateTooBig() {
 76         Arena.ofAuto().allocate(Long.MAX_VALUE, 1);
 77     }
 78 
 79     @Test(expectedExceptions = OutOfMemoryError.class)
 80     public void testNativeAllocationTooBig() {
 81         Arena scope = Arena.ofAuto();
 82         MemorySegment segment = scope.allocate(1024L * 1024 * 8 * 2, 1); // 2M
 83     }
 84 
 85     @Test
 86     public void testNativeSegmentIsZeroed() {
 87         VarHandle byteHandle = ValueLayout.JAVA_BYTE.arrayElementVarHandle();
 88         try (Arena arena = Arena.ofConfined()) {
 89             MemorySegment segment = arena.allocate(1000, 1);
 90             for (long i = 0 ; i < segment.byteSize() ; i++) {
 91                 assertEquals(0, (byte)byteHandle.get(segment, i));
 92             }
 93         }
 94     }
 95 
 96     @Test
 97     public void testSlices() {
 98         VarHandle byteHandle = ValueLayout.JAVA_BYTE.arrayElementVarHandle();
 99         try (Arena arena = Arena.ofConfined()) {
100             MemorySegment segment = arena.allocate(10, 1);
101             //init
102             for (byte i = 0 ; i < segment.byteSize() ; i++) {
103                 byteHandle.set(segment, (long)i, i);
104             }
105             for (int offset = 0 ; offset < 10 ; offset++) {
106                 MemorySegment slice = segment.asSlice(offset);
107                 for (long i = offset ; i < 10 ; i++) {
108                     assertEquals(
109                             byteHandle.get(segment, i),
110                             byteHandle.get(slice, i - offset)
111                     );
112                 }
113             }
114         }
115     }
116 
117     @Test(dataProvider = "segmentFactories")
118     public void testDerivedScopes(Supplier<MemorySegment> segmentSupplier) {

246                 () -> MemorySegment.ofArray(new byte[] { 0x00, 0x01, 0x02, 0x03 }),
247                 () -> MemorySegment.ofArray(new char[] {'a', 'b', 'c', 'd' }),
248                 () -> MemorySegment.ofArray(new double[] { 1d, 2d, 3d, 4d} ),
249                 () -> MemorySegment.ofArray(new float[] { 1.0f, 2.0f, 3.0f, 4.0f }),
250                 () -> MemorySegment.ofArray(new int[] { 1, 2, 3, 4 }),
251                 () -> MemorySegment.ofArray(new long[] { 1L, 2L, 3L, 4L } ),
252                 () -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } ),
253                 () -> Arena.ofAuto().allocate(4L, 1),
254                 () -> Arena.ofAuto().allocate(4L, 8),
255                 () -> Arena.ofAuto().allocate(JAVA_INT),
256                 () -> Arena.ofAuto().allocate(4L, 1),
257                 () -> Arena.ofAuto().allocate(4L, 8),
258                 () -> Arena.ofAuto().allocate(JAVA_INT)
259 
260         );
261         return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
262     }
263 
264     @Test(dataProvider = "segmentFactories")
265     public void testFill(Supplier<MemorySegment> segmentSupplier) {
266         VarHandle byteHandle = ValueLayout.JAVA_BYTE.arrayElementVarHandle();
267 
268         for (byte value : new byte[] {(byte) 0xFF, (byte) 0x00, (byte) 0x45}) {
269             MemorySegment segment = segmentSupplier.get();
270             segment.fill(value);
271             for (long l = 0; l < segment.byteSize(); l++) {
272                 assertEquals((byte) byteHandle.get(segment, l), value);
273             }
274 
275             // fill a slice
276             var sliceSegment = segment.asSlice(1, segment.byteSize() - 2).fill((byte) ~value);
277             for (long l = 0; l < sliceSegment.byteSize(); l++) {
278                 assertEquals((byte) byteHandle.get(sliceSegment, l), ~value);
279             }
280             // assert enclosing slice
281             assertEquals((byte) byteHandle.get(segment, 0L), value);
282             for (long l = 1; l < segment.byteSize() - 2; l++) {
283                 assertEquals((byte) byteHandle.get(segment, l), (byte) ~value);
284             }
285             assertEquals((byte) byteHandle.get(segment, segment.byteSize() - 1L), value);
286         }

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 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  * @test
 26  * @requires vm.bits == 64

 27  * @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M --enable-native-access=ALL-UNNAMED TestSegments
 28  */
 29 
 30 import java.lang.foreign.*;
 31 
 32 import org.testng.annotations.DataProvider;
 33 import org.testng.annotations.Test;
 34 
 35 import java.lang.invoke.VarHandle;
 36 import java.nio.ByteBuffer;
 37 import java.util.List;
 38 import java.util.concurrent.atomic.AtomicInteger;
 39 import java.util.concurrent.atomic.AtomicReference;
 40 import java.util.function.IntFunction;
 41 import java.util.function.Supplier;
 42 
 43 import static java.lang.foreign.ValueLayout.JAVA_INT;
 44 import static org.testng.Assert.*;
 45 
 46 public class TestSegments {

 66             MemorySegment rawAddress = MemorySegment.ofAddress(segment.address());
 67             assertEquals(rawAddress.byteSize(), 0);
 68             assertEquals(rawAddress.address() % 4, 0);
 69         }
 70     }
 71 
 72     @Test(expectedExceptions = { OutOfMemoryError.class,
 73                                  IllegalArgumentException.class })
 74     public void testAllocateTooBig() {
 75         Arena.ofAuto().allocate(Long.MAX_VALUE, 1);
 76     }
 77 
 78     @Test(expectedExceptions = OutOfMemoryError.class)
 79     public void testNativeAllocationTooBig() {
 80         Arena scope = Arena.ofAuto();
 81         MemorySegment segment = scope.allocate(1024L * 1024 * 8 * 2, 1); // 2M
 82     }
 83 
 84     @Test
 85     public void testNativeSegmentIsZeroed() {
 86         VarHandle byteHandle = ValueLayout.JAVA_BYTE.varHandle();
 87         try (Arena arena = Arena.ofConfined()) {
 88             MemorySegment segment = arena.allocate(1000, 1);
 89             for (long i = 0 ; i < segment.byteSize() ; i++) {
 90                 assertEquals(0, (byte)byteHandle.get(segment, i));
 91             }
 92         }
 93     }
 94 
 95     @Test
 96     public void testSlices() {
 97         VarHandle byteHandle = ValueLayout.JAVA_BYTE.varHandle();
 98         try (Arena arena = Arena.ofConfined()) {
 99             MemorySegment segment = arena.allocate(10, 1);
100             //init
101             for (byte i = 0 ; i < segment.byteSize() ; i++) {
102                 byteHandle.set(segment, (long)i, i);
103             }
104             for (int offset = 0 ; offset < 10 ; offset++) {
105                 MemorySegment slice = segment.asSlice(offset);
106                 for (long i = offset ; i < 10 ; i++) {
107                     assertEquals(
108                             byteHandle.get(segment, i),
109                             byteHandle.get(slice, i - offset)
110                     );
111                 }
112             }
113         }
114     }
115 
116     @Test(dataProvider = "segmentFactories")
117     public void testDerivedScopes(Supplier<MemorySegment> segmentSupplier) {

245                 () -> MemorySegment.ofArray(new byte[] { 0x00, 0x01, 0x02, 0x03 }),
246                 () -> MemorySegment.ofArray(new char[] {'a', 'b', 'c', 'd' }),
247                 () -> MemorySegment.ofArray(new double[] { 1d, 2d, 3d, 4d} ),
248                 () -> MemorySegment.ofArray(new float[] { 1.0f, 2.0f, 3.0f, 4.0f }),
249                 () -> MemorySegment.ofArray(new int[] { 1, 2, 3, 4 }),
250                 () -> MemorySegment.ofArray(new long[] { 1L, 2L, 3L, 4L } ),
251                 () -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } ),
252                 () -> Arena.ofAuto().allocate(4L, 1),
253                 () -> Arena.ofAuto().allocate(4L, 8),
254                 () -> Arena.ofAuto().allocate(JAVA_INT),
255                 () -> Arena.ofAuto().allocate(4L, 1),
256                 () -> Arena.ofAuto().allocate(4L, 8),
257                 () -> Arena.ofAuto().allocate(JAVA_INT)
258 
259         );
260         return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
261     }
262 
263     @Test(dataProvider = "segmentFactories")
264     public void testFill(Supplier<MemorySegment> segmentSupplier) {
265         VarHandle byteHandle = ValueLayout.JAVA_BYTE.varHandle();
266 
267         for (byte value : new byte[] {(byte) 0xFF, (byte) 0x00, (byte) 0x45}) {
268             MemorySegment segment = segmentSupplier.get();
269             segment.fill(value);
270             for (long l = 0; l < segment.byteSize(); l++) {
271                 assertEquals((byte) byteHandle.get(segment, l), value);
272             }
273 
274             // fill a slice
275             var sliceSegment = segment.asSlice(1, segment.byteSize() - 2).fill((byte) ~value);
276             for (long l = 0; l < sliceSegment.byteSize(); l++) {
277                 assertEquals((byte) byteHandle.get(sliceSegment, l), ~value);
278             }
279             // assert enclosing slice
280             assertEquals((byte) byteHandle.get(segment, 0L), value);
281             for (long l = 1; l < segment.byteSize() - 2; l++) {
282                 assertEquals((byte) byteHandle.get(segment, l), (byte) ~value);
283             }
284             assertEquals((byte) byteHandle.get(segment, segment.byteSize() - 1L), value);
285         }
< prev index next >