Skip to content

Commit b802173

Browse files
authored
Fix Batch regression for empty collection
This is a squashed merge of PR #743 that fixes #741.
1 parent dc9542e commit b802173

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

MoreLinq.Test/BatchTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,15 @@ public void BatchReadOnlyCollectionSmallerThanSize(int oversize)
117117
reader.Read().AssertSequenceEqual(1, 2, 3, 4, 5);
118118
reader.ReadEnd();
119119
}
120+
121+
[TestCase(SourceKind.Sequence)]
122+
[TestCase(SourceKind.BreakingList)]
123+
[TestCase(SourceKind.BreakingReadOnlyList)]
124+
[TestCase(SourceKind.BreakingCollection)]
125+
public void BatchEmptySource(SourceKind kind)
126+
{
127+
var batches = Enumerable.Empty<int>().ToSourceKind(kind).Batch(100);
128+
Assert.That(batches, Is.Empty);
129+
}
120130
}
121131
}

MoreLinq/Batch.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace MoreLinq
1919
{
2020
using System;
2121
using System.Collections.Generic;
22+
using System.Linq;
2223

2324
static partial class MoreEnumerable
2425
{
@@ -86,6 +87,10 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou
8687

8788
switch (source)
8889
{
90+
case ICollection<TSource> collection when collection.Count == 0:
91+
{
92+
return Enumerable.Empty<TResult>();
93+
}
8994
case ICollection<TSource> collection when collection.Count <= size:
9095
{
9196
return _(); IEnumerable<TResult> _()
@@ -95,6 +100,10 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou
95100
yield return resultSelector(bucket);
96101
}
97102
}
103+
case IReadOnlyList<TSource> list when list.Count == 0:
104+
{
105+
return Enumerable.Empty<TResult>();
106+
}
98107
case IReadOnlyList<TSource> list when list.Count <= size:
99108
{
100109
return _(); IEnumerable<TResult> _()

0 commit comments

Comments
 (0)