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

Refactor Window(Left|Right) #656

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8e179ad
Add TestWindowDoNotExposeItsBuffer, WindowLeftDoNotExposeItsBuffer an…
Orace Nov 6, 2019
7cc8615
Add TestWindowOnKnownResults
Orace Nov 6, 2019
8765b1b
Renamed tests Window*DoNotExposeItsBuffer => TestWindow*ReturnIsolate…
Orace Nov 6, 2019
340858b
Fix TestWindow*ReturnIsolatedList
Orace Nov 6, 2019
e41a21d
Window* methods returned elements that are not isolated from each other.
Orace Nov 6, 2019
2ca1b7a
Add TestWindowLeftOnKnownResults and TestWindowRightOnKnownResults
Orace Nov 6, 2019
207781b
Refactor Window(Left|Right) so they use the same implementation.
Orace Nov 6, 2019
f2864ce
Removed useless internal method in Window implementation
Orace Nov 6, 2019
7b4653a
Remove unused using directives.
Orace Nov 6, 2019
6c0de36
Added some test for Window(Left|Right):
Orace Nov 6, 2019
9fd0df4
Merge pull request #1 from Orace/FixWindow
Orace Nov 6, 2019
f22964f
Merge commit '2ca1b7a' into FixWindow
Orace Nov 6, 2019
a4e05ce
Merge branch 'FixWindow' into ImproveWindow
Orace Nov 6, 2019
66c29a3
Rename ModifyWindowBeforeMoveNextDoNotAffectPrevWindow to ModifyWind…
Orace Nov 6, 2019
d221cea
Rename ModifyWindowBeforeMoveNextDoNotAffectPrevWindow to ModifyWind…
Orace Nov 6, 2019
be6af97
Merge branch 'FixWindow' into ImproveWindow
Orace Nov 6, 2019
5598a8d
Simplify Window implementation
Orace Nov 6, 2019
cc21cc4
Clean-up tests
Orace Nov 8, 2019
625758f
Merge branch 'FixWindow' into ImproveWindow
Orace Nov 8, 2019
3179659
Niptick improvement in Window
Orace Nov 8, 2019
0c9bd2b
Update MoreLinq/Window.cs
Orace Nov 8, 2019
1e46804
Update MoreLinq/Window.cs
Orace Nov 8, 2019
a265e39
Merge branch 'master' into FixWindow
Orace Nov 15, 2019
f33d0fe
Merge remote-tracking branch 'origin/FixWindow' into ImproveWindow
Orace Nov 15, 2019
99f3666
Merge branch 'mlMaster' into ImproveWindow
Orace Nov 20, 2019
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
83 changes: 83 additions & 0 deletions MoreLinq.Test/WindowLeftTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
#region License and Terms
// 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 System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

[TestFixture]
public class WindowLeftTest
Expand All @@ -12,6 +30,51 @@ public void WindowLeftIsLazy()
new BreakingSequence<int>().WindowLeft(1);
}

[Test]
public void ModifyWindowBeforeMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowLeft(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
window1[1] = -1;
e.MoveNext();
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowAfterMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowLeft(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
window1[1] = -1;
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowDoNotAffectPrevWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowLeft(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
var window2 = e.Current;
window2[0] = -1;

Assert.That(window1[1], Is.EqualTo(1));
}

[Test]
public void WindowLeftWithNegativeWindowSize()
{
Expand Down Expand Up @@ -76,5 +139,25 @@ public void WindowLeftWithWindowSizeSmallerThanSequence()
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), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 3, Result = new[] {Seq(0, 1, 2), Seq(1, 2, 3), Seq(2, 3), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 4, Result = new[] {Seq(0, 1, 2, 3), Seq(1, 2, 3), Seq(2, 3), Seq(3)}}
}
select new TestCaseData(e.Source, e.Size).Returns(e.Result);

[Test, TestCaseSource(nameof(TestData))]
public IEnumerable<IEnumerable<int>> TestWindowLeftOnKnownResults(IEnumerable<int> sequence, int sizes)
{
using var testingSequence = sequence.AsTestingSequence();
var r = testingSequence.WindowLeft(sizes).ToList();
return r;
}
}
}
83 changes: 83 additions & 0 deletions MoreLinq.Test/WindowRightTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
#region License and Terms
// 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 System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

[TestFixture]
public class WindowRightTest
Expand All @@ -12,6 +30,51 @@ public void WindowRightIsLazy()
new BreakingSequence<int>().WindowRight(1);
}

[Test]
public void ModifyWindowBeforeMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowRight(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
window1[0] = -1;
e.MoveNext();
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(0));
}

[Test]
public void ModifyWindowAfterMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowRight(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
window1[0] = -1;
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(0));
}

[Test]
public void ModifyWindowDoNotAffectPrevWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowRight(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
var window2 = e.Current;
window2[0] = -1;

Assert.That(window1[0], Is.EqualTo(0));
}

[Test]
public void WindowRightWithNegativeWindowSize()
{
Expand Down Expand Up @@ -76,5 +139,25 @@ public void WindowRightWithWindowSizeSmallerThanSequence()
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), Seq(0, 1), Seq(1, 2), Seq(2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 3, Result = new[] {Seq(0), Seq(0, 1), Seq(0, 1, 2), Seq(1, 2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 4, Result = new[] {Seq(0), Seq(0, 1), Seq(0, 1, 2), Seq(0, 1, 2, 3)}}
}
select new TestCaseData(e.Source, e.Size).Returns(e.Result);

[Test, TestCaseSource(nameof(TestData))]
public IEnumerable<IEnumerable<int>> TestWindowRightOnKnownResults(IEnumerable<int> sequence, int sizes)
{
using var testingSequence = sequence.AsTestingSequence();
var r = testingSequence.WindowRight(sizes).ToList();
return r;
}
}
}
85 changes: 84 additions & 1 deletion MoreLinq.Test/WindowTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
#region License and Terms
// 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 System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

/// <summary>
/// Verify the behavior of the Window operator
Expand All @@ -17,6 +36,51 @@ public void TestWindowIsLazy()
new BreakingSequence<int>().Window(1);
}

[Test]
public void ModifyWindowBeforeMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.Window(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
window1[1] = -1;
e.MoveNext();
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowAfterMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.Window(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
window1[1] = -1;
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowDoNotAffectPrevWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.Window(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
var window2 = e.Current;
window2[0] = -1;

Assert.That(window1[1], Is.EqualTo(1));
}

/// <summary>
/// Verify that a negative window size results in an exception
/// </summary>
Expand All @@ -25,7 +89,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 +178,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();
}
}
}
Loading