Skip to content
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

CountDown should throw on negative count #133

Merged
merged 4 commits into from
Dec 28, 2022
Merged
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
74 changes: 43 additions & 31 deletions Source/SuperLinq.Async/CountDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,79 @@
public static partial class AsyncSuperEnumerable
{
/// <summary>
/// Provides a countdown counter for a given count of elements at the
/// tail of the sequence where zero always represents the last element,
/// one represents the second-last element, two represents the
/// third-last element and so on.
/// Provides a countdown counter for a given count of elements at the tail of the sequence where zero always
/// represents the last element, one represents the second-last element, two represents the third-last element and
/// so on.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/></typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="count">Count of tail elements of
/// <paramref name="source"/> to count down.</param>
/// <param name="count">Count of tail elements of <paramref name="source"/> to count down.</param>
/// <returns>
/// A sequence of tuples of the element and it's count from the end of the sequence.
/// </returns>
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="count"/> is <c>0</c> or negative.
/// </exception>
/// <remarks>
/// This method uses deferred execution semantics and streams its
/// results. At most, <paramref name="count"/> elements of the source
/// sequence may be buffered at any one time unless
/// <paramref name="source"/> is a collection or a list.
/// <para>
/// This method uses deferred execution semantics and streams its results. At most, <paramref name="count"/>
/// elements of the source sequence may be buffered at any one time unless <paramref name="source"/> is a collection
/// or a list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<(TSource item, int? count)> CountDown<TSource>(this IAsyncEnumerable<TSource> source, int count)
{
return source.CountDown(count, ValueTuple.Create);
}

/// <summary>
/// Provides a countdown counter for a given count of elements at the
/// tail of the sequence where zero always represents the last element,
/// one represents the second-last element, two represents the
/// third-last element and so on.
/// Provides a countdown counter for a given count of elements at the tail of the sequence where zero always
/// represents the last element, one represents the second-last element, two represents the third-last element and
/// so on.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/></typeparam>
/// <typeparam name="TResult">
/// The type of elements of the resulting sequence.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="count">Count of tail elements of
/// <paramref name="source"/> to count down.</param>
/// <param name="count">Count of tail elements of <paramref name="source"/> to count down.</param>
/// <param name="resultSelector">
/// A function that receives the element and the current countdown
/// value for the element and which returns those mapped to a
/// result returned in the resulting sequence. For elements before
/// the last <paramref name="count"/>, the countdown value is
/// <see langword="null"/>.</param>
/// A function that receives the element and the current countdown value for the element and which returns those
/// mapped to a result returned in the resulting sequence. For elements before the last <paramref name="count"/>,
/// the countdown value is <see langword="null"/>.</param>
/// <returns>
/// A sequence of results returned by
/// <paramref name="resultSelector"/>.</returns>
/// A sequence of results returned by <paramref name="resultSelector"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="resultSelector"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="count"/> is <c>0</c> or negative.
/// </exception>
/// <remarks>
/// This method uses deferred execution semantics and streams its
/// results. At most, <paramref name="count"/> elements of the source
/// sequence may be buffered at any one time unless
/// <paramref name="source"/> is a collection or a list.
/// <para>
/// This method uses deferred execution semantics and streams its results. At most, <paramref name="count"/>
/// elements of the source sequence may be buffered at any one time unless <paramref name="source"/> is a collection
/// or a list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="resultSelector"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<TResult> CountDown<TSource, TResult>(
this IAsyncEnumerable<TSource> source,
int count, Func<TSource, int?, TResult> resultSelector)
{
Guard.IsNotNull(source);
Guard.IsNotNull(resultSelector);
Guard.IsGreaterThanOrEqualTo(count, 1);

return _(source, count, resultSelector);

Expand Down
73 changes: 42 additions & 31 deletions Source/SuperLinq/CountDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,79 @@
public static partial class SuperEnumerable
{
/// <summary>
/// Provides a countdown counter for a given count of elements at the
/// tail of the sequence where zero always represents the last element,
/// one represents the second-last element, two represents the
/// third-last element and so on.
/// Provides a countdown counter for a given count of elements at the tail of the sequence where zero always
/// represents the last element, one represents the second-last element, two represents the third-last element and
/// so on.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/></typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="count">Count of tail elements of
/// <paramref name="source"/> to count down.</param>
/// <param name="count">Count of tail elements of <paramref name="source"/> to count down.</param>
/// <returns>
/// A sequence of tuples of the element and it's count from the end of the sequence.
/// </returns>
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="count"/> is <c>0</c> or negative.
/// </exception>
/// <remarks>
/// This method uses deferred execution semantics and streams its
/// results. At most, <paramref name="count"/> elements of the source
/// sequence may be buffered at any one time unless
/// <paramref name="source"/> is a collection or a list.
/// <para>
/// This method uses deferred execution semantics and streams its results. At most, <paramref name="count"/>
/// elements of the source sequence may be buffered at any one time unless <paramref name="source"/> is a collection
/// or a list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
public static IEnumerable<(TSource item, int? count)> CountDown<TSource>(this IEnumerable<TSource> source, int count)
{
return source.CountDown(count, ValueTuple.Create);
}

/// <summary>
/// Provides a countdown counter for a given count of elements at the
/// tail of the sequence where zero always represents the last element,
/// one represents the second-last element, two represents the
/// third-last element and so on.
/// Provides a countdown counter for a given count of elements at the tail of the sequence where zero always
/// represents the last element, one represents the second-last element, two represents the third-last element and
/// so on.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/></typeparam>
/// <typeparam name="TResult">
/// The type of elements of the resulting sequence.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="count">Count of tail elements of
/// <paramref name="source"/> to count down.</param>
/// <param name="count">Count of tail elements of <paramref name="source"/> to count down.</param>
/// <param name="resultSelector">
/// A function that receives the element and the current countdown
/// value for the element and which returns those mapped to a
/// result returned in the resulting sequence. For elements before
/// the last <paramref name="count"/>, the countdown value is
/// <see langword="null"/>.</param>
/// A function that receives the element and the current countdown value for the element and which returns those
/// mapped to a result returned in the resulting sequence. For elements before the last <paramref name="count"/>,
/// the countdown value is <see langword="null"/>.</param>
/// <returns>
/// A sequence of results returned by
/// <paramref name="resultSelector"/>.
/// A sequence of results returned by <paramref name="resultSelector"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="resultSelector"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="count"/> is <c>0</c> or negative.
/// </exception>
/// <remarks>
/// This method uses deferred execution semantics and streams its
/// results. At most, <paramref name="count"/> elements of the source
/// sequence may be buffered at any one time unless
/// <paramref name="source"/> is a collection or a list.
/// <para>
/// This method uses deferred execution semantics and streams its results. At most, <paramref name="count"/>
/// elements of the source sequence may be buffered at any one time unless <paramref name="source"/> is a collection
/// or a list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="resultSelector"/> is <see langword="null"/>.</exception>
public static IEnumerable<TResult> CountDown<TSource, TResult>(
this IEnumerable<TSource> source,
int count, Func<TSource, int?, TResult> resultSelector)
{
Guard.IsNotNull(source);
Guard.IsNotNull(resultSelector);
Guard.IsGreaterThanOrEqualTo(count, 1);

return source.TryGetCollectionCount(out var _)
? IterateCollection(source, count, resultSelector)
Expand Down
17 changes: 8 additions & 9 deletions Tests/SuperLinq.Async.Test/CountDownTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Test.Async;
using SuperLinq;

namespace Test.Async;

public class CountDownTest
{
Expand All @@ -9,20 +11,17 @@ public void IsLazy()
.CountDown(42, BreakingFunc.Of<object, int?, object>());
}

[Fact]
public Task WithNegativeCount()
[Theory]
[InlineData(0), InlineData(-1)]
public void ExceptionOnNegativeCount(int param)
{
const int count = 10;
return AsyncEnumerable.Range(1, count)
.CountDown(-1000, (_, cd) => cd)
.AssertSequenceEqual(Enumerable.Repeat((int?)null, count));
Assert.Throws<ArgumentOutOfRangeException>("count", () =>
AsyncSeq(1).CountDown(param));
}

private static IEnumerable<T> GetData<T>(Func<int[], int, int?[], T> selector)
{
var xs = Enumerable.Range(0, 5).ToArray();
yield return selector(xs, -1, new int?[] { null, null, null, null, null });
yield return selector(xs, 0, new int?[] { null, null, null, null, null });
yield return selector(xs, 1, new int?[] { null, null, null, null, 0 });
yield return selector(xs, 2, new int?[] { null, null, null, 1, 0 });
yield return selector(xs, 3, new int?[] { null, null, 2, 1, 0 });
Expand Down
12 changes: 5 additions & 7 deletions Tests/SuperLinq.Test/CountDownTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ public void IsLazy()
.CountDown(42, BreakingFunc.Of<object, int?, object>());
}

[Fact]
public void WithNegativeCount()
[Theory]
[InlineData(0), InlineData(-1)]
public void ExceptionOnNegativeCount(int param)
{
Enumerable.Range(1, 10)
.CountDown(-1000, (_, cd) => cd)
.AssertSequenceEqual(Enumerable.Repeat((int?)null, 10));
Assert.Throws<ArgumentOutOfRangeException>("count", () =>
Seq(1).CountDown(param));
}

public static IEnumerable<T> GetData<T>(Func<int[], int, int?[], T> selector)
{
var xs = Enumerable.Range(0, 5).ToArray();
yield return selector(xs, -1, new int?[] { null, null, null, null, null });
yield return selector(xs, 0, new int?[] { null, null, null, null, null });
yield return selector(xs, 1, new int?[] { null, null, null, null, 0 });
yield return selector(xs, 2, new int?[] { null, null, null, 1, 0 });
yield return selector(xs, 3, new int?[] { null, null, 2, 1, 0 });
Expand Down