2021-11-24 11:51:16 +00:00
|
|
|
/*
|
2023-04-27 09:00:58 +00:00
|
|
|
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
|
2021-11-24 11:51:16 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @run testng TestSegmentCopy
|
|
|
|
*/
|
|
|
|
|
2023-04-27 09:00:58 +00:00
|
|
|
import java.lang.foreign.Arena;
|
2022-05-12 16:17:45 +00:00
|
|
|
import java.lang.foreign.MemorySegment;
|
|
|
|
import java.lang.foreign.ValueLayout;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
2021-11-24 11:51:16 +00:00
|
|
|
import java.lang.invoke.VarHandle;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.util.ArrayList;
|
2023-06-12 15:55:49 +00:00
|
|
|
import java.util.Arrays;
|
2021-11-24 11:51:16 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.IntFunction;
|
|
|
|
|
2023-06-12 15:55:49 +00:00
|
|
|
import org.testng.SkipException;
|
2022-05-12 16:17:45 +00:00
|
|
|
import org.testng.annotations.DataProvider;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
|
|
|
|
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
|
2021-11-24 11:51:16 +00:00
|
|
|
import static org.testng.Assert.*;
|
|
|
|
|
|
|
|
public class TestSegmentCopy {
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
static final int TEST_BYTE_SIZE = 16;
|
|
|
|
|
|
|
|
@Test(dataProvider = "segmentKinds")
|
|
|
|
public void testByteCopy(SegmentKind kind1, SegmentKind kind2) {
|
|
|
|
MemorySegment s1 = kind1.makeSegment(TEST_BYTE_SIZE);
|
|
|
|
MemorySegment s2 = kind2.makeSegment(TEST_BYTE_SIZE);
|
|
|
|
|
|
|
|
// for all offsets
|
|
|
|
for (int s1Offset = 0; s1Offset < s1.byteSize(); s1Offset++) {
|
|
|
|
for (int s2Offset = 0; s2Offset < s2.byteSize(); s2Offset++) {
|
|
|
|
long slice1ByteSize = s1.byteSize() - s1Offset;
|
|
|
|
long slice2ByteSize = s2.byteSize() - s2Offset;
|
|
|
|
|
|
|
|
long copySize = Math.min(slice1ByteSize, slice2ByteSize);
|
|
|
|
|
|
|
|
//prepare source slice
|
|
|
|
for (int i = 0 ; i < copySize; i++) {
|
|
|
|
Type.BYTE.set(s1, s1Offset, i, i);
|
|
|
|
}
|
|
|
|
//perform copy
|
|
|
|
MemorySegment.copy(s1, Type.BYTE.layout, s1Offset, s2, Type.BYTE.layout, s2Offset, copySize);
|
|
|
|
//check that copy actually worked
|
|
|
|
for (int i = 0; i < copySize; i++) {
|
|
|
|
Type.BYTE.check(s2, s2Offset, i, i);
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 07:52:31 +00:00
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "segmentKinds")
|
2023-06-12 15:55:49 +00:00
|
|
|
public void testReadOnlyCopy(SegmentKind kind1, SegmentKind kind2) {
|
|
|
|
MemorySegment s1 = kind1.makeSegment(TEST_BYTE_SIZE);
|
|
|
|
MemorySegment s2 = kind2.makeSegment(TEST_BYTE_SIZE);
|
|
|
|
// check failure with read-only dest
|
|
|
|
MemorySegment.copy(s1, Type.BYTE.layout, 0, s2.asReadOnly(), Type.BYTE.layout, 0, 0);
|
|
|
|
}
|
|
|
|
|
2023-12-11 07:52:31 +00:00
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class,
|
|
|
|
expectedExceptionsMessageRegExp = ".*Attempt to write a read-only segment.*")
|
|
|
|
public void badCopy6Arg() {
|
|
|
|
try (Arena scope = Arena.ofConfined()) {
|
|
|
|
MemorySegment dest = scope.allocate(ValueLayout.JAVA_INT).asReadOnly();
|
|
|
|
MemorySegment.copy(new int[1],0, dest, ValueLayout.JAVA_INT, 0 ,1); // should throw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:55:49 +00:00
|
|
|
@Test(expectedExceptions = IndexOutOfBoundsException.class, dataProvider = "types")
|
|
|
|
public void testBadOverflow(Type type) {
|
|
|
|
if (type.layout.byteSize() > 1) {
|
|
|
|
MemorySegment segment = MemorySegment.ofArray(new byte[100]);
|
|
|
|
MemorySegment.copy(segment, type.layout, 0, segment, type.layout, 0, Long.MAX_VALUE);
|
|
|
|
} else {
|
|
|
|
throw new SkipException("Byte layouts do not overflow");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
@Test(dataProvider = "segmentKindsAndTypes")
|
|
|
|
public void testElementCopy(SegmentKind kind1, SegmentKind kind2, Type type1, Type type2) {
|
|
|
|
MemorySegment s1 = kind1.makeSegment(TEST_BYTE_SIZE);
|
|
|
|
MemorySegment s2 = kind2.makeSegment(TEST_BYTE_SIZE);
|
|
|
|
|
|
|
|
// for all offsets
|
|
|
|
for (int s1Offset = 0; s1Offset < s1.byteSize(); s1Offset++) {
|
|
|
|
for (int s2Offset = 0; s2Offset < s2.byteSize(); s2Offset++) {
|
|
|
|
long slice1ByteSize = s1.byteSize() - s1Offset;
|
|
|
|
long slice2ByteSize = s2.byteSize() - s2Offset;
|
|
|
|
|
|
|
|
long slice1ElementSize = slice1ByteSize / type1.size();
|
|
|
|
long slice2ElementSize = slice2ByteSize / type2.size();
|
|
|
|
|
|
|
|
long copySize = Math.min(slice1ElementSize, slice2ElementSize);
|
|
|
|
|
|
|
|
//prepare source slice
|
|
|
|
for (int i = 0 ; i < copySize; i++) {
|
|
|
|
type1.set(s1, s1Offset, i, i);
|
|
|
|
}
|
|
|
|
//perform copy
|
|
|
|
MemorySegment.copy(s1, type1.layout, s1Offset, s2, type2.layout, s2Offset, copySize);
|
|
|
|
//check that copy actually worked
|
|
|
|
for (int i = 0; i < copySize; i++) {
|
|
|
|
type2.check(s2, s2Offset, i, i);
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 10:47:34 +00:00
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testHyperAlignedSrc() {
|
|
|
|
MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
|
2023-05-22 14:57:00 +00:00
|
|
|
MemorySegment.copy(segment, 0, segment, JAVA_BYTE.withByteAlignment(2), 0, 4);
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 10:47:34 +00:00
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testHyperAlignedDst() {
|
|
|
|
MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
|
2023-05-22 14:57:00 +00:00
|
|
|
MemorySegment.copy(segment, JAVA_BYTE.withByteAlignment(2), 0, segment, 0, 4);
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
// Byte
|
2022-05-12 16:17:45 +00:00
|
|
|
BYTE(byte.class, JAVA_BYTE, i -> (byte)i),
|
2021-11-24 11:51:16 +00:00
|
|
|
//LE
|
2023-05-02 10:42:49 +00:00
|
|
|
SHORT_LE(short.class, ValueLayout.JAVA_SHORT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i -> (short)i),
|
|
|
|
CHAR_LE(char.class, ValueLayout.JAVA_CHAR_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i -> (char)i),
|
|
|
|
INT_LE(int.class, ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i -> i),
|
|
|
|
FLOAT_LE(float.class, ValueLayout.JAVA_FLOAT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i -> (float)i),
|
|
|
|
LONG_LE(long.class, ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i -> (long)i),
|
|
|
|
DOUBLE_LE(double.class, ValueLayout.JAVA_DOUBLE_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN), i -> (double)i),
|
2021-11-24 11:51:16 +00:00
|
|
|
//BE
|
2023-05-02 10:42:49 +00:00
|
|
|
SHORT_BE(short.class, ValueLayout.JAVA_SHORT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (short)i),
|
|
|
|
CHAR_BE(char.class, ValueLayout.JAVA_CHAR_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (char)i),
|
|
|
|
INT_BE(int.class, ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> i),
|
|
|
|
FLOAT_BE(float.class, ValueLayout.JAVA_FLOAT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (float)i),
|
|
|
|
LONG_BE(long.class, ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (long)i),
|
|
|
|
DOUBLE_BE(double.class, ValueLayout.JAVA_DOUBLE_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (double)i);
|
2021-11-24 11:51:16 +00:00
|
|
|
|
|
|
|
final ValueLayout layout;
|
|
|
|
final IntFunction<Object> valueConverter;
|
|
|
|
final Class<?> carrier;
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
<Z> Type(Class<Z> carrier, ValueLayout layout, IntFunction<Z> valueConverter) {
|
|
|
|
this.carrier = carrier;
|
|
|
|
this.layout = layout;
|
|
|
|
this.valueConverter = (IntFunction<Object>)valueConverter;
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
long size() {
|
|
|
|
return layout.byteSize();
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarHandle handle() {
|
2023-10-12 19:50:08 +00:00
|
|
|
return layout.varHandle();
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
void set(MemorySegment segment, long offset, int index, int val) {
|
|
|
|
handle().set(segment, offset + (index * size()), valueConverter.apply(val));
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
void check(MemorySegment segment, long offset, int index, int val) {
|
|
|
|
assertEquals(handle().get(segment, offset + (index * size())), valueConverter.apply(val));
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
enum SegmentKind {
|
|
|
|
NATIVE(i -> Arena.ofAuto().allocate(i, 1)),
|
|
|
|
ARRAY(i -> MemorySegment.ofArray(new byte[i]));
|
2021-11-24 11:51:16 +00:00
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
final IntFunction<MemorySegment> segmentFactory;
|
2021-11-24 11:51:16 +00:00
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
SegmentKind(IntFunction<MemorySegment> segmentFactory) {
|
|
|
|
this.segmentFactory = segmentFactory;
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
MemorySegment makeSegment(int size) {
|
|
|
|
return segmentFactory.apply(size);
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
2023-05-02 10:42:49 +00:00
|
|
|
}
|
2021-11-24 11:51:16 +00:00
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
@DataProvider
|
|
|
|
static Object[][] segmentKinds() {
|
|
|
|
List<Object[]> cases = new ArrayList<>();
|
|
|
|
for (SegmentKind kind1 : SegmentKind.values()) {
|
|
|
|
for (SegmentKind kind2 : SegmentKind.values()) {
|
|
|
|
cases.add(new Object[] {kind1, kind2});
|
|
|
|
}
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
2023-05-02 10:42:49 +00:00
|
|
|
return cases.toArray(Object[][]::new);
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 15:55:49 +00:00
|
|
|
@DataProvider
|
|
|
|
static Object[][] types() {
|
|
|
|
return Arrays.stream(Type.values())
|
|
|
|
.map(t -> new Object[] { t })
|
|
|
|
.toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:42:49 +00:00
|
|
|
@DataProvider
|
|
|
|
static Object[][] segmentKindsAndTypes() {
|
|
|
|
List<Object[]> cases = new ArrayList<>();
|
|
|
|
for (Object[] segmentKinds : segmentKinds()) {
|
|
|
|
for (Type type1 : Type.values()) {
|
|
|
|
for (Type type2 : Type.values()) {
|
|
|
|
if (type1.layout.carrier() == type2.layout.carrier()) {
|
|
|
|
cases.add(new Object[]{segmentKinds[0], segmentKinds[1], type1, type2});
|
|
|
|
}
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 10:42:49 +00:00
|
|
|
return cases.toArray(Object[][]::new);
|
2021-11-24 11:51:16 +00:00
|
|
|
}
|
|
|
|
}
|