未验证 提交 f2148079 编写于 作者: C Cyl18 提交者: GitHub

Add APIs to make nint/nuint match Int32 and others (#47595)

* Add APIs to make nint/nuint match Int32 and others

* Add default argument to nint/nuint TryFormat

* Fix Api Compat error

* Add test to nint/nuint

* fix test error
上级 dcef2897
...@@ -207,13 +207,14 @@ public unsafe int CompareTo(object? value) ...@@ -207,13 +207,14 @@ public unsafe int CompareTo(object? value)
public unsafe string ToString(IFormatProvider? provider) => ((nint_t)_value).ToString(provider); public unsafe string ToString(IFormatProvider? provider) => ((nint_t)_value).ToString(provider);
public unsafe string ToString(string? format, IFormatProvider? provider) => ((nint_t)_value).ToString(format, provider); public unsafe string ToString(string? format, IFormatProvider? provider) => ((nint_t)_value).ToString(format, provider);
unsafe bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) => public unsafe bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null) =>
((nint_t)_value).TryFormat(destination, out charsWritten, format, provider); ((nint_t)_value).TryFormat(destination, out charsWritten, format, provider);
public static IntPtr Parse(string s) => (IntPtr)nint_t.Parse(s); public static IntPtr Parse(string s) => (IntPtr)nint_t.Parse(s);
public static IntPtr Parse(string s, NumberStyles style) => (IntPtr)nint_t.Parse(s, style); public static IntPtr Parse(string s, NumberStyles style) => (IntPtr)nint_t.Parse(s, style);
public static IntPtr Parse(string s, IFormatProvider? provider) => (IntPtr)nint_t.Parse(s, provider); public static IntPtr Parse(string s, IFormatProvider? provider) => (IntPtr)nint_t.Parse(s, provider);
public static IntPtr Parse(string s, NumberStyles style, IFormatProvider? provider) => (IntPtr)nint_t.Parse(s, style, provider); public static IntPtr Parse(string s, NumberStyles style, IFormatProvider? provider) => (IntPtr)nint_t.Parse(s, style, provider);
public static IntPtr Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null) => (IntPtr)nint_t.Parse(s, style, provider);
public static bool TryParse(string? s, out IntPtr result) public static bool TryParse(string? s, out IntPtr result)
{ {
...@@ -226,5 +227,17 @@ public static bool TryParse(string? s, NumberStyles style, IFormatProvider? prov ...@@ -226,5 +227,17 @@ public static bool TryParse(string? s, NumberStyles style, IFormatProvider? prov
Unsafe.SkipInit(out result); Unsafe.SkipInit(out result);
return nint_t.TryParse(s, style, provider, out Unsafe.As<IntPtr, nint_t>(ref result)); return nint_t.TryParse(s, style, provider, out Unsafe.As<IntPtr, nint_t>(ref result));
} }
public static bool TryParse(ReadOnlySpan<char> s, out IntPtr result)
{
Unsafe.SkipInit(out result);
return nint_t.TryParse(s, out Unsafe.As<IntPtr, nint_t>(ref result));
}
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out IntPtr result)
{
Unsafe.SkipInit(out result);
return nint_t.TryParse(s, style, provider, out Unsafe.As<IntPtr, nint_t>(ref result));
}
} }
} }
...@@ -199,13 +199,14 @@ public unsafe int CompareTo(object? value) ...@@ -199,13 +199,14 @@ public unsafe int CompareTo(object? value)
public unsafe string ToString(IFormatProvider? provider) => ((nuint_t)_value).ToString(provider); public unsafe string ToString(IFormatProvider? provider) => ((nuint_t)_value).ToString(provider);
public unsafe string ToString(string? format, IFormatProvider? provider) => ((nuint_t)_value).ToString(format, provider); public unsafe string ToString(string? format, IFormatProvider? provider) => ((nuint_t)_value).ToString(format, provider);
unsafe bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) => public unsafe bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null) =>
((nuint_t)_value).TryFormat(destination, out charsWritten, format, provider); ((nuint_t)_value).TryFormat(destination, out charsWritten, format, provider);
public static UIntPtr Parse(string s) => (UIntPtr)nuint_t.Parse(s); public static UIntPtr Parse(string s) => (UIntPtr)nuint_t.Parse(s);
public static UIntPtr Parse(string s, NumberStyles style) => (UIntPtr)nuint_t.Parse(s, style); public static UIntPtr Parse(string s, NumberStyles style) => (UIntPtr)nuint_t.Parse(s, style);
public static UIntPtr Parse(string s, IFormatProvider? provider) => (UIntPtr)nuint_t.Parse(s, provider); public static UIntPtr Parse(string s, IFormatProvider? provider) => (UIntPtr)nuint_t.Parse(s, provider);
public static UIntPtr Parse(string s, NumberStyles style, IFormatProvider? provider) => (UIntPtr)nuint_t.Parse(s, style, provider); public static UIntPtr Parse(string s, NumberStyles style, IFormatProvider? provider) => (UIntPtr)nuint_t.Parse(s, style, provider);
public static UIntPtr Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null) => (UIntPtr)nuint_t.Parse(s, style, provider);
public static bool TryParse(string? s, out UIntPtr result) public static bool TryParse(string? s, out UIntPtr result)
{ {
...@@ -218,5 +219,17 @@ public static bool TryParse(string? s, NumberStyles style, IFormatProvider? prov ...@@ -218,5 +219,17 @@ public static bool TryParse(string? s, NumberStyles style, IFormatProvider? prov
Unsafe.SkipInit(out result); Unsafe.SkipInit(out result);
return nuint_t.TryParse(s, style, provider, out Unsafe.As<UIntPtr, nuint_t>(ref result)); return nuint_t.TryParse(s, style, provider, out Unsafe.As<UIntPtr, nuint_t>(ref result));
} }
public static bool TryParse(ReadOnlySpan<char> s, out UIntPtr result)
{
Unsafe.SkipInit(out result);
return nuint_t.TryParse(s, out Unsafe.As<UIntPtr, nuint_t>(ref result));
}
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out UIntPtr result)
{
Unsafe.SkipInit(out result);
return nuint_t.TryParse(s, style, provider, out Unsafe.As<UIntPtr, nuint_t>(ref result));
}
} }
} }
...@@ -2534,10 +2534,12 @@ public sealed partial class InsufficientMemoryException : System.OutOfMemoryExce ...@@ -2534,10 +2534,12 @@ public sealed partial class InsufficientMemoryException : System.OutOfMemoryExce
public unsafe static explicit operator System.IntPtr (void* value) { throw null; } public unsafe static explicit operator System.IntPtr (void* value) { throw null; }
public static bool operator !=(System.IntPtr value1, System.IntPtr value2) { throw null; } public static bool operator !=(System.IntPtr value1, System.IntPtr value2) { throw null; }
public static System.IntPtr operator -(System.IntPtr pointer, int offset) { throw null; } public static System.IntPtr operator -(System.IntPtr pointer, int offset) { throw null; }
public bool TryFormat(System.Span<char> destination, out int charsWritten, System.ReadOnlySpan<char> format = default(System.ReadOnlySpan<char>), System.IFormatProvider? provider = null) { throw null; }
public static System.IntPtr Parse(string s) { throw null; } public static System.IntPtr Parse(string s) { throw null; }
public static System.IntPtr Parse(string s, System.Globalization.NumberStyles style) { throw null; } public static System.IntPtr Parse(string s, System.Globalization.NumberStyles style) { throw null; }
public static System.IntPtr Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; } public static System.IntPtr Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; }
public static System.IntPtr Parse(string s, System.IFormatProvider? provider) { throw null; } public static System.IntPtr Parse(string s, System.IFormatProvider? provider) { throw null; }
public static System.IntPtr Parse(System.ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, System.IFormatProvider? provider = null) { throw null; }
public static System.IntPtr Subtract(System.IntPtr pointer, int offset) { throw null; } public static System.IntPtr Subtract(System.IntPtr pointer, int offset) { throw null; }
void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public int ToInt32() { throw null; } public int ToInt32() { throw null; }
...@@ -2550,6 +2552,8 @@ public sealed partial class InsufficientMemoryException : System.OutOfMemoryExce ...@@ -2550,6 +2552,8 @@ public sealed partial class InsufficientMemoryException : System.OutOfMemoryExce
public string ToString(string? format, System.IFormatProvider? provider) { throw null; } public string ToString(string? format, System.IFormatProvider? provider) { throw null; }
public static bool TryParse(string? s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.IntPtr result) { throw null; } public static bool TryParse(string? s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.IntPtr result) { throw null; }
public static bool TryParse(string? s, out System.IntPtr result) { throw null; } public static bool TryParse(string? s, out System.IntPtr result) { throw null; }
public static bool TryParse(System.ReadOnlySpan<char> s, out System.IntPtr result) { throw null; }
public static bool TryParse(System.ReadOnlySpan<char> s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.IntPtr result) { throw null; }
} }
public partial class InvalidCastException : System.SystemException public partial class InvalidCastException : System.SystemException
{ {
...@@ -4635,10 +4639,12 @@ public partial class TypeUnloadedException : System.SystemException ...@@ -4635,10 +4639,12 @@ public partial class TypeUnloadedException : System.SystemException
public unsafe static explicit operator System.UIntPtr (void* value) { throw null; } public unsafe static explicit operator System.UIntPtr (void* value) { throw null; }
public static bool operator !=(System.UIntPtr value1, System.UIntPtr value2) { throw null; } public static bool operator !=(System.UIntPtr value1, System.UIntPtr value2) { throw null; }
public static System.UIntPtr operator -(System.UIntPtr pointer, int offset) { throw null; } public static System.UIntPtr operator -(System.UIntPtr pointer, int offset) { throw null; }
public bool TryFormat(System.Span<char> destination, out int charsWritten, System.ReadOnlySpan<char> format = default(System.ReadOnlySpan<char>), System.IFormatProvider? provider = null) { throw null; }
public static System.UIntPtr Parse(string s) { throw null; } public static System.UIntPtr Parse(string s) { throw null; }
public static System.UIntPtr Parse(string s, System.Globalization.NumberStyles style) { throw null; } public static System.UIntPtr Parse(string s, System.Globalization.NumberStyles style) { throw null; }
public static System.UIntPtr Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; } public static System.UIntPtr Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; }
public static System.UIntPtr Parse(string s, System.IFormatProvider? provider) { throw null; } public static System.UIntPtr Parse(string s, System.IFormatProvider? provider) { throw null; }
public static System.UIntPtr Parse(System.ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, System.IFormatProvider? provider = null) { throw null; }
public static System.UIntPtr Subtract(System.UIntPtr pointer, int offset) { throw null; } public static System.UIntPtr Subtract(System.UIntPtr pointer, int offset) { throw null; }
void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public unsafe void* ToPointer() { throw null; } public unsafe void* ToPointer() { throw null; }
...@@ -4650,6 +4656,8 @@ public partial class TypeUnloadedException : System.SystemException ...@@ -4650,6 +4656,8 @@ public partial class TypeUnloadedException : System.SystemException
public ulong ToUInt64() { throw null; } public ulong ToUInt64() { throw null; }
public static bool TryParse(string? s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.UIntPtr result) { throw null; } public static bool TryParse(string? s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.UIntPtr result) { throw null; }
public static bool TryParse(string? s, out System.UIntPtr result) { throw null; } public static bool TryParse(string? s, out System.UIntPtr result) { throw null; }
public static bool TryParse(System.ReadOnlySpan<char> s, out System.UIntPtr result) { throw null; }
public static bool TryParse(System.ReadOnlySpan<char> s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.UIntPtr result) { throw null; }
} }
public partial class UnauthorizedAccessException : System.SystemException public partial class UnauthorizedAccessException : System.SystemException
{ {
......
...@@ -831,7 +831,7 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData() ...@@ -831,7 +831,7 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData()
{ {
foreach (object[] inputs in Parse_Valid_TestData()) foreach (object[] inputs in Parse_Valid_TestData())
{ {
yield return new object[] { inputs[0], (IntPtr)0, ((string)inputs[0]).Length, inputs[1], inputs[2], inputs[3] }; yield return new object[] { inputs[0], 0, ((string)inputs[0]).Length, inputs[1], inputs[2], inputs[3] };
} }
NumberFormatInfo samePositiveNegativeFormat = new NumberFormatInfo() NumberFormatInfo samePositiveNegativeFormat = new NumberFormatInfo()
...@@ -844,18 +844,18 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData() ...@@ -844,18 +844,18 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData()
NumberFormatInfo emptyNegativeFormat = new NumberFormatInfo() { NegativeSign = "" }; NumberFormatInfo emptyNegativeFormat = new NumberFormatInfo() { NegativeSign = "" };
// None // None
yield return new object[] { "2147483647", (IntPtr)1, (IntPtr)9, NumberStyles.None, null, (IntPtr)147483647 }; yield return new object[] { "2147483647", 1, 9, NumberStyles.None, null, (IntPtr)147483647 };
yield return new object[] { "2147483647", (IntPtr)1, (IntPtr)1, NumberStyles.None, null, (IntPtr)1 }; yield return new object[] { "2147483647", 1, 1, NumberStyles.None, null, (IntPtr)1 };
yield return new object[] { "123\0\0", (IntPtr)2, (IntPtr)2, NumberStyles.None, null, (IntPtr)3 }; yield return new object[] { "123\0\0", 2, 2, NumberStyles.None, null, (IntPtr)3 };
// Hex // Hex
yield return new object[] { "abc", (IntPtr)0, (IntPtr)1, NumberStyles.HexNumber, null, (IntPtr)0xa }; yield return new object[] { "abc", 0, 1, NumberStyles.HexNumber, null, (IntPtr)0xa };
yield return new object[] { "ABC", (IntPtr)1, (IntPtr)1, NumberStyles.HexNumber, null, (IntPtr)0xB }; yield return new object[] { "ABC", 1, 1, NumberStyles.HexNumber, null, (IntPtr)0xB };
yield return new object[] { "FFFFFFFF", (IntPtr)6, (IntPtr)2, NumberStyles.HexNumber, null, (IntPtr)0xFF }; yield return new object[] { "FFFFFFFF", 6, 2, NumberStyles.HexNumber, null, (IntPtr)0xFF };
yield return new object[] { "FFFFFFFF", (IntPtr)0, (IntPtr)1, NumberStyles.HexNumber, null, (IntPtr)0xF }; yield return new object[] { "FFFFFFFF", 0, 1, NumberStyles.HexNumber, null, (IntPtr)0xF };
// Currency // Currency
yield return new object[] { "-$1000", (IntPtr)1, (IntPtr)5, NumberStyles.Currency, new NumberFormatInfo() yield return new object[] { "-$1000", 1, 5, NumberStyles.Currency, new NumberFormatInfo()
{ {
CurrencySymbol = "$", CurrencySymbol = "$",
CurrencyGroupSeparator = "|", CurrencyGroupSeparator = "|",
...@@ -863,8 +863,8 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData() ...@@ -863,8 +863,8 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData()
}, (IntPtr)1000 }; }, (IntPtr)1000 };
NumberFormatInfo emptyCurrencyFormat = new NumberFormatInfo() { CurrencySymbol = "" }; NumberFormatInfo emptyCurrencyFormat = new NumberFormatInfo() { CurrencySymbol = "" };
yield return new object[] { "100", (IntPtr)1, (IntPtr)2, NumberStyles.Currency, emptyCurrencyFormat, (IntPtr)0 }; yield return new object[] { "100", 1, 2, NumberStyles.Currency, emptyCurrencyFormat, (IntPtr)0 };
yield return new object[] { "100", (IntPtr)0, (IntPtr)1, NumberStyles.Currency, emptyCurrencyFormat, (IntPtr)1 }; yield return new object[] { "100", 0, 1, NumberStyles.Currency, emptyCurrencyFormat, (IntPtr)1 };
// If CurrencySymbol and Negative are the same, NegativeSign is preferred // If CurrencySymbol and Negative are the same, NegativeSign is preferred
NumberFormatInfo sameCurrencyNegativeSignFormat = new NumberFormatInfo() NumberFormatInfo sameCurrencyNegativeSignFormat = new NumberFormatInfo()
...@@ -872,23 +872,107 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData() ...@@ -872,23 +872,107 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData()
NegativeSign = "|", NegativeSign = "|",
CurrencySymbol = "|" CurrencySymbol = "|"
}; };
yield return new object[] { "1000", (IntPtr)1, (IntPtr)3, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowLeadingSign, sameCurrencyNegativeSignFormat, (IntPtr)0 }; yield return new object[] { "1000", 1, 3, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowLeadingSign, sameCurrencyNegativeSignFormat, (IntPtr)0 };
yield return new object[] { "|1000", (IntPtr)0, (IntPtr)2, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowLeadingSign, sameCurrencyNegativeSignFormat, -1 }; yield return new object[] { "|1000", 0, 2, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowLeadingSign, sameCurrencyNegativeSignFormat, (IntPtr)(-1) };
// Any // Any
yield return new object[] { "123", (IntPtr)0, (IntPtr)2, NumberStyles.Any, null, (IntPtr)12 }; yield return new object[] { "123", 0, 2, NumberStyles.Any, null, (IntPtr)12 };
// AllowLeadingSign // AllowLeadingSign
yield return new object[] { "-2147483648", (IntPtr)0, (IntPtr)10, NumberStyles.AllowLeadingSign, null, -214748364 }; yield return new object[] { "-2147483648", 0, 10, NumberStyles.AllowLeadingSign, null, (IntPtr)(-214748364) };
// AllowTrailingSign // AllowTrailingSign
yield return new object[] { "123-", (IntPtr)0, (IntPtr)3, NumberStyles.AllowTrailingSign, null, (IntPtr)123 }; yield return new object[] { "123-", 0, 3, NumberStyles.AllowTrailingSign, null, (IntPtr)123 };
// AllowExponent // AllowExponent
yield return new object[] { "1E2", (IntPtr)0, (IntPtr)1, NumberStyles.AllowExponent, null, (IntPtr)1 }; yield return new object[] { "1E2", 0, 1, NumberStyles.AllowExponent, null, (IntPtr)1 };
yield return new object[] { "1E+2", (IntPtr)3, (IntPtr)1, NumberStyles.AllowExponent, null, (IntPtr)2 }; yield return new object[] { "1E+2", 3, 1, NumberStyles.AllowExponent, null, (IntPtr)2 };
yield return new object[] { "(1E2)", (IntPtr)1, (IntPtr)3, NumberStyles.AllowExponent | NumberStyles.AllowParentheses, null, (IntPtr)1E2 }; yield return new object[] { "(1E2)", 1, 3, NumberStyles.AllowExponent | NumberStyles.AllowParentheses, null, (IntPtr)1E2 };
yield return new object[] { "-1E2", (IntPtr)1, (IntPtr)3, NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, null, (IntPtr)1E2 }; yield return new object[] { "-1E2", 1, 3, NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, null, (IntPtr)1E2 };
}
[Theory]
[MemberData(nameof(Parse_ValidWithOffsetCount_TestData))]
public static void Parse_Span_Valid(string value, int offset, int count, NumberStyles style, IFormatProvider provider, IntPtr expected)
{
IntPtr result;
// Default style and provider
if (style == NumberStyles.Integer && provider == null)
{
Assert.True(IntPtr.TryParse(value.AsSpan(offset, count), out result));
Assert.Equal(expected, result);
}
Assert.Equal(expected, IntPtr.Parse(value.AsSpan(offset, count), style, provider));
Assert.True(IntPtr.TryParse(value.AsSpan(offset, count), style, provider, out result));
Assert.Equal(expected, result);
}
[Theory]
[MemberData(nameof(Parse_Invalid_TestData))]
public static void Parse_Span_Invalid(string value, NumberStyles style, IFormatProvider provider, Type exceptionType)
{
if (value != null)
{
IntPtr result;
// Default style and provider
if (style == NumberStyles.Integer && provider == null)
{
Assert.False(IntPtr.TryParse(value.AsSpan(), out result));
Assert.Equal(default, result);
}
Assert.Throws(exceptionType, () => int.Parse(value.AsSpan(), style, provider));
Assert.False(IntPtr.TryParse(value.AsSpan(), style, provider, out result));
Assert.Equal(default, result);
}
}
[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(IntPtr i, string format, IFormatProvider provider, string expected)
{
char[] actual;
int charsWritten;
// Just right
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected, new string(actual));
// Longer than needed
actual = new char[expected.Length + 1];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected, new string(actual, 0, charsWritten));
// Too short
if (expected.Length > 0)
{
actual = new char[expected.Length - 1];
Assert.False(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(0, charsWritten);
}
if (format != null)
{
// Upper format
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToUpperInvariant(), provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected.ToUpperInvariant(), new string(actual));
// Lower format
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToLowerInvariant(), provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected.ToLowerInvariant(), new string(actual));
}
} }
} }
} }
...@@ -456,11 +456,95 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData() ...@@ -456,11 +456,95 @@ public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData()
yield return new object[] { "123", 0, 2, NumberStyles.Integer, null, (UIntPtr)12 }; yield return new object[] { "123", 0, 2, NumberStyles.Integer, null, (UIntPtr)12 };
yield return new object[] { "123", 1, 2, NumberStyles.Integer, null, (UIntPtr)23 }; yield return new object[] { "123", 1, 2, NumberStyles.Integer, null, (UIntPtr)23 };
yield return new object[] { "4294967295", 0, 1, NumberStyles.Integer, null, 4 }; yield return new object[] { "4294967295", 0, 1, NumberStyles.Integer, null, (UIntPtr)4 };
yield return new object[] { "4294967295", 9, 1, NumberStyles.Integer, null, 5 }; yield return new object[] { "4294967295", 9, 1, NumberStyles.Integer, null, (UIntPtr)5 };
yield return new object[] { "12", 0, 1, NumberStyles.HexNumber, null, (UIntPtr)0x1 }; yield return new object[] { "12", 0, 1, NumberStyles.HexNumber, null, (UIntPtr)0x1 };
yield return new object[] { "12", 1, 1, NumberStyles.HexNumber, null, (UIntPtr)0x2 }; yield return new object[] { "12", 1, 1, NumberStyles.HexNumber, null, (UIntPtr)0x2 };
yield return new object[] { "$1,000", 1, 3, NumberStyles.Currency, new NumberFormatInfo() { CurrencySymbol = "$" }, (UIntPtr)10 }; yield return new object[] { "$1,000", 1, 3, NumberStyles.Currency, new NumberFormatInfo() { CurrencySymbol = "$" }, (UIntPtr)10 };
} }
[Theory]
[MemberData(nameof(Parse_ValidWithOffsetCount_TestData))]
public static void Parse_Span_Valid(string value, int offset, int count, NumberStyles style, IFormatProvider provider, UIntPtr expected)
{
UIntPtr result;
// Default style and provider
if (style == NumberStyles.Integer && provider == null)
{
Assert.True(UIntPtr.TryParse(value.AsSpan(offset, count), out result));
Assert.Equal(expected, result);
}
Assert.Equal(expected, UIntPtr.Parse(value.AsSpan(offset, count), style, provider));
Assert.True(UIntPtr.TryParse(value.AsSpan(offset, count), style, provider, out result));
Assert.Equal(expected, result);
}
[Theory]
[MemberData(nameof(Parse_Invalid_TestData))]
public static void Parse_Span_Invalid(string value, NumberStyles style, IFormatProvider provider, Type exceptionType)
{
if (value != null)
{
UIntPtr result;
// Default style and provider
if (style == NumberStyles.Integer && provider == null)
{
Assert.False(UIntPtr.TryParse(value.AsSpan(), out result));
Assert.Equal(default, result);
}
Assert.Throws(exceptionType, () => UIntPtr.Parse(value.AsSpan(), style, provider));
Assert.False(UIntPtr.TryParse(value.AsSpan(), style, provider, out result));
Assert.Equal(default, result);
}
}
[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(UIntPtr i, string format, IFormatProvider provider, string expected)
{
char[] actual;
int charsWritten;
// Just right
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected, new string(actual));
// Longer than needed
actual = new char[expected.Length + 1];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected, new string(actual, 0, charsWritten));
// Too short
if (expected.Length > 0)
{
actual = new char[expected.Length - 1];
Assert.False(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(0, charsWritten);
}
if (format != null)
{
// Upper format
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToUpperInvariant(), provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected.ToUpperInvariant(), new string(actual));
// Lower format
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToLowerInvariant(), provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected.ToLowerInvariant(), new string(actual));
}
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册