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 * @modules java.base/sun.nio.ch java.base/jdk.internal.foreign
28 * @run testng/othervm/timeout=600 --enable-native-access=ALL-UNNAMED TestByteBuffer
29 */
30
31 import java.lang.foreign.*;
32 import java.lang.foreign.MemoryLayout.PathElement;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.lang.invoke.MethodHandle;
37 import java.lang.invoke.MethodHandles;
38 import java.lang.invoke.VarHandle;
39 import java.lang.ref.Cleaner;
40 import java.lang.ref.Reference;
41 import java.lang.ref.WeakReference;
42 import java.lang.reflect.InvocationTargetException;
43 import java.lang.reflect.Method;
44 import java.lang.reflect.Modifier;
45 import java.net.URI;
46 import java.nio.Buffer;
105
106 static SequenceLayout tuples = MemoryLayout.sequenceLayout(500,
107 MemoryLayout.structLayout(
108 BB_INT.withName("index"),
109 BB_FLOAT.withName("value")
110 ));
111
112 static final SequenceLayout bytes = MemoryLayout.sequenceLayout(100, JAVA_BYTE);
113 static final SequenceLayout chars = MemoryLayout.sequenceLayout(100, BB_CHAR);
114 static final SequenceLayout shorts = MemoryLayout.sequenceLayout(100, BB_SHORT);
115 static final SequenceLayout ints = MemoryLayout.sequenceLayout(100, BB_INT);
116 static final SequenceLayout floats = MemoryLayout.sequenceLayout(100, BB_FLOAT);
117 static final SequenceLayout longs = MemoryLayout.sequenceLayout(100, BB_LONG);
118 static final SequenceLayout doubles = MemoryLayout.sequenceLayout(100, BB_DOUBLE);
119
120 static final VarHandle indexHandle = tuples.varHandle(PathElement.sequenceElement(), PathElement.groupElement("index"));
121 static final VarHandle valueHandle = tuples.varHandle(PathElement.sequenceElement(), PathElement.groupElement("value"));
122
123 static void initTuples(MemorySegment base, long count) {
124 for (long i = 0; i < count ; i++) {
125 indexHandle.set(base, i, (int)i);
126 valueHandle.set(base, i, (float)(i / 500f));
127 }
128 }
129
130 static void checkTuples(MemorySegment base, ByteBuffer bb, long count) {
131 for (long i = 0; i < count ; i++) {
132 int index;
133 float value;
134 assertEquals(index = bb.getInt(), (int)indexHandle.get(base, i));
135 assertEquals(value = bb.getFloat(), (float)valueHandle.get(base, i));
136 assertEquals(value, index / 500f);
137 }
138 }
139
140 static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
141 for (long i = 0; i < seq.elementCount() ; i++) {
142 handleSetter.accept(base, i);
143 }
144 }
145
146 static <Z extends Buffer> void checkBytes(MemorySegment base, SequenceLayout layout,
147 Function<ByteBuffer, Z> bufFactory,
148 BiFunction<MemorySegment, Long, Object> handleExtractor,
149 Function<Z, Object> bufferExtractor) {
150 long nelems = layout.elementCount();
151 long elemSize = layout.elementLayout().byteSize();
152 for (long i = 0 ; i < nelems ; i++) {
153 long limit = nelems - i;
154 MemorySegment resizedSegment = base.asSlice(i * elemSize, limit * elemSize);
155 ByteBuffer bb = resizedSegment.asByteBuffer();
|
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 * @modules java.base/sun.nio.ch java.base/jdk.internal.foreign
27 * @run testng/othervm/timeout=600 --enable-native-access=ALL-UNNAMED TestByteBuffer
28 */
29
30 import java.lang.foreign.*;
31 import java.lang.foreign.MemoryLayout.PathElement;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.lang.invoke.MethodHandle;
36 import java.lang.invoke.MethodHandles;
37 import java.lang.invoke.VarHandle;
38 import java.lang.ref.Cleaner;
39 import java.lang.ref.Reference;
40 import java.lang.ref.WeakReference;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.lang.reflect.Modifier;
44 import java.net.URI;
45 import java.nio.Buffer;
104
105 static SequenceLayout tuples = MemoryLayout.sequenceLayout(500,
106 MemoryLayout.structLayout(
107 BB_INT.withName("index"),
108 BB_FLOAT.withName("value")
109 ));
110
111 static final SequenceLayout bytes = MemoryLayout.sequenceLayout(100, JAVA_BYTE);
112 static final SequenceLayout chars = MemoryLayout.sequenceLayout(100, BB_CHAR);
113 static final SequenceLayout shorts = MemoryLayout.sequenceLayout(100, BB_SHORT);
114 static final SequenceLayout ints = MemoryLayout.sequenceLayout(100, BB_INT);
115 static final SequenceLayout floats = MemoryLayout.sequenceLayout(100, BB_FLOAT);
116 static final SequenceLayout longs = MemoryLayout.sequenceLayout(100, BB_LONG);
117 static final SequenceLayout doubles = MemoryLayout.sequenceLayout(100, BB_DOUBLE);
118
119 static final VarHandle indexHandle = tuples.varHandle(PathElement.sequenceElement(), PathElement.groupElement("index"));
120 static final VarHandle valueHandle = tuples.varHandle(PathElement.sequenceElement(), PathElement.groupElement("value"));
121
122 static void initTuples(MemorySegment base, long count) {
123 for (long i = 0; i < count ; i++) {
124 indexHandle.set(base, 0L, i, (int)i);
125 valueHandle.set(base, 0L, i, (float)(i / 500f));
126 }
127 }
128
129 static void checkTuples(MemorySegment base, ByteBuffer bb, long count) {
130 for (long i = 0; i < count ; i++) {
131 int index;
132 float value;
133 assertEquals(index = bb.getInt(), (int)indexHandle.get(base, 0L, i));
134 assertEquals(value = bb.getFloat(), (float)valueHandle.get(base, 0L, i));
135 assertEquals(value, index / 500f);
136 }
137 }
138
139 static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
140 for (long i = 0; i < seq.elementCount() ; i++) {
141 handleSetter.accept(base, i);
142 }
143 }
144
145 static <Z extends Buffer> void checkBytes(MemorySegment base, SequenceLayout layout,
146 Function<ByteBuffer, Z> bufFactory,
147 BiFunction<MemorySegment, Long, Object> handleExtractor,
148 Function<Z, Object> bufferExtractor) {
149 long nelems = layout.elementCount();
150 long elemSize = layout.elementLayout().byteSize();
151 for (long i = 0 ; i < nelems ; i++) {
152 long limit = nelems - i;
153 MemorySegment resizedSegment = base.asSlice(i * elemSize, limit * elemSize);
154 ByteBuffer bb = resizedSegment.asByteBuffer();
|