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

Add BatchBy #659

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
218 changes: 218 additions & 0 deletions MoreLinq.Test/BatchByTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
using NUnit.Framework.Interfaces;

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

[TestFixture]
public class BatchByTests
{
public static readonly IEnumerable<ITestCaseData> TestData =
from d in new[]
{
new {
s = new[] {"Q1", "Q2", "A1", "A2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
new {
s = new[] {"Q1", "A1", "Q2", "A2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
new {
s = new[] {"A1", "Q1", "Q2", "A2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
new {
s = new[] {"Q1", "A1", "A2", "Q2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
new {
s = new[] {"A1", "Q1", "A2", "Q2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
new {
s = new[] {"A1", "A2", "Q1", "Q2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
new {
s = new[] {"Q1", null, "A1", "#", "Q2", "A2"},
k = new[] {"Q", "A"},
r = new[] {
new [] {("A", "A1"), ("Q", "Q1")},
new [] {("A", "A2"), ("Q", "Q2")}}},
}
select new TestCaseData(d.s, d.k).Returns(d.r);


[Test, TestCaseSource(nameof(TestData))]
public (string Key, string Value)[][] BatchByOnKnownCase(string[] source, string[] acceptedKeys)
{
static string KeySelector(string s) => s?.FirstOrDefault().ToString();
var equalityComparer = EqualityComparer<string>.Default;

return source.BatchBy(acceptedKeys, KeySelector, equalityComparer)
.Select(d => d.Select(kvp => (kvp.Key, kvp.Value)).OrderBy(kvp => kvp.Key).ToArray())
.ToArray();
}

[Test]
public void BatchByDoNotEnumerateSourceOnEmptyAcceptedKeys()
{
var source = MoreEnumerable.From<int>(() => throw new TestException());
static string KeySelector(int i) => $"{i}";
var acceptedKeys = Enumerable.Empty<string>();
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer);
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByDoNotReEnumerateSourceOnMultipleEnumeration()
{
var source = Enumerable.Range(0, 5);
static string KeySelector(int i) => $"{i}";
var acceptedKeys = TestingSequence.Of("0", "1", "2", "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
var enumerable = source.BatchBy(acceptedKeys, KeySelector, equalityComparer);
enumerable.Consume();
enumerable.Consume();
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByDoNotThrowOnDuplicateAcceptedKeyAtCreation()
{
using var source = TestingSequence.Of(0, 1, 2, 3);
static string KeySelector(int i) => $"{i}";
var acceptedKeys = TestingSequence.Of("0", "1", "1", "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer);
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByDoNotThrowOnNullAcceptedKeyAtCreation()
{
using var source = TestingSequence.Of(0, 1, 2, 3);
static string KeySelector(int i) => $"{i}";
var acceptedKeys = TestingSequence.Of("0", "1", null, "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer);
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByDoNotThrowOnNullKeyFromKeySelector()
{
using var source = TestingSequence.Of(0, 1, 2, 3);
static string KeySelector(int i) => i == 0 ? null : $"{i}";
var acceptedKeys = TestingSequence.Of("0", "1", "1", "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer);
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByDoNotThrowOnUnknownKeyFromKeySelector()
{
using var source = TestingSequence.Of(0, 1, 2, 3);
static string KeySelector(int i) => $"#{i}#";
var acceptedKeys = TestingSequence.Of("0", "1", "1", "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer);
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByIsLazy()
{
var source = new BreakingSequence<int>();
var keySelector = BreakingFunc.Of<int, string>();
var acceptedKeys = new BreakingSequence<string>();
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, keySelector, equalityComparer);
}

Assert.DoesNotThrow(Code);
}

[Test]
public void BatchByThrowOnDuplicateAcceptedKeyOnFirstIteration()
{
using var source = TestingSequence.Of(0, 1, 2, 3);
static string KeySelector(int i) => $"{i}";
var acceptedKeys = TestingSequence.Of("0", "1", "1", "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer).GetEnumerator().MoveNext();
}

Assert.Throws<ArgumentException>(Code);
}

[Test]
public void BatchByThrowOnNullAcceptedKeyOnFirstIteration()
{
using var source = TestingSequence.Of(0, 1, 2, 3);
static string KeySelector(int i) => $"{i}";
var acceptedKeys = TestingSequence.Of("0", "1", null, "3");
var equalityComparer = EqualityComparer<string>.Default;

void Code()
{
source.BatchBy(acceptedKeys, KeySelector, equalityComparer).GetEnumerator().MoveNext();
}

Assert.Throws<ArgumentNullException>(Code);
}
}
}
1 change: 1 addition & 0 deletions MoreLinq.Test/MoreLinq.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="..\MoreLinq\Disposable.cs" Link="Disposable.cs" />
<Compile Include="..\MoreLinq\Reactive\Subject.cs" Link="Subject.cs" />
<Compile Include="AssertThrowsArgument.cs" />
<Compile Include="BatchByTests.cs" />
<Compile Include="BreakingCollection.cs" />
<Compile Include="BreakingAction.cs" />
<Compile Include="BreakingFunc.cs" />
Expand Down
Loading