diff options
3 files changed, 35 insertions, 25 deletions
diff --git a/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs b/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs index c3f5335106..dac8530996 100644 --- a/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs +++ b/src/System.Memory/tests/Base64/Base64EncoderUnitTests.cs @@ -2,6 +2,7 @@ // 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.SpanTests; using System.Text; using Xunit; @@ -75,20 +76,44 @@ namespace System.Buffers.Text.Tests [OuterLoop] public void EncodeTooLargeSpan() { + + if (IntPtr.Size < 8) + return; + + bool allocatedFirst = false; + bool allocatedSecond = false; + IntPtr memBlockFirst = IntPtr.Zero; + IntPtr memBlockSecond = IntPtr.Zero; + // int.MaxValue - (int.MaxValue % 4) => 2147483644, largest multiple of 4 less than int.MaxValue // CLR default limit of 2 gigabytes (GB). + // 1610612734, larger than MaximumEncodeLength, requires output buffer of size 2147483648 (which is > int.MaxValue) + const int sourceCount = (int.MaxValue >> 2) * 3 + 1; + const int encodedCount = 2000000000; + try { - // 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)); - Assert.Equal((encodedBytes.Length >> 2) * 3, consumed); // encoding 1500000000 bytes fits into buffer of 2000000000 bytes - Assert.Equal(encodedBytes.Length, encodedBytesCount); + allocatedFirst = AllocationHelper.TryAllocNative((IntPtr)sourceCount, out memBlockFirst); + allocatedSecond = AllocationHelper.TryAllocNative((IntPtr)encodedCount, out memBlockSecond); + if (allocatedFirst && allocatedSecond) + { + unsafe + { + var source = new Span<byte>(memBlockFirst.ToPointer(), sourceCount); + var encodedBytes = new Span<byte>(memBlockSecond.ToPointer(), encodedCount); + + Assert.Equal(OperationStatus.DestinationTooSmall, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount)); + Assert.Equal((encodedBytes.Length >> 2) * 3, consumed); // encoding 1500000000 bytes fits into buffer of 2000000000 bytes + Assert.Equal(encodedBytes.Length, encodedBytesCount); + } + } } - catch (OutOfMemoryException) + finally { - // do nothing + if (allocatedFirst) + AllocationHelper.ReleaseNative(ref memBlockFirst); + if (allocatedSecond) + AllocationHelper.ReleaseNative(ref memBlockSecond); } } diff --git a/src/System.Memory/tests/ParsersAndFormatters/Parser/ParserTests.2gbOverflow.cs b/src/System.Memory/tests/ParsersAndFormatters/Parser/ParserTests.2gbOverflow.cs index 6981d9ab8c..a47934c671 100644 --- a/src/System.Memory/tests/ParsersAndFormatters/Parser/ParserTests.2gbOverflow.cs +++ b/src/System.Memory/tests/ParsersAndFormatters/Parser/ParserTests.2gbOverflow.cs @@ -93,20 +93,11 @@ namespace System.Buffers.Text.Tests { get { - yield return new ParserTestData<int>("0", 0, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; - yield return new ParserTestData<int>("2", 2, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; - yield return new ParserTestData<int>("21", 21, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; - yield return new ParserTestData<int>("+2", 2, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; yield return new ParserTestData<int>("-2", -2, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; yield return new ParserTestData<int>("2147483647", 2147483647, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; - yield return new ParserTestData<int>("-2147483648", -2147483648, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; yield return new ParserTestData<int>("2147483648", default, 'D', expectedSuccess: false); - yield return new ParserTestData<int>("-2147483649", default, 'D', expectedSuccess: false); yield return new ParserTestData<int>("12345abcdefg1", 12345, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB - 8 }; - yield return new ParserTestData<int>("1234145abcdefg1", 1234145, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB - 8 }; yield return new ParserTestData<int>("abcdefghijklmnop1", 0, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB - 17 }; - yield return new ParserTestData<int>("1147483648", 1147483648, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; - yield return new ParserTestData<int>("-1147483649", -1147483649, 'D', expectedSuccess: true) { ExpectedBytesConsumed = TwoGiB }; } } } diff --git a/src/System.Memory/tests/Span/Clear.cs b/src/System.Memory/tests/Span/Clear.cs index 117146191f..d6bdbf0b1a 100644 --- a/src/System.Memory/tests/Span/Clear.cs +++ b/src/System.Memory/tests/Span/Clear.cs @@ -249,20 +249,14 @@ namespace System.SpanTests try { - ref int data = ref Unsafe.AsRef<int>(memory.ToPointer()); - - int initial = 5; - for (int i = 0; i < length; i++) - { - Unsafe.Add(ref data, i) = initial; - } - Span<int> span = new Span<int>(memory.ToPointer(), length); + span.Fill(5); // Act span.Clear(); // Assert using custom code for perf and to avoid allocating extra memory + ref int data = ref Unsafe.AsRef<int>(memory.ToPointer()); for (int i = 0; i < length; i++) { var actual = Unsafe.Add(ref data, i); |