ToDict, ToLookup, ToList, TOHashSet, ToObservable

master
neuecc 2020-05-10 02:39:13 +09:00
parent f37cd703a9
commit 716decd199
20 changed files with 1133 additions and 5591 deletions

View File

@ -41,7 +41,6 @@ namespace Cysharp.Threading.Tasks
public Enumerator GetAsyncEnumerator()
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(enumerable.GetAsyncEnumerator(cancellationToken));
}

View File

@ -1,775 +0,0 @@
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class AsEnumerable
{
}
}

View File

@ -0,0 +1,10 @@
namespace Cysharp.Threading.Tasks.Linq
{
public static partial class UniTaskAsyncEnumerable
{
public static IUniTaskAsyncEnumerable<TSource> AsUniTaskAsyncEnumerable<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
{
return source;
}
}
}

View File

@ -20,7 +20,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Enumerator.Instance;
}

View File

@ -20,7 +20,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(cancellationToken);
}

View File

@ -31,7 +31,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<int> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(start, end, cancellationToken);
}

View File

@ -26,7 +26,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TElement> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(element, count, cancellationToken);
}

View File

@ -22,7 +22,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(value, cancellationToken);
}

View File

@ -68,7 +68,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, selector, cancellationToken);
}
@ -112,7 +111,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, selector, cancellationToken);
}
@ -157,7 +155,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, selector, cancellationToken);
}
@ -197,7 +194,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, selector, cancellationToken);
}
@ -238,7 +234,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, selector, cancellationToken);
}
@ -278,7 +273,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, selector, cancellationToken);
}

View File

@ -9,21 +9,24 @@ namespace Cysharp.Threading.Tasks.Linq
{
public static UniTask<TSource[]> ToArrayAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
return Cysharp.Threading.Tasks.Linq.ToArray<TSource>.InvokeAsync(source, cancellationToken);
Error.ThrowArgumentNullException(source, nameof(source));
return Cysharp.Threading.Tasks.Linq.ToArray.InvokeAsync(source, cancellationToken);
}
}
internal static class ToArray<TSource>
internal static class ToArray
{
internal static async UniTask<TSource[]> InvokeAsync(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
internal static async UniTask<TSource[]> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
var pool = ArrayPool<TSource>.Shared;
var array = pool.Rent(16);
TSource[] result = default;
var e = source.GetAsyncEnumerator(cancellationToken);
IUniTaskAsyncEnumerator<TSource> e = default;
try
{
e = source.GetAsyncEnumerator(cancellationToken);
var i = 0;
while (await e.MoveNextAsync())
{

File diff suppressed because it is too large Load Diff

View File

@ -1,775 +0,0 @@
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class ToEnumerable
{
}
}

View File

@ -1,775 +1,42 @@
namespace Cysharp.Threading.Tasks.Linq
using Cysharp.Threading.Tasks.Internal;
using System.Collections.Generic;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class ToHashSet
public static partial class UniTaskAsyncEnumerable
{
public static UniTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return Cysharp.Threading.Tasks.Linq.ToHashSet.InvokeAsync(source, cancellationToken);
}
}
internal static class ToHashSet
{
internal static async UniTask<HashSet<TSource>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
var set = new HashSet<TSource>();
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
set.Add(e.Current);
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return set;
}
}
}

View File

@ -1,775 +1,42 @@
namespace Cysharp.Threading.Tasks.Linq
using Cysharp.Threading.Tasks.Internal;
using System.Collections.Generic;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class ToList
public static partial class UniTaskAsyncEnumerable
{
public static UniTask<List<TSource>> ToListAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return Cysharp.Threading.Tasks.Linq.ToList.InvokeAsync(source, cancellationToken);
}
}
internal static class ToList
{
internal static async UniTask<List<TSource>> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
var list = new List<TSource>();
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
list.Add(e.Current);
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
return list;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,775 +1,97 @@
namespace Cysharp.Threading.Tasks.Linq
using Cysharp.Threading.Tasks.Internal;
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{
internal sealed class ToObservable
public static partial class UniTaskAsyncEnumerable
{
public static IObservable<TSource> ToObservable<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
{
Error.ThrowArgumentNullException(source, nameof(source));
return new ToObservable<TSource>(source);
}
}
internal sealed class ToObservable<T> : IObservable<T>
{
readonly IUniTaskAsyncEnumerable<T> source;
public ToObservable(IUniTaskAsyncEnumerable<T> source)
{
this.source = source;
}
public IDisposable Subscribe(IObserver<T> observer)
{
var ctd = new CancellationTokenDisposable();
RunAsync(source, observer, ctd.Token).Forget();
return ctd;
}
static async UniTaskVoid RunAsync(IUniTaskAsyncEnumerable<T> src, IObserver<T> observer, CancellationToken cancellationToken)
{
// cancellationToken.IsCancellationRequested is called when Rx's Disposed.
// when disposed, finish silently.
var e = src.GetAsyncEnumerator(cancellationToken);
try
{
bool hasNext;
do
{
try
{
hasNext = await e.MoveNextAsync();
}
catch (Exception ex)
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
observer.OnError(ex);
return;
}
if (hasNext)
{
observer.OnNext(e.Current);
}
else
{
observer.OnCompleted();
return;
}
} while (!cancellationToken.IsCancellationRequested);
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
internal sealed class CancellationTokenDisposable : IDisposable
{
readonly CancellationTokenSource cts = new CancellationTokenSource();
public CancellationToken Token => cts.Token;
public void Dispose()
{
if (!cts.IsCancellationRequested)
{
cts.Cancel();
}
}
}
}
}

View File

@ -48,7 +48,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, cancellationToken);
}
@ -103,7 +102,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, cancellationToken);
}
@ -157,7 +155,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, cancellationToken);
}
@ -211,7 +208,6 @@ namespace Cysharp.Threading.Tasks.Linq
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return new Enumerator(source, cancellationToken);
}

View File

@ -28,11 +28,6 @@ namespace ___Dummy
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> Cast<TResult>(this IUniTaskAsyncEnumerable<Object> source)
{
@ -551,165 +546,11 @@ namespace ___Dummy
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IEnumerable<TSource> source)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this Task<TSource> task)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IEnumerable<TSource> ToEnumerable<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
{
throw new NotImplementedException();
}
public static UniTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<List<TSource>> ToListAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TElement>> ToLookupAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TSource>> ToLookupAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TSource>> ToLookupAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<ILookup<TKey, TElement>> ToLookupAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IObservable<TSource> ToObservable<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> Union<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
{

View File

@ -31,15 +31,24 @@ namespace NetCoreSandbox
}
}
static async Task Main(string[] args)
static async IAsyncEnumerable<int> FooAsync([EnumeratorCancellation]CancellationToken cancellationToken = default)
{
new int[] { }.Aggregate((x, y) => x + y);
yield return 1;
await Task.Delay(10, cancellationToken);
}
static void Main(string[] args)
{
// Create Canceled token.
var cts = new CancellationTokenSource();
cts.Cancel();
// OK, don't throw.
var e1 = FooAsync(cts.Token).GetAsyncEnumerator(cts.Token);
Console.WriteLine("OK:FooAsyunc().GetAsyncEnumerator()");
await Task.Yield();
// Ix.Async LINQ Operator throws OperationCanceledException
var e2 = FooAsync(cts.Token).Select(x => x).GetAsyncEnumerator(cts.Token);
}

View File

@ -28,6 +28,21 @@ namespace NetCoreTests.Linq
}
}
[Fact]
public async Task ToObservable()
{
{
var xs = await UniTaskAsyncEnumerable.Range(1, 10).ToObservable().ToArray();
xs.Should().BeEquivalentTo(Enumerable.Range(1, 10));
}
{
var xs = await UniTaskAsyncEnumerable.Range(1, 0).ToObservable().ToArray();
xs.Should().BeEquivalentTo(Enumerable.Range(1, 0));
}
}
[Fact]
public async Task ToAsyncEnumerableTask()
{
@ -73,6 +88,108 @@ namespace NetCoreTests.Linq
}
}
[Fact]
public async Task ToDictionary()
{
{
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x);
var ys = Enumerable.Range(1, 100).ToDictionary(x => x);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
}
{
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x);
var ys = Enumerable.Range(1, 0).ToDictionary(x => x);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
}
{
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x, x => x * 2);
var ys = Enumerable.Range(1, 100).ToDictionary(x => x, x => x * 2);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
}
{
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToDictionaryAsync(x => x, x => x * 2);
var ys = Enumerable.Range(1, 0).ToDictionary(x => x, x => x * 2);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
}
}
[Fact]
public async Task ToLookup()
{
var arr = new[] { 1, 4, 10, 10, 4, 5, 10, 9 };
{
var xs = await arr.ToUniTaskAsyncEnumerable().ToLookupAsync(x => x);
var ys = arr.ToLookup(x => x);
xs.Count.Should().Be(ys.Count);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
foreach (var key in xs.Select(x => x.Key))
{
xs[key].Should().BeEquivalentTo(ys[key]);
}
}
{
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToLookupAsync(x => x);
var ys = Enumerable.Range(1, 0).ToLookup(x => x);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
}
{
var xs = await arr.ToUniTaskAsyncEnumerable().ToLookupAsync(x => x, x => x * 2);
var ys = arr.ToLookup(x => x, x => x * 2);
xs.Count.Should().Be(ys.Count);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
foreach (var key in xs.Select(x => x.Key))
{
xs[key].Should().BeEquivalentTo(ys[key]);
}
}
{
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().ToLookupAsync(x => x, x => x * 2);
var ys = Enumerable.Range(1, 0).ToLookup(x => x, x => x * 2);
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
}
}
[Fact]
public async Task ToList()
{
{
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToListAsync();
var ys = Enumerable.Range(1, 100).ToList();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToListAsync();
var ys = Enumerable.Empty<int>().ToList();
xs.Should().BeEquivalentTo(ys);
}
}
[Fact]
public async Task ToHashSet()
{
{
var xs = await new[] { 1, 20, 4, 5, 20, 4, 6 }.ToUniTaskAsyncEnumerable().ToHashSetAsync();
var ys = new[] { 1, 20, 4, 5, 20, 4, 6 }.ToHashSet();
xs.OrderBy(x => x).Should().BeEquivalentTo(ys.OrderBy(x => x));
}
{
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToHashSetAsync();
var ys = Enumerable.Empty<int>().ToHashSet();
xs.Should().BeEquivalentTo(ys);
}
}
}