1 /*
 2  * Copyright (c) 2019, 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 import jdk.incubator.foreign.MemoryLayout;
26 import jdk.incubator.foreign.MemorySegment;
27 
28 import java.lang.invoke.VarHandle;
29 
30 import jdk.incubator.foreign.ResourceScope;
31 import jdk.incubator.foreign.ValueLayout;
32 import org.testng.annotations.*;
33 import static org.testng.Assert.*;
34 
35 /*
36  * @test
37  * @run testng/othervm -Xverify:all TestSlices
38  */
39 public class TestSlices {
40 
41     static MemoryLayout LAYOUT = MemoryLayout.sequenceLayout(2,
42             MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_INT));
43 
44     static VarHandle VH_ALL = LAYOUT.varHandle(
45             MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.sequenceElement());
46 
47     @Test(dataProvider = "slices")
48     public void testSlices(VarHandle handle, int lo, int hi, int[] values) {
49         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
50             MemorySegment segment = MemorySegment.allocateNative(LAYOUT, scope);
51             //init
52             for (long i = 0 ; i < 2 ; i++) {
53                 for (long j = 0 ; j < 5 ; j++) {
54                     VH_ALL.set(segment, i, j, (int)j + 1 + ((int)i * 5));
55                 }
56             }
57 
58             checkSlice(segment, handle, lo, hi, values);
59         }
60     }
61 
62     static void checkSlice(MemorySegment segment, VarHandle handle, long i_max, long j_max, int... values) {
63         int index = 0;
64         for (long i = 0 ; i < i_max ; i++) {
65             for (long j = 0 ; j < j_max ; j++) {
66                 int x = (int) handle.get(segment, i, j);
67                 assertEquals(x, values[index++]);
68             }
69         }
70         assertEquals(index, values.length);
71     }
72 
73     @DataProvider(name = "slices")
74     static Object[][] slices() {
75         return new Object[][] {
76                 // x
77                 { VH_ALL, 2, 5, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } },
78                 // x[0::2]
79                 { LAYOUT.varHandle(MemoryLayout.PathElement.sequenceElement(),
80                         MemoryLayout.PathElement.sequenceElement(0, 2)), 2, 3, new int[] { 1, 3, 5, 6, 8, 10 } },
81                 // x[1::2]
82                 { LAYOUT.varHandle(MemoryLayout.PathElement.sequenceElement(),
83                         MemoryLayout.PathElement.sequenceElement(1, 2)), 2, 2, new int[] { 2, 4, 7, 9 } },
84                 // x[4::-2]
85                 { LAYOUT.varHandle(MemoryLayout.PathElement.sequenceElement(),
86                         MemoryLayout.PathElement.sequenceElement(4, -2)), 2, 3, new int[] { 5, 3, 1, 10, 8, 6 } },
87                 // x[3::-2]
88                 { LAYOUT.varHandle(MemoryLayout.PathElement.sequenceElement(),
89                         MemoryLayout.PathElement.sequenceElement(3, -2)), 2, 2, new int[] { 4, 2, 9, 7 } },
90         };
91     }
92 }