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 TestArrays
  28  */
  29 
  30 import jdk.incubator.foreign.MemoryAddress;
  31 import jdk.incubator.foreign.MemoryLayout;
  32 import jdk.incubator.foreign.MemoryLayout.PathElement;
  33 import jdk.incubator.foreign.MemoryLayouts;
  34 import jdk.incubator.foreign.MemorySegment;
  35 import jdk.incubator.foreign.SequenceLayout;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.util.function.BiConsumer;
  39 import java.util.function.Consumer;
  40 
  41 import org.testng.annotations.*;
  42 import static org.testng.Assert.*;
  43 
  44 public class TestArrays {
  45 
  46     static SequenceLayout bytes = MemoryLayout.ofSequence(100,
  47             MemoryLayouts.JAVA_BYTE
  48     );
  49 
  50     static SequenceLayout chars = MemoryLayout.ofSequence(100,
  51             MemoryLayouts.JAVA_CHAR
  52     );
  53 
  54     static SequenceLayout shorts = MemoryLayout.ofSequence(100,
  55             MemoryLayouts.JAVA_SHORT
  56     );
  57 
  58     static SequenceLayout ints = MemoryLayout.ofSequence(100,
  59             MemoryLayouts.JAVA_INT
  60     );
  61 
  62     static SequenceLayout floats = MemoryLayout.ofSequence(100,
  63             MemoryLayouts.JAVA_FLOAT
  64     );
  65 
  66     static SequenceLayout longs = MemoryLayout.ofSequence(100,
  67             MemoryLayouts.JAVA_LONG
  68     );
  69 
  70     static SequenceLayout doubles = MemoryLayout.ofSequence(100,
  71             MemoryLayouts.JAVA_DOUBLE
  72     );
  73 
  74     static VarHandle byteHandle = bytes.varHandle(byte.class, PathElement.sequenceElement());
  75     static VarHandle charHandle = chars.varHandle(char.class, PathElement.sequenceElement());
  76     static VarHandle shortHandle = shorts.varHandle(short.class, PathElement.sequenceElement());
  77     static VarHandle intHandle = ints.varHandle(int.class, PathElement.sequenceElement());
  78     static VarHandle floatHandle = floats.varHandle(float.class, PathElement.sequenceElement());
  79     static VarHandle longHandle = doubles.varHandle(long.class, PathElement.sequenceElement());
  80     static VarHandle doubleHandle = longs.varHandle(double.class, PathElement.sequenceElement());
  81 
  82     static void initBytes(MemoryAddress base, SequenceLayout seq, BiConsumer<MemoryAddress, Long> handleSetter) {
  83         for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
  84             handleSetter.accept(base, i);
  85         }
  86     }
  87 
  88     static void checkBytes(MemoryAddress base, SequenceLayout layout) {
  89         long nBytes = layout.elementCount().getAsLong() * layout.elementLayout().byteSize();
  90         byte[] arr = base.segment().toByteArray();
  91         for (long i = 0 ; i < nBytes ; i++) {
  92             byte expected = (byte)byteHandle.get(base, i);
  93             byte found = arr[(int)i];
  94             assertEquals(expected, found);
  95         }
  96     }
  97 
  98     @Test(dataProvider = "arrays")
  99     public void testArrays(Consumer<MemoryAddress> init, SequenceLayout layout) {
 100         try (MemorySegment segment = MemorySegment.allocateNative(layout)) {
 101             init.accept(segment.baseAddress());
 102             checkBytes(segment.baseAddress(), layout);
 103         }
 104     }
 105 
 106     @Test(expectedExceptions = UnsupportedOperationException.class)
 107     public void testTooBigForArray() {
 108         MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).toByteArray();
 109     }
 110 
 111     @Test(expectedExceptions = IllegalStateException.class)
 112     public void testArrayFromClosedSegment() {
 113         MemorySegment segment = MemorySegment.allocateNative(8);
 114         segment.close();
 115         segment.toByteArray();
 116     }
 117 
 118     @DataProvider(name = "arrays")
 119     public Object[][] nativeAccessOps() {
 120         Consumer<MemoryAddress> byteInitializer =
 121                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
 122         Consumer<MemoryAddress> charInitializer =
 123                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
 124         Consumer<MemoryAddress> shortInitializer =
 125                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
 126         Consumer<MemoryAddress> intInitializer =
 127                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));
 128         Consumer<MemoryAddress> floatInitializer =
 129                 (base) -> initBytes(base, floats, (addr, pos) -> floatHandle.set(addr, pos, (float)(long)pos));
 130         Consumer<MemoryAddress> longInitializer =
 131                 (base) -> initBytes(base, longs, (addr, pos) -> longHandle.set(addr, pos, (long)pos));
 132         Consumer<MemoryAddress> doubleInitializer =
 133                 (base) -> initBytes(base, doubles, (addr, pos) -> doubleHandle.set(addr, pos, (double)(long)pos));
 134 
 135         return new Object[][]{
 136                 {byteInitializer, bytes},
 137                 {charInitializer, chars},
 138                 {shortInitializer, shorts},
 139                 {intInitializer, ints},
 140                 {floatInitializer, floats},
 141                 {longInitializer, longs},
 142                 {doubleInitializer, doubles}
 143         };
 144     }
 145 }