From 0569d1e41fe32fa18858c593a93b9757c8a48a39 Mon Sep 17 00:00:00 2001 From: John Lambert Date: Fri, 11 Oct 2024 10:20:01 -0400 Subject: [PATCH 1/2] If there are no enumerators, it enters an infinite loop. --- src/SIL.Machine/Utils/EnumerableExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SIL.Machine/Utils/EnumerableExtensions.cs b/src/SIL.Machine/Utils/EnumerableExtensions.cs index 1c2fc72b0..8bab692e2 100644 --- a/src/SIL.Machine/Utils/EnumerableExtensions.cs +++ b/src/SIL.Machine/Utils/EnumerableExtensions.cs @@ -13,6 +13,8 @@ Func, TResult> selector { // ToList is necessary to avoid deferred execution List> enumerators = source.Select(seq => seq.GetEnumerator()).ToList(); + if (enumerators.Count == 0) + yield break; try { while (true) From 4c31e2f1a8559b32502907d9ed0749824d17a8ab Mon Sep 17 00:00:00 2001 From: John Lambert Date: Fri, 11 Oct 2024 15:10:54 -0400 Subject: [PATCH 2/2] Add test --- .../Utils/EnumberableExtensionTests.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/SIL.Machine.Tests/Utils/EnumberableExtensionTests.cs diff --git a/tests/SIL.Machine.Tests/Utils/EnumberableExtensionTests.cs b/tests/SIL.Machine.Tests/Utils/EnumberableExtensionTests.cs new file mode 100644 index 000000000..7524087e1 --- /dev/null +++ b/tests/SIL.Machine.Tests/Utils/EnumberableExtensionTests.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; + +namespace SIL.Machine.Utils; + +[TestFixture] +public class EnumerableExtensionTests +{ + [Test] + public void ZipMany_None() + { + var seqs = new List>(); + IEnumerable result = seqs.ZipMany(x => x.Sum()); + Assert.That(result, Is.Empty); + } + + [Test] + public void ZipMany_Two() + { + var seqs = new List> { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } }; + IEnumerable result = seqs.ZipMany(x => x.Sum()); + Assert.That(result, Is.EqualTo(new[] { 5, 7, 9 })); + } +}