From 61f3bbcf3fced1e722da1ada2931eaced2b02f55 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 19 Dec 2024 08:53:16 +1100 Subject: [PATCH] Update PolyfillTests_TryFormat.cs --- src/Tests/PolyfillTests_TryFormat.cs | 409 +++++++++++++++++++++++++-- 1 file changed, 392 insertions(+), 17 deletions(-) diff --git a/src/Tests/PolyfillTests_TryFormat.cs b/src/Tests/PolyfillTests_TryFormat.cs index 989ddff5..539b78e4 100644 --- a/src/Tests/PolyfillTests_TryFormat.cs +++ b/src/Tests/PolyfillTests_TryFormat.cs @@ -10,7 +10,29 @@ public void TryFormatSByte() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSByteSmaller() + { + sbyte value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSByteLarger() + { + sbyte value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -21,7 +43,29 @@ public void TryFormatByte() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatByteSmaller() + { + byte value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatByteLarger() + { + byte value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -32,7 +76,29 @@ public void TryFormatInt16() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt16Smaller() + { + short value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt16Larger() + { + short value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -43,7 +109,29 @@ public void TryFormatUInt16() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt16Smaller() + { + ushort value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt16Larger() + { + ushort value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -54,7 +142,29 @@ public void TryFormatInt32() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt32Smaller() + { + var value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt32Larger() + { + var value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -65,7 +175,29 @@ public void TryFormatUInt32() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt32Smaller() + { + uint value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt32Larger() + { + uint value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -76,7 +208,29 @@ public void TryFormatInt64() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt64Smaller() + { + long value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt64Larger() + { + long value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -90,6 +244,28 @@ public void TryFormatUInt64() Assert.AreEqual(buffer.Length, written); } + [Test] + public void TryFormatUInt64Smaller() + { + ulong value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt64Larger() + { + ulong value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); + } + [Test] public void TryFormatSingle() { @@ -98,7 +274,29 @@ public void TryFormatSingle() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSingleSmaller() + { + float value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSingleLarger() + { + float value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -112,6 +310,28 @@ public void TryFormatGuid() Assert.AreEqual(buffer.Length, written); } + [Test] + public void TryFormatGuidSmaller() + { + var value = new Guid("97008c2d-2114-4396-ae19-392c8e6f8f1b"); + Span buffer = stackalloc char[40]; + var result = value.TryFormat(buffer, out var written); + Assert.True(result); + Assert.AreEqual("97008c2d-2114-4396-ae19-392c8e6f8f1b\0\0\0\0", buffer.ToString()); + Assert.AreEqual(36, written); + } + + [Test] + public void TryFormatGuidLarger() + { + var value = new Guid("97008c2d-2114-4396-ae19-392c8e6f8f1b"); + Span buffer = stackalloc char[35]; + var result = value.TryFormat(buffer, out var written); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(0, written); + } + [Test] public void TryFormatDouble() { @@ -120,7 +340,29 @@ public void TryFormatDouble() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDoubleSmaller() + { + double value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDoubleLarger() + { + double value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -131,7 +373,29 @@ public void TryFormatDecimal() var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("9", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDecimalSmaller() + { + decimal value = 9; + Span buffer = stackalloc char[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", buffer.ToString()); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDecimalLarger() + { + decimal value = 99; + Span buffer = stackalloc char[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -142,7 +406,29 @@ public void TryFormatDateTimeOffset() var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeOffsetSmaller() + { + var value = new DateTimeOffset(new DateTime(2001, 10, 1), TimeSpan.Zero); + Span buffer = stackalloc char[40]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT\0\0\0\0\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeOffsetLarger() + { + var value = new DateTimeOffset(new DateTime(2001, 10, 1), TimeSpan.Zero); + Span buffer = stackalloc char[28]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -153,7 +439,29 @@ public void TryFormatDateTime() var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeSmaller() + { + var value = new DateTime(2001, 10, 1); + Span buffer = stackalloc char[40]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT\0\0\0\0\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeLarger() + { + var value = new DateTime(2001, 10, 1); + Span buffer = stackalloc char[28]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(0, written); } [Test] @@ -164,29 +472,96 @@ public void TryFormatBoolean() var result = value.TryFormat(buffer, out var written); Assert.True(result); Assert.AreEqual("True", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(4, written); + } + + [Test] + public void TryFormatBooleanSmaller() + { + var value = true; + Span buffer = stackalloc char[10]; + var result = value.TryFormat(buffer, out var written); + Assert.True(result); + Assert.AreEqual("True\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(4, written); + } + + [Test] + public void TryFormatBooleanLarger() + { + var value = true; + Span buffer = stackalloc char[3]; + var result = value.TryFormat(buffer, out var written); + Assert.False(result); + Assert.AreEqual("\0\0\0", buffer.ToString()); + Assert.AreEqual(0, written); } #if NET6_0_OR_GREATER [Test] public void TryFormatDate() { - var value = new DateOnly(2001,10,1); + var value = new DateOnly(2001, 10, 1); Span buffer = stackalloc char[16]; var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("Mon, 01 Oct 2001", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(16, written); } + + [Test] + public void TryFormatDateSmaller() + { + var value = new DateOnly(2001, 10, 1); + Span buffer = stackalloc char[20]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001\0\0\0\0", buffer.ToString()); + Assert.AreEqual(16, written); + } + + [Test] + public void TryFormatDateLarger() + { + var value = new DateOnly(2001, 10, 1); + Span buffer = stackalloc char[15]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(0, written); + } + [Test] public void TryFormatTime() { - var value = new TimeOnly(10,1); + var value = new TimeOnly(10, 1); Span buffer = stackalloc char[8]; var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); Assert.True(result); Assert.AreEqual("10:01:00", buffer.ToString()); - Assert.AreEqual(buffer.Length, written); + Assert.AreEqual(8, written); + } + + [Test] + public void TryFormatTimeSmaller() + { + var value = new TimeOnly(10, 1); + Span buffer = stackalloc char[10]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("10:01:00\0\0", buffer.ToString()); + Assert.AreEqual(8, written); + } + + [Test] + public void TryFormatTimeLarger() + { + var value = new TimeOnly(10, 1); + Span buffer = stackalloc char[7]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0", buffer.ToString()); + Assert.AreEqual(0, written); } #endif } \ No newline at end of file