-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add MemoryExtensions.Min/Max #128306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manandre
wants to merge
13
commits into
dotnet:main
Choose a base branch
from
manandre:span-min-max
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add MemoryExtensions.Min/Max #128306
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
baff8f3
Add MemoryExtensions.Min/Max
manandre 6ef9e87
Add specific exception message when span is empty
manandre a1023cc
Reject null comparer
manandre d214de8
Update documentation
manandre f5107bd
Remove cache file
manandre 93c617a
Apply Copilot suggestions
manandre b4fb827
Align implementation on enumerable logic
manandre 8d32597
Add fast paths
manandre 69566d1
Add tests for integer types
manandre c2c8dfa
Add tests for NaN case
manandre 55b1cf7
Small fixes
manandre 0acc339
Reduce code duplication
manandre 9c71813
Add test
manandre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class ReadOnlySpanTests | ||
| { | ||
| [Fact] | ||
| public static void MinMax_Empty_NonNullableValueType_Throws() | ||
| { | ||
| ReadOnlySpan<int> span = ReadOnlySpan<int>.Empty; | ||
| IComparer<int> nonDefaultComparer = Comparer<int>.Create((x, y) => x.CompareTo(y)); | ||
|
|
||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, _ => _.Min()); | ||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, _ => _.Max()); | ||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, _ => _.Min(Comparer<int>.Default)); | ||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, _ => _.Max(Comparer<int>.Default)); | ||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, _ => _.Min(nonDefaultComparer)); | ||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, _ => _.Max(nonDefaultComparer)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_Empty_ReferenceAndNullableValueType_ReturnsNull() | ||
| { | ||
| ReadOnlySpan<string?> strings = ReadOnlySpan<string?>.Empty; | ||
| ReadOnlySpan<int?> nullableInts = ReadOnlySpan<int?>.Empty; | ||
|
|
||
| Assert.Null(strings.Min()); | ||
| Assert.Null(strings.Max()); | ||
| Assert.Null(nullableInts.Min()); | ||
| Assert.Null(nullableInts.Max()); | ||
|
|
||
| Assert.Null(strings.Min(comparer: null)); | ||
| Assert.Null(strings.Max(comparer: null)); | ||
| Assert.Null(nullableInts.Min(comparer: null)); | ||
| Assert.Null(nullableInts.Max(comparer: null)); | ||
| } | ||
|
manandre marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public static void MinMax_AllNull_ReturnsNull() | ||
| { | ||
| ReadOnlySpan<string?> strings = new string?[] { null, null, null }; | ||
| ReadOnlySpan<int?> nullableInts = new int?[] { null, null, null }; | ||
|
|
||
| Assert.Null(strings.Min()); | ||
| Assert.Null(strings.Max()); | ||
| Assert.Null(nullableInts.Min()); | ||
| Assert.Null(nullableInts.Max()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_NullNotFirst_NullIgnoredForComparison() | ||
| { | ||
| ReadOnlySpan<string?> strings = new string?[] { "charlie", null, "bravo", null, "delta" }; | ||
| ReadOnlySpan<int?> nullableInts = new int?[] { 4, null, -1, null, 7 }; | ||
|
|
||
| Assert.Equal("bravo", strings.Min()); | ||
| Assert.Equal("delta", strings.Max()); | ||
| Assert.Equal(-1, nullableInts.Min()); | ||
| Assert.Equal(7, nullableInts.Max()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_DefaultComparer_ProducesExpectedValues() | ||
|
manandre marked this conversation as resolved.
|
||
| { | ||
| ReadOnlySpan<int> ints = new int[] { 4, -1, 7, 2 }; | ||
| ReadOnlySpan<string?> strings = new string?[] { null, "charlie", "bravo", null, "delta" }; | ||
|
|
||
| Assert.Equal(-1, ints.Min()); | ||
| Assert.Equal(7, ints.Max()); | ||
|
|
||
| Assert.Equal("bravo", strings.Min()); | ||
| Assert.Equal("delta", strings.Max()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_CustomComparer_IsUsed() | ||
| { | ||
| ReadOnlySpan<int> ints = new int[] { 4, -1, 7, 2 }; | ||
| IComparer<int> reverse = Comparer<int>.Create((left, right) => right.CompareTo(left)); | ||
|
manandre marked this conversation as resolved.
|
||
|
|
||
| Assert.Equal(7, ints.Min(reverse)); | ||
| Assert.Equal(-1, ints.Max(reverse)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_IntegerTypes_DefaultAndNullComparer_ProduceExpectedValues() | ||
| { | ||
| AssertMinMaxValues(new byte[] { 12, 3, 255, 7 }, (byte)3, (byte)255); | ||
| AssertMinMaxValues(new sbyte[] { 12, -9, 100, 7 }, (sbyte)-9, (sbyte)100); | ||
| AssertMinMaxValues(new ushort[] { 12, 3, 65535, 7 }, (ushort)3, ushort.MaxValue); | ||
| AssertMinMaxValues(new short[] { 12, -9, 100, 7 }, (short)-9, (short)100); | ||
| AssertMinMaxValues(new char[] { 'x', 'b', 'm', 'z' }, 'b', 'z'); | ||
| AssertMinMaxValues(new uint[] { 12u, 3u, 400u, 7u }, 3u, 400u); | ||
| AssertMinMaxValues(new int[] { 12, -9, 400, 7 }, -9, 400); | ||
| AssertMinMaxValues(new ulong[] { 12ul, 3ul, 400ul, 7ul }, 3ul, 400ul); | ||
| AssertMinMaxValues(new long[] { 12L, -9L, 400L, 7L }, -9L, 400L); | ||
| AssertMinMaxValues(new nuint[] { (nuint)12, (nuint)3, (nuint)400, (nuint)7 }, (nuint)3, (nuint)400); | ||
| AssertMinMaxValues(new nint[] { (nint)12, (nint)(-9), (nint)400, (nint)7 }, (nint)(-9), (nint)400); | ||
| AssertMinMaxValues(new Int128[] { (Int128)12, (Int128)(-9), (Int128)400, (Int128)7 }, (Int128)(-9), (Int128)400); | ||
| AssertMinMaxValues(new UInt128[] { (UInt128)12, (UInt128)3, (UInt128)400, (UInt128)7 }, (UInt128)3, (UInt128)400); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(float.NaN, 1f, 2f)] | ||
| [InlineData(1f, float.NaN, 2f)] | ||
| [InlineData(1f, 2f, float.NaN)] | ||
| public static void MinMax_Float_NaN_UsesExpectedOrdering(float first, float second, float third) | ||
| { | ||
| float[] values = [first, second, third]; | ||
| ReadOnlySpan<float> span = values; | ||
|
|
||
| Assert.True(float.IsNaN(span.Min())); | ||
| Assert.True(float.IsNaN(span.Min(comparer: null))); | ||
| Assert.Equal(2f, span.Max()); | ||
| Assert.Equal(2f, span.Max(comparer: null)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(double.NaN, 1d, 2d)] | ||
| [InlineData(1d, double.NaN, 2d)] | ||
| [InlineData(1d, 2d, double.NaN)] | ||
| public static void MinMax_Double_NaN_UsesExpectedOrdering(double first, double second, double third) | ||
| { | ||
| double[] values = [first, second, third]; | ||
| ReadOnlySpan<double> span = values; | ||
|
|
||
| Assert.True(double.IsNaN(span.Min())); | ||
| Assert.True(double.IsNaN(span.Min(comparer: null))); | ||
| Assert.Equal(2d, span.Max()); | ||
| Assert.Equal(2d, span.Max(comparer: null)); | ||
| } | ||
|
|
||
| private static void AssertMinMaxValues<T>(T[] values, T expectedMin, T expectedMax) | ||
| where T : IComparable<T> | ||
| { | ||
| ReadOnlySpan<T> span = values; | ||
| IComparer<T> comparer = Comparer<T>.Create(static (left, right) => left.CompareTo(right)); | ||
| IComparer<T> reverseComparer = Comparer<T>.Create(static (left, right) => right.CompareTo(left)); | ||
|
|
||
| Assert.Equal(expectedMin, span.Min()); | ||
| Assert.Equal(expectedMax, span.Max()); | ||
| Assert.Equal(expectedMin, span.Min(comparer: null)); | ||
| Assert.Equal(expectedMax, span.Max(comparer: null)); | ||
| Assert.Equal(expectedMin, span.Min(comparer)); | ||
| Assert.Equal(expectedMax, span.Max(comparer)); | ||
| Assert.Equal(expectedMax, span.Min(reverseComparer)); | ||
| Assert.Equal(expectedMin, span.Max(reverseComparer)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.