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