Path: blob/master/test/jdk/java/foreign/TestMemoryCopy.java
41144 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @run testng TestMemoryCopy27*/2829import jdk.incubator.foreign.MemoryLayouts;30import jdk.incubator.foreign.MemorySegment;31import jdk.incubator.foreign.ResourceScope;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435import java.lang.invoke.VarHandle;36import java.util.ArrayList;37import java.util.List;38import java.util.function.IntFunction;3940import static org.testng.Assert.*;4142public class TestMemoryCopy {4344final static VarHandle BYTE_HANDLE = MemoryLayouts.JAVA_BYTE.varHandle(byte.class);4546@Test(dataProvider = "slices")47public void testCopy(SegmentSlice s1, SegmentSlice s2) {48int size = Math.min(s1.size(), s2.size());49//prepare source and target segments50for (int i = 0 ; i < size ; i++) {51BYTE_HANDLE.set(s2.segment.asSlice(i), (byte)0);52}53for (int i = 0 ; i < size ; i++) {54BYTE_HANDLE.set(s1.segment.asSlice(i), (byte) i);55}56//perform copy57s2.segment.copyFrom(s1.segment.asSlice(0, size));58//check that copy actually worked59for (int i = 0 ; i < size ; i++) {60assertEquals((byte)i, BYTE_HANDLE.get(s2.segment.asSlice(i)));61}62}6364static class SegmentSlice {6566enum Kind {67NATIVE(i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope())),68ARRAY(i -> MemorySegment.ofArray(new byte[i]));6970final IntFunction<MemorySegment> segmentFactory;7172Kind(IntFunction<MemorySegment> segmentFactory) {73this.segmentFactory = segmentFactory;74}7576MemorySegment makeSegment(int elems) {77return segmentFactory.apply(elems);78}79}8081final Kind kind;82final int first;83final int last;84final MemorySegment segment;8586public SegmentSlice(Kind kind, int first, int last, MemorySegment segment) {87this.kind = kind;88this.first = first;89this.last = last;90this.segment = segment;91}9293int size() {94return last - first + 1;95}96}9798@DataProvider(name = "slices")99static Object[][] slices() {100int[] sizes = { 16, 8, 4, 2, 1 };101List<SegmentSlice> slices = new ArrayList<>();102for (SegmentSlice.Kind kind : SegmentSlice.Kind.values()) {103MemorySegment segment = kind.makeSegment(16);104//compute all slices105for (int size : sizes) {106for (int index = 0 ; index < 16 ; index += size) {107MemorySegment slice = segment.asSlice(index, size);108slices.add(new SegmentSlice(kind, index, index + size - 1, slice));109}110}111}112Object[][] sliceArray = new Object[slices.size() * slices.size()][];113for (int i = 0 ; i < slices.size() ; i++) {114for (int j = 0 ; j < slices.size() ; j++) {115sliceArray[i * slices.size() + j] = new Object[] { slices.get(i), slices.get(j) };116}117}118return sliceArray;119}120}121122123