From b959af1f04286ffa22f442075ea6086863f1183e Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 21:37:01 +0100 Subject: [PATCH 1/4] Update ExcludeTest.cs Add TestExclude to test known cases. --- MoreLinq.Test/ExcludeTest.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MoreLinq.Test/ExcludeTest.cs b/MoreLinq.Test/ExcludeTest.cs index fbe71da82..a86c62291 100644 --- a/MoreLinq.Test/ExcludeTest.cs +++ b/MoreLinq.Test/ExcludeTest.cs @@ -143,5 +143,14 @@ public void TestExcludeStartIndexGreaterThanSequenceLength() Assert.That(result, Is.EqualTo(sequence)); } + + [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 6, ExpectedResult = new int[0])] + [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 6, ExpectedResult = new[] { 0, 1 })] + [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 3, ExpectedResult = new[] { 3, 4, 5 })] + [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 3, ExpectedResult = new[] { 0, 1, 5 })] + public int[] TestExclude(int[] source, int startIndex, int count) + { + return source.AsTestingSequence().Exclude(startIndex, count).ToArray(); + } } } From c0f165e374b80269b13ca8149528b63a79ee53cf Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 21:43:48 +0100 Subject: [PATCH 2/4] Update Exclude.cs Fix calls to MoveNext on sequence end. --- MoreLinq/Exclude.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/MoreLinq/Exclude.cs b/MoreLinq/Exclude.cs index 6572753ca..60c77ddf9 100644 --- a/MoreLinq/Exclude.cs +++ b/MoreLinq/Exclude.cs @@ -43,15 +43,25 @@ public static IEnumerable Exclude(this IEnumerable sequence, int startI return _(); IEnumerable _() { - var index = -1; + var index = 0; var endIndex = startIndex + count; using var iter = sequence.GetEnumerator(); + // yield the first part of the sequence - while (iter.MoveNext() && ++index < startIndex) + for (; index < startIndex; index++) + { + if (!iter.MoveNext()) + yield break; yield return iter.Current; + } + // skip the next part (up to count items) - while (++index < endIndex && iter.MoveNext()) - continue; + for (; index < endIndex; index++) + { + if (!iter.MoveNext()) + yield break; + } + // yield the remainder of the sequence while (iter.MoveNext()) yield return iter.Current; From 6a598218eec8fec4ac55f292f1056b43bb25e7bd Mon Sep 17 00:00:00 2001 From: Orace Date: Tue, 12 Nov 2019 21:45:17 +0100 Subject: [PATCH 3/4] Update ExcludeTest.cs Fix trailing white space. --- MoreLinq.Test/ExcludeTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/ExcludeTest.cs b/MoreLinq.Test/ExcludeTest.cs index a86c62291..222db4700 100644 --- a/MoreLinq.Test/ExcludeTest.cs +++ b/MoreLinq.Test/ExcludeTest.cs @@ -143,7 +143,7 @@ public void TestExcludeStartIndexGreaterThanSequenceLength() Assert.That(result, Is.EqualTo(sequence)); } - + [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 6, ExpectedResult = new int[0])] [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 6, ExpectedResult = new[] { 0, 1 })] [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 3, ExpectedResult = new[] { 3, 4, 5 })] From 7a85c958675ac13406ae523c652308c1a19589b1 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sat, 21 Jan 2023 00:15:07 +0100 Subject: [PATCH 4/4] Update existing tests to exercise said bug --- MoreLinq.Test/ExcludeTest.cs | 38 ++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/MoreLinq.Test/ExcludeTest.cs b/MoreLinq.Test/ExcludeTest.cs index 1f72bd39d..ff87a8545 100644 --- a/MoreLinq.Test/ExcludeTest.cs +++ b/MoreLinq.Test/ExcludeTest.cs @@ -60,7 +60,7 @@ public void TestExcludeNegativeCountException() [Test] public void TestExcludeWithCountEqualsZero() { - var sequence = Enumerable.Range(1, 10); + using var sequence = Enumerable.Range(1, 10).AsTestingSequence(); var resultA = sequence.Exclude(5, 0); Assert.That(resultA, Is.SameAs(sequence)); @@ -88,10 +88,11 @@ public void TestExcludeEmptySequence() public void TestExcludeSequenceHead() { const int count = 10; - var sequence = Enumerable.Range(1, count); + var xs = Enumerable.Range(1, count); + using var sequence = xs.AsTestingSequence(); var result = sequence.Exclude(0, count / 2); - Assert.That(result, Is.EqualTo(sequence.Skip(count / 2))); + Assert.That(result, Is.EqualTo(xs.Skip(count / 2))); } /// @@ -101,10 +102,11 @@ public void TestExcludeSequenceHead() public void TestExcludeSequenceTail() { const int count = 10; - var sequence = Enumerable.Range(1, count); + var xs = Enumerable.Range(1, count); + using var sequence = xs.AsTestingSequence(); var result = sequence.Exclude(count / 2, count); - Assert.That(result, Is.EqualTo(sequence.Take(count / 2))); + Assert.That(result, Is.EqualTo(xs.Take(count / 2))); } /// @@ -116,10 +118,11 @@ public void TestExcludeSequenceMiddle() const int count = 10; const int startIndex = 3; const int excludeCount = 5; - var sequence = Enumerable.Range(1, count); + var xs = Enumerable.Range(1, count); + using var sequence = xs.AsTestingSequence(); var result = sequence.Exclude(startIndex, excludeCount); - Assert.That(result, Is.EqualTo(sequence.Take(startIndex).Concat(sequence.Skip(startIndex + excludeCount)))); + Assert.That(result, Is.EqualTo(xs.Take(startIndex).Concat(xs.Skip(startIndex + excludeCount)))); } /// @@ -129,7 +132,7 @@ public void TestExcludeSequenceMiddle() public void TestExcludeEntireSequence() { const int count = 10; - var sequence = Enumerable.Range(1, count); + using var sequence = Enumerable.Range(1, count).AsTestingSequence(); var result = sequence.Exclude(0, count); Assert.That(result, Is.Empty); @@ -142,10 +145,11 @@ public void TestExcludeEntireSequence() public void TestExcludeCountGreaterThanSequenceLength() { const int count = 10; - var sequence = Enumerable.Range(1, count); + var xs = Enumerable.Range(1, count); + using var sequence = xs.AsTestingSequence(); var result = sequence.Exclude(1, count * 10); - Assert.That(result, Is.EqualTo(sequence.Take(1))); + Assert.That(result, Is.EqualTo(xs.Take(1))); } /// @@ -155,19 +159,11 @@ public void TestExcludeCountGreaterThanSequenceLength() public void TestExcludeStartIndexGreaterThanSequenceLength() { const int count = 10; - var sequence = Enumerable.Range(1, count); + var xs = Enumerable.Range(1, count); + using var sequence = xs.AsTestingSequence(); var result = sequence.Exclude(count + 5, count); - Assert.That(result, Is.EqualTo(sequence)); - } - - [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 6, ExpectedResult = new int[0])] - [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 6, ExpectedResult = new[] { 0, 1 })] - [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 3, ExpectedResult = new[] { 3, 4, 5 })] - [TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 3, ExpectedResult = new[] { 0, 1, 5 })] - public int[] TestExclude(int[] source, int startIndex, int count) - { - return source.AsTestingSequence().Exclude(startIndex, count).ToArray(); + Assert.That(result, Is.EqualTo(xs)); } } }