-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Implement Enumerable.Reverse(TSource[]) #107957
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,21 @@ public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> so | |
return new ReverseIterator<TSource>(source); | ||
} | ||
|
||
public static IEnumerable<TSource> Reverse<TSource>(this TSource[] source) | ||
{ | ||
if (source is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | ||
} | ||
|
||
if (source.Length == 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I would probably just have this method call the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, my thought process was "I could just call |
||
{ | ||
return []; | ||
} | ||
|
||
return new ReverseIterator<TSource>(source); | ||
} | ||
|
||
/// <summary> | ||
/// An iterator that yields the items of an <see cref="IEnumerable{TSource}"/> in reverse. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ public class ReverseTests : EnumerableTests | |
[Fact] | ||
public void InvalidArguments() | ||
{ | ||
AssertExtensions.Throws<ArgumentNullException>("source", () => Enumerable.Reverse<string>(null)); | ||
AssertExtensions.Throws<ArgumentNullException>("source", () => Enumerable.Reverse<string>((IEnumerable<string>)null)); | ||
AssertExtensions.Throws<ArgumentNullException>("source", () => Enumerable.Reverse<string>((string[])null)); | ||
} | ||
|
||
[Theory] | ||
|
@@ -48,6 +49,40 @@ public void Reverse<T>(IEnumerable<T> source) | |
Assert.Equal(actual, actual); // Repeat the enumeration against itself. | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ReverseData))] | ||
public void ReverseArray<T>(IEnumerable<T> source) | ||
{ | ||
T[] expected = source.ToArray(); | ||
Array.Reverse(expected); | ||
|
||
IEnumerable<T> actual = source.ToArray().Reverse(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this test exercise the case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, an empty array is in the input data. |
||
|
||
Assert.Equal(expected, actual); | ||
Assert.Equal(expected.Count(), actual.Count()); // Count may be optimized. | ||
Assert.Equal(expected, actual.ToArray()); | ||
Assert.Equal(expected, actual.ToList()); | ||
|
||
Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); | ||
Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); | ||
|
||
for (int i = 0; i < expected.Length; i++) | ||
{ | ||
Assert.Equal(expected[i], actual.ElementAt(i)); | ||
|
||
Assert.Equal(expected.Skip(i), actual.Skip(i)); | ||
Assert.Equal(expected.Take(i), actual.Take(i)); | ||
} | ||
|
||
Assert.Equal(default(T), actual.ElementAtOrDefault(-1)); | ||
Assert.Equal(default(T), actual.ElementAtOrDefault(expected.Length)); | ||
|
||
Assert.Equal(expected, actual.Select(_ => _)); | ||
Assert.Equal(expected, actual.Where(_ => true)); | ||
|
||
Assert.Equal(actual, actual); // Repeat the enumeration against itself. | ||
} | ||
|
||
[Theory, MemberData(nameof(ReverseData))] | ||
public void RunOnce<T>(IEnumerable<T> source) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish there was a simple way to distinguish between the different overloads, but I suppose this should suffice.