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