Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public override string ToString()
private string ToStringFromEnd()
{
Span<char> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
26 changes: 20 additions & 6 deletions src/libraries/System.Numerics.Tensors/tests/NIndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 18 additions & 6 deletions src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading