Add tests for EncoderFallback and DecoderFallback in ASCIIEncoding

Commit migrated from https://github.com/dotnet/corefx/commit/4b5eafc330a6193e9e8778a1ef1bd0d74d8c43e0
This commit is contained in:
Hugh Bellamy committed 2016-04-26 21:36:16 +01:00
1 parent fcfbbf3682
commit 4e663948b2
3 files changed
+95 -41

No files matched your search

@@ -16,12 +16,6 @@ namespace System.Text.Tests
yield return new object[] { testString, 4, 5 };
yield return new object[] { "ABCDEFGH", 0, 8 };
yield return new object[] { "\u1234\u2345", 0, 2 };
yield return new object[] { "a\u1234\u2345b", 0, 4 };
yield return new object[] { "\uD800\uDC00", 0, 2 };
yield return new object[] { "a\uD800\uDC00b", 0, 2 };
yield return new object[] { string.Empty, 0, 0 };
}
@@ -30,38 +24,70 @@ namespace System.Text.Tests
[MemberData(nameof(Encode_TestData))]
public void Encode(string source, int index, int count)
{
byte[] expectedBytes = new byte[count];
for (int i = 0; i < expectedBytes.Length; i++)
byte[] expected = GetBytes(source, index, count);
EncodingHelpers.Encode(new ASCIIEncoding(), source, index, count, expected);
// Encoding valid chars should not throw with an EncoderExceptionFallback
Encoding exceptionEncoding = Encoding.GetEncoding("ascii", new EncoderExceptionFallback(), new DecoderReplacementFallback("?"));
EncodingHelpers.Encode(exceptionEncoding, source, index, count, expected);
}
public static IEnumerable<object[]> Encode_InvalidChars_TestData()
{
yield return new object[] { "\u1234\u2345", 0, 2 };
yield return new object[] { "a\u1234\u2345b", 0, 4 };
yield return new object[] { "\uD800\uDC00", 0, 2 };
yield return new object[] { "a\uD800\uDC00b", 0, 2 };
}
[Theory]
[MemberData(nameof(Encode_InvalidChars_TestData))]
public void Encode_InvalidChars(string source, int index, int count)
{
byte[] expected = GetBytes(source, index, count);
EncodingHelpers.Encode(new ASCIIEncoding(), source, index, count, expected);
// Encoding invalid chars should throw with an EncoderExceptionFallback
Encoding exceptionEncoding = Encoding.GetEncoding("ascii", new EncoderExceptionFallback(), new DecoderReplacementFallback("?"));
NegativeEncodingTests.Encode_Invalid(exceptionEncoding, source, index, count);
}
[Fact]
public void Encode_InvalidChars()
{
// TODO: move to Encode_InvalidChars_TestData when #7166 is fixed
Encode_InvalidChars("\uD800", 0, 1); // Lone high surrogate
Encode_InvalidChars("\uDC00", 0, 1); // Lone low surrogate
Encode_InvalidChars("\uD800\uDC00", 0, 1); // Surrogate pair out of range
Encode_InvalidChars("\uD800\uDC00", 1, 1); // Surrogate pair out of range
Encode_InvalidChars("\uD800\uD800", 0, 2); // High, high
Encode_InvalidChars("\uDC00\uD800", 0, 2); // Low, high
Encode_InvalidChars("\uDC00\uDC00", 0, 2); // Low, low
// High BMP non-chars
Encode_InvalidChars("\uFFFD", 0, 1);
Encode_InvalidChars("\uFFFE", 0, 1);
Encode_InvalidChars("\uFFFF", 0, 1);
}
private static byte[] GetBytes(string source, int index, int count)
{
byte[] bytes = new byte[count];
for (int i = 0; i < bytes.Length; i++)
{
if (source[i] <= 0x7f)
{
expectedBytes[i] = (byte)source[i + index];
bytes[i] = (byte)source[i + index];
}
else
{
// Verify the fallback character for non-ASCII chars
expectedBytes[i] = (byte)'?';
bytes[i] = (byte)'?';
}
}
EncodingHelpers.Encode(new ASCIIEncoding(), source, index, count, expectedBytes);
}
[Fact]
public void Encode_InvalidUnicode()
{
// TODO: move to Encode_TestData when #7166 is fixedEncode("\uD800", 0, 1, unicodeReplacementBytes1); // Lone high surrogate
Encode("\uDC00", 0, 1); // Lone low surrogate
Encode("\uD800\uDC00", 0, 1); // Surrogate pair out of range
Encode("\uD800\uDC00", 1, 1); // Surrogate pair out of range
Encode("\uD800\uD800", 0, 2); // High, high
Encode("\uDC00\uD800", 0, 2); // Low, high
Encode("\uDC00\uDC00", 0, 2); // Low, low
// High BMP non-chars
Encode("\uFFFD", 0, 1);
Encode("\uFFFE", 0, 1);
Encode("\uFFFF", 0, 1);
return bytes;
}
}
}