diff --git a/MoreLinq.Test/SkipUntilTest.cs b/MoreLinq.Test/SkipUntilTest.cs index 066ffe643..a1c541b7b 100644 --- a/MoreLinq.Test/SkipUntilTest.cs +++ b/MoreLinq.Test/SkipUntilTest.cs @@ -18,6 +18,8 @@ namespace MoreLinq.Test { using NUnit.Framework; + using NUnit.Framework.Interfaces; + using System.Collections.Generic; [TestFixture] public class SkipUntilTest @@ -57,5 +59,25 @@ public void SkipUntilEvaluatesPredicateLazily() var sequence = Enumerable.Range(-2, 5).SkipUntil(x => 1 / x == -1); sequence.AssertSequenceEqual(0, 1, 2); } + + public static readonly IEnumerable TestData = + from e in new[] + { + new { Source = new int[0] , Min = 0, Expected = new int[0] }, // empty sequence + new { Source = new[] { 0 }, Min = 0, Expected = new int[0] }, // one-item sequence, predicate succeed + new { Source = new[] { 0 }, Min = 1, Expected = new int[0] }, // one-item sequence, predicate don't succeed + new { Source = new[] { 1, 2, 3 }, Min = 0, Expected = new[] { 2, 3 } }, // predicate succeed on first item + new { Source = new[] { 1, 2, 3 }, Min = 1, Expected = new[] { 2, 3 } }, + new { Source = new[] { 1, 2, 3 }, Min = 2, Expected = new[] { 3 } }, + new { Source = new[] { 1, 2, 3 }, Min = 3, Expected = new int[0] }, // predicate succeed on last item + new { Source = new[] { 1, 2, 3 }, Min = 4, Expected = new int[0] } // predicate never succeed + } + select new TestCaseData(e.Source, e.Min).Returns(e.Expected); + + [Test, TestCaseSource(nameof(TestData))] + public int[] TestSkipUntil(int[] source, int min) + { + return source.AsTestingSequence().SkipUntil(v => v >= min).ToArray(); + } } } diff --git a/MoreLinq/SkipUntil.cs b/MoreLinq/SkipUntil.cs index debbc04a8..7a738c35c 100644 --- a/MoreLinq/SkipUntil.cs +++ b/MoreLinq/SkipUntil.cs @@ -59,16 +59,17 @@ public static IEnumerable SkipUntil(this IEnumerable return _(); IEnumerable _() { - using var iterator = source.GetEnumerator(); + using var enumerator = source.GetEnumerator(); - while (iterator.MoveNext()) + do { - if (predicate(iterator.Current)) - break; + if (!enumerator.MoveNext()) + yield break; } + while (!predicate(enumerator.Current)); - while (iterator.MoveNext()) - yield return iterator.Current; + while (enumerator.MoveNext()) + yield return enumerator.Current; } } }