diff options
author | Ahson Khan <ahkha@microsoft.com> | 2017-12-07 21:27:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-07 21:27:48 +0300 |
commit | d2193783e6b009295f78e122ccf228e533f3f94c (patch) | |
tree | 1a6c5ec396ecdfa599f4389cdf382c25f6624987 /src/System.Memory/tests/Base64 | |
parent | 6d57c82817cd58672af8c3b39bf2019247bc78b4 (diff) |
Implement Span LastIndexOf extension method and add tests (#25748)
* Implement Span LastIndexOf and add tests
* Add LastIndexOfSequence tests and fix implementation
* Vectorize LastIndexOf<byte> similar to IndexOf<byte>
* Add LastIndexOf performance tests
* Adding IndexOf and LastIndexOf tests for reference type (string).
* Use abbreviated version of 'default' literal
* Remove unnecessary type specifiers in generic function calls.
* Cleanup and format all files in the solution to follow coding style.
* Cleaning up leftover unused using directives and extra spaces
Diffstat (limited to 'src/System.Memory/tests/Base64')
-rw-r--r-- | src/System.Memory/tests/Base64/Base64DecoderUnitTests.cs | 31 | ||||
-rw-r--r-- | src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs | 8 | ||||
-rw-r--r-- | src/System.Memory/tests/Base64/Base64TestHelper.cs | 2 |
3 files changed, 21 insertions, 20 deletions
diff --git a/src/System.Memory/tests/Base64/Base64DecoderUnitTests.cs b/src/System.Memory/tests/Base64/Base64DecoderUnitTests.cs index 4bc0f20063..15a6733060 100644 --- a/src/System.Memory/tests/Base64/Base64DecoderUnitTests.cs +++ b/src/System.Memory/tests/Base64/Base64DecoderUnitTests.cs @@ -24,7 +24,7 @@ namespace System.Buffers.Text.Tests Base64TestHelper.InitalizeDecodableBytes(source, numBytes); Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)]; - Assert.Equal(OperationStatus.Done, + Assert.Equal(OperationStatus.Done, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount)); Assert.Equal(source.Length, consumed); Assert.Equal(decodedBytes.Length, decodedByteCount); @@ -38,7 +38,7 @@ namespace System.Buffers.Text.Tests Span<byte> source = Span<byte>.Empty; Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)]; - Assert.Equal(OperationStatus.Done, + Assert.Equal(OperationStatus.Done, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount)); Assert.Equal(source.Length, consumed); Assert.Equal(decodedBytes.Length, decodedByteCount); @@ -62,7 +62,7 @@ namespace System.Buffers.Text.Tests Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)]; int expectedConsumed = source.Length / 4 * 4; // only consume closest multiple of four since isFinalBlock is false - Assert.Equal(OperationStatus.NeedMoreData, + Assert.Equal(OperationStatus.NeedMoreData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount, isFinalBlock: false)); Assert.Equal(expectedConsumed, consumed); Assert.Equal(decodedBytes.Length, decodedByteCount); @@ -151,7 +151,7 @@ namespace System.Buffers.Text.Tests // 123-255 byte[] invalidBytes = Base64TestHelper.InvalidBytes; Assert.Equal(byte.MaxValue + 1 - 64, invalidBytes.Length); // 192 - + for (int j = 0; j < 8; j++) { Span<byte> source = new byte[8] { 50, 50, 50, 50, 80, 80, 80, 80 }; // valid input - "2222PPPP" @@ -160,14 +160,15 @@ namespace System.Buffers.Text.Tests for (int i = 0; i < invalidBytes.Length; i++) { // Don't test padding (byte 61 i.e. '='), which is tested in DecodingInvalidBytesPadding - if (invalidBytes[i] == Base64TestHelper.s_encodingPad) continue; + if (invalidBytes[i] == Base64TestHelper.s_encodingPad) + continue; // replace one byte with an invalid input source[j] = invalidBytes[i]; Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount)); - + if (j < 4) { Assert.Equal(0, consumed); @@ -302,7 +303,7 @@ namespace System.Buffers.Text.Tests source[11] = Base64TestHelper.s_encodingPad; Span<byte> decodedBytes = new byte[6]; - Assert.Equal(OperationStatus.DestinationTooSmall, + Assert.Equal(OperationStatus.DestinationTooSmall, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int written)); int expectedConsumed = 8; Assert.Equal(expectedConsumed, consumed); @@ -316,7 +317,7 @@ namespace System.Buffers.Text.Tests source[11] = Base64TestHelper.s_encodingPad; Span<byte> decodedBytes = new byte[7]; - Assert.Equal(OperationStatus.DestinationTooSmall, + Assert.Equal(OperationStatus.DestinationTooSmall, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int written)); int expectedConsumed = 8; Assert.Equal(expectedConsumed, consumed); @@ -335,10 +336,10 @@ namespace System.Buffers.Text.Tests int requiredSize = Base64.GetMaxDecodedFromUtf8Length(source.Length); Span<byte> decodedBytes = new byte[outputSize]; - Assert.Equal(OperationStatus.DestinationTooSmall, + Assert.Equal(OperationStatus.DestinationTooSmall, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int decodedByteCount)); int expectedConsumed = decodedBytes.Length / 3 * 4; - Assert.Equal(expectedConsumed, consumed); + Assert.Equal(expectedConsumed, consumed); Assert.Equal(decodedBytes.Length, decodedByteCount); Assert.True(Base64TestHelper.VerifyDecodingCorrectness(expectedConsumed, decodedBytes.Length, source, decodedBytes)); @@ -380,7 +381,6 @@ namespace System.Buffers.Text.Tests Assert.Throws<ArgumentOutOfRangeException>(() => Base64.GetMaxDecodedFromUtf8Length(int.MinValue)); } - [Fact] public void DecodeInPlace() { @@ -429,7 +429,7 @@ namespace System.Buffers.Text.Tests public void DecodeInPlaceInvalidBytes() { byte[] invalidBytes = Base64TestHelper.InvalidBytes; - + for (int j = 0; j < 8; j++) { for (int i = 0; i < invalidBytes.Length; i++) @@ -437,14 +437,15 @@ namespace System.Buffers.Text.Tests Span<byte> buffer = new byte[8] { 50, 50, 50, 50, 80, 80, 80, 80 }; // valid input - "2222PPPP" // Don't test padding (byte 61 i.e. '='), which is tested in DecodeInPlaceInvalidBytesPadding - if (invalidBytes[i] == Base64TestHelper.s_encodingPad) continue; + if (invalidBytes[i] == Base64TestHelper.s_encodingPad) + continue; // replace one byte with an invalid input buffer[j] = invalidBytes[i]; string sourceString = Encoding.ASCII.GetString(buffer.Slice(0, 4).ToArray()); Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8InPlace(buffer, out int bytesWritten)); - + if (j < 4) { Assert.Equal(0, bytesWritten); @@ -523,7 +524,7 @@ namespace System.Buffers.Text.Tests Span<byte> expectedBytes = Convert.FromBase64String(sourceString); Assert.True(expectedBytes.SequenceEqual(buffer.Slice(0, bytesWritten))); } - + { Span<byte> buffer = new byte[] { 50, 50, 50, 50, 80, 80, 80, 80 }; buffer[7] = Base64TestHelper.s_encodingPad; // valid input - "2222PPP=" diff --git a/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs b/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs index d2e25a497a..c3f5335106 100644 --- a/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs +++ b/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs @@ -64,7 +64,7 @@ namespace System.Buffers.Text.Tests { Span<byte> source = Span<byte>.Empty; Span<byte> encodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)]; - + Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount)); Assert.Equal(source.Length, consumed); Assert.Equal(encodedBytes.Length, encodedBytesCount); @@ -79,7 +79,7 @@ namespace System.Buffers.Text.Tests // CLR default limit of 2 gigabytes (GB). try { - // 1610612734, larger than MaximumEncodeLength, requires output buffer of size 2147483648 (which is > int.MaxValue) + // 1610612734, larger than MaximumEncodeLength, requires output buffer of size 2147483648 (which is > int.MaxValue) Span<byte> source = new byte[(int.MaxValue >> 2) * 3 + 1]; Span<byte> encodedBytes = new byte[2000000000]; Assert.Equal(OperationStatus.DestinationTooSmall, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount)); @@ -249,7 +249,7 @@ namespace System.Buffers.Text.Tests [Fact] public void EncodeInPlaceOutputTooSmall() { - byte[] testBytes = {1, 2, 3}; + byte[] testBytes = { 1, 2, 3 }; for (int numberOfBytesToTest = 1; numberOfBytesToTest <= testBytes.Length; numberOfBytesToTest++) { @@ -261,7 +261,7 @@ namespace System.Buffers.Text.Tests [Fact] public void EncodeInPlaceDataLengthTooLarge() { - byte[] testBytes = {1, 2, 3}; + byte[] testBytes = { 1, 2, 3 }; Assert.Equal(OperationStatus.DestinationTooSmall, Base64.EncodeToUtf8InPlace(testBytes, testBytes.Length + 1, out int bytesWritten)); Assert.Equal(0, bytesWritten); } diff --git a/src/System.Memory/tests/Base64/Base64TestHelper.cs b/src/System.Memory/tests/Base64/Base64TestHelper.cs index 1026a97683..1aa8271674 100644 --- a/src/System.Memory/tests/Base64/Base64TestHelper.cs +++ b/src/System.Memory/tests/Base64/Base64TestHelper.cs @@ -57,7 +57,7 @@ namespace System.Buffers.Text.Tests // Workaroudn for indices.Cast<byte>().ToArray() since it throws // InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Byte' byte[] bytes = new byte[indices.Length]; - for(int i = 0; i < indices.Length; i++) + for (int i = 0; i < indices.Length; i++) { bytes[i] = (byte)indices[i]; } |