1 /*
  2  * Copyright (c) 2019, 2023, 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 java.lang.foreign.*;
 31 import java.lang.foreign.MemoryLayout.PathElement;
 32 
 33 import java.lang.invoke.MethodHandle;
 34 import java.lang.invoke.MethodHandles;
 35 import java.lang.invoke.MethodType;
 36 import java.lang.invoke.VarHandle;
 37 import java.util.function.BiConsumer;
 38 import java.util.function.BiFunction;
 39 import java.util.function.Consumer;
 40 import java.util.function.Function;
 41 
 42 import org.testng.annotations.*;
 43 
 44 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 45 import static java.lang.foreign.ValueLayout.JAVA_CHAR;
 46 import static java.lang.foreign.ValueLayout.JAVA_DOUBLE;
 47 import static java.lang.foreign.ValueLayout.JAVA_FLOAT;
 48 import static java.lang.foreign.ValueLayout.JAVA_INT;
 49 import static java.lang.foreign.ValueLayout.JAVA_LONG;
 50 import static java.lang.foreign.ValueLayout.JAVA_SHORT;
 51 import static org.testng.Assert.*;
 52 
 53 public class TestArrays {
 54 
 55     static SequenceLayout bytes = MemoryLayout.sequenceLayout(100,
 56             JAVA_BYTE
 57     );
 58 
 59     static SequenceLayout chars = MemoryLayout.sequenceLayout(100,
 60             JAVA_CHAR
 61     );
 62 
 63     static SequenceLayout shorts = MemoryLayout.sequenceLayout(100,
 64             JAVA_SHORT
 65     );
 66 
 67     static SequenceLayout ints = MemoryLayout.sequenceLayout(100,
 68             JAVA_INT
 69     );
 70 
 71     static SequenceLayout floats = MemoryLayout.sequenceLayout(100,
 72             JAVA_FLOAT
 73     );
 74 
 75     static SequenceLayout longs = MemoryLayout.sequenceLayout(100,
 76             JAVA_LONG
 77     );
 78 
 79     static SequenceLayout doubles = MemoryLayout.sequenceLayout(100,
 80             JAVA_DOUBLE
 81     );
 82 
 83     static VarHandle byteHandle = bytes.varHandle(PathElement.sequenceElement());
 84     static VarHandle charHandle = chars.varHandle(PathElement.sequenceElement());
 85     static VarHandle shortHandle = shorts.varHandle(PathElement.sequenceElement());
 86     static VarHandle intHandle = ints.varHandle(PathElement.sequenceElement());
 87     static VarHandle floatHandle = floats.varHandle(PathElement.sequenceElement());
 88     static VarHandle longHandle = longs.varHandle(PathElement.sequenceElement());
 89     static VarHandle doubleHandle = doubles.varHandle(PathElement.sequenceElement());
 90 
 91     static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
 92         for (long i = 0; i < seq.elementCount() ; i++) {
 93             handleSetter.accept(base, i);
 94         }
 95     }
 96 
 97     static void checkBytes(MemorySegment base, SequenceLayout layout, Function<MemorySegment, Object> arrayFactory, BiFunction<MemorySegment, Long, Object> handleGetter) {
 98         int nelems = (int)layout.elementCount();
 99         Object arr = arrayFactory.apply(base);
100         for (int i = 0; i < nelems; i++) {
101             Object found = handleGetter.apply(base, (long) i);
102             Object expected = java.lang.reflect.Array.get(arr, i);
103             assertEquals(expected, found);
104         }
105     }
106 
107     @Test(dataProvider = "arrays")
108     public void testArrays(Consumer<MemorySegment> init, Consumer<MemorySegment> checker, MemoryLayout layout) {
109         Arena scope = Arena.ofAuto();
110         MemorySegment segment = scope.allocate(layout);
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.NULL.reinterpret(seq.byteSize());
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 (Arena arena = Arena.ofConfined()) {
130             long byteSize = layout.byteSize() + 1;
131             long byteAlignment = layout.byteSize();
132             MemorySegment segment = arena.allocate(byteSize, byteAlignment);
133             arrayFactory.apply(segment);
134         }
135     }
136 
137     @Test(dataProvider = "elemLayouts",
138             expectedExceptions = IllegalStateException.class)
139     public void testArrayFromClosedSegment(MemoryLayout layout, Function<MemorySegment, Object> arrayFactory) {
140         Arena arena = Arena.ofConfined();
141         MemorySegment segment = arena.allocate(layout);
142         arena.close();
143         arrayFactory.apply(segment);
144     }
145 
146     @DataProvider(name = "arrays")
147     public Object[][] nativeAccessOps() {
148         Consumer<MemorySegment> byteInitializer =
149                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, 0L, pos, (byte)(long)pos));
150         Consumer<MemorySegment> charInitializer =
151                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, 0L, pos, (char)(long)pos));
152         Consumer<MemorySegment> shortInitializer =
153                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, 0L, pos, (short)(long)pos));
154         Consumer<MemorySegment> intInitializer =
155                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, 0L, pos, (int)(long)pos));
156         Consumer<MemorySegment> floatInitializer =
157                 (base) -> initBytes(base, floats, (addr, pos) -> floatHandle.set(addr, 0L, pos, (float)(long)pos));
158         Consumer<MemorySegment> longInitializer =
159                 (base) -> initBytes(base, longs, (addr, pos) -> longHandle.set(addr, 0L, pos, (long)pos));
160         Consumer<MemorySegment> doubleInitializer =
161                 (base) -> initBytes(base, doubles, (addr, pos) -> doubleHandle.set(addr, 0L, pos, (double)(long)pos));
162 
163         Consumer<MemorySegment> byteChecker =
164                 (base) -> checkBytes(base, bytes, s -> s.toArray(JAVA_BYTE), (addr, pos) -> (byte)byteHandle.get(addr, 0L, pos));
165         Consumer<MemorySegment> shortChecker =
166                 (base) -> checkBytes(base, shorts, s -> s.toArray(JAVA_SHORT), (addr, pos) -> (short)shortHandle.get(addr, 0L, pos));
167         Consumer<MemorySegment> charChecker =
168                 (base) -> checkBytes(base, chars, s -> s.toArray(JAVA_CHAR), (addr, pos) -> (char)charHandle.get(addr, 0L, pos));
169         Consumer<MemorySegment> intChecker =
170                 (base) -> checkBytes(base, ints, s -> s.toArray(JAVA_INT), (addr, pos) -> (int)intHandle.get(addr, 0L, pos));
171         Consumer<MemorySegment> floatChecker =
172                 (base) -> checkBytes(base, floats, s -> s.toArray(JAVA_FLOAT), (addr, pos) -> (float)floatHandle.get(addr, 0L, pos));
173         Consumer<MemorySegment> longChecker =
174                 (base) -> checkBytes(base, longs, s -> s.toArray(JAVA_LONG), (addr, pos) -> (long)longHandle.get(addr, 0L, pos));
175         Consumer<MemorySegment> doubleChecker =
176                 (base) -> checkBytes(base, doubles, s -> s.toArray(JAVA_DOUBLE), (addr, pos) -> (double)doubleHandle.get(addr, 0L, pos));
177 
178         return new Object[][]{
179                 {byteInitializer, byteChecker, bytes},
180                 {charInitializer, charChecker, chars},
181                 {shortInitializer, shortChecker, shorts},
182                 {intInitializer, intChecker, ints},
183                 {floatInitializer, floatChecker, floats},
184                 {longInitializer, longChecker, longs},
185                 {doubleInitializer, doubleChecker, doubles}
186         };
187     }
188 
189     @DataProvider(name = "elemLayouts")
190     public Object[][] elemLayouts() {
191         return new Object[][] {
192                 { JAVA_BYTE, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_BYTE)},
193                 { JAVA_SHORT, (Function<MemorySegment, Object>) s -> s.toArray(JAVA_SHORT)},
194                 { JAVA_CHAR, (Function<MemorySegment, Object>) s -> s.toArray(JAVA_CHAR)},
195                 { JAVA_INT, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_INT)},
196                 { JAVA_FLOAT, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_FLOAT)},
197                 { JAVA_LONG, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_LONG)},
198                 { JAVA_DOUBLE, (Function<MemorySegment, Object>)s -> s.toArray(JAVA_DOUBLE)}
199         };
200     }
201 }