8316190: Improve MemorySegment::toString

Reviewed-by: rriggs
This commit is contained in:
Per Minborg 2023-09-15 05:43:57 +00:00
parent 783e44d07e
commit 8dc2d9280e
3 changed files with 26 additions and 2 deletions

View File

@ -505,7 +505,11 @@ public abstract sealed class AbstractMemorySegmentImpl
@Override
public String toString() {
return "MemorySegment{ heapBase: " + heapBase() + " address: 0x" + Long.toHexString(address()) + " byteSize: " + length + " }";
return "MemorySegment{ " +
heapBase().map(hb -> "heapBase: " + hb + ", ").orElse("") +
"address: " + Utils.toHexString(address()) +
", byteSize: " + length +
" }";
}
@Override

View File

@ -273,4 +273,9 @@ public final class Utils {
public static boolean containsNullChars(String s) {
return s.indexOf('\u0000') >= 0;
}
public static String toHexString(long value) {
return "0x" + Long.toHexString(value);
}
}

View File

@ -225,6 +225,21 @@ public class TestSegments {
assertTrue(segment.isAccessibleBy(new Thread()) != isConfined);
}
@Test(dataProvider = "segmentFactories")
public void testToString(Supplier<MemorySegment> segmentSupplier) {
var segment = segmentSupplier.get();
String s = segment.toString();
assertTrue(s.startsWith("MemorySegment{"));
assertTrue(s.contains("address: 0x"));
assertTrue(s.contains("byteSize: "));
if (segment.heapBase().isPresent()) {
assertTrue(s.contains("heapBase: ["));
} else {
assertFalse(s.contains("heapBase: "));
}
assertFalse(s.contains("Optional"));
}
@DataProvider(name = "segmentFactories")
public Object[][] segmentFactories() {
List<Supplier<MemorySegment>> l = List.of(
@ -233,7 +248,7 @@ public class TestSegments {
() -> 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 long[] { 1L, 2L, 3L, 4L } ),
() -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } ),
() -> Arena.ofAuto().allocate(4L, 1),
() -> Arena.ofAuto().allocate(4L, 8),