1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @enablePreview
* @run testng/othervm TestSegmentOverlap
*/
import java.io.File;
import java.io.IOException;
import java.lang.foreign.Arena;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.function.Supplier;
import java.lang.foreign.MemorySegment;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import static java.lang.System.out;
import static org.testng.Assert.*;
public class TestSegmentOverlap {
static Path tempPath;
static {
try {
File file = File.createTempFile("buffer", "txt");
file.deleteOnExit();
tempPath = file.toPath();
Files.write(file.toPath(), new byte[16], StandardOpenOption.WRITE);
} catch (IOException ex) {
throw new ExceptionInInitializerError(ex);
}
}
@DataProvider(name = "segmentFactories")
public Object[][] segmentFactories() {
List<Supplier<MemorySegment>> l = List.of(
() -> Arena.ofAuto().allocate(16, 1),
() -> {
try (FileChannel fileChannel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
return fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 16L, Arena.ofAuto());
} catch (IOException e) {
throw new RuntimeException(e);
}
},
() -> MemorySegment.ofArray(new byte[] { 0x00, 0x01, 0x02, 0x03 } ),
() -> MemorySegment.ofArray(new char[] {'a', 'b', 'c', 'd' } ),
() -> MemorySegment.ofArray(new double[] { 1d, 2d, 3d, 4d} ),
() -> MemorySegment.ofArray(new float[] { 1.0f, 2.0f, 3.0f, 4.0f } ),
() -> MemorySegment.ofArray(new int[] { 1, 2, 3, 4 }),
() -> MemorySegment.ofArray(new long[] { 1L, 2L, 3L, 4L } ),
() -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } )
);
return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
}
@Test(dataProvider="segmentFactories")
public void testBasic(Supplier<MemorySegment> segmentSupplier) {
var s1 = segmentSupplier.get();
var s2 = segmentSupplier.get();
var sOther = s1.isNative() ? OtherSegmentFactory.HEAP.factory.get()
: OtherSegmentFactory.NATIVE.factory.get();
out.format("testBasic s1:%s, s2:%s, sOther:%s\n", s1, s2, sOther);
assertTrue(s1.asOverlappingSlice(s2).isEmpty());
assertTrue(s2.asOverlappingSlice(s1).isEmpty());
assertTrue(s1.asOverlappingSlice(sOther).isEmpty());
}
@Test(dataProvider="segmentFactories")
public void testIdentical(Supplier<MemorySegment> segmentSupplier) {
var s1 = segmentSupplier.get();
var s2 = s1.asReadOnly();
out.format("testIdentical s1:%s, s2:%s\n", s1, s2);
assertEquals(s1.asOverlappingSlice(s2).get().byteSize(), s1.byteSize());
assertEquals(s1.asOverlappingSlice(s2).get().scope(), s1.scope());
assertEquals(s2.asOverlappingSlice(s1).get().byteSize(), s2.byteSize());
assertEquals(s2.asOverlappingSlice(s1).get().scope(), s2.scope());
if (s1.isNative()) {
assertEquals(s1.asOverlappingSlice(s2).get().address(), s1.address());
assertEquals(s2.asOverlappingSlice(s1).get().address(), s2.address());
}
}
@Test(dataProvider="segmentFactories")
public void testSlices(Supplier<MemorySegment> segmentSupplier) {
MemorySegment s1 = segmentSupplier.get();
MemorySegment s2 = segmentSupplier.get();
for (int offset = 0 ; offset < 4 ; offset++) {
MemorySegment slice = s1.asSlice(offset);
out.format("testSlices s1:%s, s2:%s, slice:%s, offset:%d\n", s1, s2, slice, offset);
assertEquals(s1.asOverlappingSlice(slice).get().byteSize(), s1.byteSize() - offset);
assertEquals(s1.asOverlappingSlice(slice).get().scope(), s1.scope());
assertEquals(slice.asOverlappingSlice(s1).get().byteSize(), slice.byteSize());
assertEquals(slice.asOverlappingSlice(s1).get().scope(), slice.scope());
if (s1.isNative()) {
assertEquals(s1.asOverlappingSlice(slice).get().address(), s1.address() + offset);
assertEquals(slice.asOverlappingSlice(s1).get().address(), slice.address());
}
assertTrue(s2.asOverlappingSlice(slice).isEmpty());
}
}
enum OtherSegmentFactory {
NATIVE(() -> Arena.ofAuto().allocate(16, 1)),
HEAP(() -> MemorySegment.ofArray(new byte[]{16}));
final Supplier<MemorySegment> factory;
OtherSegmentFactory(Supplier<MemorySegment> segmentFactory) {
this.factory = segmentFactory;
}
}
}
|