Skip to content

Commit

Permalink
Fix Batch regression for empty collection
Browse files Browse the repository at this point in the history
This is a squashed merge of PR #743 that fixes #741.
  • Loading branch information
atifaziz authored Dec 27, 2019
1 parent dc9542e commit b802173
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit b802173

Please sign in to comment.