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

Restore Slice to supported operator #588

Merged
merged 2 commits into from
Nov 24, 2023
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
1 change: 0 additions & 1 deletion Source/SuperLinq/Slice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public static partial class SuperEnumerable
/// return all elements up to that point. There is no guarantee that the resulting sequence will contain the
/// number of elements requested - it may have anywhere from 0 to <paramref name="count"/>.
/// </remarks>
[Obsolete("Slice has been replaced by Take(Range).")]
public static IEnumerable<T> Slice<T>(this IEnumerable<T> source, int startIndex, int count)
{
return source.Take(startIndex..(startIndex + count));
Expand Down
76 changes: 76 additions & 0 deletions Tests/SuperLinq.Test/SliceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace Test;

/// <summary>
/// Verify the behavior of the Slice operator
/// </summary>
public class SliceTests
{
/// <summary>
/// Verify that Slice evaluates in a lazy manner.
/// </summary>
[Fact]
public void TestSliceIsLazy()
{
_ = new BreakingSequence<int>().Slice(10, 10);
}

public static IEnumerable<object[]> GetSequences() =>
Enumerable.Range(1, 5)
.GetCollectionSequences()
.Select(x => new object[] { x });

[Theory]
[MemberData(nameof(GetSequences))]
public void TestSliceIdentity(IDisposableEnumerable<int> seq)
{
using (seq)
{
var result = seq.Slice(0, 5);
result.AssertSequenceEqual(1, 2, 3, 4, 5);
}
}

[Theory]
[MemberData(nameof(GetSequences))]
public void TestSliceFirstItem(IDisposableEnumerable<int> seq)
{
using (seq)
{
var result = seq.Slice(0, 1);
result.AssertSequenceEqual(1);
}
}

[Theory]
[MemberData(nameof(GetSequences))]
public void TestSliceLastItem(IDisposableEnumerable<int> seq)
{
using (seq)
{
var result = seq.Slice(4, 1);
result.AssertSequenceEqual(5);
}
}

[Theory]
[MemberData(nameof(GetSequences))]
public void TestSliceSmallerThanSequence(IDisposableEnumerable<int> seq)
{
using (seq)
{
var result = seq.Slice(1, 2);
result.AssertSequenceEqual(2, 3);
}
}

[Theory]
[MemberData(nameof(GetSequences))]
public void TestSliceLongerThanSequence(IDisposableEnumerable<int> seq)
{
using (seq)
{
var result = seq.Slice(3, 5);
result.AssertSequenceEqual(4, 5);
}
}
}