File: TestArrayCopy.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (566 lines) | stat: -rw-r--r-- 24,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
 * Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
 * 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
 * @enablePreview
 * @run testng TestArrayCopy
 */

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static java.lang.foreign.ValueLayout.JAVA_BYTE;
import static java.lang.foreign.ValueLayout.JAVA_INT;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

/**
 * These tests exercise the MemoryCopy copyFromArray(...) and copyToArray(...).
 * To make these tests more challenging the segment is a view of the given array,
 * which makes the copy operations overlapping self-copies.  Thus, this checks the claim:
 *
 * <p>If the source (destination) segment is actually a view of the destination (source) array,
 * and if the copy region of the source overlaps with the copy region of the destination,
 * the copy of the overlapping region is performed as if the data in the overlapping region
 * were first copied into a temporary segment before being copied to the destination.</p>
 */
public class TestArrayCopy {
    private static final ByteOrder NATIVE_ORDER = ByteOrder.nativeOrder();
    private static final ByteOrder NON_NATIVE_ORDER = NATIVE_ORDER == ByteOrder.LITTLE_ENDIAN
            ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;

    private static final int SEG_LENGTH_BYTES = 32;
    private static final int SEG_OFFSET_BYTES = 8;

    @Test(dataProvider = "copyModesAndHelpers")
    public void testSelfCopy(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        int indexShifts = SEG_OFFSET_BYTES / bytesPerElement;
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        MemorySegment truth = truthSegment(base, helper, indexShifts, mode);
        ByteOrder bo = mode.swap ? NON_NATIVE_ORDER : NATIVE_ORDER;
        //CopyFrom
        Object srcArr = helper.toArray(base);
        int srcIndex = mode.direction ? 0 : indexShifts;
        int srcCopyLen = helper.length(srcArr) - indexShifts;
        MemorySegment dstSeg = helper.fromArray(srcArr);
        long dstOffsetBytes = mode.direction ? SEG_OFFSET_BYTES : 0;
        helper.copyFromArray(srcArr, srcIndex, srcCopyLen, dstSeg, dstOffsetBytes, bo);
        assertEquals(truth.mismatch(dstSeg), -1);
        //CopyTo
        long srcOffsetBytes = mode.direction ? 0 : SEG_OFFSET_BYTES;
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        int dstIndex = mode.direction ? indexShifts : 0;
        int dstCopyLen = helper.length(dstArr) - indexShifts;
        helper.copyToArray(srcSeg, srcOffsetBytes, dstArr, dstIndex, dstCopyLen, bo);
        MemorySegment result = helper.fromArray(dstArr);
        assertEquals(truth.mismatch(result), -1);
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testUnalignedCopy(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        int indexShifts = SEG_OFFSET_BYTES / bytesPerElement;
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        ByteOrder bo = mode.swap ? NON_NATIVE_ORDER : NATIVE_ORDER;
        //CopyFrom
        Object srcArr = helper.toArray(base);
        int srcIndex = mode.direction ? 0 : indexShifts;
        int srcCopyLen = helper.length(srcArr) - indexShifts;
        MemorySegment dstSeg = helper.fromArray(srcArr);
        long dstOffsetBytes = mode.direction ? (SEG_OFFSET_BYTES - 1) : 0;
        helper.copyFromArray(srcArr, srcIndex, srcCopyLen, dstSeg, dstOffsetBytes, bo);
        //CopyTo
        long srcOffsetBytes = mode.direction ? 0 : (SEG_OFFSET_BYTES - 1);
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        int dstIndex = mode.direction ? indexShifts : 0;
        int dstCopyLen = helper.length(dstArr) - indexShifts;
        helper.copyToArray(srcSeg, srcOffsetBytes, dstArr, dstIndex, dstCopyLen, bo);
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testCopyOobLength(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        //CopyFrom
        Object srcArr = helper.toArray(base);
        MemorySegment dstSeg = helper.fromArray(srcArr);
        try {
            helper.copyFromArray(srcArr, 0, (SEG_LENGTH_BYTES / bytesPerElement) * 2, dstSeg, 0, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
        //CopyTo
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        try {
            helper.copyToArray(srcSeg, 0, dstArr, 0, (SEG_LENGTH_BYTES / bytesPerElement) * 2, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testCopyNegativeIndices(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        //CopyFrom
        Object srcArr = helper.toArray(base);
        MemorySegment dstSeg = helper.fromArray(srcArr);
        try {
            helper.copyFromArray(srcArr, -1, SEG_LENGTH_BYTES / bytesPerElement, dstSeg, 0, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
        //CopyTo
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        try {
            helper.copyToArray(srcSeg, 0, dstArr, -1, SEG_LENGTH_BYTES / bytesPerElement, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testCopyNegativeOffsets(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        //CopyFrom
        Object srcArr = helper.toArray(base);
        MemorySegment dstSeg = helper.fromArray(srcArr);
        try {
            helper.copyFromArray(srcArr, 0, SEG_LENGTH_BYTES / bytesPerElement, dstSeg, -1, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
        //CopyTo
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        try {
            helper.copyToArray(srcSeg, -1, dstArr, 0, SEG_LENGTH_BYTES / bytesPerElement, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testCopyOobIndices(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        //CopyFrom
        Object srcArr = helper.toArray(base);
        MemorySegment dstSeg = helper.fromArray(srcArr);
        try {
            helper.copyFromArray(srcArr, helper.length(srcArr) + 1, SEG_LENGTH_BYTES / bytesPerElement, dstSeg, 0, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
        //CopyTo
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        try {
            helper.copyToArray(srcSeg, 0, dstArr, helper.length(dstArr) + 1, SEG_LENGTH_BYTES / bytesPerElement, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testCopyOobOffsets(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        //CopyFrom
        Object srcArr = helper.toArray(base);
        MemorySegment dstSeg = helper.fromArray(srcArr);
        try {
            helper.copyFromArray(srcArr, 0, SEG_LENGTH_BYTES / bytesPerElement, dstSeg, SEG_LENGTH_BYTES + 1, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
        //CopyTo
        Object dstArr = helper.toArray(base);
        MemorySegment srcSeg = helper.fromArray(dstArr).asReadOnly();
        try {
            helper.copyToArray(srcSeg, SEG_OFFSET_BYTES + 1, dstArr, 0, SEG_LENGTH_BYTES / bytesPerElement, ByteOrder.nativeOrder());
            fail();
        } catch (IndexOutOfBoundsException ex) {
            //ok
        }
    }

    @Test(dataProvider = "copyModesAndHelpers")
    public void testCopyReadOnlyDest(CopyMode mode, CopyHelper<Object, ValueLayout> helper, String helperDebugString) {
        int bytesPerElement = (int)helper.elementLayout.byteSize();
        MemorySegment base = srcSegment(SEG_LENGTH_BYTES);
        //CopyFrom
        Object srcArr = helper.toArray(base);
        MemorySegment dstSeg = helper.fromArray(srcArr).asReadOnly();
        try {
            helper.copyFromArray(srcArr, 0, SEG_LENGTH_BYTES / bytesPerElement, dstSeg, 0, ByteOrder.nativeOrder());
            fail();
        } catch (UnsupportedOperationException ex) {
            //ok
        }
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testNotAnArraySrc() {
        MemorySegment segment = MemorySegment.ofArray(new int[] {1, 2, 3, 4});
        MemorySegment.copy(segment, JAVA_BYTE, 0, new String[] { "hello" }, 0, 4);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testNotAnArrayDst() {
        MemorySegment segment = MemorySegment.ofArray(new int[] {1, 2, 3, 4});
        MemorySegment.copy(new String[] { "hello" }, 0, segment, JAVA_BYTE, 0, 4);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testCarrierMismatchSrc() {
        MemorySegment segment = MemorySegment.ofArray(new int[] {1, 2, 3, 4});
        MemorySegment.copy(segment, JAVA_INT, 0, new byte[] { 1, 2, 3, 4 }, 0, 4);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testCarrierMismatchDst() {
        MemorySegment segment = MemorySegment.ofArray(new int[] {1, 2, 3, 4});
        MemorySegment.copy(new byte[] { 1, 2, 3, 4 }, 0, segment, JAVA_INT, 0, 4);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testHyperAlignedSrc() {
        MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
        MemorySegment.copy(new byte[] { 1, 2, 3, 4 }, 0, segment, JAVA_BYTE.withByteAlignment(2), 0, 4);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testHyperAlignedDst() {
        MemorySegment segment = MemorySegment.ofArray(new byte[] {1, 2, 3, 4});
        MemorySegment.copy(segment, JAVA_BYTE.withByteAlignment(2), 0, new byte[] { 1, 2, 3, 4 }, 0, 4);
    }

    /***** Utilities *****/

    public static MemorySegment srcSegment(int bytesLength) {
        byte[] arr = new byte[bytesLength];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (byte)i;
        }
        return MemorySegment.ofArray(arr);
    }

    public static MemorySegment truthSegment(MemorySegment srcSeg, CopyHelper<?, ?> helper, int indexShifts, CopyMode mode) {
        VarHandle indexedHandleNO = helper.elementLayout.withOrder(NATIVE_ORDER).arrayElementVarHandle();
        VarHandle indexedHandleNNO = helper.elementLayout.withOrder(NON_NATIVE_ORDER).arrayElementVarHandle();
        MemorySegment dstSeg = MemorySegment.ofArray(srcSeg.toArray(JAVA_BYTE));
        int indexLength = (int) dstSeg.byteSize() / (int)helper.elementLayout.byteSize();
        if (mode.direction) {
            if (mode.swap) {
                for (int i = indexLength - 1; i >= indexShifts; i--) {
                    Object v = indexedHandleNNO.get(dstSeg, i - indexShifts);
                    indexedHandleNO.set(dstSeg, i, v);
                }
            } else {
                for (int i = indexLength - 1; i >= indexShifts; i--) {
                    Object v = indexedHandleNO.get(dstSeg, i - indexShifts);
                    indexedHandleNO.set(dstSeg, i, v);
                }
            }
        } else { //down
            if (mode.swap) {
                for (int i = indexShifts; i < indexLength; i++) {
                    Object v = indexedHandleNNO.get(dstSeg, i);
                    indexedHandleNO.set(dstSeg, i - indexShifts, v);
                }
            } else {
                for (int i = indexShifts; i < indexLength; i++) {
                    Object v = indexedHandleNO.get(dstSeg, i);
                    indexedHandleNO.set(dstSeg, i - indexShifts, v);
                }
            }
        }
        return dstSeg;
    }

    enum CopyMode {
        UP_NO_SWAP(true, false),
        UP_SWAP(true, true),
        DOWN_NO_SWAP(false, false),
        DOWN_SWAP(false, true);

        final boolean direction;
        final boolean swap;

        CopyMode(boolean direction, boolean swap) {
            this.direction = direction;
            this.swap = swap;
        }
    }

    abstract static class CopyHelper<X, L extends ValueLayout> {

        final L elementLayout;
        final Class<?> carrier;

        @SuppressWarnings("unchecked")
        public CopyHelper(L elementLayout, Class<X> carrier) {
            this.elementLayout = (L)elementLayout.withByteAlignment(1);
            this.carrier = carrier;
        }

        abstract void copyFromArray(X srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo);
        abstract void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, X dstArr, int dstIndex, int dstCopyLen, ByteOrder bo);
        abstract X toArray(MemorySegment segment);
        abstract MemorySegment fromArray(X array);
        abstract int length(X arr);

        @Override
        public String toString() {
            return "CopyHelper{" +
                    "elementLayout=" + elementLayout +
                    ", carrier=" + carrier.getName() +
                    '}';
        }

        static final CopyHelper<byte[], ValueLayout.OfByte> BYTE = new CopyHelper<>(JAVA_BYTE, byte[].class) {
            @Override
            void copyFromArray(byte[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, byte[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            byte[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(byte[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(byte[] arr) {
                return arr.length;
            }
        };

        static final CopyHelper<char[], ValueLayout.OfChar> CHAR = new CopyHelper<>(ValueLayout.JAVA_CHAR, char[].class) {
            @Override
            void copyFromArray(char[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, char[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            char[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(char[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(char[] arr) {
                return arr.length;
            }
        };

        static final CopyHelper<short[], ValueLayout.OfShort> SHORT = new CopyHelper<>(ValueLayout.JAVA_SHORT, short[].class) {
            @Override
            void copyFromArray(short[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, short[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            short[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(short[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(short[] arr) {
                return arr.length;
            }
        };

        static final CopyHelper<int[], ValueLayout.OfInt> INT = new CopyHelper<>(ValueLayout.JAVA_INT, int[].class) {
            @Override
            void copyFromArray(int[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, int[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            int[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(int[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(int[] arr) {
                return arr.length;
            }
        };

        static final CopyHelper<float[], ValueLayout.OfFloat> FLOAT = new CopyHelper<>(ValueLayout.JAVA_FLOAT, float[].class) {
            @Override
            void copyFromArray(float[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, float[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            float[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(float[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(float[] arr) {
                return arr.length;
            }
        };

        static final CopyHelper<long[], ValueLayout.OfLong> LONG = new CopyHelper<>(ValueLayout.JAVA_LONG, long[].class) {
            @Override
            void copyFromArray(long[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, long[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            long[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(long[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(long[] arr) {
                return arr.length;
            }
        };

        static final CopyHelper<double[], ValueLayout.OfDouble> DOUBLE = new CopyHelper<>(ValueLayout.JAVA_DOUBLE, double[].class) {
            @Override
            void copyFromArray(double[] srcArr, int srcIndex, int srcCopyLen, MemorySegment dstSeg, long dstOffsetBytes, ByteOrder bo) {
                MemorySegment.copy(srcArr, srcIndex, dstSeg, elementLayout.withOrder(bo), dstOffsetBytes, srcCopyLen);
            }

            @Override
            void copyToArray(MemorySegment srcSeg, long srcOffsetBytes, double[] dstArr, int dstIndex, int dstCopyLen, ByteOrder bo) {
                MemorySegment.copy(srcSeg, elementLayout.withOrder(bo), srcOffsetBytes, dstArr, dstIndex, dstCopyLen);
            }

            @Override
            double[] toArray(MemorySegment segment) {
                return segment.toArray(elementLayout);
            }

            @Override
            MemorySegment fromArray(double[] array) {
                return MemorySegment.ofArray(array);
            }

            @Override
            int length(double[] arr) {
                return arr.length;
            }
        };
    }

    @DataProvider
    Object[][] copyModesAndHelpers() {
        CopyHelper<?, ?>[] helpers = { CopyHelper.BYTE, CopyHelper.CHAR, CopyHelper.SHORT, CopyHelper.INT,
                                    CopyHelper.FLOAT, CopyHelper.LONG, CopyHelper.DOUBLE };
        List<Object[]> results = new ArrayList<>();
        for (CopyHelper<?, ?> helper : helpers) {
            for (CopyMode mode : CopyMode.values()) {
                results.add(new Object[] { mode, helper, helper.toString() });
            }
        }
        return results.stream().toArray(Object[][]::new);
    }
}