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

Improve Window tests #653

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
17 changes: 17 additions & 0 deletions MoreLinq.Test/WindowLeftTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ public void WindowLeftIsLazy()
new BreakingSequence<int>().WindowLeft(1);
}

/// <summary>
/// Verify that Window doesn't return it's internal buffer
/// </summary>
[Test]
public void WindowLeftDoNotExposeItsBuffer()
{
var sequence = Enumerable.Repeat(0, 3);
var expected = new[] { 0, 0, 0 };
var actual = sequence.WindowLeft(2).Select(l =>
{
if (l.Count > 1)
l[1] = 1;
return l[0];
}).ToList();
CollectionAssert.AreEqual(expected, actual);
}

[Test]
public void WindowLeftWithNegativeWindowSize()
{
Expand Down
17 changes: 17 additions & 0 deletions MoreLinq.Test/WindowRightTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ public void WindowRightIsLazy()
new BreakingSequence<int>().WindowRight(1);
}

/// <summary>
/// Verify that Window doesn't return it's internal buffer
/// </summary>
[Test]
public void WindowRightDoNotExposeItsBuffer()
{
var sequence = Enumerable.Repeat(0, 3);
var expected = new[] { 0, 0, 0 };
var actual = sequence.WindowRight(2).Select(l =>
{
if (l.Count > 1)
l[1] = 1;
return l[0];
}).ToList();
CollectionAssert.AreEqual(expected, actual);
}

[Test]
public void WindowRightWithNegativeWindowSize()
{
Expand Down
41 changes: 40 additions & 1 deletion MoreLinq.Test/WindowTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using NUnit.Framework.Interfaces;

namespace MoreLinq.Test
{
using NUnit.Framework;
Expand All @@ -17,6 +21,22 @@ public void TestWindowIsLazy()
new BreakingSequence<int>().Window(1);
}

/// <summary>
/// Verify that Window doesn't return it's internal buffer
/// </summary>
[Test]
public void TestWindowDoNotExposeItsBuffer()
{
var sequence = Enumerable.Repeat(0, 3);
var expected = new[] {0, 0};
var actual = sequence.Window(2).Select(l =>
{
l[1] = 1;
return l[0];
}).ToList();
CollectionAssert.AreEqual(expected, actual);
}

/// <summary>
/// Verify that a negative window size results in an exception
/// </summary>
Expand All @@ -25,7 +45,7 @@ public void TestWindowNegativeWindowSizeException()
{
var sequence = Enumerable.Repeat(1, 10);

AssertThrowsArgument.OutOfRangeException("size",() =>
AssertThrowsArgument.OutOfRangeException("size", () =>
sequence.Window(-5));
}

Expand Down Expand Up @@ -114,5 +134,24 @@ public void TestWindowWindowsImmutability()
reader.ReadEnd();
}
}

static IEnumerable<T> Seq<T>(params T[] values) => values;

public static readonly IEnumerable<ITestCaseData> TestData =
from e in new[]
{
new {Source = Enumerable.Range(0, 4), Size = 1, Result = new[] {Seq(0), Seq(1), Seq(2), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 2, Result = new[] {Seq(0, 1), Seq(1, 2), Seq(2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 3, Result = new[] {Seq(0, 1, 2), Seq(1, 2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 4, Result = new[] {Seq(0, 1, 2, 3)}}
}
select new TestCaseData(e.Source, e.Size).Returns(e.Result);

[Test, TestCaseSource(nameof(TestData))]
public IEnumerable<IEnumerable<int>> TestWindowOnKnownResults(IEnumerable<int> sequence, int sizes)
{
using var testingSequence = sequence.AsTestingSequence();
return testingSequence.Window(sizes).ToList();
}
}
}