--- /dev/null
+// 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.Linq;
+using Xunit;
+
+namespace System.Text.Tests
+{
+ public partial class ASCIIEncodingEncode
+ {
+ [Theory]
+ [InlineData("hello!", 6)]
+ [InlineData("hello\u0080there!", 16)]
+ [InlineData("\u00ff\u00ff", 10)]
+ public void GetCharCount_WithReplacementFallback(string input, int expectedCharCount)
+ {
+ Encoding encoding = Encoding.GetEncoding("ascii", EncoderFallback.ExceptionFallback, new DecoderReplacementFallback("abcde"));
+ Assert.Equal(expectedCharCount, encoding.GetCharCount(WideToNarrowStr(input)));
+ }
+
+ [Fact]
+ public void GetCharCount_WithInvalidFallbackBuffer_ValidatesAscii()
+ {
+ // Internal fallback logic should notice that we're about to write out a standalone
+ // surrogate character and should abort the operation.
+
+ Encoding encoding = Encoding.GetEncoding("ascii", EncoderFallback.ExceptionFallback, new StandaloneLowSurrogateDecoderFallback());
+ Assert.Throws<ArgumentException>(() => encoding.GetCharCount(new byte[] { 0x80 }));
+ }
+
+ [Theory]
+ [InlineData("hello!", "hello!")]
+ [InlineData("hello\u0080there!", "helloabcdethere!")]
+ [InlineData("\u00ff\u00ff", "abcdeabcde")]
+ public void GetChars_WithReplacementFallback(string input, string expectedResult)
+ {
+ Encoding encoding = Encoding.GetEncoding("ascii", EncoderFallback.ExceptionFallback, new DecoderReplacementFallback("abcde"));
+ Assert.Equal(expectedResult, encoding.GetChars(WideToNarrowStr(input)));
+ }
+
+ [Fact]
+ public void GetChars_WithNonAsciiInput_AndSingleCharNonAsciiReplacementFallback_Throws()
+ {
+ // Internal fallback logic should notice that we're about to write out a standalone
+ // surrogate character and should abort the operation.
+
+ Encoding encoding = Encoding.GetEncoding("ascii", EncoderFallback.ExceptionFallback, new StandaloneLowSurrogateDecoderFallback());
+ Assert.Throws<ArgumentException>(() => encoding.GetChars(new byte[] { 0x80 }));
+ }
+
+ private static byte[] WideToNarrowStr(string input)
+ {
+ return input.Select(ch => checked((byte)ch)).ToArray(); // makes sure each char is 00..FF
+ }
+
+ private class StandaloneLowSurrogateDecoderFallback : DecoderFallback
+ {
+ public override int MaxCharCount => 1;
+
+ public override DecoderFallbackBuffer CreateFallbackBuffer()
+ {
+ return new InnerFallbackBuffer();
+ }
+
+ private class InnerFallbackBuffer : DecoderFallbackBuffer
+ {
+ private int _remaining;
+
+ public override int Remaining => _remaining;
+
+ public override bool Fallback(byte[] bytesUnknown, int index)
+ {
+ _remaining = 1;
+ return true;
+ }
+
+ public override char GetNextChar()
+ {
+ // Return a standalone low surrogate
+ return (--_remaining >= 0) ? '\udc00' : default;
+ }
+
+ public override bool MovePrevious()
+ {
+ throw new NotImplementedException();
+ }
+ }
+ }
+ }
+}
// Call Convert to convert partial of a Unicode character array with UTF8 encoder
[Fact]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "coreclr #23020 is not fixed in netfx.")]
public void EncoderUTF8ConvertUnicodeCharArrayPartial()
{
char[] chars = "\uD83D\uDE01Test".ToCharArray();
byte[] bytes = new byte[chars.Length * 2];
Encoder encoder = Encoding.UTF8.GetEncoder();
- VerificationHelper(encoder, chars, 0, 1, bytes, 0, 0, true, 1, 0, expectedCompleted: true);
- VerificationHelper(encoder, chars, 0, 1, bytes, 0, 3, false, 1, 3, expectedCompleted: true);
- VerificationHelper(encoder, chars, 0, 2, bytes, 0, 7, false, 2, 7, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 0, 1, bytes, 0, 3, true, 1, 3, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 0, 2, bytes, 0, 7, false, 2, 4, expectedCompleted: true);
VerificationHelper(encoder, chars, 0, 4, bytes, 0, 6, false, 4, 6, expectedCompleted: true);
VerificationHelper(encoder, chars, 0, 4, bytes, 0, 6, true, 4, 6, expectedCompleted: true);
VerificationHelper(encoder, chars, 2, 2, bytes, 0, 2, true, 2, 2, expectedCompleted: true);
VerificationHelper(encoder, chars, 1, 3, bytes, 1, 3, false, 1, 3, expectedCompleted: false);
VerificationHelper(encoder, chars, 1, 3, bytes, 1, 5, true, 3, 5, expectedCompleted: true);
- VerificationHelper(encoder, chars, 0, 1, bytes, 0, bytes.Length, false, 1, 0, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 0, 1, bytes, 0, bytes.Length, false, 1, 0, expectedCompleted: false);
VerificationHelper(encoder, chars, 1, 1, bytes, 0, bytes.Length, true, 1, 4, expectedCompleted: true);
}
// Call Convert to convert partial of a ASCII+Unicode character array with ASCII encoder
[Fact]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "coreclr #23020 is not fixed in netfx.")]
public void EncoderASCIIConvertMixedASCIIUnicodeCharArrayPartial()
{
char[] chars = "T\uD83D\uDE01est".ToCharArray();
Encoder encoder = Encoding.ASCII.GetEncoder();
VerificationHelper(encoder, chars, 0, 1, bytes, 0, 1, true, 1, 1, expectedCompleted: true);
- VerificationHelper(encoder, chars, 0, 2, bytes, 0, 1, false, 1, 1, expectedCompleted: false);
- VerificationHelper(encoder, chars, 1, 2, bytes, 0, 2, false, 2, 2, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 0, 4, bytes, 0, 1, false, 3, 1, expectedCompleted: false);
+ VerificationHelper(encoder, chars, 3, 1, bytes, 0, 2, false, 0, 2, expectedCompleted: false);
+ VerificationHelper(encoder, chars, 3, 1, bytes, 0, 2, false, 1, 1, expectedCompleted: true);
VerificationHelper(encoder, chars, 0, 5, bytes, 0, 5, false, 5, 5, expectedCompleted: true);
VerificationHelper(encoder, chars, 0, 4, bytes, 0, 4, true, 4, 4, expectedCompleted: true);
VerificationHelper(encoder, chars, 2, 2, bytes, 0, 2, true, 2, 2, expectedCompleted: true);
// Call Convert to convert partial of a ASCII+Unicode character array with UTF8 encoder
[Fact]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "coreclr #23020 is not fixed in netfx.")]
public void EncoderUTF8ConvertMixedASCIIUnicodeCharArrayPartial()
{
char[] chars = "T\uD83D\uDE01est".ToCharArray();
Encoder encoder = Encoding.UTF8.GetEncoder();
VerificationHelper(encoder, chars, 0, 1, bytes, 0, 1, true, 1, 1, expectedCompleted: true);
- VerificationHelper(encoder, chars, 0, 2, bytes, 0, 1, false, 2, 1, expectedCompleted: true);
- VerificationHelper(encoder, chars, 1, 2, bytes, 0, 7, false, 2, 7, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 0, 2, bytes, 0, 1, false, 2, 1, expectedCompleted: false);
+ VerificationHelper(encoder, chars, 2, 1, bytes, 0, 5, false, 1, 4, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 1, 2, bytes, 0, 7, false, 2, 4, expectedCompleted: true);
VerificationHelper(encoder, chars, 0, 5, bytes, 0, 7, false, 5, 7, expectedCompleted: true);
VerificationHelper(encoder, chars, 0, 4, bytes, 0, 6, true, 4, 6, expectedCompleted: true);
VerificationHelper(encoder, chars, 2, 2, bytes, 0, 3, true, 1, 3, expectedCompleted: false);
VerificationHelper(encoder, chars, 1, 3, bytes, 1, 5, false, 3, 5, expectedCompleted: true);
VerificationHelper(encoder, chars, 1, 3, bytes, 1, 5, true, 3, 5, expectedCompleted: true);
- VerificationHelper(encoder, chars, 0, 2, bytes, 0, bytes.Length, false, 2, 1, expectedCompleted: true);
+ VerificationHelper(encoder, chars, 0, 2, bytes, 0, bytes.Length, false, 2, 1, expectedCompleted: false);
+ VerificationHelper(encoder, chars, 2, 2, bytes, 0, bytes.Length, false, 2, 5, expectedCompleted: true);
VerificationHelper(encoder, chars, 1, 1, bytes, 0, bytes.Length, true, 1, 3, expectedCompleted: true);
}
int bytesUsed;
bool completed;
- encoder.Convert(chars, charIndex, charCount, bytes, byteIndex, byteCount, false, out charsUsed, out bytesUsed, out completed);
+ encoder.Convert(chars, charIndex, charCount, bytes, byteIndex, byteCount, flush, out charsUsed, out bytesUsed, out completed);
Assert.Equal(expectedCharsUsed, charsUsed);
Assert.Equal(expectedBytesUsed, bytesUsed);
Assert.Equal(expectedCompleted, completed);