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.MemoryHandles;
  26 import jdk.incubator.foreign.MemoryLayout;
  27 import jdk.incubator.foreign.MemoryLayouts;
  28 import jdk.incubator.foreign.MemorySegment;
  29 
  30 import java.lang.invoke.VarHandle;
  31 
  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.ofSequence(2,
  42             MemoryLayout.ofSequence(5, MemoryLayouts.JAVA_INT));
  43 
  44     static VarHandle VH_ALL = LAYOUT.varHandle(int.class,
  45             MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.sequenceElement());
  46 
  47     static VarHandle VH_INT = MemoryLayouts.JAVA_INT.varHandle(int.class);
  48 
  49     @Test(dataProvider = "slices")
  50     public void testSlices(VarHandle handle, int lo, int hi, int[] values) {
  51         try (MemorySegment segment = MemorySegment.allocateNative(LAYOUT)) {
  52             //init
  53             for (long i = 0 ; i < 2 ; i++) {
  54                 for (long j = 0 ; j < 5 ; j++) {
  55                     VH_ALL.set(segment.baseAddress(), i, j, (int)j + 1 + ((int)i * 5));
  56                 }
  57             }
  58 
  59             checkSlice(segment, handle, lo, hi, values);
  60         }
  61     }
  62 
  63     static void checkSlice(MemorySegment segment, VarHandle handle, long i_max, long j_max, int... values) {
  64         int index = 0;
  65         for (long i = 0 ; i < i_max ; i++) {
  66             for (long j = 0 ; j < j_max ; j++) {
  67                 int x = (int) handle.get(segment.baseAddress(), i, j);
  68                 assertEquals(x, values[index++]);
  69             }
  70         }
  71         assertEquals(index, values.length);
  72     }
  73 
  74     @DataProvider(name = "slices")
  75     static Object[][] slices() {
  76         return new Object[][] {
  77                 // x
  78                 { VH_ALL, 2, 5, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } },
  79                 // x[0::2]
  80                 { LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),
  81                         MemoryLayout.PathElement.sequenceElement(0, 2)), 2, 3, new int[] { 1, 3, 5, 6, 8, 10 } },
  82                 { MemoryHandles.withStride(MemoryHandles.withStride(VH_INT, 8), 20), 2, 3, new int[] { 1, 3, 5, 6, 8, 10 } },
  83                 // x[1::2]
  84                 { LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),
  85                         MemoryLayout.PathElement.sequenceElement(1, 2)), 2, 2, new int[] { 2, 4, 7, 9 } },
  86                 { MemoryHandles.withOffset(MemoryHandles.withStride(MemoryHandles.withStride(VH_INT, 8), 20), 4), 2, 2, new int[] { 2, 4, 7, 9 } },
  87                 // x[4::-2]
  88                 { LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),
  89                         MemoryLayout.PathElement.sequenceElement(4, -2)), 2, 3, new int[] { 5, 3, 1, 10, 8, 6 } },
  90                 { MemoryHandles.withOffset(MemoryHandles.withStride(MemoryHandles.withStride(VH_INT, -8), 20), 16), 2, 3, new int[] { 5, 3, 1, 10, 8, 6 } },
  91                 // x[3::-2]
  92                 { LAYOUT.varHandle(int.class, MemoryLayout.PathElement.sequenceElement(),
  93                         MemoryLayout.PathElement.sequenceElement(3, -2)), 2, 2, new int[] { 4, 2, 9, 7 } },
  94                 { MemoryHandles.withOffset(MemoryHandles.withStride(MemoryHandles.withStride(VH_INT, -8), 20), 12), 2, 2, new int[] { 4, 2, 9, 7 } },
  95         };
  96     }
  97 }