Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Expose/test Span-based Convert methods #24474

Merged
merged 1 commit into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public static partial class Convert
public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) { throw null; }
public static string ToBase64String(byte[] inArray, Base64FormattingOptions options) { throw null; }
public static string ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) { throw null; }
public static string ToBase64String(System.ReadOnlySpan<byte> bytes, Base64FormattingOptions options = Base64FormattingOptions.None) { throw null; }
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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 :)

public static bool ToBoolean(char value) { throw null; }
public static bool ToBoolean(DateTime value) { throw null; }
public static byte ToByte(DateTime value) { throw null; }
Expand Down Expand Up @@ -620,6 +621,9 @@ public static partial class Convert
public static ulong ToUInt64(uint value) { throw null; }
[System.CLSCompliantAttribute(false)]
public static ulong ToUInt64(ulong value) { throw null; }
public static bool TryToBase64Chars(System.ReadOnlySpan<byte> bytes, System.Span<char> chars, out int charsWritten, Base64FormattingOptions options = Base64FormattingOptions.None) { throw null; }
public static bool TryFromBase64String(string s, System.Span<byte> bytes, out int bytesWritten) { throw null; }
public static bool TryFromBase64Chars(System.ReadOnlySpan<char> chars, System.Span<byte> bytes, out int bytesWritten) { throw null; }
}
public static partial class Environment
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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" />
Expand Down
161 changes: 161 additions & 0 deletions src/System.Runtime.Extensions/tests/System/Convert.netcoreapp.cs
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]);
}
}
}