Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a913bc7

Browse files
authored
Expose and test TryFormat methods on {S}Byte, {U}Int16/32/64, Boolean (#25307)
1 parent 9201969 commit a913bc7

18 files changed

+605
-94
lines changed

src/System.Runtime/ref/System.Runtime.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ public partial struct Boolean : System.IComparable, System.IComparable<bool>, Sy
483483
uint System.IConvertible.ToUInt32(System.IFormatProvider provider) { throw null; }
484484
ulong System.IConvertible.ToUInt64(System.IFormatProvider provider) { throw null; }
485485
public override string ToString() { throw null; }
486+
public bool TryFormat(Span<char> destination, out int charsWritten) { throw null; }
486487
public static bool TryParse(string value, out bool result) { throw null; }
487488
public static bool TryParse(System.ReadOnlySpan<char> value, out bool result) { throw null; }
488489
}
@@ -532,6 +533,7 @@ public partial struct Byte : System.IComparable, System.IComparable<byte>, Syste
532533
public string ToString(System.IFormatProvider provider) { throw null; }
533534
public string ToString(string format) { throw null; }
534535
public string ToString(string format, System.IFormatProvider provider) { throw null; }
536+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
535537
public static bool TryParse(string s, out byte result) { throw null; }
536538
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out byte result) { throw null; }
537539
public static bool TryParse(System.ReadOnlySpan<char> s, out byte result) { throw null; }
@@ -1448,6 +1450,7 @@ public partial struct Int16 : System.IComparable, System.IComparable<short>, Sys
14481450
public string ToString(System.IFormatProvider provider) { throw null; }
14491451
public string ToString(string format) { throw null; }
14501452
public string ToString(string format, System.IFormatProvider provider) { throw null; }
1453+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
14511454
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out short result) { throw null; }
14521455
public static bool TryParse(string s, out short result) { throw null; }
14531456
public static bool TryParse(System.ReadOnlySpan<char> s, out short result) { throw null; }
@@ -1488,6 +1491,7 @@ public partial struct Int32 : System.IComparable, System.IComparable<int>, Syste
14881491
public string ToString(System.IFormatProvider provider) { throw null; }
14891492
public string ToString(string format) { throw null; }
14901493
public string ToString(string format, System.IFormatProvider provider) { throw null; }
1494+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
14911495
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out int result) { throw null; }
14921496
public static bool TryParse(string s, out int result) { throw null; }
14931497
public static bool TryParse(System.ReadOnlySpan<char> s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out int result) { throw null; }
@@ -1528,6 +1532,7 @@ public partial struct Int64 : System.IComparable, System.IComparable<long>, Syst
15281532
public string ToString(System.IFormatProvider provider) { throw null; }
15291533
public string ToString(string format) { throw null; }
15301534
public string ToString(string format, System.IFormatProvider provider) { throw null; }
1535+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
15311536
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out long result) { throw null; }
15321537
public static bool TryParse(string s, out long result) { throw null; }
15331538
public static bool TryParse(System.ReadOnlySpan<char> s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out long result) { throw null; }
@@ -2023,6 +2028,7 @@ public partial struct SByte : System.IComparable, System.IComparable<sbyte>, Sys
20232028
public string ToString(System.IFormatProvider provider) { throw null; }
20242029
public string ToString(string format) { throw null; }
20252030
public string ToString(string format, System.IFormatProvider provider) { throw null; }
2031+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
20262032
[System.CLSCompliantAttribute(false)]
20272033
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out sbyte result) { throw null; }
20282034
[System.CLSCompliantAttribute(false)]
@@ -3053,6 +3059,7 @@ public partial struct UInt16 : System.IComparable, System.IComparable<ushort>, S
30533059
public string ToString(System.IFormatProvider provider) { throw null; }
30543060
public string ToString(string format) { throw null; }
30553061
public string ToString(string format, System.IFormatProvider provider) { throw null; }
3062+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
30563063
[System.CLSCompliantAttribute(false)]
30573064
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out ushort result) { throw null; }
30583065
[System.CLSCompliantAttribute(false)]
@@ -3103,6 +3110,7 @@ public partial struct UInt32 : System.IComparable, System.IComparable<uint>, Sys
31033110
public string ToString(System.IFormatProvider provider) { throw null; }
31043111
public string ToString(string format) { throw null; }
31053112
public string ToString(string format, System.IFormatProvider provider) { throw null; }
3113+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
31063114
[System.CLSCompliantAttribute(false)]
31073115
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out uint result) { throw null; }
31083116
[System.CLSCompliantAttribute(false)]
@@ -3153,6 +3161,7 @@ public partial struct UInt64 : System.IComparable, System.IComparable<ulong>, Sy
31533161
public string ToString(System.IFormatProvider provider) { throw null; }
31543162
public string ToString(string format) { throw null; }
31553163
public string ToString(string format, System.IFormatProvider provider) { throw null; }
3164+
public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, System.IFormatProvider provider = null) { throw null; }
31563165
[System.CLSCompliantAttribute(false)]
31573166
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out ulong result) { throw null; }
31583167
[System.CLSCompliantAttribute(false)]

src/System.Runtime/tests/System/BooleanTests.netcoreapp.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,34 @@ public static void Parse_Span_Invalid(string value, Type exceptionType)
3030
Assert.Equal(false, result);
3131
}
3232
}
33+
34+
[Theory]
35+
[InlineData(true, "True")]
36+
[InlineData(false, "False")]
37+
public static void TryFormat(bool i, string expected)
38+
{
39+
char[] actual;
40+
int charsWritten;
41+
42+
// Just right
43+
actual = new char[expected.Length];
44+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten));
45+
Assert.Equal(expected.Length, charsWritten);
46+
Assert.Equal(expected, new string(actual));
47+
48+
// Longer than needed
49+
actual = new char[expected.Length + 1];
50+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten));
51+
Assert.Equal(expected.Length, charsWritten);
52+
Assert.Equal(expected, new string(actual, 0, charsWritten));
53+
54+
// Too short
55+
if (expected.Length > 0)
56+
{
57+
actual = new char[expected.Length - 1];
58+
Assert.False(i.TryFormat(actual.AsSpan(), out charsWritten));
59+
Assert.Equal(0, charsWritten);
60+
}
61+
}
3362
}
3463
}

src/System.Runtime/tests/System/ByteTests.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,35 @@ public void GetTypeCode_Invoke_ReturnsByte()
8888

8989
public static IEnumerable<object[]> ToString_TestData()
9090
{
91-
NumberFormatInfo emptyFormat = NumberFormatInfo.CurrentInfo;
92-
yield return new object[] { (byte)0, "G", emptyFormat, "0" };
93-
yield return new object[] { (byte)123, "G", emptyFormat, "123" };
94-
yield return new object[] { byte.MaxValue, "G", emptyFormat, "255" };
91+
foreach (NumberFormatInfo emptyFormat in new[] { null, NumberFormatInfo.CurrentInfo })
92+
{
93+
yield return new object[] { (byte)0, "G", emptyFormat, "0" };
94+
yield return new object[] { (byte)123, "G", emptyFormat, "123" };
95+
yield return new object[] { byte.MaxValue, "G", emptyFormat, "255" };
9596

96-
yield return new object[] { (byte)0x24, "x", emptyFormat, "24" };
97-
yield return new object[] { (byte)24, "N", emptyFormat, string.Format("{0:N}", 24.00) };
97+
yield return new object[] { (byte)123, "D", emptyFormat, "123" };
98+
yield return new object[] { (byte)123, "D99", emptyFormat, "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123" };
9899

99-
NumberFormatInfo customFormat = new NumberFormatInfo();
100-
customFormat.NegativeSign = "#";
101-
customFormat.NumberDecimalSeparator = "~";
102-
customFormat.NumberGroupSeparator = "*";
100+
yield return new object[] { (byte)0x24, "x", emptyFormat, "24" };
101+
yield return new object[] { (byte)24, "N", emptyFormat, string.Format("{0:N}", 24.00) };
102+
}
103+
104+
var customFormat = new NumberFormatInfo()
105+
{
106+
NegativeSign = "#",
107+
NumberDecimalSeparator = "~",
108+
NumberGroupSeparator = "*",
109+
PositiveSign = "&",
110+
NumberDecimalDigits = 2,
111+
PercentSymbol = "@",
112+
PercentGroupSeparator = ",",
113+
PercentDecimalSeparator = ".",
114+
PercentDecimalDigits = 5
115+
};
103116
yield return new object[] { (byte)24, "N", customFormat, "24~00" };
117+
yield return new object[] { (byte)123, "E", customFormat, "1~230000E&002" };
118+
yield return new object[] { (byte)123, "F", customFormat, "123~00" };
119+
yield return new object[] { (byte)123, "P", customFormat, "12,300.00000 @" };
104120
}
105121

106122
[Theory]

src/System.Runtime/tests/System/ByteTests.netcoreapp.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,48 @@ public static void Parse_Span_Invalid(string value, NumberStyles style, IFormatP
3131
Assert.Equal(0, result);
3232
}
3333
}
34+
35+
[Theory]
36+
[MemberData(nameof(ToString_TestData))]
37+
public static void TryFormat(byte i, string format, IFormatProvider provider, string expected)
38+
{
39+
char[] actual;
40+
int charsWritten;
41+
42+
// Just right
43+
actual = new char[expected.Length];
44+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
45+
Assert.Equal(expected.Length, charsWritten);
46+
Assert.Equal(expected, new string(actual));
47+
48+
// Longer than needed
49+
actual = new char[expected.Length + 1];
50+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
51+
Assert.Equal(expected.Length, charsWritten);
52+
Assert.Equal(expected, new string(actual, 0, charsWritten));
53+
54+
// Too short
55+
if (expected.Length > 0)
56+
{
57+
actual = new char[expected.Length - 1];
58+
Assert.False(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
59+
Assert.Equal(0, charsWritten);
60+
}
61+
62+
if (format != null)
63+
{
64+
// Upper format
65+
actual = new char[expected.Length];
66+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToUpperInvariant(), provider));
67+
Assert.Equal(expected.Length, charsWritten);
68+
Assert.Equal(expected.ToUpperInvariant(), new string(actual));
69+
70+
// Lower format
71+
actual = new char[expected.Length];
72+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToLowerInvariant(), provider));
73+
Assert.Equal(expected.Length, charsWritten);
74+
Assert.Equal(expected.ToLowerInvariant(), new string(actual));
75+
}
76+
}
3477
}
3578
}

src/System.Runtime/tests/System/Int16Tests.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,40 @@ public void GetTypeCode_Invoke_ReturnsInt16()
9595

9696
public static IEnumerable<object[]> ToString_TestData()
9797
{
98-
NumberFormatInfo emptyFormat = NumberFormatInfo.CurrentInfo;
99-
yield return new object[] { short.MinValue, "G", emptyFormat, "-32768" };
100-
yield return new object[] { (short)-4567, "G", emptyFormat, "-4567" };
101-
yield return new object[] { (short)0, "G", emptyFormat, "0" };
102-
yield return new object[] { (short)4567, "G", emptyFormat, "4567" };
103-
yield return new object[] { short.MaxValue, "G", emptyFormat, "32767" };
98+
foreach (NumberFormatInfo defaultFormat in new[] { null, NumberFormatInfo.CurrentInfo })
99+
{
100+
yield return new object[] { short.MinValue, "G", defaultFormat, "-32768" };
101+
yield return new object[] { (short)-4567, "G", defaultFormat, "-4567" };
102+
yield return new object[] { (short)0, "G", defaultFormat, "0" };
103+
yield return new object[] { (short)4567, "G", defaultFormat, "4567" };
104+
yield return new object[] { short.MaxValue, "G", defaultFormat, "32767" };
104105

105-
yield return new object[] { (short)0x2468, "x", emptyFormat, "2468" };
106-
yield return new object[] { (short)-0x2468, "x", emptyFormat, "DB98" };
106+
yield return new object[] { (short)4567, "D", defaultFormat, "4567" };
107+
yield return new object[] { (short)4567, "D99", defaultFormat, "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004567" };
107108

108-
yield return new object[] { (short)2468, "N", emptyFormat, string.Format("{0:N}", 2468.00) };
109+
yield return new object[] { (short)0x2468, "x", defaultFormat, "2468" };
110+
yield return new object[] { (short)-0x2468, "x", defaultFormat, "db98" };
109111

110-
NumberFormatInfo customFormat = new NumberFormatInfo();
111-
customFormat.NegativeSign = "#";
112-
customFormat.NumberDecimalSeparator = "~";
113-
customFormat.NumberGroupSeparator = "*";
112+
yield return new object[] { (short)2468, "N", defaultFormat, string.Format("{0:N}", 2468.00) };
113+
}
114+
115+
var customFormat = new NumberFormatInfo()
116+
{
117+
NegativeSign = "#",
118+
NumberDecimalSeparator = "~",
119+
NumberGroupSeparator = "*",
120+
PositiveSign = "&",
121+
NumberDecimalDigits = 2,
122+
PercentSymbol = "@",
123+
PercentGroupSeparator = ",",
124+
PercentDecimalSeparator = ".",
125+
PercentDecimalDigits = 5
126+
};
114127
yield return new object[] { (short)-2468, "N", customFormat, "#2*468~00" };
115128
yield return new object[] { (short)2468, "N", customFormat, "2*468~00" };
129+
yield return new object[] { (short)123, "E", customFormat, "1~230000E&002" };
130+
yield return new object[] { (short)123, "F", customFormat, "123~00" };
131+
yield return new object[] { (short)123, "P", customFormat, "12,300.00000 @" };
116132
}
117133

118134
[Theory]

src/System.Runtime/tests/System/Int16Tests.netcoreapp.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,48 @@ public static void Parse_Span_Invalid(string value, NumberStyles style, IFormatP
3131
Assert.Equal(0, result);
3232
}
3333
}
34+
35+
[Theory]
36+
[MemberData(nameof(ToString_TestData))]
37+
public static void TryFormat(short i, string format, IFormatProvider provider, string expected)
38+
{
39+
char[] actual;
40+
int charsWritten;
41+
42+
// Just right
43+
actual = new char[expected.Length];
44+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
45+
Assert.Equal(expected.Length, charsWritten);
46+
Assert.Equal(expected, new string(actual));
47+
48+
// Longer than needed
49+
actual = new char[expected.Length + 1];
50+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
51+
Assert.Equal(expected.Length, charsWritten);
52+
Assert.Equal(expected, new string(actual, 0, charsWritten));
53+
54+
// Too short
55+
if (expected.Length > 0)
56+
{
57+
actual = new char[expected.Length - 1];
58+
Assert.False(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
59+
Assert.Equal(0, charsWritten);
60+
}
61+
62+
if (format != null)
63+
{
64+
// Upper format
65+
actual = new char[expected.Length];
66+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToUpperInvariant(), provider));
67+
Assert.Equal(expected.Length, charsWritten);
68+
Assert.Equal(expected.ToUpperInvariant(), new string(actual));
69+
70+
// Lower format
71+
actual = new char[expected.Length];
72+
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToLowerInvariant(), provider));
73+
Assert.Equal(expected.Length, charsWritten);
74+
Assert.Equal(expected.ToLowerInvariant(), new string(actual));
75+
}
76+
}
3477
}
3578
}

0 commit comments

Comments
 (0)