This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add some unit tests for ASCIIEncoding #35331
Closed
GrabYourPitchforks
wants to merge
1
commit into
dotnet:master
from
GrabYourPitchforks:asciiencoding_tests_1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingDecode.netcoreapp.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 ASCIIEncodingDecode | ||
{ | ||
[Theory] | ||
[InlineData("hello!", 6)] | ||
[InlineData("hello\u1234there!", 16)] | ||
[InlineData("\ud800\udc00", 10)] | ||
public void GetByteCount_WithReplacementFallback(string input, int expectedByteCount) | ||
{ | ||
Encoding encoding = Encoding.GetEncoding("ascii", new EncoderReplacementFallback("abcde"), DecoderFallback.ExceptionFallback); | ||
Assert.Equal(expectedByteCount, encoding.GetByteCount(input)); | ||
} | ||
|
||
[Fact] | ||
public void GetByteCount_WithSingleCharNonAsciiReplacementFallback_ValidatesAscii() | ||
{ | ||
// Tests trying to replace one non-ASCII character with another, which should cause | ||
// fallback logic to identify the invalid data and abort the operation. | ||
|
||
Encoding encoding = Encoding.GetEncoding("ascii", new EncoderReplacementFallback("\u1234"), DecoderFallback.ExceptionFallback); | ||
Assert.Throws<ArgumentException>("chars", () => encoding.GetByteCount("\u0080")); | ||
} | ||
|
||
[Theory] | ||
[InlineData("hello!", "hello!")] | ||
[InlineData("hello\u1234there!", "helloabcdethere!")] | ||
[InlineData("\ud800\udc00", "abcdeabcde")] | ||
public void GetBytes_WithReplacementFallback(string input, string expectedResult) | ||
{ | ||
Encoding encoding = Encoding.GetEncoding("ascii", new EncoderReplacementFallback("abcde"), DecoderFallback.ExceptionFallback); | ||
Assert.Equal(WideToAsciiStr(expectedResult), encoding.GetBytes(input)); | ||
} | ||
|
||
[Fact] | ||
public void GetBytes_WithNonAsciiInput_AndSingleCharNonAsciiReplacementFallback_Throws() | ||
{ | ||
// Tests trying to replace one non-ASCII character with another, which should cause | ||
// fallback logic to identify the invalid data and abort the operation. | ||
|
||
Encoding encoding = Encoding.GetEncoding("ascii", new EncoderReplacementFallback("\u1234"), DecoderFallback.ExceptionFallback); | ||
Assert.Throws<ArgumentException>("chars", () => encoding.GetBytes("\u0080")); | ||
} | ||
|
||
private static byte[] WideToAsciiStr(string input) | ||
{ | ||
return input.Select(ch => (byte)checked((sbyte)ch)).ToArray(); // makes sure each char is 00..7F | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingEncode.netcoreapp.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewers: This test case currently fails due to a bug in the current implementation of
ASCIIEncoding
. The actual behavior is thatGetByteCount
returns 1, but correct behavior would instead be for it to throwArgumentException
.I'm working on correcting this issue at the same time that the other
ASCIIEncoding
changes come online.