1 /*
  2  *  Copyright (c) 2021, 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  * @test
 26  * @run testng TestSegmentOffset
 27  */
 28 
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.ResourceScope;
 31 import org.testng.annotations.DataProvider;
 32 import org.testng.annotations.Test;
 33 import java.util.ArrayList;
 34 import java.util.List;
 35 import java.util.function.IntFunction;
 36 import static java.lang.System.out;
 37 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 38 import static org.testng.Assert.*;
 39 
 40 public class TestSegmentOffset {
 41 
 42     @Test(dataProvider = "slices")
 43     public void testOffset(SegmentSlice s1, SegmentSlice s2) {
 44         if (s1.contains(s2)) {
 45             // check that a segment and its overlapping segment point to same elements
 46             long offset = s1.segment.segmentOffset(s2.segment);
 47             for (int i = 0; i < s2.size(); i++) {
 48                 out.format("testOffset s1:%s, s2:%s, offset:%d, i:%s\n", s1, s2, offset, i);
 49                 byte expected = s2.segment.get(JAVA_BYTE, i);
 50                 byte found = s1.segment.get(JAVA_BYTE, i + offset);
 51                 assertEquals(found, expected);
 52             }
 53         } else if (s1.kind != s2.kind) {
 54             // check that offset from s1 to s2 fails
 55             try {
 56                 long offset = s1.segment.segmentOffset(s2.segment);
 57                 out.format("testOffset s1:%s, s2:%s, offset:%d\n", s1, s2, offset);
 58                 fail("offset unexpectedly passed!");
 59             } catch (UnsupportedOperationException ex) {
 60                 assertTrue(ex.getMessage().contains("Cannot compute offset from native to heap (or vice versa)."));
 61             }
 62         } else if (!s2.contains(s1)) {
 63             // disjoint segments - check that offset is out of bounds
 64             long offset = s1.segment.segmentOffset(s2.segment);
 65             for (int i = 0; i < s2.size(); i++) {
 66                 out.format("testOffset s1:%s, s2:%s, offset:%d, i:%s\n", s1, s2, offset, i);
 67                 s2.segment.get(JAVA_BYTE, i);
 68                 try {
 69                     s1.segment.get(JAVA_BYTE, i + offset);
 70                     fail("Offset on a disjoint segment is not out of bounds!");
 71                 } catch (IndexOutOfBoundsException ex) {
 72                     assertTrue(true);
 73                 }
 74             }
 75         }
 76     }
 77 
 78     static class SegmentSlice {
 79 
 80         enum Kind {
 81             NATIVE(i -> MemorySegment.allocateNative(i, ResourceScope.newConfinedScope())),
 82             ARRAY(i -> MemorySegment.ofArray(new byte[i]));
 83 
 84             final IntFunction<MemorySegment> segmentFactory;
 85 
 86             Kind(IntFunction<MemorySegment> segmentFactory) {
 87                 this.segmentFactory = segmentFactory;
 88             }
 89 
 90             MemorySegment makeSegment(int elems) {
 91                 return segmentFactory.apply(elems);
 92             }
 93         }
 94 
 95         final Kind kind;
 96         final int first;
 97         final int last;
 98         final MemorySegment segment;
 99 
100         public SegmentSlice(Kind kind, int first, int last, MemorySegment segment) {
101             this.kind = kind;
102             this.first = first;
103             this.last = last;
104             this.segment = segment;
105         }
106 
107         boolean contains(SegmentSlice other) {
108             return kind == other.kind &&
109                     first <= other.first &&
110                     last >= other.last;
111         }
112 
113         int size() {
114             return last - first + 1;
115         }
116     }
117 
118     @DataProvider(name = "slices")
119     static Object[][] slices() {
120         int[] sizes = { 16, 8, 4, 2, 1 };
121         List<SegmentSlice> slices = new ArrayList<>();
122         for (SegmentSlice.Kind kind : SegmentSlice.Kind.values()) {
123             // init root segment
124             MemorySegment segment = kind.makeSegment(16);
125             for (int i = 0 ; i < 16 ; i++) {
126                 segment.set(JAVA_BYTE, i, (byte)i);
127             }
128             // compute all slices
129             for (int size : sizes) {
130                 for (int index = 0 ; index < 16 ; index += size) {
131                     MemorySegment slice = segment.asSlice(index, size);
132                     slices.add(new SegmentSlice(kind, index, index + size - 1, slice));
133                 }
134             }
135         }
136         Object[][] sliceArray = new Object[slices.size() * slices.size()][];
137         for (int i = 0 ; i < slices.size() ; i++) {
138             for (int j = 0 ; j < slices.size() ; j++) {
139                 sliceArray[i * slices.size() + j] = new Object[] { slices.get(i), slices.get(j) };
140             }
141         }
142         return sliceArray;
143     }
144 }