Files
dotnet-runtime/src/libraries/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingEncode.cs
T
Hugh Bellamy 0ddf973612 Add AsciiEnccoding tests
- Invalid bytes
- Valid surrogate pairs
- Invalid surrogate pairs
- Invalid chars


Commit migrated from https://github.com/dotnet/corefx/commit/7f7621f597c65b30ae7be93a28f73e2283726b90
2016-04-21 16:57:58 +01:00

66 lines
2.3 KiB
C#

// 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);
}
}
}