-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e17bb06
commit 8716d12
Showing
4 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#region License and Terms | ||
// MoreLINQ - Extensions to LINQ to Objects | ||
// Copyright (c) 2017 Leandro F. Vieira (leandromoh). 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 | ||
|
||
using NUnit.Framework; | ||
using System.Linq; | ||
|
||
namespace MoreLinq.Test | ||
{ | ||
[TestFixture] | ||
public class SkipLastTest | ||
{ | ||
[Test] | ||
public void SkipLastWithNullSequence() | ||
{ | ||
Assert.ThrowsArgumentNullException("source", () => MoreEnumerable.SkipLast<int>(null, 1)); | ||
} | ||
|
||
[TestCase( 0)] | ||
[TestCase(-1)] | ||
public void SkipLastWithCountLesserThanOne(int skip) | ||
{ | ||
var numbers = Enumerable.Range(1, 5); | ||
|
||
Assert.IsTrue(numbers.SkipLast(skip).SequenceEqual(numbers)); | ||
} | ||
|
||
[Test] | ||
public void SkipLast() | ||
{ | ||
const int take = 100; | ||
const int skip = 20; | ||
|
||
var sequence = Enumerable.Range(1, take); | ||
|
||
var expectations = sequence.Take(take - skip); | ||
|
||
Assert.That(expectations, Is.EquivalentTo(sequence.SkipLast(skip))); | ||
} | ||
|
||
[TestCase(5)] | ||
[TestCase(6)] | ||
public void SkipLastWithSequenceShorterThanCount(int skip) | ||
{ | ||
Assert.IsFalse(Enumerable.Range(1, 5).SkipLast(skip).Any()); | ||
} | ||
|
||
[Test] | ||
public void SkipLastIsLazy() | ||
{ | ||
new BreakingSequence<object>().SkipLast(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#region License and Terms | ||
// MoreLINQ - Extensions to LINQ to Objects | ||
// Copyright (c) 2017 Leandro F. Vieira (leandromoh). 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 | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
static partial class MoreEnumerable | ||
{ | ||
/// <summary> | ||
/// Bypasses a specified number of elements at the end of the sequence. | ||
/// </summary> | ||
/// <param name="source">The source sequence.</param> | ||
/// <param name="count">The number of elements to bypass at the end of the source sequence.</param> | ||
/// <returns> | ||
/// An <see cref="IEnumerable{T}"/> containing the source sequence elements except for the bypassed ones at the end. | ||
/// </returns> | ||
|
||
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source, int count) | ||
{ | ||
if (source == null) throw new ArgumentNullException(nameof(source)); | ||
|
||
if (count < 1) | ||
return source; | ||
|
||
var col = source as ICollection<T>; | ||
if (col != null) | ||
return col.Take(col.Count - count); | ||
|
||
return SkipLastImpl(source, count); | ||
} | ||
|
||
private static IEnumerable<T> SkipLastImpl<T>(IEnumerable<T> source, int count) | ||
{ | ||
var queue = new Queue<T>(count); | ||
|
||
foreach (var item in source) | ||
{ | ||
if (queue.Count < count) | ||
{ | ||
queue.Enqueue(item); | ||
continue; | ||
} | ||
|
||
yield return queue.Dequeue(); | ||
queue.Enqueue(item); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters