< prev index next >

test/jdk/java/foreign/TestMemoryCopy.java

Print this page
rev 57245 : [mq]: 8234049_v4


  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 /*
  26  * @test
  27  * @run testng TestMemoryCopy
  28  */
  29 
  30 import jdk.incubator.foreign.MemoryAddress;


  31 import jdk.incubator.foreign.MemorySegment;
  32 import org.testng.annotations.DataProvider;
  33 import org.testng.annotations.Test;
  34 

  35 import java.util.ArrayList;
  36 import java.util.Collections;
  37 import java.util.List;
  38 import java.util.function.IntFunction;
  39 import java.util.stream.Collectors;
  40 import java.util.stream.IntStream;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class TestMemoryCopy {
  45 


  46     @Test(dataProvider = "slices")
  47     public void testBadCopy(SegmentSlice s1, SegmentSlice s2) {
  48         MemoryAddress addr1 = s1.segment.baseAddress();
  49         MemoryAddress addr2 = s2.segment.baseAddress();
  50         int size = Math.min(s1.size(), s2.size());
  51         boolean overlap = SegmentSlice.overlap(s1, s2, size);





  52         try {
  53             MemoryAddress.copy(addr1, addr2, size);
  54             assertFalse(overlap);





  55         } catch (IllegalArgumentException ex) {
  56             assertTrue(overlap);
  57         }
  58     }
  59 
  60     static class SegmentSlice {
  61 
  62         enum Kind {
  63             NATIVE(MemorySegment::allocateNative),
  64             ARRAY(i -> MemorySegment.ofArray(new byte[i]));
  65 
  66             final IntFunction<MemorySegment> segmentFactory;
  67 
  68             Kind(IntFunction<MemorySegment> segmentFactory) {
  69                 this.segmentFactory = segmentFactory;
  70             }
  71 
  72             MemorySegment makeSegment(int elems) {
  73                 return segmentFactory.apply(elems);
  74             }




  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 /*
  26  * @test
  27  * @run testng TestMemoryCopy
  28  */
  29 
  30 import jdk.incubator.foreign.MemoryAddress;
  31 import jdk.incubator.foreign.MemoryHandles;
  32 import jdk.incubator.foreign.MemoryLayouts;
  33 import jdk.incubator.foreign.MemorySegment;
  34 import org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.util.function.IntFunction;
  42 import java.util.stream.Collectors;
  43 import java.util.stream.IntStream;
  44 
  45 import static org.testng.Assert.*;
  46 
  47 public class TestMemoryCopy {
  48 
  49     final static VarHandle BYTE_HANDLE = MemoryLayouts.JAVA_BYTE.varHandle(byte.class);
  50 
  51     @Test(dataProvider = "slices")
  52     public void testCopy(SegmentSlice s1, SegmentSlice s2) {
  53         MemoryAddress addr1 = s1.segment.baseAddress();
  54         MemoryAddress addr2 = s2.segment.baseAddress();
  55         int size = Math.min(s1.size(), s2.size());
  56         boolean overlap = SegmentSlice.overlap(s1, s2, size);
  57         //prepare source and target segments
  58         for (int i = 0 ; i < size ; i++) {
  59             BYTE_HANDLE.set(addr1.offset(i), (byte)i);
  60             BYTE_HANDLE.set(addr2.offset(i), (byte)0);
  61         }
  62         try {
  63             MemoryAddress.copy(addr1, addr2, size);
  64             assertFalse(overlap);
  65             //check that copy actually worked
  66             for (int i = 0 ; i < size ; i++) {
  67                 assertEquals((byte)i, BYTE_HANDLE.get(addr2.offset(i)));
  68             }
  69 
  70         } catch (IllegalArgumentException ex) {
  71             assertTrue(overlap);
  72         }
  73     }
  74 
  75     static class SegmentSlice {
  76 
  77         enum Kind {
  78             NATIVE(MemorySegment::allocateNative),
  79             ARRAY(i -> MemorySegment.ofArray(new byte[i]));
  80 
  81             final IntFunction<MemorySegment> segmentFactory;
  82 
  83             Kind(IntFunction<MemorySegment> segmentFactory) {
  84                 this.segmentFactory = segmentFactory;
  85             }
  86 
  87             MemorySegment makeSegment(int elems) {
  88                 return segmentFactory.apply(elems);
  89             }


< prev index next >