Path: blob/master/test/jdk/java/foreign/TestSlices.java
41145 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324import jdk.incubator.foreign.MemoryLayout;25import jdk.incubator.foreign.MemoryLayouts;26import jdk.incubator.foreign.MemorySegment;2728import java.lang.invoke.VarHandle;2930import jdk.incubator.foreign.ResourceScope;31import org.testng.annotations.*;32import static org.testng.Assert.*;3334/*35* @test36* @run testng/othervm -Xverify:all TestSlices37*/38public class TestSlices {3940static MemoryLayout LAYOUT = MemoryLayout.sequenceLayout(2,41MemoryLayout.sequenceLayout(5, MemoryLayouts.JAVA_INT));4243static VarHandle VH_ALL = LAYOUT.varHandle(int.class,44MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.sequenceElement());4546@Test(dataProvider = "slices")47public void testSlices(VarHandle handle, int lo, int hi, int[] values) {48try (ResourceScope scope = ResourceScope.newConfinedScope()) {49MemorySegment segment = MemorySegment.allocateNative(LAYOUT, scope);50//init51for (long i = 0 ; i < 2 ; i++) {52for (long j = 0 ; j < 5 ; j++) {53VH_ALL.set(segment, i, j, (int)j + 1 + ((int)i * 5));54}55}5657checkSlice(segment, handle, lo, hi, values);58}59}6061static void checkSlice(MemorySegment segment, VarHandle handle, long i_max, long j_max, int... values) {62int index = 0;63for (long i = 0 ; i < i_max ; i++) {64for (long j = 0 ; j < j_max ; j++) {65int x = (int) handle.get(segment, i, j);66assertEquals(x, values[index++]);67}68}69assertEquals(index, values.length);70}7172@DataProvider(name = "slices")73static Object[][] slices() {74return new Object[][] {75// x76{ VH_ALL, 2, 5, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } },77// x[0::2]78{ LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),79MemoryLayout.PathElement.sequenceElement(0, 2)), 2, 3, new int[] { 1, 3, 5, 6, 8, 10 } },80// x[1::2]81{ LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),82MemoryLayout.PathElement.sequenceElement(1, 2)), 2, 2, new int[] { 2, 4, 7, 9 } },83// x[4::-2]84{ LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),85MemoryLayout.PathElement.sequenceElement(4, -2)), 2, 3, new int[] { 5, 3, 1, 10, 8, 6 } },86// x[3::-2]87{ LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),88MemoryLayout.PathElement.sequenceElement(3, -2)), 2, 2, new int[] { 4, 2, 9, 7 } },89};90}91}929394