Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Lead that's missing enumerator disposal & test #668

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 34 additions & 0 deletions MoreLinq.Test/LeadTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
#region License and Terms
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this header but it belongs in a separate PR.

// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq.Test
{
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System.Collections.Generic;

/// <summary>
/// Verify the behavior of the Lead operator.
Expand Down Expand Up @@ -116,5 +135,20 @@ public void TestLeadPassesCorrectValueOffsetBy2()
Assert.IsTrue(result.Take(count - 2).All(x => x.B == (x.A + 2)));
Assert.IsTrue(result.Skip(count - 2).All(x => x.B == leadDefault && (x.A == count || x.A == count - 1)));
}

public static readonly IEnumerable<ITestCaseData> TestData =
from e in new[]
{
new {s = new[] {0, 1, 2}, o = 1, r = new[] {(0, 1), (1, 2), (2, 0)}},
new {s = new[] {0, 1, 2}, o = 2, r = new[] {(0, 2), (1, 0), (2, 0)}},
new {s = new[] {0, 1, 2}, o = 3, r = new[] {(0, 0), (1, 0), (2, 0)}}
}
select new TestCaseData(e.s, e.o).Returns(e.r);

[Test, TestCaseSource(nameof(TestData))]
public (int e, int l)[] TestLeadOnKnownInput(int[] source, int offset)
{
return source.AsTestingSequence().Lead(offset, (e, l) => (e, l)).ToArray();
}
}
}
2 changes: 1 addition & 1 deletion MoreLinq/Lead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IEnumerable<TResult> Lead<TSource, TResult>(this IEnumerable<TSour
return _(); IEnumerable<TResult> _()
{
var leadQueue = new Queue<TSource>(offset);
var iter = source.GetEnumerator();
using var iter = source.GetEnumerator();

bool hasMore;
// first, prefetch and populate the lead queue with the next step of
Expand Down