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

MemoryExtensions ToUpper / ToLower throws for overlapping buffer #38778

Closed
wants to merge 5 commits into from
Closed
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
95 changes: 54 additions & 41 deletions src/Common/tests/Tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5152,50 +5152,57 @@ public static void SameSpanToLower()
Assert.Equal(expected, s1.ToLower(CultureInfo.CurrentCulture).ToArray());
Assert.Equal(expected, s1.ToLowerInvariant().ToArray());
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;
Assert.Equal(source.Length, source.ToLower(destination, CultureInfo.CurrentCulture));
Assert.Equal(expected, destination.ToArray());
Assert.Equal(expected, source.ToArray());
TestHelpers.AssertThrows<InvalidOperationException, char>(a, a =>
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;

source.ToLower(destination, CultureInfo.CurrentCulture);
});
}
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;
Assert.Equal(source.Length, source.ToLowerInvariant(destination));
Assert.Equal(expected, destination.ToArray());
Assert.Equal(expected, source.ToArray());
{
TestHelpers.AssertThrows<InvalidOperationException, char>(a, a =>
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;

source.ToLowerInvariant(destination);
});
}
}

[Fact]
public static void ToLowerOverlapping()
{
var expectedSource = new char[3] { 'B', 'c', 'b' };
var expectedDestination = new char[3] { 'b', 'c', 'b' };

{
char[] a = { 'a', 'B', 'c', 'B', 'c', 'B' };

string s1 = new string(a, 1, 3);
string s1 = new string(a, 1, 3);
Assert.Equal(expectedDestination, s1.ToLower(CultureInfo.CurrentCulture).ToArray());

var source = new ReadOnlySpan<char>(a, 1, 3);
var destination = new Span<char>(a, 3, 3);
Assert.Equal(source.Length, source.ToLower(destination, CultureInfo.CurrentCulture));
Assert.Equal(expectedDestination, destination.ToArray());
Assert.Equal(expectedSource, source.ToArray());
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved

TestHelpers.AssertThrows<InvalidOperationException,char>(source, source =>
{
var destination = new Span<char>(a, 3, 3);
source.ToLower(destination, CultureInfo.CurrentCulture);
});
}
{
char[] a = { 'a', 'B', 'c', 'B', 'c', 'B' };

string s1 = new string(a, 1, 3);
string s1 = new string(a, 1, 3);
Assert.Equal(expectedDestination, s1.ToLowerInvariant().ToArray());

var source = new ReadOnlySpan<char>(a, 1, 3);
var destination = new Span<char>(a, 3, 3);
Assert.Equal(source.Length, source.ToLowerInvariant(destination));
Assert.Equal(expectedDestination, destination.ToArray());
Assert.Equal(expectedSource, source.ToArray());

TestHelpers.AssertThrows<InvalidOperationException, char>(source, source =>
{
var destination = new Span<char>(a, 3, 3);
source.ToLowerInvariant(destination);
});
}
}

Expand Down Expand Up @@ -5358,25 +5365,28 @@ public static void SameSpanToUpper()
Assert.Equal(expected, s1.ToUpper(CultureInfo.CurrentCulture).ToArray());
Assert.Equal(expected, s1.ToUpperInvariant().ToArray());
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;
Assert.Equal(source.Length, source.ToUpper(destination, CultureInfo.CurrentCulture));
Assert.Equal(expected, destination.ToArray());
Assert.Equal(expected, source.ToArray());
TestHelpers.AssertThrows<InvalidOperationException, char>(a, a =>
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;

source.ToUpper(destination, CultureInfo.CurrentCulture);
});
}
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;
Assert.Equal(source.Length, source.ToUpperInvariant(destination));
Assert.Equal(expected, destination.ToArray());
Assert.Equal(expected, source.ToArray());
TestHelpers.AssertThrows<InvalidOperationException, char>(a, a =>
{
ReadOnlySpan<char> source = a;
Span<char> destination = a;

source.ToUpperInvariant(destination);
});
}
}

[Fact]
public static void ToUpperOverlapping()
{
var expectedSource = new char[3] { 'b', 'C', 'B' };
var expectedDestination = new char[3] { 'B', 'C', 'B' };

{
Expand All @@ -5386,10 +5396,12 @@ public static void ToUpperOverlapping()
Assert.Equal(expectedDestination, s1.ToUpper(CultureInfo.CurrentCulture).ToArray());

var source = new ReadOnlySpan<char>(a, 1, 3);
var destination = new Span<char>(a, 3, 3);
Assert.Equal(source.Length, source.ToUpper(destination, CultureInfo.CurrentCulture));
Assert.Equal(expectedDestination, destination.ToArray());
Assert.Equal(expectedSource, source.ToArray());
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved

TestHelpers.AssertThrows<InvalidOperationException, char>(source, source =>
{
var destination = new Span<char>(a, 3, 3);
source.ToUpper(destination, CultureInfo.CurrentCulture);
});
}
{
char[] a = { 'a', 'b', 'C', 'b', 'C', 'b' };
Expand All @@ -5398,10 +5410,11 @@ public static void ToUpperOverlapping()
Assert.Equal(expectedDestination, s1.ToUpperInvariant().ToArray());

var source = new ReadOnlySpan<char>(a, 1, 3);
var destination = new Span<char>(a, 3, 3);
Assert.Equal(source.Length, source.ToUpperInvariant(destination));
Assert.Equal(expectedDestination, destination.ToArray());
Assert.Equal(expectedSource, source.ToArray());
TestHelpers.AssertThrows<InvalidOperationException, char>(source, source =>
{
var destination = new Span<char>(a, 3, 3);
source.ToUpperInvariant(destination);
});
}
}

Expand Down
65 changes: 65 additions & 0 deletions src/System.Memory/tests/ReadOnlySpan/ToUpperLower.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 Xunit;
using System.Collections.Generic;

namespace System.SpanTests
{
public partial class ReadOnlySpanTests
{
public static IEnumerable<object[]> MemoryExtensionsToUpperLowerOverlapping()
{
// full overlap, overlap in the middle, overlap at start, overlap at the end

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLower(buffer, null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLower(buffer.Slice(1, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLower(buffer.Slice(0, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLower(buffer.Slice(2, 1), null) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLower(buffer, buffer, null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLower(buffer, buffer.Slice(1, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLower(buffer, buffer.Slice(0, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLower(buffer, buffer.Slice(2, 1), null) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLowerInvariant(buffer) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLowerInvariant(buffer.Slice(1, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLowerInvariant(buffer.Slice(0, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToLowerInvariant(buffer.Slice(2, 1)) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLowerInvariant(buffer, buffer) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLowerInvariant(buffer, buffer.Slice(1, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLowerInvariant(buffer, buffer.Slice(0, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToLowerInvariant(buffer, buffer.Slice(2, 1)) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpper(buffer, null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpper(buffer.Slice(1, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpper(buffer.Slice(0, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpper(buffer.Slice(2, 1), null) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpper(buffer, buffer, null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpper(buffer, buffer.Slice(1, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpper(buffer, buffer.Slice(0, 1), null) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpper(buffer, buffer.Slice(2, 1), null) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpperInvariant(buffer) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpperInvariant(buffer.Slice(1, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpperInvariant(buffer.Slice(0, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => ((ReadOnlySpan<char>)buffer).ToUpperInvariant(buffer.Slice(2, 1)) };

yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpperInvariant(buffer, buffer) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpperInvariant(buffer, buffer.Slice(1, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpperInvariant(buffer, buffer.Slice(0, 1)) };
yield return new TestHelpers.AssertThrowsAction<char>[] { (Span<char> buffer) => MemoryExtensions.ToUpperInvariant(buffer, buffer.Slice(2, 1)) };
}

[Theory]
[MemberData(nameof(MemoryExtensionsToUpperLowerOverlapping))]
public static void MemoryExtensionsToUpperLowerOverlappingThrows(TestHelpers.AssertThrowsAction<char> action)
{
Span<char> buffer = new char[] { 'a', 'b', 'c', 'd' };
TestHelpers.AssertThrows<InvalidOperationException, char>(buffer, action);
}
}
}
1 change: 1 addition & 0 deletions src/System.Memory/tests/System.Memory.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<Compile Include="ReadOnlySpan\StartsWith.T.cs" />
<Compile Include="ReadOnlySpan\ToArray.cs" />
<Compile Include="ReadOnlySpan\ToString.cs" />
<Compile Include="ReadOnlySpan\ToUpperLower.cs" />
<Compile Include="$(CommonTestPath)\Tests\System\SpanTestHelpers.cs">
<Link>ReadOnlySpan\SpanTestHelpers.cs</Link>
</Compile>
Expand Down