< prev index next >

test/jdk/java/foreign/TestArrayCopy.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  * @run testng TestArrayCopy
 28  */
 29 

 30 import java.lang.foreign.MemorySegment;
 31 import java.lang.foreign.ValueLayout;



 32 import java.lang.invoke.VarHandle;
 33 import java.nio.ByteOrder;
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import org.testng.annotations.DataProvider;
 37 import org.testng.annotations.Test;
 38 
 39 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 40 import static java.lang.foreign.ValueLayout.JAVA_INT;
 41 import static org.testng.Assert.assertEquals;
 42 import static org.testng.Assert.fail;
 43 
 44 /**
 45  * These tests exercise the MemoryCopy copyFromArray(...) and copyToArray(...).
 46  * To make these tests more challenging the segment is a view of the given array,
 47  * which makes the copy operations overlapping self-copies.  Thus, this checks the claim:
 48  *
 49  * <p>If the source (destination) segment is actually a view of the destination (source) array,
 50  * and if the copy region of the source overlaps with the copy region of the destination,
 51  * the copy of the overlapping region is performed as if the data in the overlapping region

271         MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
272         MemorySegment.copy(new byte[] { 1, 2, 3, 4 }, 0, segment, JAVA_BYTE.withByteAlignment(2), 0, 4);
273     }
274 
275     @Test(expectedExceptions = IllegalArgumentException.class)
276     public void testHyperAlignedDst() {
277         MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
278         MemorySegment.copy(segment, JAVA_BYTE.withByteAlignment(2), 0, new byte[] { 1, 2, 3, 4 }, 0, 4);
279     }
280 
281     /***** Utilities *****/
282 
283     public static MemorySegment srcSegment(int bytesLength) {
284         byte[] arr = new byte[bytesLength];
285         for (int i = 0; i < arr.length; i++) {
286             arr[i] = (byte)i;
287         }
288         return MemorySegment.ofArray(arr);
289     }
290 





291     public static MemorySegment truthSegment(MemorySegment srcSeg, CopyHelper<?, ?> helper, int indexShifts, CopyMode mode) {
292         VarHandle indexedHandleNO = helper.elementLayout.withOrder(NATIVE_ORDER).arrayElementVarHandle();
293         VarHandle indexedHandleNNO = helper.elementLayout.withOrder(NON_NATIVE_ORDER).arrayElementVarHandle();
294         MemorySegment dstSeg = MemorySegment.ofArray(srcSeg.toArray(JAVA_BYTE));
295         int indexLength = (int) dstSeg.byteSize() / (int)helper.elementLayout.byteSize();
296         if (mode.direction) {
297             if (mode.swap) {
298                 for (int i = indexLength - 1; i >= indexShifts; i--) {
299                     Object v = indexedHandleNNO.get(dstSeg, i - indexShifts);
300                     indexedHandleNO.set(dstSeg, i, v);
301                 }
302             } else {
303                 for (int i = indexLength - 1; i >= indexShifts; i--) {
304                     Object v = indexedHandleNO.get(dstSeg, i - indexShifts);
305                     indexedHandleNO.set(dstSeg, i, v);
306                 }
307             }
308         } else { //down
309             if (mode.swap) {
310                 for (int i = indexShifts; i < indexLength; i++) {
311                     Object v = indexedHandleNNO.get(dstSeg, i);
312                     indexedHandleNO.set(dstSeg, i - indexShifts, v);
313                 }

  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  * @run testng TestArrayCopy
 27  */
 28 
 29 import java.lang.foreign.MemoryLayout;
 30 import java.lang.foreign.MemorySegment;
 31 import java.lang.foreign.ValueLayout;
 32 import java.lang.invoke.MethodHandle;
 33 import java.lang.invoke.MethodHandles;
 34 import java.lang.invoke.MethodType;
 35 import java.lang.invoke.VarHandle;
 36 import java.nio.ByteOrder;
 37 import java.util.ArrayList;
 38 import java.util.List;
 39 import org.testng.annotations.DataProvider;
 40 import org.testng.annotations.Test;
 41 
 42 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 43 import static java.lang.foreign.ValueLayout.JAVA_INT;
 44 import static org.testng.Assert.assertEquals;
 45 import static org.testng.Assert.fail;
 46 
 47 /**
 48  * These tests exercise the MemoryCopy copyFromArray(...) and copyToArray(...).
 49  * To make these tests more challenging the segment is a view of the given array,
 50  * which makes the copy operations overlapping self-copies.  Thus, this checks the claim:
 51  *
 52  * <p>If the source (destination) segment is actually a view of the destination (source) array,
 53  * and if the copy region of the source overlaps with the copy region of the destination,
 54  * the copy of the overlapping region is performed as if the data in the overlapping region

274         MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
275         MemorySegment.copy(new byte[] { 1, 2, 3, 4 }, 0, segment, JAVA_BYTE.withByteAlignment(2), 0, 4);
276     }
277 
278     @Test(expectedExceptions = IllegalArgumentException.class)
279     public void testHyperAlignedDst() {
280         MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
281         MemorySegment.copy(segment, JAVA_BYTE.withByteAlignment(2), 0, new byte[] { 1, 2, 3, 4 }, 0, 4);
282     }
283 
284     /***** Utilities *****/
285 
286     public static MemorySegment srcSegment(int bytesLength) {
287         byte[] arr = new byte[bytesLength];
288         for (int i = 0; i < arr.length; i++) {
289             arr[i] = (byte)i;
290         }
291         return MemorySegment.ofArray(arr);
292     }
293 
294     private static VarHandle arrayVarHandle(ValueLayout layout) {
295         return MethodHandles.collectCoordinates(layout.varHandle(),
296                 1, MethodHandles.insertArguments(layout.scaleHandle(), 0, 0L));
297     }
298 
299     public static MemorySegment truthSegment(MemorySegment srcSeg, CopyHelper<?, ?> helper, int indexShifts, CopyMode mode) {
300         VarHandle indexedHandleNO = arrayVarHandle(helper.elementLayout.withOrder(NATIVE_ORDER));
301         VarHandle indexedHandleNNO = arrayVarHandle(helper.elementLayout.withOrder(NON_NATIVE_ORDER));
302         MemorySegment dstSeg = MemorySegment.ofArray(srcSeg.toArray(JAVA_BYTE));
303         int indexLength = (int) dstSeg.byteSize() / (int)helper.elementLayout.byteSize();
304         if (mode.direction) {
305             if (mode.swap) {
306                 for (int i = indexLength - 1; i >= indexShifts; i--) {
307                     Object v = indexedHandleNNO.get(dstSeg, i - indexShifts);
308                     indexedHandleNO.set(dstSeg, i, v);
309                 }
310             } else {
311                 for (int i = indexLength - 1; i >= indexShifts; i--) {
312                     Object v = indexedHandleNO.get(dstSeg, i - indexShifts);
313                     indexedHandleNO.set(dstSeg, i, v);
314                 }
315             }
316         } else { //down
317             if (mode.swap) {
318                 for (int i = indexShifts; i < indexLength; i++) {
319                     Object v = indexedHandleNNO.get(dstSeg, i);
320                     indexedHandleNO.set(dstSeg, i - indexShifts, v);
321                 }
< prev index next >