-
Notifications
You must be signed in to change notification settings - Fork 416
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
5ad0a65
commit 0ec9b4f
Showing
9 changed files
with
538 additions
and
31 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
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,86 @@ | ||
#region License and Terms | ||
// MoreLINQ - Extensions to LINQ to Objects | ||
// Copyright (c) 2019 Phillip Palk. 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; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class TuplewiseTest | ||
{ | ||
private void TuplewiseNWide<TSource, TResult, TFunc>(Func<IEnumerable<TSource>, TFunc, IEnumerable<TResult>> tuplewise, TFunc resultSelector, IEnumerable<TSource> source, params TResult[] fullResult) | ||
{ | ||
var arity = resultSelector.GetType().GetGenericArguments().Length - 1; | ||
|
||
for (var i = 0; i < fullResult.Length; ++i) | ||
using (var ts = source.Take(i).AsTestingSequence()) | ||
Assert.That(tuplewise(ts, resultSelector), Is.EqualTo(fullResult.Take(i - arity + 1))); | ||
} | ||
|
||
private void TuplewiseNWideInt<TFunc>(Func<IEnumerable<int>, TFunc, IEnumerable<int>> tuplewise, TFunc resultSelector) | ||
{ | ||
const int rangeLen = 100; | ||
var arity = resultSelector.GetType().GetGenericArguments().Length - 1; | ||
|
||
TuplewiseNWide( | ||
tuplewise, | ||
resultSelector, | ||
Enumerable.Range(1, rangeLen), | ||
Enumerable.Range(1, rangeLen - (arity - 1)).Select(x => x * arity + Enumerable.Range(1, arity - 1).Sum()).ToArray() | ||
); | ||
} | ||
|
||
private void TuplewiseNWideString<TFunc>(Func<IEnumerable<char>, TFunc, IEnumerable<string>> tuplewise, TFunc resultSelector) | ||
{ | ||
const string alphabet = "abcdefghijklmnopqrstuvwxyz"; | ||
var arity = resultSelector.GetType().GetGenericArguments().Length - 1; | ||
|
||
TuplewiseNWide( | ||
tuplewise, | ||
resultSelector, | ||
alphabet, | ||
Enumerable.Range(0, alphabet.Length - (arity - 1)).Select(i => alphabet.Skip(i).Take(arity)).ToArray() | ||
); | ||
} | ||
|
||
[Test] | ||
public void TuplewiseIsLazy() | ||
{ | ||
new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<object, object, int>()); | ||
new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<object, object, object, int>()); | ||
new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<object, object, object, object, int>()); | ||
} | ||
|
||
[Test] | ||
public void TuplewiseIntegers() | ||
{ | ||
TuplewiseNWideInt<Func<int, int, int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b ) => a + b ); | ||
TuplewiseNWideInt<Func<int, int, int, int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c ) => a + b + c ); | ||
TuplewiseNWideInt<Func<int, int, int, int, int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c, d) => a + b + c + d); | ||
} | ||
|
||
[Test] | ||
public void TuplewiseStrings() | ||
{ | ||
TuplewiseNWideString<Func<char, char, string>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b ) => string.Join(string.Empty, a, b )); | ||
TuplewiseNWideString<Func<char, char, char, string>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c ) => string.Join(string.Empty, a, b, c )); | ||
TuplewiseNWideString<Func<char, char, char, char, string>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c, d) => string.Join(string.Empty, a, b, c, d)); | ||
} | ||
} | ||
} |
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,103 @@ | ||
<#@ template debug="false" hostspecific="false" language="C#" #> | ||
<#@ output extension=".cs" #> | ||
<#@ assembly name="System.Core" #> | ||
<#@ import namespace="System.Globalization" #> | ||
<#@ import namespace="System.Linq" #> | ||
#region License and Terms | ||
// MoreLINQ - Extensions to LINQ to Objects | ||
// Copyright (c) 2019 Phillip Palk. 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; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class TuplewiseTest | ||
{ | ||
private void TuplewiseNWide<TSource, TResult, TFunc>(Func<IEnumerable<TSource>, TFunc, IEnumerable<TResult>> tuplewise, TFunc resultSelector, IEnumerable<TSource> source, params TResult[] fullResult) | ||
{ | ||
var arity = resultSelector.GetType().GetGenericArguments().Length - 1; | ||
|
||
for (var i = 0; i < fullResult.Length; ++i) | ||
using (var ts = source.Take(i).AsTestingSequence()) | ||
Assert.That(tuplewise(ts, resultSelector), Is.EqualTo(fullResult.Take(i - arity + 1))); | ||
} | ||
|
||
private void TuplewiseNWideInt<TFunc>(Func<IEnumerable<int>, TFunc, IEnumerable<int>> tuplewise, TFunc resultSelector) | ||
{ | ||
const int rangeLen = 100; | ||
var arity = resultSelector.GetType().GetGenericArguments().Length - 1; | ||
|
||
TuplewiseNWide( | ||
tuplewise, | ||
resultSelector, | ||
Enumerable.Range(1, rangeLen), | ||
Enumerable.Range(1, rangeLen - (arity - 1)).Select(x => x * arity + Enumerable.Range(1, arity - 1).Sum()).ToArray() | ||
); | ||
} | ||
|
||
private void TuplewiseNWideString<TFunc>(Func<IEnumerable<char>, TFunc, IEnumerable<string>> tuplewise, TFunc resultSelector) | ||
{ | ||
const string alphabet = "abcdefghijklmnopqrstuvwxyz"; | ||
var arity = resultSelector.GetType().GetGenericArguments().Length - 1; | ||
|
||
TuplewiseNWide( | ||
tuplewise, | ||
resultSelector, | ||
alphabet, | ||
Enumerable.Range(0, alphabet.Length - (arity - 1)).Select(i => alphabet.Skip(i).Take(arity)).ToArray() | ||
); | ||
} | ||
|
||
<# const int max = 4; | ||
const string alphabet = "abcdefghijklmnopqrstuvwxyz"; | ||
#> | ||
[Test] | ||
public void TuplewiseIsLazy() | ||
{ | ||
<# for (int i = 2; i <= max; ++i) { | ||
var funcTypeArgs = string.Join(" ", Enumerable.Repeat("object,", i).Concat(Enumerable.Repeat(" ", max - i))); | ||
#> | ||
new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<<#= funcTypeArgs #> int>()); | ||
<# } #> | ||
} | ||
|
||
[Test] | ||
public void TuplewiseIntegers() | ||
{ | ||
<# for (int i = 2; i <= max; ++i) { | ||
var funcTypeArgs = string.Join(" ", Enumerable.Repeat("int,", i).Concat(Enumerable.Repeat(" ", max - i))); | ||
var functorArgs = string.Join(", ", Enumerable.Range(1, i).Select(j => alphabet.Substring(j - 1, 1))) + new string(' ', 3 * (max - i)); | ||
var functorBody = string.Join(" + ", Enumerable.Range(1, i).Select(j => alphabet.Substring(j - 1, 1))) + new string(' ', 4 * (max - i)); | ||
#> | ||
TuplewiseNWideInt<Func<<#= funcTypeArgs #> int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (<#= functorArgs #>) => <#= functorBody #>); | ||
<# } #> | ||
} | ||
|
||
[Test] | ||
public void TuplewiseStrings() | ||
{ | ||
<# for (int i = 2; i <= max; ++i) { | ||
var funcTypeArgs = string.Join(" ", Enumerable.Repeat("char,", i).Concat(Enumerable.Repeat(" ", max - i))); | ||
var functorArgs = string.Join(", ", Enumerable.Range(1, i).Select(j => alphabet.Substring(j - 1, 1))) + new string(' ', 3 * (max - i)); | ||
#> | ||
TuplewiseNWideString<Func<<#= funcTypeArgs #> string>>((source, func) => MoreEnumerable.Tuplewise(source, func), (<#= functorArgs #>) => string.Join(string.Empty, <#= functorArgs #>)); | ||
<# } #> | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.