|
| 1 | +#region License and Terms |
| 2 | +// MoreLINQ - Extensions to LINQ to Objects |
| 3 | +// Copyright (c) 2019 Phillip Palk. All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +#endregion |
| 17 | + |
| 18 | +namespace MoreLinq.Test |
| 19 | +{ |
| 20 | + using System; |
| 21 | + using System.Collections.Generic; |
| 22 | + using NUnit.Framework; |
| 23 | + |
| 24 | + [TestFixture] |
| 25 | + public class TuplewiseTest |
| 26 | + { |
| 27 | + private void TuplewiseNWide<TSource, TResult, TFunc>(Func<IEnumerable<TSource>, TFunc, IEnumerable<TResult>> tuplewise, TFunc resultSelector, IEnumerable<TSource> source, params TResult[] fullResult) |
| 28 | + { |
| 29 | + var arity = resultSelector.GetType().GetGenericArguments().Length - 1; |
| 30 | + |
| 31 | + for (var i = 0; i < fullResult.Length; ++i) |
| 32 | + using (var ts = source.Take(i).AsTestingSequence()) |
| 33 | + Assert.That(tuplewise(ts, resultSelector), Is.EqualTo(fullResult.Take(i - arity + 1))); |
| 34 | + } |
| 35 | + |
| 36 | + private void TuplewiseNWideInt<TFunc>(Func<IEnumerable<int>, TFunc, IEnumerable<int>> tuplewise, TFunc resultSelector) |
| 37 | + { |
| 38 | + const int rangeLen = 100; |
| 39 | + var arity = resultSelector.GetType().GetGenericArguments().Length - 1; |
| 40 | + |
| 41 | + TuplewiseNWide( |
| 42 | + tuplewise, |
| 43 | + resultSelector, |
| 44 | + Enumerable.Range(1, rangeLen), |
| 45 | + Enumerable.Range(1, rangeLen - (arity - 1)).Select(x => x * arity + Enumerable.Range(1, arity - 1).Sum()).ToArray() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + private void TuplewiseNWideString<TFunc>(Func<IEnumerable<char>, TFunc, IEnumerable<string>> tuplewise, TFunc resultSelector) |
| 50 | + { |
| 51 | + const string alphabet = "abcdefghijklmnopqrstuvwxyz"; |
| 52 | + var arity = resultSelector.GetType().GetGenericArguments().Length - 1; |
| 53 | + |
| 54 | + TuplewiseNWide( |
| 55 | + tuplewise, |
| 56 | + resultSelector, |
| 57 | + alphabet, |
| 58 | + Enumerable.Range(0, alphabet.Length - (arity - 1)).Select(i => alphabet.Skip(i).Take(arity)).ToArray() |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void TuplewiseIsLazy() |
| 64 | + { |
| 65 | + new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<object, object, int>()); |
| 66 | + new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<object, object, object, int>()); |
| 67 | + new BreakingSequence<object>().Tuplewise(BreakingFunc.Of<object, object, object, object, int>()); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void TuplewiseIntegers() |
| 72 | + { |
| 73 | + TuplewiseNWideInt<Func<int, int, int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b ) => a + b ); |
| 74 | + TuplewiseNWideInt<Func<int, int, int, int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c ) => a + b + c ); |
| 75 | + TuplewiseNWideInt<Func<int, int, int, int, int>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c, d) => a + b + c + d); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void TuplewiseStrings() |
| 80 | + { |
| 81 | + TuplewiseNWideString<Func<char, char, string>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b ) => string.Join(string.Empty, a, b )); |
| 82 | + TuplewiseNWideString<Func<char, char, char, string>>((source, func) => MoreEnumerable.Tuplewise(source, func), (a, b, c ) => string.Join(string.Empty, a, b, c )); |
| 83 | + 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)); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments