Path: blob/master/test/jdk/java/foreign/TestMemoryAccessStatics.java
41145 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @run testng TestMemoryAccessStatics26*/2728import jdk.incubator.foreign.MemoryAccess;29import jdk.incubator.foreign.MemoryAddress;30import jdk.incubator.foreign.MemoryLayouts;31import jdk.incubator.foreign.MemorySegment;3233import java.lang.reflect.InvocationTargetException;34import java.lang.reflect.Method;35import java.lang.reflect.Modifier;36import java.nio.ByteBuffer;37import java.nio.ByteOrder;3839import org.testng.annotations.*;40import static org.testng.Assert.*;4142public class TestMemoryAccessStatics {4344static class Accessor<X> {4546interface SegmentGetter<X> {47X get(MemorySegment segment);48}4950interface SegmentSetter<X> {51void set(MemorySegment segment, X o);52}5354interface BufferGetter<X> {55X get(ByteBuffer segment);56}5758interface BufferSetter<X> {59void set(ByteBuffer buffer, X o);60}6162final X value;63final SegmentGetter<X> segmentGetter;64final SegmentSetter<X> segmentSetter;65final BufferGetter<X> bufferGetter;66final BufferSetter<X> bufferSetter;6768Accessor(X value,69SegmentGetter<X> segmentGetter, SegmentSetter<X> segmentSetter,70BufferGetter<X> bufferGetter, BufferSetter<X> bufferSetter) {71this.value = value;72this.segmentGetter = segmentGetter;73this.segmentSetter = segmentSetter;74this.bufferGetter = bufferGetter;75this.bufferSetter = bufferSetter;76}7778void test() {79MemorySegment segment = MemorySegment.ofArray(new byte[32]);80ByteBuffer buffer = segment.asByteBuffer();81segmentSetter.set(segment, value);82assertEquals(bufferGetter.get(buffer), value);83bufferSetter.set(buffer, value);84assertEquals(value, segmentGetter.get(segment));85}8687<Z> Accessor<Z> of(Z value,88SegmentGetter<Z> segmentGetter, SegmentSetter<Z> segmentSetter,89BufferGetter<Z> bufferGetter, BufferSetter<Z> bufferSetter) {90return new Accessor<>(value, segmentGetter, segmentSetter, bufferGetter, bufferSetter);91}92}9394@Test(dataProvider = "accessors")95public void testMemoryAccess(String testName, Accessor<?> accessor) {96accessor.test();97}9899static final ByteOrder BE = ByteOrder.BIG_ENDIAN;100static final ByteOrder LE = ByteOrder.LITTLE_ENDIAN;101static final ByteOrder NE = ByteOrder.nativeOrder();102103@DataProvider(name = "accessors")104static Object[][] accessors() {105return new Object[][]{106107{"byte", new Accessor<>((byte) 42,108MemoryAccess::getByte, MemoryAccess::setByte,109(bb) -> bb.get(0), (bb, v) -> bb.put(0, v))110},111{"char", new Accessor<>((char) 42,112MemoryAccess::getChar, MemoryAccess::setChar,113(bb) -> bb.order(NE).getChar(0), (bb, v) -> bb.order(NE).putChar(0, v))114},115{"char/LE", new Accessor<>((char) 42,116s -> MemoryAccess.getChar(s, LE), (s, x) -> MemoryAccess.setChar(s, LE, x),117(bb) -> bb.order(LE).getChar(0), (bb, v) -> bb.order(LE).putChar(0, v))118},119{"char/BE", new Accessor<>((char) 42,120s -> MemoryAccess.getChar(s, BE), (s, x) -> MemoryAccess.setChar(s, BE, x),121(bb) -> bb.order(BE).getChar(0), (bb, v) -> bb.order(BE).putChar(0, v))122},123{"short", new Accessor<>((short) 42,124MemoryAccess::getShort, MemoryAccess::setShort,125(bb) -> bb.order(NE).getShort(0), (bb, v) -> bb.order(NE).putShort(0, v))126},127{"short/LE", new Accessor<>((short) 42,128s -> MemoryAccess.getShort(s, LE), (s, x) -> MemoryAccess.setShort(s, LE, x),129(bb) -> bb.order(LE).getShort(0), (bb, v) -> bb.order(LE).putShort(0, v))130},131{"short/BE", new Accessor<>((short) 42,132s -> MemoryAccess.getShort(s, BE), (s, x) -> MemoryAccess.setShort(s, BE, x),133(bb) -> bb.order(BE).getShort(0), (bb, v) -> bb.order(BE).putShort(0, v))134},135{"int", new Accessor<>(42,136MemoryAccess::getInt, MemoryAccess::setInt,137(bb) -> bb.order(NE).getInt(0), (bb, v) -> bb.order(NE).putInt(0, v))138},139{"int/LE", new Accessor<>(42,140s -> MemoryAccess.getInt(s, LE), (s, x) -> MemoryAccess.setInt(s, LE, x),141(bb) -> bb.order(LE).getInt(0), (bb, v) -> bb.order(LE).putInt(0, v))142},143{"int/BE", new Accessor<>(42,144s -> MemoryAccess.getInt(s, BE), (s, x) -> MemoryAccess.setInt(s, BE, x),145(bb) -> bb.order(BE).getInt(0), (bb, v) -> bb.order(BE).putInt(0, v))146},147// float, no offset148{"float", new Accessor<>(42f,149MemoryAccess::getFloat, MemoryAccess::setFloat,150(bb) -> bb.order(NE).getFloat(0), (bb, v) -> bb.order(NE).putFloat(0, v))151},152{"float/LE", new Accessor<>(42f,153s -> MemoryAccess.getFloat(s, LE), (s, x) -> MemoryAccess.setFloat(s, LE, x),154(bb) -> bb.order(LE).getFloat(0), (bb, v) -> bb.order(LE).putFloat(0, v))155},156{"float/BE", new Accessor<>(42f,157s -> MemoryAccess.getFloat(s, BE), (s, x) -> MemoryAccess.setFloat(s, BE, x),158(bb) -> bb.order(BE).getFloat(0), (bb, v) -> bb.order(BE).putFloat(0, v))159},160// double, no offset161{"double", new Accessor<>(42d,162MemoryAccess::getDouble, MemoryAccess::setDouble,163(bb) -> bb.order(NE).getDouble(0), (bb, v) -> bb.order(NE).putDouble(0, v))164},165{"double/LE", new Accessor<>(42d,166s -> MemoryAccess.getDouble(s, LE), (s, x) -> MemoryAccess.setDouble(s, LE, x),167(bb) -> bb.order(LE).getDouble(0), (bb, v) -> bb.order(LE).putDouble(0, v))168},169{"double/BE", new Accessor<>(42d,170s -> MemoryAccess.getDouble(s, BE), (s, x) -> MemoryAccess.setDouble(s, BE, x),171(bb) -> bb.order(BE).getDouble(0), (bb, v) -> bb.order(BE).putDouble(0, v))172},173174175// byte, offset176{"byte/offset", new Accessor<>((byte) 42,177s -> MemoryAccess.getByteAtOffset(s, 4), (s, x) -> MemoryAccess.setByteAtOffset(s, 4, x),178(bb) -> bb.get(4), (bb, v) -> bb.put(4, v))179},180// char, offset181{"char/offset", new Accessor<>((char) 42,182s -> MemoryAccess.getCharAtOffset(s, 4), (s, x) -> MemoryAccess.setCharAtOffset(s, 4, x),183(bb) -> bb.order(NE).getChar(4), (bb, v) -> bb.order(NE).putChar(4, v))184},185{"char/offset/LE", new Accessor<>((char) 42,186s -> MemoryAccess.getCharAtOffset(s, 4, LE), (s, x) -> MemoryAccess.setCharAtOffset(s, 4, LE, x),187(bb) -> bb.order(LE).getChar(4), (bb, v) -> bb.order(LE).putChar(4, v))188},189{"char/offset/BE", new Accessor<>((char) 42,190s -> MemoryAccess.getCharAtOffset(s, 4, BE), (s, x) -> MemoryAccess.setCharAtOffset(s, 4, BE, x),191(bb) -> bb.order(BE).getChar(4), (bb, v) -> bb.order(BE).putChar(4, v))192},193// short, offset194{"short/offset", new Accessor<>((short) 42,195s -> MemoryAccess.getShortAtOffset(s, 4), (s, x) -> MemoryAccess.setShortAtOffset(s, 4, x),196(bb) -> bb.order(NE).getShort(4), (bb, v) -> bb.order(NE).putShort(4, v))197},198{"short/offset/LE", new Accessor<>((short) 42,199s -> MemoryAccess.getShortAtOffset(s, 4, LE), (s, x) -> MemoryAccess.setShortAtOffset(s, 4, LE, x),200(bb) -> bb.order(LE).getShort(4), (bb, v) -> bb.order(LE).putShort(4, v))201},202{"short/offset/BE", new Accessor<>((short) 42,203s -> MemoryAccess.getShortAtOffset(s, 4, BE), (s, x) -> MemoryAccess.setShortAtOffset(s, 4, BE, x),204(bb) -> bb.order(BE).getShort(4), (bb, v) -> bb.order(BE).putShort(4, v))205},206// int, offset207{"int/offset", new Accessor<>(42,208s -> MemoryAccess.getIntAtOffset(s, 4), (s, x) -> MemoryAccess.setIntAtOffset(s, 4, x),209(bb) -> bb.order(NE).getInt(4), (bb, v) -> bb.order(NE).putInt(4, v))210},211{"int/offset/LE", new Accessor<>(42,212s -> MemoryAccess.getIntAtOffset(s, 4, LE), (s, x) -> MemoryAccess.setIntAtOffset(s, 4, LE, x),213(bb) -> bb.order(LE).getInt(4), (bb, v) -> bb.order(LE).putInt(4, v))214},215{"int/offset/BE", new Accessor<>(42,216s -> MemoryAccess.getIntAtOffset(s, 4, BE), (s, x) -> MemoryAccess.setIntAtOffset(s, 4, BE, x),217(bb) -> bb.order(BE).getInt(4), (bb, v) -> bb.order(BE).putInt(4, v))218},219// float, offset220{"float/offset", new Accessor<>(42f,221s -> MemoryAccess.getFloatAtOffset(s, 4), (s, x) -> MemoryAccess.setFloatAtOffset(s, 4, x),222(bb) -> bb.order(NE).getFloat(4), (bb, v) -> bb.order(NE).putFloat(4, v))223},224{"float/offset/LE", new Accessor<>(42f,225s -> MemoryAccess.getFloatAtOffset(s, 4, LE), (s, x) -> MemoryAccess.setFloatAtOffset(s, 4, LE, x),226(bb) -> bb.order(LE).getFloat(4), (bb, v) -> bb.order(LE).putFloat(4, v))227},228{"float/offset/BE", new Accessor<>(42f,229s -> MemoryAccess.getFloatAtOffset(s, 4, BE), (s, x) -> MemoryAccess.setFloatAtOffset(s, 4, BE, x),230(bb) -> bb.order(BE).getFloat(4), (bb, v) -> bb.order(BE).putFloat(4, v))231},232// double, offset233{"double/offset", new Accessor<>(42d,234s -> MemoryAccess.getDoubleAtOffset(s, 4), (s, x) -> MemoryAccess.setDoubleAtOffset(s, 4, x),235(bb) -> bb.order(NE).getDouble(4), (bb, v) -> bb.order(NE).putDouble(4, v))236},237{"double/offset/LE", new Accessor<>(42d,238s -> MemoryAccess.getDoubleAtOffset(s, 4, LE), (s, x) -> MemoryAccess.setDoubleAtOffset(s, 4, LE, x),239(bb) -> bb.order(LE).getDouble(4), (bb, v) -> bb.order(LE).putDouble(4, v))240},241{"double/offset/BE", new Accessor<>(42d,242s -> MemoryAccess.getDoubleAtOffset(s, 4, BE), (s, x) -> MemoryAccess.setDoubleAtOffset(s, 4, BE, x),243(bb) -> bb.order(BE).getDouble(4), (bb, v) -> bb.order(BE).putDouble(4, v))244},245246247// char, index248{"char/index", new Accessor<>((char) 42,249s -> MemoryAccess.getCharAtIndex(s, 2), (s, x) -> MemoryAccess.setCharAtIndex(s, 2, x),250(bb) -> bb.order(NE).asCharBuffer().get(2), (bb, v) -> bb.order(NE).asCharBuffer().put(2, v))251},252{"char/index/LE", new Accessor<>((char) 42,253s -> MemoryAccess.getCharAtIndex(s, 2, LE), (s, x) -> MemoryAccess.setCharAtIndex(s, 2, LE, x),254(bb) -> bb.order(LE).asCharBuffer().get(2), (bb, v) -> bb.order(LE).asCharBuffer().put(2, v))255},256{"char/index/BE", new Accessor<>((char) 42,257s -> MemoryAccess.getCharAtIndex(s, 2, BE), (s, x) -> MemoryAccess.setCharAtIndex(s, 2, BE, x),258(bb) -> bb.order(BE).asCharBuffer().get(2), (bb, v) -> bb.order(BE).asCharBuffer().put(2, v))259},260// short, index261{"short/index", new Accessor<>((short) 42,262s -> MemoryAccess.getShortAtIndex(s, 2), (s, x) -> MemoryAccess.setShortAtIndex(s, 2, x),263(bb) -> bb.order(NE).asShortBuffer().get(2), (bb, v) -> bb.order(NE).asShortBuffer().put(2, v))264},265{"short/index/LE", new Accessor<>((short) 42,266s -> MemoryAccess.getShortAtIndex(s, 2, LE), (s, x) -> MemoryAccess.setShortAtIndex(s, 2, LE, x),267(bb) -> bb.order(LE).asShortBuffer().get(2), (bb, v) -> bb.order(LE).asShortBuffer().put(2, v))268},269{"short/index/BE", new Accessor<>((short) 42,270s -> MemoryAccess.getShortAtIndex(s, 2, BE), (s, x) -> MemoryAccess.setShortAtIndex(s, 2, BE, x),271(bb) -> bb.order(BE).asShortBuffer().get(2), (bb, v) -> bb.order(BE).asShortBuffer().put(2, v))272},273{"int/index", new Accessor<>(42,274s -> MemoryAccess.getIntAtIndex(s, 2), (s, x) -> MemoryAccess.setIntAtIndex(s, 2, x),275(bb) -> bb.order(NE).asIntBuffer().get(2), (bb, v) -> bb.order(NE).asIntBuffer().put(2, v))276},277{"int/index/LE", new Accessor<>(42,278s -> MemoryAccess.getIntAtIndex(s, 2, LE), (s, x) -> MemoryAccess.setIntAtIndex(s, 2, LE, x),279(bb) -> bb.order(LE).asIntBuffer().get(2), (bb, v) -> bb.order(LE).asIntBuffer().put(2, v))280},281{"int/index/BE", new Accessor<>(42,282s -> MemoryAccess.getIntAtIndex(s, 2, BE), (s, x) -> MemoryAccess.setIntAtIndex(s, 2, BE, x),283(bb) -> bb.order(BE).asIntBuffer().get(2), (bb, v) -> bb.order(BE).asIntBuffer().put(2, v))284},285{"float/index", new Accessor<>(42f,286s -> MemoryAccess.getFloatAtIndex(s, 2), (s, x) -> MemoryAccess.setFloatAtIndex(s, 2, x),287(bb) -> bb.order(NE).asFloatBuffer().get(2), (bb, v) -> bb.order(NE).asFloatBuffer().put(2, v))288},289{"float/index/LE", new Accessor<>(42f,290s -> MemoryAccess.getFloatAtIndex(s, 2, LE), (s, x) -> MemoryAccess.setFloatAtIndex(s, 2, LE, x),291(bb) -> bb.order(LE).asFloatBuffer().get(2), (bb, v) -> bb.order(LE).asFloatBuffer().put(2, v))292},293{"float/index/BE", new Accessor<>(42f,294s -> MemoryAccess.getFloatAtIndex(s, 2, BE), (s, x) -> MemoryAccess.setFloatAtIndex(s, 2, BE, x),295(bb) -> bb.order(BE).asFloatBuffer().get(2), (bb, v) -> bb.order(BE).asFloatBuffer().put(2, v))296},297{"double/index", new Accessor<>(42d,298s -> MemoryAccess.getDoubleAtIndex(s, 2), (s, x) -> MemoryAccess.setDoubleAtIndex(s, 2, x),299(bb) -> bb.order(NE).asDoubleBuffer().get(2), (bb, v) -> bb.order(NE).asDoubleBuffer().put(2, v))300},301{"double/index/LE", new Accessor<>(42d,302s -> MemoryAccess.getDoubleAtIndex(s, 2, LE), (s, x) -> MemoryAccess.setDoubleAtIndex(s, 2, LE, x),303(bb) -> bb.order(LE).asDoubleBuffer().get(2), (bb, v) -> bb.order(LE).asDoubleBuffer().put(2, v))304},305{"double/index/BE", new Accessor<>(42d,306s -> MemoryAccess.getDoubleAtIndex(s, 2, BE), (s, x) -> MemoryAccess.setDoubleAtIndex(s, 2, BE, x),307(bb) -> bb.order(BE).asDoubleBuffer().get(2), (bb, v) -> bb.order(BE).asDoubleBuffer().put(2, v))308},309310{ "address", new Accessor<>(MemoryAddress.ofLong(42),311MemoryAccess::getAddress, MemoryAccess::setAddress,312(bb) -> {313ByteBuffer nb = bb.order(NE);314long addr = MemoryLayouts.ADDRESS.byteSize() == 8 ?315nb.getLong(0) : nb.getInt(0);316return MemoryAddress.ofLong(addr);317},318(bb, v) -> {319ByteBuffer nb = bb.order(NE);320if (MemoryLayouts.ADDRESS.byteSize() == 8) {321nb.putLong(0, v.toRawLongValue());322} else {323nb.putInt(0, (int)v.toRawLongValue());324}325})326},327{ "address/offset", new Accessor<>(MemoryAddress.ofLong(42),328s -> MemoryAccess.getAddressAtOffset(s, 4), (s, x) -> MemoryAccess.setAddressAtOffset(s, 4, x),329(bb) -> {330ByteBuffer nb = bb.order(NE);331long addr = MemoryLayouts.ADDRESS.byteSize() == 8 ?332nb.getLong(4) : nb.getInt(4);333return MemoryAddress.ofLong(addr);334},335(bb, v) -> {336ByteBuffer nb = bb.order(NE);337if (MemoryLayouts.ADDRESS.byteSize() == 8) {338nb.putLong(4, v.toRawLongValue());339} else {340nb.putInt(4, (int)v.toRawLongValue());341}342})343},344{ "address/index", new Accessor<>(MemoryAddress.ofLong(42),345s -> MemoryAccess.getAddressAtIndex(s, 2), (s, x) -> MemoryAccess.setAddressAtIndex(s, 2, x),346(bb) -> {347ByteBuffer nb = bb.order(NE);348long addr = MemoryLayouts.ADDRESS.byteSize() == 8 ?349nb.asLongBuffer().get(2) : nb.asIntBuffer().get(2);350return MemoryAddress.ofLong(addr);351},352(bb, v) -> {353ByteBuffer nb = bb.order(NE);354if (MemoryLayouts.ADDRESS.byteSize() == 8) {355nb.asLongBuffer().put(2, v.toRawLongValue());356} else {357nb.asIntBuffer().put(2, (int)v.toRawLongValue());358}359})360},361};362}363}364365366