Skip to content

Commit dca7ee6

Browse files
committed
Address PR feedback: Reuse existing decoding method with whitespace
1 parent 8482bbd commit dca7ee6

File tree

5 files changed

+170
-354
lines changed

5 files changed

+170
-354
lines changed

src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ public void DecodeInPlaceInvalidBytesPadding()
661661

662662
[Theory]
663663
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
664-
public void BasicDecodingIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
664+
public void BasicDecodingIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes)
665665
{
666666
byte[] utf8BytesWithByteToBeIgnored = UTF8Encoding.UTF8.GetBytes(utf8WithCharsToBeIgnored);
667667
byte[] resultBytes = new byte[5];
@@ -671,17 +671,16 @@ public void BasicDecodingIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf
671671
byte[] stringBytes = Convert.FromBase64String(utf8WithCharsToBeIgnored);
672672

673673
Assert.Equal(OperationStatus.Done, result);
674-
Assert.Equal(expectedBytesConsumed, bytesConsumed);
674+
Assert.Equal(utf8WithCharsToBeIgnored.Length, bytesConsumed);
675675
Assert.Equal(expectedBytes.Length, bytesWritten);
676676
Assert.True(expectedBytes.SequenceEqual(resultBytes));
677677
Assert.True(stringBytes.SequenceEqual(resultBytes));
678678
}
679679

680680
[Theory]
681681
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
682-
public void DecodeInPlaceIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
682+
public void DecodeInPlaceIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes)
683683
{
684-
_ = expectedBytesConsumed;
685684
Span<byte> utf8BytesWithByteToBeIgnored = UTF8Encoding.UTF8.GetBytes(utf8WithCharsToBeIgnored);
686685
OperationStatus result = Base64.DecodeFromUtf8InPlace(utf8BytesWithByteToBeIgnored, out int bytesWritten);
687686
Span<byte> bytesOverwritten = utf8BytesWithByteToBeIgnored.Slice(0, bytesWritten);

src/libraries/System.Memory/tests/Base64/Base64TestBase.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,41 @@ public static IEnumerable<object[]> ValidBase64Strings_WithCharsThatMustBeIgnore
2424
// One will have 1 char, another will have 3
2525

2626
// Line feed
27-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 1), utf8Bytes, utf8Bytes.Length + 4 };
28-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 3), utf8Bytes, utf8Bytes.Length + 6 };
27+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 1), utf8Bytes };
28+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 3), utf8Bytes };
2929

3030
// Horizontal tab
31-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 1), utf8Bytes, utf8Bytes.Length + 4 };
32-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 3), utf8Bytes, utf8Bytes.Length + 6 };
31+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 1), utf8Bytes };
32+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 3), utf8Bytes };
3333

3434
// Carriage return
35-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 1), utf8Bytes, utf8Bytes.Length + 4 };
36-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 3), utf8Bytes, utf8Bytes.Length + 6 };
35+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 1), utf8Bytes };
36+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 3), utf8Bytes };
3737

3838
// Space
39-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 1), utf8Bytes, utf8Bytes.Length + 4 };
40-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 3), utf8Bytes, utf8Bytes.Length + 6 };
39+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 1), utf8Bytes };
40+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 3), utf8Bytes };
4141

4242
string GetBase64StringWithPassedCharInsertedInTheMiddle(char charToInsert, int numberOfTimesToInsert) => $"{firstSegment}{new string(charToInsert, numberOfTimesToInsert)}{secondSegment}";
4343

4444
// Insert ignored chars at the start of the base 64 string
4545
// One will have 1 char, another will have 3
4646

4747
// Line feed
48-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 1), utf8Bytes, utf8Bytes.Length + 4 };
49-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 3), utf8Bytes, utf8Bytes.Length + 6 };
48+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 1), utf8Bytes };
49+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 3), utf8Bytes };
5050

5151
// Horizontal tab
52-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 1), utf8Bytes, utf8Bytes.Length + 4 };
53-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 3), utf8Bytes, utf8Bytes.Length + 6 };
52+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 1), utf8Bytes };
53+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 3), utf8Bytes };
5454

5555
// Carriage return
56-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 1), utf8Bytes, utf8Bytes.Length + 4 };
57-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 3), utf8Bytes, utf8Bytes.Length + 6 };
56+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 1), utf8Bytes };
57+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 3), utf8Bytes };
5858

5959
// Space
60-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 1), utf8Bytes, utf8Bytes.Length + 4 };
61-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 3), utf8Bytes, utf8Bytes.Length + 6 };
60+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 1), utf8Bytes };
61+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 3), utf8Bytes };
6262

6363
string GetBase64StringWithPassedCharInsertedAtTheStart(char charToInsert, int numberOfTimesToInsert) => $"{new string(charToInsert, numberOfTimesToInsert)}{firstSegment}{secondSegment}";
6464

@@ -67,20 +67,20 @@ public static IEnumerable<object[]> ValidBase64Strings_WithCharsThatMustBeIgnore
6767
// Whitespace after end/padding is not included in consumed bytes
6868

6969
// Line feed
70-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 1), utf8Bytes, utf8Bytes.Length + 3 };
71-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 3), utf8Bytes, utf8Bytes.Length + 3 };
70+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 1), utf8Bytes };
71+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 3), utf8Bytes };
7272

7373
// Horizontal tab
74-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 1), utf8Bytes, utf8Bytes.Length + 3 };
75-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 3), utf8Bytes, utf8Bytes.Length + 3 };
74+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 1), utf8Bytes };
75+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 3), utf8Bytes };
7676

7777
// Carriage return
78-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 1), utf8Bytes, utf8Bytes.Length + 3 };
79-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 3), utf8Bytes, utf8Bytes.Length + 3 };
78+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 1), utf8Bytes };
79+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 3), utf8Bytes };
8080

8181
// Space
82-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 1), utf8Bytes, utf8Bytes.Length + 3 };
83-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 3), utf8Bytes, utf8Bytes.Length + 3 };
82+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 1), utf8Bytes };
83+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 3), utf8Bytes };
8484

8585
string GetBase64StringWithPassedCharInsertedAtTheEnd(char charToInsert, int numberOfTimesToInsert) => $"{firstSegment}{secondSegment}{new string(charToInsert, numberOfTimesToInsert)}";
8686
}

src/libraries/System.Memory/tests/Base64/Base64ValidationUnitTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ public void ValidateGuidChars()
147147

148148
[Theory]
149149
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
150-
public void ValidateBytesIgnoresCharsToBeIgnoredBytes(string utf8WithByteToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
150+
public void ValidateBytesIgnoresCharsToBeIgnoredBytes(string utf8WithByteToBeIgnored, byte[] expectedBytes)
151151
{
152-
_ = expectedBytesConsumed;
153152
byte[] utf8BytesWithByteToBeIgnored = UTF8Encoding.UTF8.GetBytes(utf8WithByteToBeIgnored);
154153

155154
Assert.True(Base64.IsValid(utf8BytesWithByteToBeIgnored));
@@ -159,9 +158,8 @@ public void ValidateBytesIgnoresCharsToBeIgnoredBytes(string utf8WithByteToBeIgn
159158

160159
[Theory]
161160
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
162-
public void ValidateBytesIgnoresCharsToBeIgnoredChars(string utf8WithByteToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
161+
public void ValidateBytesIgnoresCharsToBeIgnoredChars(string utf8WithByteToBeIgnored, byte[] expectedBytes)
163162
{
164-
_ = expectedBytesConsumed;
165163
ReadOnlySpan<char> utf8BytesWithByteToBeIgnored = utf8WithByteToBeIgnored.ToArray();
166164

167165
Assert.True(Base64.IsValid(utf8BytesWithByteToBeIgnored));

0 commit comments

Comments
 (0)