< prev index next >

test/jdk/java/foreign/TestArrays.java

Print this page

  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,

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)},

  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,

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)},
< prev index next >