< prev index next >

test/jdk/java/foreign/TestSegmentCopy.java

Print this page

  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  * @enablePreview
 28  * @run testng TestSegmentCopy
 29  */
 30 
 31 import java.lang.foreign.Arena;
 32 import java.lang.foreign.MemorySegment;
 33 import java.lang.foreign.ValueLayout;
 34 import java.lang.invoke.MethodHandles;
 35 import java.lang.invoke.VarHandle;
 36 import java.nio.ByteOrder;
 37 import java.util.ArrayList;
 38 import java.util.Arrays;
 39 import java.util.List;
 40 import java.util.function.IntFunction;
 41 
 42 import org.testng.SkipException;
 43 import org.testng.annotations.DataProvider;
 44 import org.testng.annotations.Test;
 45 
 46 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 47 import static org.testng.Assert.*;

154         FLOAT_BE(float.class, ValueLayout.JAVA_FLOAT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (float)i),
155         LONG_BE(long.class, ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (long)i),
156         DOUBLE_BE(double.class, ValueLayout.JAVA_DOUBLE_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (double)i);
157 
158         final ValueLayout layout;
159         final IntFunction<Object> valueConverter;
160         final Class<?> carrier;
161 
162         @SuppressWarnings("unchecked")
163         <Z> Type(Class<Z> carrier, ValueLayout layout, IntFunction<Z> valueConverter) {
164             this.carrier = carrier;
165             this.layout = layout;
166             this.valueConverter = (IntFunction<Object>)valueConverter;
167         }
168 
169         long size() {
170             return layout.byteSize();
171         }
172 
173         VarHandle handle() {
174             return MethodHandles.memorySegmentViewVarHandle(layout);
175         }
176 
177         void set(MemorySegment segment, long offset, int index, int val) {
178             handle().set(segment, offset + (index * size()), valueConverter.apply(val));
179         }
180 
181         void check(MemorySegment segment, long offset, int index, int val) {
182             assertEquals(handle().get(segment, offset + (index * size())), valueConverter.apply(val));
183         }
184     }
185 
186     enum SegmentKind {
187         NATIVE(i -> Arena.ofAuto().allocate(i, 1)),
188         ARRAY(i -> MemorySegment.ofArray(new byte[i]));
189 
190         final IntFunction<MemorySegment> segmentFactory;
191 
192         SegmentKind(IntFunction<MemorySegment> segmentFactory) {
193             this.segmentFactory = segmentFactory;
194         }

  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 TestSegmentCopy
 28  */
 29 
 30 import java.lang.foreign.Arena;
 31 import java.lang.foreign.MemorySegment;
 32 import java.lang.foreign.ValueLayout;
 33 import java.lang.invoke.MethodHandles;
 34 import java.lang.invoke.VarHandle;
 35 import java.nio.ByteOrder;
 36 import java.util.ArrayList;
 37 import java.util.Arrays;
 38 import java.util.List;
 39 import java.util.function.IntFunction;
 40 
 41 import org.testng.SkipException;
 42 import org.testng.annotations.DataProvider;
 43 import org.testng.annotations.Test;
 44 
 45 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 46 import static org.testng.Assert.*;

153         FLOAT_BE(float.class, ValueLayout.JAVA_FLOAT_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (float)i),
154         LONG_BE(long.class, ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (long)i),
155         DOUBLE_BE(double.class, ValueLayout.JAVA_DOUBLE_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), i -> (double)i);
156 
157         final ValueLayout layout;
158         final IntFunction<Object> valueConverter;
159         final Class<?> carrier;
160 
161         @SuppressWarnings("unchecked")
162         <Z> Type(Class<Z> carrier, ValueLayout layout, IntFunction<Z> valueConverter) {
163             this.carrier = carrier;
164             this.layout = layout;
165             this.valueConverter = (IntFunction<Object>)valueConverter;
166         }
167 
168         long size() {
169             return layout.byteSize();
170         }
171 
172         VarHandle handle() {
173             return layout.varHandle();
174         }
175 
176         void set(MemorySegment segment, long offset, int index, int val) {
177             handle().set(segment, offset + (index * size()), valueConverter.apply(val));
178         }
179 
180         void check(MemorySegment segment, long offset, int index, int val) {
181             assertEquals(handle().get(segment, offset + (index * size())), valueConverter.apply(val));
182         }
183     }
184 
185     enum SegmentKind {
186         NATIVE(i -> Arena.ofAuto().allocate(i, 1)),
187         ARRAY(i -> MemorySegment.ofArray(new byte[i]));
188 
189         final IntFunction<MemorySegment> segmentFactory;
190 
191         SegmentKind(IntFunction<MemorySegment> segmentFactory) {
192             this.segmentFactory = segmentFactory;
193         }
< prev index next >