From b90b4fc370f1f00c565aff9dd46699cebcdb7664 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 27 Dec 2019 10:01:36 +0100 Subject: [PATCH 1/2] Add tests to demo issue #741 --- MoreLinq.Test/BatchTest.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/MoreLinq.Test/BatchTest.cs b/MoreLinq.Test/BatchTest.cs index 09cdadbae..f297b2969 100644 --- a/MoreLinq.Test/BatchTest.cs +++ b/MoreLinq.Test/BatchTest.cs @@ -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().ToSourceKind(kind).Batch(100); + Assert.That(batches, Is.Empty); + } } } From 5b6a8082addeb86475086ca79cb9bdb7945f627d Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 27 Dec 2019 10:04:30 +0100 Subject: [PATCH 2/2] Fix bug for when source is an empty collection --- MoreLinq/Batch.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MoreLinq/Batch.cs b/MoreLinq/Batch.cs index c6db41d82..99afb6102 100644 --- a/MoreLinq/Batch.cs +++ b/MoreLinq/Batch.cs @@ -19,6 +19,7 @@ namespace MoreLinq { using System; using System.Collections.Generic; + using System.Linq; static partial class MoreEnumerable { @@ -86,6 +87,10 @@ public static IEnumerable Batch(this IEnumerable collection when collection.Count == 0: + { + return Enumerable.Empty(); + } case ICollection collection when collection.Count <= size: { return _(); IEnumerable _() @@ -95,6 +100,10 @@ public static IEnumerable Batch(this IEnumerable list when list.Count == 0: + { + return Enumerable.Empty(); + } case IReadOnlyList list when list.Count <= size: { return _(); IEnumerable _()