1 /*
   2  * Copyright (c) 2019, 2020, 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 = longs.varHandle(long.class, PathElement.sequenceElement());
  80     static VarHandle doubleHandle = doubles.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                                  IllegalArgumentException.class })
 108     public void testTooBigForArray() {
 109         MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).toByteArray();
 110     }
 111 
 112     @Test(expectedExceptions = IllegalStateException.class)
 113     public void testArrayFromClosedSegment() {
 114         MemorySegment segment = MemorySegment.allocateNative(8);
 115         segment.close();
 116         segment.toByteArray();
 117     }
 118 
 119     @DataProvider(name = "arrays")
 120     public Object[][] nativeAccessOps() {
 121         Consumer<MemoryAddress> byteInitializer =
 122                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
 123         Consumer<MemoryAddress> charInitializer =
 124                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
 125         Consumer<MemoryAddress> shortInitializer =
 126                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
 127         Consumer<MemoryAddress> intInitializer =
 128                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));
 129         Consumer<MemoryAddress> floatInitializer =
 130                 (base) -> initBytes(base, floats, (addr, pos) -> floatHandle.set(addr, pos, (float)(long)pos));
 131         Consumer<MemoryAddress> longInitializer =
 132                 (base) -> initBytes(base, longs, (addr, pos) -> longHandle.set(addr, pos, (long)pos));
 133         Consumer<MemoryAddress> doubleInitializer =
 134                 (base) -> initBytes(base, doubles, (addr, pos) -> doubleHandle.set(addr, pos, (double)(long)pos));
 135 
 136         return new Object[][]{
 137                 {byteInitializer, bytes},
 138                 {charInitializer, chars},
 139                 {shortInitializer, shorts},
 140                 {intInitializer, ints},
 141                 {floatInitializer, floats},
 142                 {longInitializer, longs},
 143                 {doubleInitializer, doubles}
 144         };
 145     }
 146 }