Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions MoreLinq.Test/SkipUntilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace MoreLinq.Test
{
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System.Collections.Generic;

[TestFixture]
public class SkipUntilTest
Expand Down Expand Up @@ -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<ITestCaseData> 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();
}
}
}
13 changes: 7 additions & 6 deletions MoreLinq/SkipUntil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ public static IEnumerable<TSource> SkipUntil<TSource>(this IEnumerable<TSource>

return _(); IEnumerable<TSource> _()
{
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;
}
}
}
Expand Down