Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/foreign/TestMemoryCopy.java
41144 views
1
/*
2
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
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
/*
26
* @test
27
* @run testng TestMemoryCopy
28
*/
29
30
import jdk.incubator.foreign.MemoryLayouts;
31
import jdk.incubator.foreign.MemorySegment;
32
import jdk.incubator.foreign.ResourceScope;
33
import org.testng.annotations.DataProvider;
34
import org.testng.annotations.Test;
35
36
import java.lang.invoke.VarHandle;
37
import java.util.ArrayList;
38
import java.util.List;
39
import java.util.function.IntFunction;
40
41
import static org.testng.Assert.*;
42
43
public class TestMemoryCopy {
44
45
final static VarHandle BYTE_HANDLE = MemoryLayouts.JAVA_BYTE.varHandle(byte.class);
46
47
@Test(dataProvider = "slices")
48
public void testCopy(SegmentSlice s1, SegmentSlice s2) {
49
int size = Math.min(s1.size(), s2.size());
50
//prepare source and target segments
51
for (int i = 0 ; i < size ; i++) {
52
BYTE_HANDLE.set(s2.segment.asSlice(i), (byte)0);
53
}
54
for (int i = 0 ; i < size ; i++) {
55
BYTE_HANDLE.set(s1.segment.asSlice(i), (byte) i);
56
}
57
//perform copy
58
s2.segment.copyFrom(s1.segment.asSlice(0, size));
59
//check that copy actually worked
60
for (int i = 0 ; i < size ; i++) {
61
assertEquals((byte)i, BYTE_HANDLE.get(s2.segment.asSlice(i)));
62
}
63
}
64
65
static class SegmentSlice {
66
67
enum Kind {
68
NATIVE(i -> MemorySegment.allocateNative(i, ResourceScope.newImplicitScope())),
69
ARRAY(i -> MemorySegment.ofArray(new byte[i]));
70
71
final IntFunction<MemorySegment> segmentFactory;
72
73
Kind(IntFunction<MemorySegment> segmentFactory) {
74
this.segmentFactory = segmentFactory;
75
}
76
77
MemorySegment makeSegment(int elems) {
78
return segmentFactory.apply(elems);
79
}
80
}
81
82
final Kind kind;
83
final int first;
84
final int last;
85
final MemorySegment segment;
86
87
public SegmentSlice(Kind kind, int first, int last, MemorySegment segment) {
88
this.kind = kind;
89
this.first = first;
90
this.last = last;
91
this.segment = segment;
92
}
93
94
int size() {
95
return last - first + 1;
96
}
97
}
98
99
@DataProvider(name = "slices")
100
static Object[][] slices() {
101
int[] sizes = { 16, 8, 4, 2, 1 };
102
List<SegmentSlice> slices = new ArrayList<>();
103
for (SegmentSlice.Kind kind : SegmentSlice.Kind.values()) {
104
MemorySegment segment = kind.makeSegment(16);
105
//compute all slices
106
for (int size : sizes) {
107
for (int index = 0 ; index < 16 ; index += size) {
108
MemorySegment slice = segment.asSlice(index, size);
109
slices.add(new SegmentSlice(kind, index, index + size - 1, slice));
110
}
111
}
112
}
113
Object[][] sliceArray = new Object[slices.size() * slices.size()][];
114
for (int i = 0 ; i < slices.size() ; i++) {
115
for (int j = 0 ; j < slices.size() ; j++) {
116
sliceArray[i * slices.size() + j] = new Object[] { slices.get(i), slices.get(j) };
117
}
118
}
119
return sliceArray;
120
}
121
}
122
123