This repository has been 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
Expose/test Span-based Convert methods #24474
Merged
Merged
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 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 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 |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
<ItemGroup Condition="'$(TargetGroup)'!='netstandard'"> | ||
<Compile Include="System\BitConverterSpan.cs" /> | ||
<Compile Include="System\BitConverter.netcoreapp.cs" /> | ||
<Compile Include="System\Convert.netcoreapp.cs" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: This will also run on UWP but I guess we don't have a good naming conventions for non-netstandard stuff There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah |
||
<Compile Include="System\IO\Path.GetRelativePath.cs" /> | ||
<Compile Include="System\MathTests.netcoreapp.cs" /> | ||
<Compile Include="System\MathF.netcoreapp.cs" /> | ||
|
161 changes: 161 additions & 0 deletions
161
src/System.Runtime.Extensions/tests/System/Convert.netcoreapp.cs
This file contains 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,161 @@ | ||
// 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.Tests | ||
{ | ||
public partial class ConvertTests | ||
{ | ||
[Theory] | ||
[InlineData(new byte[0], "")] | ||
[InlineData(new byte[] { 5, 6, 7, 8 }, "BQYHCA==")] | ||
public void ToBase64String_Span_ProducesExpectedOutput(byte[] input, string expected) | ||
{ | ||
Assert.Equal(expected, Convert.ToBase64String(input.AsReadOnlySpan())); | ||
Assert.Equal(expected, Convert.ToBase64String(input.AsReadOnlySpan(), Base64FormattingOptions.None)); | ||
Assert.Equal(expected, Convert.ToBase64String(input.AsReadOnlySpan(), Base64FormattingOptions.InsertLineBreaks)); | ||
} | ||
|
||
[Fact] | ||
public void ToBase64String_Span_LongWithOptions_ProducesExpectedOutput() | ||
{ | ||
byte[] input = Enumerable.Range(0, 120).Select(i => (byte)i).ToArray(); | ||
|
||
Assert.Equal( | ||
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4" + | ||
"OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx" + | ||
"cnN0dXZ3", | ||
Convert.ToBase64String(input)); | ||
|
||
Assert.Equal( | ||
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4" + | ||
"OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx" + | ||
"cnN0dXZ3", | ||
Convert.ToBase64String(input, Base64FormattingOptions.None)); | ||
|
||
Assert.Equal( | ||
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4\r\n" + | ||
"OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx\r\n" + | ||
"cnN0dXZ3", | ||
Convert.ToBase64String(input, Base64FormattingOptions.InsertLineBreaks)); | ||
} | ||
|
||
[Theory] | ||
[InlineData((Base64FormattingOptions)(-1))] | ||
[InlineData((Base64FormattingOptions)(2))] | ||
public void ToBase64String_Span_InvalidOptions_Throws(Base64FormattingOptions invalidOption) | ||
{ | ||
AssertExtensions.Throws<ArgumentException>("options", () => Convert.ToBase64String(new byte[0].AsReadOnlySpan(), invalidOption)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new byte[0], "")] | ||
[InlineData(new byte[] { 5, 6, 7, 8 }, "BQYHCA==")] | ||
public void TryToBase64Chars_ProducesExpectedOutput(byte[] input, string expected) | ||
{ | ||
Span<char> dest; | ||
|
||
// Just right | ||
dest = new char[expected.Length]; | ||
Assert.True(Convert.TryToBase64Chars(input.AsReadOnlySpan(), dest, out int charsWritten)); | ||
Assert.Equal(expected.Length, charsWritten); | ||
Assert.Equal<char>(expected.ToCharArray(), dest.ToArray()); | ||
|
||
// Too short | ||
if (expected.Length > 0) | ||
{ | ||
dest = new char[expected.Length - 1]; | ||
Assert.False(Convert.TryToBase64Chars(input.AsReadOnlySpan(), dest, out charsWritten)); | ||
Assert.Equal(0, charsWritten); | ||
} | ||
|
||
// Longer than needed | ||
dest = new char[expected.Length + 1]; | ||
Assert.True(Convert.TryToBase64Chars(input.AsReadOnlySpan(), dest, out charsWritten)); | ||
Assert.Equal(expected.Length, charsWritten); | ||
Assert.Equal<char>(expected.ToCharArray(), dest.Slice(0, expected.Length).ToArray()); | ||
Assert.Equal(0, dest[dest.Length - 1]); | ||
} | ||
|
||
[Theory] | ||
[InlineData((Base64FormattingOptions)(-1))] | ||
[InlineData((Base64FormattingOptions)(2))] | ||
public void TryToBase64Chars_InvalidOptions_Throws(Base64FormattingOptions invalidOption) | ||
{ | ||
AssertExtensions.Throws<ArgumentException>("options", | ||
() => Convert.TryToBase64Chars(new byte[0].AsReadOnlySpan(), new char[0].AsSpan(), out int charsWritten, invalidOption)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("BQYHCA==")] | ||
[InlineData( | ||
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4\r\n" + | ||
"OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx\r\n" + | ||
"cnN0dXZ3")] | ||
public void TryFromBase64String_MatchesFromBase64String(string stringInput) | ||
{ | ||
byte[] expected = Convert.FromBase64String(stringInput); | ||
Span<byte> dest; | ||
|
||
// Just the right length | ||
dest = new byte[expected.Length]; | ||
Assert.True(Convert.TryFromBase64String(stringInput, dest, out int bytesWritten)); | ||
Assert.Equal(expected.Length, bytesWritten); | ||
Assert.Equal<byte>(expected, dest.ToArray()); | ||
|
||
// Too short | ||
if (expected.Length > 0) | ||
{ | ||
dest = new byte[expected.Length - 1]; | ||
Assert.False(Convert.TryFromBase64String(stringInput, dest, out bytesWritten)); | ||
Assert.Equal(0, bytesWritten); | ||
} | ||
|
||
// Longer than needed | ||
dest = new byte[expected.Length + 1]; | ||
Assert.True(Convert.TryFromBase64String(stringInput, dest, out bytesWritten)); | ||
Assert.Equal(expected.Length, bytesWritten); | ||
Assert.Equal<byte>(expected, dest.Slice(0, expected.Length).ToArray()); | ||
Assert.Equal(0, dest[dest.Length - 1]); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("BQYHCA==")] | ||
[InlineData( | ||
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4\r\n" + | ||
"OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx\r\n" + | ||
"cnN0dXZ3")] | ||
public void TryFromBase64Chars_MatchesFromBase64CharArray(string stringInput) | ||
{ | ||
char[] charArrayInput = stringInput.ToCharArray(); | ||
byte[] expected = Convert.FromBase64CharArray(charArrayInput, 0, charArrayInput.Length); | ||
Span<byte> dest; | ||
|
||
// Just the right length | ||
dest = new byte[expected.Length]; | ||
Assert.True(Convert.TryFromBase64Chars(charArrayInput.AsReadOnlySpan(), dest, out int bytesWritten)); | ||
Assert.Equal(expected.Length, bytesWritten); | ||
Assert.Equal<byte>(expected, dest.ToArray()); | ||
|
||
// Too short | ||
if (expected.Length > 0) | ||
{ | ||
dest = new byte[expected.Length - 1]; | ||
Assert.False(Convert.TryFromBase64Chars(charArrayInput.AsReadOnlySpan(), dest, out bytesWritten)); | ||
Assert.Equal(0, bytesWritten); | ||
} | ||
|
||
// Longer than needed | ||
dest = new byte[expected.Length + 1]; | ||
Assert.True(Convert.TryFromBase64Chars(charArrayInput.AsReadOnlySpan(), dest, out bytesWritten)); | ||
Assert.Equal(expected.Length, bytesWritten); | ||
Assert.Equal<byte>(expected, dest.Slice(0, dest.Length - 1).ToArray()); | ||
Assert.Equal(0, dest[dest.Length - 1]); | ||
} | ||
} | ||
} |
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.
Is this done in corert as well? If not then it will fail on uap and uapaot builds.
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.
Convert.cs is in the shared partition. Once it's merged into coreclr, it'll be mirrored over to corert.
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.
I see, that's great! I missed that part on your coreclr PR :)