From 257329df2501df99788567a68fe08f8827dad1e4 Mon Sep 17 00:00:00 2001 From: Orace Date: Fri, 8 Nov 2019 20:07:00 +0100 Subject: [PATCH 1/7] Add TestSkipUntilOnKnownInput --- MoreLinq.Test/SkipUntilTest.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/MoreLinq.Test/SkipUntilTest.cs b/MoreLinq.Test/SkipUntilTest.cs index 066ffe643..4ad3451a9 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,20 @@ 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 {s = new[] {1, 2, 3}, o = 1, r = new[] {2, 3}}, + new {s = new[] {1, 2, 3}, o = 2, r = new[] {3}}, + new {s = new[] {1, 2, 3}, o = 3, r = new int[0]} + } + select new TestCaseData(e.s, e.o).Returns(e.r); + + [Test, TestCaseSource(nameof(TestData))] + public int[] TestSkipUntilOnKnownInput(int[] source, int min) + { + return source.AsTestingSequence().SkipUntil(v => v >= min).ToArray(); + } } } From 1abdb31bdcef4b5b0c5db3ab77b9fd7b4c4f925a Mon Sep 17 00:00:00 2001 From: Orace Date: Fri, 8 Nov 2019 20:07:38 +0100 Subject: [PATCH 2/7] Fix SkipUntil implementation Avoid calls to MoveNext after enumerator end. --- MoreLinq/SkipUntil.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MoreLinq/SkipUntil.cs b/MoreLinq/SkipUntil.cs index debbc04a8..bd0b83196 100644 --- a/MoreLinq/SkipUntil.cs +++ b/MoreLinq/SkipUntil.cs @@ -61,14 +61,19 @@ public static IEnumerable SkipUntil(this IEnumerable { using var iterator = source.GetEnumerator(); - while (iterator.MoveNext()) + for(;;) { + if (!iterator.MoveNext()) + yield break; + if (predicate(iterator.Current)) break; } while (iterator.MoveNext()) + { yield return iterator.Current; + } } } } From 6e660870e56694abcd59eb0149a66a4b987b06ed Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 09:31:50 +0100 Subject: [PATCH 3/7] Add test for SkipUntil - empty sequence for input - one-item sequence, predicate succeed - one-item sequence, predicate don't succeed - predicate succeed on first item - predicate succeed on last item - predicate never succeed --- MoreLinq.Test/SkipUntilTest.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MoreLinq.Test/SkipUntilTest.cs b/MoreLinq.Test/SkipUntilTest.cs index 4ad3451a9..fd9519117 100644 --- a/MoreLinq.Test/SkipUntilTest.cs +++ b/MoreLinq.Test/SkipUntilTest.cs @@ -63,9 +63,14 @@ public void SkipUntilEvaluatesPredicateLazily() public static readonly IEnumerable TestData = from e in new[] { + new {s = new int[0] , o = 0, r = new int[0]}, // empty sequence + new {s = new[] {0} , o = 0, r = new int[0]}, // one-item sequence, predicate succeed + new {s = new[] {0} , o = 1, r = new int[0]}, // one-item sequence, predicate don't succeed + new {s = new[] {1, 2, 3}, o = 0, r = new[] {2, 3}}, // predicate succeed on first item new {s = new[] {1, 2, 3}, o = 1, r = new[] {2, 3}}, new {s = new[] {1, 2, 3}, o = 2, r = new[] {3}}, - new {s = new[] {1, 2, 3}, o = 3, r = new int[0]} + new {s = new[] {1, 2, 3}, o = 3, r = new int[0]}, // predicate succeed on last item + new {s = new[] {1, 2, 3}, o = 4, r = new int[0]} // predicate never succeed } select new TestCaseData(e.s, e.o).Returns(e.r); From 1cda627118f69f0d70a4aa36a92879cb9e226e8d Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 20:59:42 +0100 Subject: [PATCH 4/7] Clean-up SkipUntil and SkipUntilTest * Update SkipUntil.cs: Formatting * Update SkipUntilTest.cs: Test readability --- MoreLinq.Test/SkipUntilTest.cs | 20 ++++++++++---------- MoreLinq/SkipUntil.cs | 17 ++++++----------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/MoreLinq.Test/SkipUntilTest.cs b/MoreLinq.Test/SkipUntilTest.cs index fd9519117..25a2df3aa 100644 --- a/MoreLinq.Test/SkipUntilTest.cs +++ b/MoreLinq.Test/SkipUntilTest.cs @@ -63,19 +63,19 @@ public void SkipUntilEvaluatesPredicateLazily() public static readonly IEnumerable TestData = from e in new[] { - new {s = new int[0] , o = 0, r = new int[0]}, // empty sequence - new {s = new[] {0} , o = 0, r = new int[0]}, // one-item sequence, predicate succeed - new {s = new[] {0} , o = 1, r = new int[0]}, // one-item sequence, predicate don't succeed - new {s = new[] {1, 2, 3}, o = 0, r = new[] {2, 3}}, // predicate succeed on first item - new {s = new[] {1, 2, 3}, o = 1, r = new[] {2, 3}}, - new {s = new[] {1, 2, 3}, o = 2, r = new[] {3}}, - new {s = new[] {1, 2, 3}, o = 3, r = new int[0]}, // predicate succeed on last item - new {s = new[] {1, 2, 3}, o = 4, r = new int[0]} // predicate never succeed + 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.s, e.o).Returns(e.r); + select new TestCaseData(e.Source, e.Min).Returns(e.r); [Test, TestCaseSource(nameof(TestData))] - public int[] TestSkipUntilOnKnownInput(int[] source, int min) + 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 bd0b83196..8a32ff726 100644 --- a/MoreLinq/SkipUntil.cs +++ b/MoreLinq/SkipUntil.cs @@ -59,21 +59,16 @@ public static IEnumerable SkipUntil(this IEnumerable return _(); IEnumerable _() { - using var iterator = source.GetEnumerator(); + using var enumerator = source.GetEnumerator(); - for(;;) + do { - if (!iterator.MoveNext()) + if (!enumerator.MoveNext()) yield break; + } while(!predicate(enumerator.Current)) - if (predicate(iterator.Current)) - break; - } - - while (iterator.MoveNext()) - { - yield return iterator.Current; - } + while (enumerator.MoveNext()) + yield return enumerator.Current; } } } From 1679bc970d975d3ad381f672f564193ab86bd634 Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 21:08:09 +0100 Subject: [PATCH 5/7] Update MoreLinq/SkipUntil.cs --- MoreLinq/SkipUntil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/SkipUntil.cs b/MoreLinq/SkipUntil.cs index 8a32ff726..70c5d5035 100644 --- a/MoreLinq/SkipUntil.cs +++ b/MoreLinq/SkipUntil.cs @@ -65,7 +65,7 @@ public static IEnumerable SkipUntil(this IEnumerable { if (!enumerator.MoveNext()) yield break; - } while(!predicate(enumerator.Current)) + } while(!predicate(enumerator.Current)); while (enumerator.MoveNext()) yield return enumerator.Current; From 7618ca89be6540b4a2e0a98a9a792495ce4d148b Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 21:22:08 +0100 Subject: [PATCH 6/7] Update SkipUntilTest.cs Coding may be hard --- MoreLinq.Test/SkipUntilTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/SkipUntilTest.cs b/MoreLinq.Test/SkipUntilTest.cs index 25a2df3aa..438e65025 100644 --- a/MoreLinq.Test/SkipUntilTest.cs +++ b/MoreLinq.Test/SkipUntilTest.cs @@ -72,7 +72,7 @@ from e in new[] 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.r); + select new TestCaseData(e.Source, e.Min).Returns(e.Expected); [Test, TestCaseSource(nameof(TestData))] public int[] TestSkipUntil(int[] source, int min) From a049471e2ec9831df2d8607812154b7ffdcbc59e Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Wed, 13 Nov 2019 16:43:11 +0100 Subject: [PATCH 7/7] Fix code formatting --- MoreLinq.Test/SkipUntilTest.cs | 16 ++++++++-------- MoreLinq/SkipUntil.cs | 3 ++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/MoreLinq.Test/SkipUntilTest.cs b/MoreLinq.Test/SkipUntilTest.cs index 438e65025..a1c541b7b 100644 --- a/MoreLinq.Test/SkipUntilTest.cs +++ b/MoreLinq.Test/SkipUntilTest.cs @@ -63,14 +63,14 @@ public void SkipUntilEvaluatesPredicateLazily() 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 + 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); diff --git a/MoreLinq/SkipUntil.cs b/MoreLinq/SkipUntil.cs index 70c5d5035..7a738c35c 100644 --- a/MoreLinq/SkipUntil.cs +++ b/MoreLinq/SkipUntil.cs @@ -65,7 +65,8 @@ public static IEnumerable SkipUntil(this IEnumerable { if (!enumerator.MoveNext()) yield break; - } while(!predicate(enumerator.Current)); + } + while (!predicate(enumerator.Current)); while (enumerator.MoveNext()) yield return enumerator.Current;