mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Add AsciiEnccoding tests
- Invalid bytes - Valid surrogate pairs - Invalid surrogate pairs - Invalid chars Commit migrated from https://github.com/dotnet/corefx/commit/7f7621f597c65b30ae7be93a28f73e2283726b90
This commit is contained in:
1 parent
891f59bbff
commit
0ddf973612
5 files changed
+115
-92
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Text.Tests
|
||||
{
|
||||
public class ASCIIEncodingEncode
|
||||
{
|
||||
public static IEnumerable<object[]> Encode_TestData()
|
||||
{
|
||||
string testString = "Hello World123#?!";
|
||||
yield return new object[] { testString, 0, testString.Length };
|
||||
yield return new object[] { testString, 4, 5 };
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[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++)
|
||||
{
|
||||
if (source[i] <= 0x7f)
|
||||
{
|
||||
expectedBytes[i] = (byte)source[i + index];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Verify the fallback character for non-ASCII chars
|
||||
expectedBytes[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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user