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

Fix Batch regression when source is an empty collection #743

Merged
merged 2 commits into from
Dec 27, 2019
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
10 changes: 10 additions & 0 deletions MoreLinq.Test/BatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,15 @@ public void BatchReadOnlyCollectionSmallerThanSize(int oversize)
reader.Read().AssertSequenceEqual(1, 2, 3, 4, 5);
reader.ReadEnd();
}

[TestCase(SourceKind.Sequence)]
[TestCase(SourceKind.BreakingList)]
[TestCase(SourceKind.BreakingReadOnlyList)]
[TestCase(SourceKind.BreakingCollection)]
public void BatchEmptySource(SourceKind kind)
{
var batches = Enumerable.Empty<int>().ToSourceKind(kind).Batch(100);
Assert.That(batches, Is.Empty);
}
}
}
9 changes: 9 additions & 0 deletions MoreLinq/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Linq;

static partial class MoreEnumerable
{
Expand Down Expand Up @@ -86,6 +87,10 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou

switch (source)
{
case ICollection<TSource> collection when collection.Count == 0:
{
return Enumerable.Empty<TResult>();
}
case ICollection<TSource> collection when collection.Count <= size:
{
return _(); IEnumerable<TResult> _()
Expand All @@ -95,6 +100,10 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou
yield return resultSelector(bucket);
}
}
case IReadOnlyList<TSource> list when list.Count == 0:
{
return Enumerable.Empty<TResult>();
}
case IReadOnlyList<TSource> list when list.Count <= size:
{
return _(); IEnumerable<TResult> _()
Expand Down