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/othervm --enable-native-access=ALL-UNNAMED 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.MemorySegment;
 34 import jdk.incubator.foreign.ResourceScope;
 35 import jdk.incubator.foreign.SequenceLayout;
 36 
 37 import java.lang.invoke.VarHandle;
 38 import java.util.function.BiConsumer;
 39 import java.util.function.BiFunction;
 40 import java.util.function.Consumer;
 41 import java.util.function.Function;
 42 
 43 import org.testng.annotations.*;
 44 
 45 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 46 import static jdk.incubator.foreign.ValueLayout.JAVA_CHAR;
 47 import static jdk.incubator.foreign.ValueLayout.JAVA_DOUBLE;
 48 import static jdk.incubator.foreign.ValueLayout.JAVA_FLOAT;
 49 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 50 import static jdk.incubator.foreign.ValueLayout.JAVA_LONG;
 51 import static jdk.incubator.foreign.ValueLayout.JAVA_SHORT;
 52 import static org.testng.Assert.*;
 53 
 54 public class TestArrays {
 55 
 56     static SequenceLayout bytes = MemoryLayout.sequenceLayout(100,
 57             JAVA_BYTE
 58     );
 59 
 60     static SequenceLayout chars = MemoryLayout.sequenceLayout(100,
 61             JAVA_CHAR
 62     );
 63 
 64     static SequenceLayout shorts = MemoryLayout.sequenceLayout(100,
 65             JAVA_SHORT
 66     );
 67 
 68     static SequenceLayout ints = MemoryLayout.sequenceLayout(100,
 69             JAVA_INT
 70     );
 71 
 72     static SequenceLayout floats = MemoryLayout.sequenceLayout(100,
 73             JAVA_FLOAT
 74     );
 75 
 76     static SequenceLayout longs = MemoryLayout.sequenceLayout(100,
 77             JAVA_LONG
 78     );
 79 
 80     static SequenceLayout doubles = MemoryLayout.sequenceLayout(100,
 81             JAVA_DOUBLE
 82     );
 83 
 84     static VarHandle byteHandle = bytes.varHandle(PathElement.sequenceElement());
 85     static VarHandle charHandle = chars.varHandle(PathElement.sequenceElement());
 86     static VarHandle shortHandle = shorts.varHandle(PathElement.sequenceElement());
 87     static VarHandle intHandle = ints.varHandle(PathElement.sequenceElement());
 88     static VarHandle floatHandle = floats.varHandle(PathElement.sequenceElement());
 89     static VarHandle longHandle = longs.varHandle(PathElement.sequenceElement());
 90     static VarHandle doubleHandle = doubles.varHandle(PathElement.sequenceElement());
 91 
 92     static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
 93         for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
 94             handleSetter.accept(base, i);
 95         }
 96     }
 97 
 98     static void checkBytes(MemorySegment base, SequenceLayout layout, Function<MemorySegment, Object> arrayFactory, BiFunction<MemorySegment, Long, Object> handleGetter) {
 99         int nelems = (int)layout.elementCount().getAsLong();
100         Object arr = arrayFactory.apply(base);
101         for (int i = 0; i < nelems; i++) {
102             Object found = handleGetter.apply(base, (long) i);
103             Object expected = java.lang.reflect.Array.get(arr, i);
104             assertEquals(expected, found);
105         }
106     }
107 
108     @Test(dataProvider = "arrays")
109     public void testArrays(Consumer<MemorySegment> init, Consumer<MemorySegment> checker, MemoryLayout layout) {
110         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
111         init.accept(segment);
112         assertFalse(segment.isReadOnly());
113         checker.accept(segment);
114     }
115 
116     @Test(dataProvider = "elemLayouts",
117             expectedExceptions = IllegalStateException.class)
118     public void testTooBigForArray(MemoryLayout layout, Function<MemorySegment, Object> arrayFactory) {
119         MemoryLayout seq = MemoryLayout.sequenceLayout((Integer.MAX_VALUE * layout.byteSize()) + 1, layout);
120         //do not really allocate here, as it's way too much memory
121         MemorySegment segment = MemorySegment.ofAddressNative(MemoryAddress.NULL, seq.byteSize(), ResourceScope.globalScope());
122         arrayFactory.apply(segment);
123     }
124 
125     @Test(dataProvider = "elemLayouts",
126             expectedExceptions = IllegalStateException.class)
127     public void testBadSize(MemoryLayout layout, Function<MemorySegment, Object> arrayFactory) {
128         if (layout.byteSize() == 1) throw new IllegalStateException(); //make it fail
129         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
130             MemorySegment segment = MemorySegment.allocateNative(layout.byteSize() + 1, layout.byteSize(), scope);
131             arrayFactory.apply(segment);
132         }
133     }
134 
135     @Test(dataProvider = "elemLayouts",
136             expectedExceptions = IllegalStateException.class)
137     public void testArrayFromClosedSegment(MemoryLayout layout, Function<MemorySegment, Object> arrayFactory) {
138         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newConfinedScope());
139         segment.scope().close();
140         arrayFactory.apply(segment);
141     }
142 
143     @DataProvider(name = "arrays")
144     public Object[][] nativeAccessOps() {
145         Consumer<MemorySegment> byteInitializer =
146                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
147         Consumer<MemorySegment> charInitializer =
148                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
149         Consumer<MemorySegment> shortInitializer =
150                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
151         Consumer<MemorySegment> intInitializer =
152                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));
153         Consumer<MemorySegment> floatInitializer =
154                 (base) -> initBytes(base, floats, (addr, pos) -> floatHandle.set(addr, pos, (float)(long)pos));
155         Consumer<MemorySegment> longInitializer =
156                 (base) -> initBytes(base, longs, (addr, pos) -> longHandle.set(addr, pos, (long)pos));
157         Consumer<MemorySegment> doubleInitializer =
158                 (base) -> initBytes(base, doubles, (addr, pos) -> doubleHandle.set(addr, pos, (double)(long)pos));
159 
160         Consumer<MemorySegment> byteChecker =
161                 (base) -> checkBytes(base, bytes, s -> s.toArray(JAVA_BYTE), (addr, pos) -> (byte)byteHandle.get(addr, pos));
162         Consumer<MemorySegment> shortChecker =
163                 (base) -> checkBytes(base, shorts, s -> s.toArray(JAVA_SHORT), (addr, pos) -> (short)shortHandle.get(addr, pos));
164         Consumer<MemorySegment> charChecker =
165                 (base) -> checkBytes(base, chars, s -> s.toArray(JAVA_CHAR), (addr, pos) -> (char)charHandle.get(addr, pos));
166         Consumer<MemorySegment> intChecker =
167                 (base) -> checkBytes(base, ints, s -> s.toArray(JAVA_INT), (addr, pos) -> (int)intHandle.get(addr, pos));
168         Consumer<MemorySegment> floatChecker =
169                 (base) -> checkBytes(base, floats, s -> s.toArray(JAVA_FLOAT), (addr, pos) -> (float)floatHandle.get(addr, pos));
170         Consumer<MemorySegment> longChecker =
171                 (base) -> checkBytes(base, longs, s -> s.toArray(JAVA_LONG), (addr, pos) -> (long)longHandle.get(addr, pos));
172         Consumer<MemorySegment> doubleChecker =
173                 (base) -> checkBytes(base, doubles, s -> s.toArray(JAVA_DOUBLE), (addr, pos) -> (double)doubleHandle.get(addr, pos));
174 
175         return new Object[][]{
176                 {byteInitializer, byteChecker, bytes},
177                 {charInitializer, charChecker, chars},
178                 {shortInitializer, shortChecker, shorts},
179                 {intInitializer, intChecker, ints},
180                 {floatInitializer, floatChecker, floats},
181                 {longInitializer, longChecker, longs},
182                 {doubleInitializer, doubleChecker, doubles}
183         };
184     }
185 
186     @DataProvider(name = "elemLayouts")
187     public Object[][] elemLayouts() {
188         return new Object[][] {
189                 { JAVA_BYTE, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_BYTE)},
190                 { JAVA_SHORT, (Function<MemorySegment, Object>) s -> s.toArray(JAVA_SHORT)},
191                 { JAVA_CHAR, (Function<MemorySegment, Object>) s -> s.toArray(JAVA_CHAR)},
192                 { JAVA_INT, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_INT)},
193                 { JAVA_FLOAT, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_FLOAT)},
194                 { JAVA_LONG, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_LONG)},
195                 { JAVA_DOUBLE, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_DOUBLE)}
196         };
197     }
198 }