diff --git a/src/libraries/System.Numerics.Tensors/src/System/Buffers/NIndex.cs b/src/libraries/System.Numerics.Tensors/src/System/Buffers/NIndex.cs index a3e6a8947eb2c8..0199d29a73f34b 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Buffers/NIndex.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Buffers/NIndex.cs @@ -175,7 +175,7 @@ public override string ToString() private string ToStringFromEnd() { Span span = stackalloc char[21]; // 1 for ^ and 20 for longest possible nuint value - bool formatted = ((uint)Value).TryFormat(span.Slice(1), out int charsWritten); + bool formatted = ((nuint)Value).TryFormat(span.Slice(1), out int charsWritten); Debug.Assert(formatted); span[0] = '^'; return new string(span.Slice(0, charsWritten + 1)); diff --git a/src/libraries/System.Numerics.Tensors/src/System/Buffers/NRange.cs b/src/libraries/System.Numerics.Tensors/src/System/Buffers/NRange.cs index ad452f8385c4b8..2d713ba71fff4b 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Buffers/NRange.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Buffers/NRange.cs @@ -70,7 +70,7 @@ public override string ToString() span[0] = '^'; pos = 1; } - bool formatted = ((uint)Start.Value).TryFormat(span.Slice(pos), out int charsWritten); + bool formatted = ((nuint)Start.Value).TryFormat(span.Slice(pos), out int charsWritten); Debug.Assert(formatted); pos += charsWritten; @@ -81,7 +81,7 @@ public override string ToString() { span[pos++] = '^'; } - formatted = ((uint)End.Value).TryFormat(span.Slice(pos), out charsWritten); + formatted = ((nuint)End.Value).TryFormat(span.Slice(pos), out charsWritten); Debug.Assert(formatted); pos += charsWritten; diff --git a/src/libraries/System.Numerics.Tensors/tests/NIndexTests.cs b/src/libraries/System.Numerics.Tensors/tests/NIndexTests.cs index 193e6b33a1d530..72d41f94951e41 100644 --- a/src/libraries/System.Numerics.Tensors/tests/NIndexTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/NIndexTests.cs @@ -106,14 +106,28 @@ public static void HashCodeTest() Assert.NotEqual(index1.GetHashCode(), index2.GetHashCode()); } - [Fact] - public static void ToStringTest() + [Theory] + [InlineData(0, false, "0")] + [InlineData(100, false, "100")] + [InlineData(0, true, "^0")] + [InlineData(50, true, "^50")] + [InlineData(int.MaxValue, false, "2147483647")] + [InlineData(int.MaxValue, true, "^2147483647")] + public static void ToStringTest(long value, bool fromEnd, string expected) { - NIndex index1 = 100; - Assert.Equal(100.ToString(), index1.ToString()); + NIndex index = new NIndex((nint)value, fromEnd); + Assert.Equal(expected, index.ToString()); + } - index1 = new NIndex(50, fromEnd: true); - Assert.Equal("^" + 50.ToString(), index1.ToString()); + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))] + [InlineData(1L + uint.MaxValue, false, "4294967296")] + [InlineData(1L + uint.MaxValue, true, "^4294967296")] + [InlineData(long.MaxValue, false, "9223372036854775807")] + [InlineData(long.MaxValue, true, "^9223372036854775807")] + public static void ToStringTest_64bit(long value, bool fromEnd, string expected) + { + NIndex index = new NIndex((nint)value, fromEnd); + Assert.Equal(expected, index.ToString()); } [Fact] diff --git a/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs b/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs index eb5d7ba88ae45e..37231c6f1986d1 100644 --- a/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs @@ -105,14 +105,26 @@ public static void HashCodeTest() Assert.NotEqual(NativeRange1.GetHashCode(), NativeRange2.GetHashCode()); } - [Fact] - public static void ToStringTest() + [Theory] + [InlineData(10, false, 20, false, "10..20")] + [InlineData(10, false, 20, true, "10..^20")] + [InlineData(0, true, 0, true, "^0..^0")] + [InlineData(5, true, 10, false, "^5..10")] + [InlineData(int.MaxValue, false, int.MaxValue, true, "2147483647..^2147483647")] + public static void ToStringTest(long startValue, bool startFromEnd, long endValue, bool endFromEnd, string expected) { - NRange NativeRange1 = new NRange(new NIndex(10, fromEnd: false), new NIndex(20, fromEnd: false)); - Assert.Equal(10.ToString() + ".." + 20.ToString(), NativeRange1.ToString()); + NRange range = new NRange(new NIndex((nint)startValue, startFromEnd), new NIndex((nint)endValue, endFromEnd)); + Assert.Equal(expected, range.ToString()); + } - NativeRange1 = new NRange(new NIndex(10, fromEnd: false), new NIndex(20, fromEnd: true)); - Assert.Equal(10.ToString() + "..^" + 20.ToString(), NativeRange1.ToString()); + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))] + [InlineData(1L + uint.MaxValue, false, 1L + uint.MaxValue, true, "4294967296..^4294967296")] + [InlineData(long.MaxValue, false, long.MaxValue, true, "9223372036854775807..^9223372036854775807")] + [InlineData(1L + uint.MaxValue, true, long.MaxValue, false, "^4294967296..9223372036854775807")] + public static void ToStringTest_64bit(long startValue, bool startFromEnd, long endValue, bool endFromEnd, string expected) + { + NRange range = new NRange(new NIndex((nint)startValue, startFromEnd), new NIndex((nint)endValue, endFromEnd)); + Assert.Equal(expected, range.ToString()); } [Fact]