mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-16 20:20:45 +00:00
Compare commits
41 Commits
2.0.2-prev
...
2.0.3-prev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a7a6fde5c | ||
|
|
6a5e259006 | ||
|
|
f6622ad29c | ||
|
|
3de29a181d | ||
|
|
090cacece5 | ||
|
|
354fd65d58 | ||
|
|
d3538bdc8f | ||
|
|
bd6906792d | ||
|
|
7fc6c6bd36 | ||
|
|
c23b9ca480 | ||
|
|
cda59ba9c2 | ||
|
|
61a3744fdd | ||
|
|
72efadd0a2 | ||
|
|
57c414a6e0 | ||
|
|
85dc70a3ab | ||
|
|
7298686d5a | ||
|
|
418586fbfb | ||
|
|
12c507574e | ||
|
|
b20b37e7a5 | ||
|
|
8ef7a66081 | ||
|
|
a5f47d4095 | ||
|
|
1316328766 | ||
|
|
c0da316cb4 | ||
|
|
4d13523df7 | ||
|
|
16c527fa89 | ||
|
|
5db5beab29 | ||
|
|
3f082f1923 | ||
|
|
93dd82e3d4 | ||
|
|
af6dbd8868 | ||
|
|
716decd199 | ||
|
|
f37cd703a9 | ||
|
|
31b788a2c9 | ||
|
|
e93bcbf564 | ||
|
|
aa8cb80866 | ||
|
|
dd6a8da96f | ||
|
|
d4511c0f67 | ||
|
|
c16433e0fe | ||
|
|
ed0990e402 | ||
|
|
856a049dd0 | ||
|
|
61b798b6e9 | ||
|
|
be45066773 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -156,4 +156,4 @@ src/UniTask/UniTask.Tests.csproj
|
|||||||
|
|
||||||
src/UniTask/UniTask.Tests.Editor.csproj
|
src/UniTask/UniTask.Tests.Editor.csproj
|
||||||
|
|
||||||
src/UniTask/UniTask.unitypackage
|
src/UniTask/UniTask.*.unitypackage
|
||||||
|
|||||||
123
src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs
Normal file
123
src/UniTask.NetCore/NetCore/UniTask.AsValueTask.cs
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
#pragma warning disable 0649
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading.Tasks.Sources;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
public static class UniTaskValueTaskExtensions
|
||||||
|
{
|
||||||
|
public static ValueTask AsValueTask(this UniTask task)
|
||||||
|
{
|
||||||
|
ref var core = ref Unsafe.As<UniTask, UniTaskToValueTask>(ref task);
|
||||||
|
if (core.source == null)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ValueTask(new UniTaskValueTaskSource(core.source), core.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ValueTask<T> AsValueTask<T>(this UniTask<T> task)
|
||||||
|
{
|
||||||
|
ref var core = ref Unsafe.As<UniTask<T>, UniTaskToValueTask<T>>(ref task);
|
||||||
|
if (core.source == null)
|
||||||
|
{
|
||||||
|
return new ValueTask<T>(core.result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ValueTask<T>(new UniTaskValueTaskSource<T>(core.source), core.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct UniTaskToValueTask
|
||||||
|
{
|
||||||
|
public IUniTaskSource source;
|
||||||
|
public short token;
|
||||||
|
}
|
||||||
|
|
||||||
|
class UniTaskValueTaskSource : IValueTaskSource
|
||||||
|
{
|
||||||
|
readonly IUniTaskSource source;
|
||||||
|
|
||||||
|
public UniTaskValueTaskSource(IUniTaskSource source)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult(short token)
|
||||||
|
{
|
||||||
|
source.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTaskSourceStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
var status = source.GetStatus(token);
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
return ValueTaskSourceStatus.Pending;
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
return ValueTaskSourceStatus.Succeeded;
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
return ValueTaskSourceStatus.Faulted;
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
return ValueTaskSourceStatus.Canceled;
|
||||||
|
default:
|
||||||
|
return (ValueTaskSourceStatus)status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
||||||
|
{
|
||||||
|
source.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct UniTaskToValueTask<T>
|
||||||
|
{
|
||||||
|
public IUniTaskSource<T> source;
|
||||||
|
public T result;
|
||||||
|
public short token;
|
||||||
|
}
|
||||||
|
|
||||||
|
class UniTaskValueTaskSource<T> : IValueTaskSource<T>
|
||||||
|
{
|
||||||
|
readonly IUniTaskSource<T> source;
|
||||||
|
|
||||||
|
public UniTaskValueTaskSource(IUniTaskSource<T> source)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T GetResult(short token)
|
||||||
|
{
|
||||||
|
return source.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTaskSourceStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
var status = source.GetStatus(token);
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
return ValueTaskSourceStatus.Pending;
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
return ValueTaskSourceStatus.Succeeded;
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
return ValueTaskSourceStatus.Faulted;
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
return ValueTaskSourceStatus.Canceled;
|
||||||
|
default:
|
||||||
|
return (ValueTaskSourceStatus)status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
||||||
|
{
|
||||||
|
source.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
112
src/UniTask.NetCore/NetCore/UniTask.Run.cs
Normal file
112
src/UniTask.NetCore/NetCore/UniTask.Run.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
public partial struct UniTask
|
||||||
|
{
|
||||||
|
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||||
|
public static async UniTask Run(Action action, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
var current = SynchronizationContext.Current;
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
action();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (current != null)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToSynchronizationContext(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
action();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||||
|
public static async UniTask Run(Action<object> action, object state, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
var current = SynchronizationContext.Current;
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
action(state);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (current != null)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToSynchronizationContext(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
action(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||||
|
public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
var current = SynchronizationContext.Current;
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return func();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (current != null)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToSynchronizationContext(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
return func();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Run action on the threadPool and return to current SynchronizationContext if configureAwait = true.</summary>
|
||||||
|
public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true)
|
||||||
|
{
|
||||||
|
if (configureAwait)
|
||||||
|
{
|
||||||
|
var current = SynchronizationContext.Current;
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return func(state);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (current != null)
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToSynchronizationContext(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
return func(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
55
src/UniTask.NetCore/NetCore/UniTask.Yield.cs
Normal file
55
src/UniTask.NetCore/NetCore/UniTask.Yield.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
public partial struct UniTask
|
||||||
|
{
|
||||||
|
public static UniTask.YieldAwaitable Yield()
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct YieldAwaitable
|
||||||
|
{
|
||||||
|
public Awaiter GetAwaiter()
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct Awaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
static readonly SendOrPostCallback SendOrPostCallbackDelegate = Continuation;
|
||||||
|
static readonly WaitCallback WaitCallbackDelegate = Continuation;
|
||||||
|
|
||||||
|
public bool IsCompleted => false;
|
||||||
|
|
||||||
|
public void GetResult() { }
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
UnsafeOnCompleted(continuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
var syncContext = SynchronizationContext.Current;
|
||||||
|
if (syncContext != null)
|
||||||
|
{
|
||||||
|
syncContext.Post(SendOrPostCallbackDelegate, continuation);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(WaitCallbackDelegate, continuation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Continuation(object state)
|
||||||
|
{
|
||||||
|
((Action)state).Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,25 +7,31 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\UniTask\Assets\Plugins\UniTask\**\*.cs"
|
<Compile Include="..\UniTask\Assets\Plugins\UniTask\Runtime\**\*.cs"
|
||||||
Exclude="..\UniTask\Assets\Plugins\UniTask\Triggers\*.cs;
|
Exclude="
|
||||||
..\UniTask\Assets\Plugins\UniTask\Editor\*.cs;
|
..\UniTask\Assets\Plugins\UniTask\Editor\*.cs;
|
||||||
|
..\UniTask\Assets\Plugins\UniTask\Runtime\Triggers\*.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\Internal\UnityEqualityComparer.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\Linq\UnityExtensions\*.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\Internal\DiagnosticsExtensions.cs;
|
|
||||||
..\UniTask\Assets\Plugins\UniTask\Internal\PlayerLoopRunner.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\Internal\UnityEqualityComparer.cs;
|
||||||
|
..\UniTask\Assets\Plugins\UniTask\Runtime\Internal\DiagnosticsExtensions.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\CancellationTokenSourceExtensions.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\Internal\PlayerLoopRunner.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\EnumeratorAsyncExtensions.cs;
|
|
||||||
..\UniTask\Assets\Plugins\UniTask\PlayerLoopHelper.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\CancellationTokenSourceExtensions.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UniTask.Delay.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\EnumeratorAsyncExtensions.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UniTask.Run.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\PlayerLoopHelper.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UniTask.Bridge.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UniTask.Delay.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UniTask.WaitUntil.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UniTask.Run.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UniTask.Bridge.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.uGUI.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UniTask.WaitUntil.cs;
|
||||||
..\UniTask\Assets\Plugins\UniTask\UnityAsyncExtensions.MonoBehaviour.cs;
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UnityAsyncExtensions.cs;
|
||||||
"/>
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UnityAsyncExtensions.uGUI.cs;
|
||||||
|
..\UniTask\Assets\Plugins\UniTask\Runtime\UnityAsyncExtensions.MonoBehaviour.cs;
|
||||||
|
" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,46 +1,243 @@
|
|||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
|
|
||||||
|
using System.Linq;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
|
||||||
namespace NetCoreSandbox
|
namespace NetCoreSandbox
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
static string FlattenGenArgs(Type type)
|
||||||
|
{
|
||||||
|
if (type.IsGenericType)
|
||||||
|
{
|
||||||
|
var t = string.Join(", ", type.GetGenericArguments().Select(x => FlattenGenArgs(x)));
|
||||||
|
return Regex.Replace(type.Name, "`.+", "") + "<" + t + ">";
|
||||||
|
}
|
||||||
|
//x.ReturnType.GetGenericArguments()
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return type.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async IAsyncEnumerable<int> FooAsync([EnumeratorCancellation]CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
yield return 1;
|
||||||
|
await Task.Delay(10, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Foo");
|
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
|
||||||
var v = await outer().AsTask();
|
.SelectAwait(x => UniTask.Run(() => x))
|
||||||
|
.TakeLast(6)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
|
{
|
||||||
|
|
||||||
|
Console.WriteLine(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// AsyncEnumerable.Range(1,10).FirstAsync(
|
||||||
|
// AsyncEnumerable.Range(1, 10).GroupBy(x=>x).Select(x=>x.first
|
||||||
|
|
||||||
|
|
||||||
|
// AsyncEnumerable.Range(1,10).WithCancellation(CancellationToken.None).WithCancellation
|
||||||
|
|
||||||
|
|
||||||
|
//Enumerable.Range(1,10).ToHashSet(
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("Bar:" + v);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async UniTask<int> outer()
|
|
||||||
|
|
||||||
|
void Foo()
|
||||||
{
|
{
|
||||||
//await Task.WhenAll();
|
|
||||||
|
|
||||||
//var foo = await Task.WhenAny(Array.Empty<Task<int>>());
|
// AsyncEnumerable.Range(1,10).Do(
|
||||||
|
|
||||||
|
// AsyncEnumerable.t
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.AppendLine(@"using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var chako = typeof(AsyncEnumerable).GetMethods()
|
||||||
|
.OrderBy(x => x.Name)
|
||||||
|
.Select(x =>
|
||||||
|
{
|
||||||
|
var ret = FlattenGenArgs(x.ReturnType);
|
||||||
|
|
||||||
|
|
||||||
|
var generics = string.Join(", ", x.GetGenericArguments().Select(x => x.Name));
|
||||||
|
|
||||||
|
if (x.GetParameters().Length == 0) return "";
|
||||||
|
|
||||||
|
var self = x.GetParameters().First();
|
||||||
|
if (x.GetCustomAttributes(typeof(ExtensionAttribute), true).Length == 0)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
var arg1Type = FlattenGenArgs(x.GetParameters().First().ParameterType);
|
||||||
|
|
||||||
|
var others = string.Join(", ", x.GetParameters().Skip(1).Select(y => FlattenGenArgs(y.ParameterType) + " " + y.Name));
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(others))
|
||||||
|
{
|
||||||
|
others = ", " + others;
|
||||||
|
}
|
||||||
|
|
||||||
|
var template = $"public static {ret} {x.Name}<{generics}>(this {arg1Type} {self.Name}{others})";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return template.Replace("ValueTask", "UniTask").Replace("IAsyncEnumerable", "IUniTaskAsyncEnumerable").Replace("<>", "");
|
||||||
|
})
|
||||||
|
.Where(x => x != "")
|
||||||
|
.Select(x => x + "\r\n{\r\n throw new NotImplementedException();\r\n}")
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
var huga = string.Join("\r\n\r\n", chako);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var item in typeof(AsyncEnumerable).GetMethods().Select(x => x.Name).Distinct())
|
||||||
|
{
|
||||||
|
if (item.EndsWith("AwaitAsync") || item.EndsWith("AwaitWithCancellationAsync") || item.EndsWith("WithCancellation"))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var item2 = item.Replace("Async", "");
|
||||||
|
item2 = item2.Replace("Await", "");
|
||||||
|
|
||||||
|
var format = @"
|
||||||
|
internal sealed class {0}
|
||||||
|
{{
|
||||||
|
}}
|
||||||
|
";
|
||||||
|
|
||||||
|
sb.Append(string.Format(format, item2));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.Append("}");
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine(sb.ToString());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await UniTask.WhenAny(new UniTask[0]);
|
|
||||||
|
|
||||||
return 10;
|
|
||||||
//var v = await DoAsync();
|
|
||||||
//return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static async UniTask<int> DoAsync()
|
public static async IAsyncEnumerable<int> AsyncGen()
|
||||||
{
|
{
|
||||||
var tcs = new UniTaskCompletionSource<int>();
|
await UniTask.SwitchToThreadPool();
|
||||||
|
yield return 10;
|
||||||
tcs.TrySetResult(100);
|
await UniTask.SwitchToThreadPool();
|
||||||
|
yield return 100;
|
||||||
|
|
||||||
var v = await tcs.Task;
|
|
||||||
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyEnumerable : IEnumerable<int>
|
||||||
|
{
|
||||||
|
public IEnumerator<int> GetEnumerator()
|
||||||
|
{
|
||||||
|
return new MyEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyEnumerator : IEnumerator<int>
|
||||||
|
{
|
||||||
|
public int Current => throw new NotImplementedException();
|
||||||
|
|
||||||
|
object IEnumerator.Current => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Called Dispose");
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class MyClass<T>
|
||||||
|
{
|
||||||
|
public CustomAsyncEnumerator<T> GetAsyncEnumerator()
|
||||||
|
{
|
||||||
|
//IAsyncEnumerable
|
||||||
|
return new CustomAsyncEnumerator<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public struct CustomAsyncEnumerator<T>
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
|
||||||
|
public T Current
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
if (count++ == 3)
|
||||||
|
{
|
||||||
|
return UniTask.FromResult(false);
|
||||||
|
//return false;
|
||||||
|
}
|
||||||
|
return UniTask.FromResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,11 @@
|
|||||||
<RootNamespace>NetCoreSandbox</RootNamespace>
|
<RootNamespace>NetCoreSandbox</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
|
||||||
|
<PackageReference Include="System.Reactive" Version="4.4.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\UniTask.NetCore\UniTask.NetCore.csproj" />
|
<ProjectReference Include="..\UniTask.NetCore\UniTask.NetCore.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
496
src/UniTask.NetCoreTests/Linq/Aggregate.cs
Normal file
496
src/UniTask.NetCoreTests/Linq/Aggregate.cs
Normal file
@@ -0,0 +1,496 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Aggregate
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(10, 0)]
|
||||||
|
[InlineData(1, 11)]
|
||||||
|
public async Task Sum(int start, int count)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).Sum();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync(x => x * 2);
|
||||||
|
var ys = Enumerable.Range(start, count).Sum(x => x * 2);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitAsync(x => UniTask.Run(() => x));
|
||||||
|
var ys = Enumerable.Range(start, count).Sum(x => x);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitCancellationAsync((x, _) => UniTask.Run(() => x));
|
||||||
|
var ys = Enumerable.Range(start, count).Sum(x => x);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> array1 = new object[][]
|
||||||
|
{
|
||||||
|
new object[]{new int[] { 1, 10, 100 } },
|
||||||
|
new object[]{new int?[] { 1, null, 100 } },
|
||||||
|
new object[]{new float[] { 1, 10, 100 } },
|
||||||
|
new object[]{new float?[] { 1, null, 100 } },
|
||||||
|
new object[]{new double[] { 1, 10, 100 } },
|
||||||
|
new object[]{new double?[] { 1, null, 100 } },
|
||||||
|
new object[]{new decimal[] { 1, 10, 100 } },
|
||||||
|
new object[]{new decimal?[] { 1, null, 100 } },
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(array1))]
|
||||||
|
public async Task Average<T>(T arr)
|
||||||
|
{
|
||||||
|
switch (arr)
|
||||||
|
{
|
||||||
|
case int[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case int?[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case float[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case float?[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case double[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case double?[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case decimal[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case decimal?[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().AverageAsync();
|
||||||
|
var ys = array.Average();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> array2 = new object[][]
|
||||||
|
{
|
||||||
|
new object[]{new int[] { } },
|
||||||
|
new object[]{new int[] { 5 } },
|
||||||
|
new object[]{new int[] { 5, 10, 100 } },
|
||||||
|
new object[]{new int[] { 10, 5,100 } },
|
||||||
|
new object[]{new int[] { 100, 10, 5 } },
|
||||||
|
|
||||||
|
new object[]{new int?[] { } },
|
||||||
|
new object[]{new int?[] { 5 } },
|
||||||
|
new object[]{new int?[] { null, null, null } },
|
||||||
|
new object[]{new int?[] { null, 5, 10, 100 } },
|
||||||
|
new object[]{new int?[] { 10, 5,100, null } },
|
||||||
|
new object[]{new int?[] { 100, 10, 5 } },
|
||||||
|
|
||||||
|
new object[]{new X[] { } },
|
||||||
|
new object[]{new X[] { new X(5) } },
|
||||||
|
new object[]{new X[] { new X(5), new X(10), new X(100) } },
|
||||||
|
new object[]{new X[] { new X(10),new X( 5),new X(100) } },
|
||||||
|
new object[]{new X[] { new X(100), new X(10),new X(5) } },
|
||||||
|
|
||||||
|
new object[]{new XX[] { } },
|
||||||
|
new object[]{new XX[] { new XX(new X(5)) } },
|
||||||
|
new object[]{new XX[] { new XX(new X(5)), new XX(new X(10)), new XX(new X(100)) } },
|
||||||
|
new object[]{new XX[] { new XX(new X(10)),new XX(new X( 5)),new XX(new X(100)) } },
|
||||||
|
new object[]{new XX[] { new XX(new X(100)), new XX(new X(10)),new XX(new X(5)) } },
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(array2))]
|
||||||
|
public async Task Min<T>(T arr)
|
||||||
|
{
|
||||||
|
switch (arr)
|
||||||
|
{
|
||||||
|
case int[] array:
|
||||||
|
{
|
||||||
|
{
|
||||||
|
if (array.Length == 0)
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MinAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Min());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync();
|
||||||
|
var ys = array.Min();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
if (array.Length == 0)
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MinAsync(x => x * 2));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Min(x => x * 2));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x * 2);
|
||||||
|
var ys = array.Min(x => x * 2);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case int?[] array:
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync();
|
||||||
|
var ys = array.Min();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x);
|
||||||
|
var ys = array.Min(x => x);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case X[] array:
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync();
|
||||||
|
var ys = array.Min();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
|
||||||
|
if (array.Length == 0)
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MinAsync(x => x.Value));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Min(x => x.Value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x.Value);
|
||||||
|
var ys = array.Min(x => x.Value);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XX[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MinAsync(x => x.Value);
|
||||||
|
var ys = array.Min(x => x.Value);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(array2))]
|
||||||
|
public async Task Max<T>(T arr)
|
||||||
|
{
|
||||||
|
switch (arr)
|
||||||
|
{
|
||||||
|
case int[] array:
|
||||||
|
{
|
||||||
|
{
|
||||||
|
if (array.Length == 0)
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MaxAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Max());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync();
|
||||||
|
var ys = array.Max();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
if (array.Length == 0)
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x * 2));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Max(x => x * 2));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x * 2);
|
||||||
|
var ys = array.Max(x => x * 2);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case int?[] array:
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync();
|
||||||
|
var ys = array.Max();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x);
|
||||||
|
var ys = array.Max(x => x);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case X[] array:
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync();
|
||||||
|
var ys = array.Max();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
|
||||||
|
if (array.Length == 0)
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x.Value));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Max(x => x.Value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x.Value);
|
||||||
|
var ys = array.Max(x => x.Value);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XX[] array:
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().MaxAsync(x => x.Value);
|
||||||
|
var ys = array.Max(x => x.Value);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class XX
|
||||||
|
{
|
||||||
|
public readonly X Value;
|
||||||
|
|
||||||
|
public XX(X value)
|
||||||
|
{
|
||||||
|
this.Value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class X : IComparable<X>
|
||||||
|
{
|
||||||
|
public readonly int Value;
|
||||||
|
|
||||||
|
public X(int value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareTo([AllowNull] X other)
|
||||||
|
{
|
||||||
|
return Comparer<int>.Default.Compare(Value, other.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(10, 0)]
|
||||||
|
[InlineData(1, 11)]
|
||||||
|
public async Task Count(int start, int count)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).Count();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync(x => x % 2 == 0);
|
||||||
|
var ys = Enumerable.Range(start, count).Count(x => x % 2 == 0);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).LongCount();
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync(x => x % 2 == 0);
|
||||||
|
var ys = Enumerable.Range(start, count).LongCount(x => x % 2 == 0);
|
||||||
|
xs.Should().Be(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AggregateTest1()
|
||||||
|
{
|
||||||
|
// 0
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await new int[] { }.ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => new int[] { }.Aggregate((x, y) => x + y));
|
||||||
|
|
||||||
|
// 1
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 1).Aggregate((x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 2).ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 2).Aggregate((x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().AggregateAsync((x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 10).Aggregate((x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AggregateTest2()
|
||||||
|
{
|
||||||
|
// 0
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 2).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 2).Aggregate(1000, (x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y);
|
||||||
|
var b = Enumerable.Range(1, 10).Aggregate(1000, (x, y) => x + y);
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AggregateTest3()
|
||||||
|
{
|
||||||
|
// 0
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
var b = Enumerable.Range(1, 1).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 2).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
var b = Enumerable.Range(1, 2).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10
|
||||||
|
{
|
||||||
|
var a = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().AggregateAsync(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
var b = Enumerable.Range(1, 10).Aggregate(1000, (x, y) => x + y, x => (x * 99).ToString());
|
||||||
|
a.Should().Be(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ForEach()
|
||||||
|
{
|
||||||
|
var list = new List<int>();
|
||||||
|
await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().ForEachAsync(x =>
|
||||||
|
{
|
||||||
|
list.Add(x);
|
||||||
|
});
|
||||||
|
|
||||||
|
list.Should().BeEquivalentTo(Enumerable.Range(1, 10));
|
||||||
|
|
||||||
|
var list2 = new List<(int, int)>();
|
||||||
|
await Enumerable.Range(5, 10).ToUniTaskAsyncEnumerable().ForEachAsync((index, x) =>
|
||||||
|
{
|
||||||
|
list2.Add((index, x));
|
||||||
|
});
|
||||||
|
|
||||||
|
var list3 = Enumerable.Range(5, 10).Select((index, x) => (index, x)).ToArray();
|
||||||
|
list2.Should().BeEquivalentTo(list3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
112
src/UniTask.NetCoreTests/Linq/AllAny.cs
Normal file
112
src/UniTask.NetCoreTests/Linq/AllAny.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class AllAny
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(1, 1)]
|
||||||
|
[InlineData(1, 2)]
|
||||||
|
[InlineData(1, 3)]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
[InlineData(0, 11)]
|
||||||
|
public async Task AllTest(int start, int count)
|
||||||
|
{
|
||||||
|
var range = Enumerable.Range(start, count);
|
||||||
|
var x = await range.ToUniTaskAsyncEnumerable().AllAsync(x => x % 2 == 0);
|
||||||
|
var y = range.All(x => x % 2 == 0);
|
||||||
|
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(1, 1)]
|
||||||
|
[InlineData(1, 2)]
|
||||||
|
[InlineData(1, 3)]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
[InlineData(0, 11)]
|
||||||
|
public async Task AnyTest(int start, int count)
|
||||||
|
{
|
||||||
|
var range = Enumerable.Range(start, count);
|
||||||
|
{
|
||||||
|
var x = await range.ToUniTaskAsyncEnumerable().AnyAsync();
|
||||||
|
var y = range.Any();
|
||||||
|
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await range.ToUniTaskAsyncEnumerable().AnyAsync(x => x % 2 == 0);
|
||||||
|
var y = range.Any(x => x % 2 == 0);
|
||||||
|
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(1, 1)]
|
||||||
|
[InlineData(1, 2)]
|
||||||
|
[InlineData(1, 3)]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
[InlineData(0, 11)]
|
||||||
|
public async Task ContainsTest(int start, int count)
|
||||||
|
{
|
||||||
|
var range = Enumerable.Range(start, count);
|
||||||
|
foreach (var c in Enumerable.Range(0, 15))
|
||||||
|
{
|
||||||
|
var x = await range.ToUniTaskAsyncEnumerable().ContainsAsync(c);
|
||||||
|
var y = range.Contains(c);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SequenceEqual()
|
||||||
|
{
|
||||||
|
// empty and empty
|
||||||
|
(await new int[0].ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[0].ToUniTaskAsyncEnumerable())).Should().BeTrue();
|
||||||
|
(new int[0].SequenceEqual(new int[0])).Should().BeTrue();
|
||||||
|
|
||||||
|
// empty and exists
|
||||||
|
(await new int[0].ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
(new int[0].SequenceEqual(new int[] { 1 })).Should().BeFalse();
|
||||||
|
|
||||||
|
// exists and empty
|
||||||
|
(await new int[] { 1 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[0].ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
(new int[] { 1 }.SequenceEqual(new int[] { })).Should().BeFalse();
|
||||||
|
|
||||||
|
// samelength same value
|
||||||
|
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable())).Should().BeTrue();
|
||||||
|
(new int[] { 1, 2, 3 }.SequenceEqual(new int[] { 1, 2, 3 })).Should().BeTrue();
|
||||||
|
|
||||||
|
// samelength different value(first)
|
||||||
|
(await new int[] { 5, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
|
||||||
|
// samelength different value(middle)
|
||||||
|
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 5, 3 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
|
||||||
|
// samelength different value(last)
|
||||||
|
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 5 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
|
||||||
|
// left is long
|
||||||
|
(await new int[] { 1, 2, 3, 4 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
(new int[] { 1, 2, 3, 4 }.SequenceEqual(new int[] { 1, 2, 3 })).Should().BeFalse();
|
||||||
|
|
||||||
|
// right is long
|
||||||
|
(await new int[] { 1, 2, 3 }.ToUniTaskAsyncEnumerable().SequenceEqualAsync(new int[] { 1, 2, 3, 4 }.ToUniTaskAsyncEnumerable())).Should().BeFalse();
|
||||||
|
(new int[] { 1, 2, 3 }.SequenceEqual(new int[] { 1, 2, 3, 4 })).Should().BeFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
144
src/UniTask.NetCoreTests/Linq/Concat.cs
Normal file
144
src/UniTask.NetCoreTests/Linq/Concat.cs
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Concat
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(0, 2)]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
public async Task Append(int start, int count)
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Append(99).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).Append(99).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AppendThrow()
|
||||||
|
{
|
||||||
|
var xs = UniTaskTestException.ThrowImmediate().Append(99).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
|
||||||
|
var ys = UniTaskTestException.ThrowAfter().Append(99).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
|
||||||
|
var zs = UniTaskTestException.ThrowInMoveNext().Append(99).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(0, 2)]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
public async Task Prepend(int start, int count)
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Prepend(99).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).Prepend(99).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PrependThrow()
|
||||||
|
{
|
||||||
|
var xs = UniTaskTestException.ThrowImmediate().Prepend(99).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
|
||||||
|
var ys = UniTaskTestException.ThrowAfter().Prepend(99).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
|
||||||
|
var zs = UniTaskTestException.ThrowInMoveNext().Prepend(99).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> array1 = new object[][]
|
||||||
|
{
|
||||||
|
new object[] { (0, 0), (0, 0) }, // empty + empty
|
||||||
|
new object[] { (0, 1), (0, 0) }, // 1 + empty
|
||||||
|
new object[] { (0, 0), (0, 1) }, // empty + 1
|
||||||
|
new object[] { (0, 5), (0, 0) }, // 5 + empty
|
||||||
|
new object[] { (0, 0), (0, 5) }, // empty + 5
|
||||||
|
new object[] { (0, 5), (0, 5) }, // 5 + 5
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(array1))]
|
||||||
|
public async Task ConcatTest((int, int) left, (int, int) right)
|
||||||
|
{
|
||||||
|
var l = Enumerable.Range(left.Item1, left.Item2);
|
||||||
|
var r = Enumerable.Range(right.Item1, right.Item2);
|
||||||
|
|
||||||
|
var xs = await l.ToUniTaskAsyncEnumerable().Concat(r.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||||
|
var ys = l.Concat(r).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConcatThrow()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = UniTaskTestException.ThrowImmediate().Concat(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
|
||||||
|
var ys = UniTaskTestException.ThrowAfter().Concat(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
|
||||||
|
var zs = UniTaskTestException.ThrowInMoveNext().Concat(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskTestException.ThrowImmediate()).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
|
||||||
|
var ys = UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskTestException.ThrowAfter()).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
|
||||||
|
var zs = UniTaskAsyncEnumerable.Range(1, 10).Concat(UniTaskTestException.ThrowInMoveNext()).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DefaultIfEmpty()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, 0).DefaultIfEmpty(99).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, 1).DefaultIfEmpty(99).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, 10).DefaultIfEmpty(99).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
// Throw
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.DefaultIfEmpty().ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
196
src/UniTask.NetCoreTests/Linq/Convert.cs
Normal file
196
src/UniTask.NetCoreTests/Linq/Convert.cs
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Convert
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ToAsyncEnumerable()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(1, 100).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(100);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[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()
|
||||||
|
{
|
||||||
|
var t = Task.FromResult(100);
|
||||||
|
var xs = await t.ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(1);
|
||||||
|
xs[0].Should().Be(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ToAsyncEnumerableUniTask()
|
||||||
|
{
|
||||||
|
var t = UniTask.FromResult(100);
|
||||||
|
var xs = await t.ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(1);
|
||||||
|
xs[0].Should().Be(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ToAsyncEnumerableObservable()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await Observable.Range(1, 100).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
var ys = await Observable.Range(1, 100).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await Observable.Range(1, 100, ThreadPoolScheduler.Instance).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
var ys = await Observable.Range(1, 100, ThreadPoolScheduler.Instance).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await Observable.Empty<int>(ThreadPoolScheduler.Instance).ToUniTaskAsyncEnumerable().ToArrayAsync();
|
||||||
|
var ys = await Observable.Empty<int>(ThreadPoolScheduler.Instance).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
66
src/UniTask.NetCoreTests/Linq/Factory.cs
Normal file
66
src/UniTask.NetCoreTests/Linq/Factory.cs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Factory
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(1, 5)]
|
||||||
|
[InlineData(1, 0)]
|
||||||
|
[InlineData(0, 11)]
|
||||||
|
[InlineData(1, 11)]
|
||||||
|
public async Task RangeTest(int start, int count)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(start, count).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("foo", 0)]
|
||||||
|
[InlineData("bar", 1)]
|
||||||
|
[InlineData("baz", 3)]
|
||||||
|
[InlineData("foobar", 10)]
|
||||||
|
[InlineData("foobarbaz", 11)]
|
||||||
|
public async Task RepeatTest(string value, int count)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Repeat(value, count).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Repeat(value, count).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EmptyTest()
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Empty<int>().ToArrayAsync();
|
||||||
|
var ys = Enumerable.Empty<int>().ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(100)]
|
||||||
|
[InlineData((string)null)]
|
||||||
|
[InlineData("foo")]
|
||||||
|
public async Task ReturnTest<T>(T value)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Return(value).ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(1);
|
||||||
|
xs[0].Should().Be(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
118
src/UniTask.NetCoreTests/Linq/Filtering.cs
Normal file
118
src/UniTask.NetCoreTests/Linq/Filtering.cs
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Filtering
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Where()
|
||||||
|
{
|
||||||
|
var range = Enumerable.Range(1, 10);
|
||||||
|
var src = range.ToUniTaskAsyncEnumerable();
|
||||||
|
|
||||||
|
{
|
||||||
|
var a = await src.Where(x => x % 2 == 0).ToArrayAsync();
|
||||||
|
var expected = range.Where(x => x % 2 == 0).ToArray();
|
||||||
|
a.Should().BeEquivalentTo(expected);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a = await src.Where((x, i) => (x + i) % 2 == 0).ToArrayAsync();
|
||||||
|
var expected = range.Where((x, i) => (x + i) % 2 == 0).ToArray();
|
||||||
|
a.Should().BeEquivalentTo(expected);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a = await src.WhereAwait(x => UniTask.Run(() => x % 2 == 0)).ToArrayAsync();
|
||||||
|
var b = await src.WhereAwait(x => UniTask.FromResult(x % 2 == 0)).ToArrayAsync();
|
||||||
|
var expected = range.Where(x => x % 2 == 0).ToArray();
|
||||||
|
a.Should().BeEquivalentTo(expected);
|
||||||
|
b.Should().BeEquivalentTo(expected);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a = await src.WhereAwait((x, i) => UniTask.Run(() => (x + i) % 2 == 0)).ToArrayAsync();
|
||||||
|
var b = await src.WhereAwait((x, i) => UniTask.FromResult((x + i) % 2 == 0)).ToArrayAsync();
|
||||||
|
var expected = range.Where((x, i) => (x + i) % 2 == 0).ToArray();
|
||||||
|
a.Should().BeEquivalentTo(expected);
|
||||||
|
b.Should().BeEquivalentTo(expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WhereException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = item.Where(x => x % 2 == 0).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.Where((x, i) => x % 2 == 0).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.WhereAwait(x => UniTask.FromResult(x % 2 == 0)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.WhereAwait((x, i) => UniTask.FromResult(x % 2 == 0)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OfType()
|
||||||
|
{
|
||||||
|
var data = new object[] { 0, null, 10, 30, null, "foo", 99 };
|
||||||
|
|
||||||
|
var a = await data.ToUniTaskAsyncEnumerable().OfType<int>().ToArrayAsync();
|
||||||
|
var b = data.OfType<int>().ToArray();
|
||||||
|
|
||||||
|
a.Should().BeEquivalentTo(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OfTypeException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Select(x => (object)x).OfType<int>().ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Cast()
|
||||||
|
{
|
||||||
|
var data = new object[] { 0, 10, 30, 99 };
|
||||||
|
|
||||||
|
var a = await data.ToUniTaskAsyncEnumerable().Cast<int>().ToArrayAsync();
|
||||||
|
var b = data.Cast<int>().ToArray();
|
||||||
|
|
||||||
|
a.Should().BeEquivalentTo(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CastException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Select(x => (object)x).Cast<int>().ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
258
src/UniTask.NetCoreTests/Linq/FirstLast.cs
Normal file
258
src/UniTask.NetCoreTests/Linq/FirstLast.cs
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class FirstLast
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task FirstTest()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().FirstAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().First());
|
||||||
|
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().FirstAsync();
|
||||||
|
var y = new[] { 99 }.First();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().FirstAsync(x => x % 98 == 0));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.First(x => x % 98 == 0));
|
||||||
|
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().FirstAsync(x => x % 2 == 0);
|
||||||
|
var y = array.First(x => x % 2 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().FirstOrDefaultAsync();
|
||||||
|
var y = Enumerable.Empty<int>().FirstOrDefault();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().FirstOrDefaultAsync();
|
||||||
|
var y = new[] { 99 }.FirstOrDefault();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().FirstOrDefaultAsync(x => x % 98 == 0);
|
||||||
|
var y = array.FirstOrDefault(x => x % 98 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().FirstAsync(x => x % 2 == 0);
|
||||||
|
var y = array.FirstOrDefault(x => x % 2 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LastTest()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().LastAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().Last());
|
||||||
|
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().LastAsync();
|
||||||
|
var y = new[] { 99 }.Last();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().LastAsync(x => x % 98 == 0));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Last(x => x % 98 == 0));
|
||||||
|
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().LastAsync(x => x % 2 == 0);
|
||||||
|
var y = array.Last(x => x % 2 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().LastOrDefaultAsync();
|
||||||
|
var y = Enumerable.Empty<int>().LastOrDefault();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().LastOrDefaultAsync();
|
||||||
|
var y = new[] { 99 }.LastOrDefault();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().LastOrDefaultAsync(x => x % 98 == 0);
|
||||||
|
var y = array.LastOrDefault(x => x % 98 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().LastOrDefaultAsync(x => x % 2 == 0);
|
||||||
|
var y = array.LastOrDefault(x => x % 2 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SingleTest()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().SingleAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().Single());
|
||||||
|
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().SingleAsync();
|
||||||
|
var y = new[] { 99 }.Single();
|
||||||
|
x.Should().Be(y);
|
||||||
|
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Single());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
// not found
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 999 == 0));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Single(x => x % 999 == 0));
|
||||||
|
// found multi
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 2 == 0));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.Single(x => x % 2 == 0));
|
||||||
|
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 144 == 0);
|
||||||
|
var y = array.Single(x => x % 144 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().SingleAsync(x => x % 800 == 0);
|
||||||
|
var y = array.Single(x => x % 800 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().SingleOrDefaultAsync();
|
||||||
|
var y = Enumerable.Empty<int>().SingleOrDefault();
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync();
|
||||||
|
var y = new[] { 99 }.SingleOrDefault();
|
||||||
|
x.Should().Be(y);
|
||||||
|
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync());
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.SingleOrDefault());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
// not found
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 999 == 0);
|
||||||
|
var y = array.SingleOrDefault(x => x % 999 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
// found multi
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(async () => await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 2 == 0));
|
||||||
|
Assert.Throws<InvalidOperationException>(() => array.SingleOrDefault(x => x % 2 == 0));
|
||||||
|
|
||||||
|
// normal
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 144 == 0);
|
||||||
|
var y = array.SingleOrDefault(x => x % 144 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().SingleOrDefaultAsync(x => x % 800 == 0);
|
||||||
|
var y = array.SingleOrDefault(x => x % 800 == 0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ElementAtTest()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ElementAtAsync(0));
|
||||||
|
Assert.Throws<ArgumentOutOfRangeException>(() => Enumerable.Empty<int>().ElementAt(0));
|
||||||
|
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().ElementAtAsync(0);
|
||||||
|
var y = new[] { 99 }.ElementAt(0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await array.ToUniTaskAsyncEnumerable().ElementAtAsync(10));
|
||||||
|
Assert.Throws<ArgumentOutOfRangeException>(() => array.ElementAt(10));
|
||||||
|
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtAsync(0);
|
||||||
|
var y = array.ElementAt(0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtAsync(3);
|
||||||
|
var y = array.ElementAt(3);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtAsync(5);
|
||||||
|
var y = array.ElementAt(5);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var x = await Enumerable.Empty<int>().ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(0);
|
||||||
|
var y = Enumerable.Empty<int>().ElementAtOrDefault(0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await new[] { 99 }.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(0);
|
||||||
|
var y = new[] { 99 }.ElementAtOrDefault(0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var array = new[] { 99, 11, 135, 10, 144, 800 };
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(10);
|
||||||
|
var y = array.ElementAtOrDefault(10);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(0);
|
||||||
|
var y = array.ElementAtOrDefault(0);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(3);
|
||||||
|
var y = array.ElementAtOrDefault(3);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var x = await array.ToUniTaskAsyncEnumerable().ElementAtOrDefaultAsync(5);
|
||||||
|
var y = array.ElementAtOrDefault(5);
|
||||||
|
x.Should().Be(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
253
src/UniTask.NetCoreTests/Linq/Joins.cs
Normal file
253
src/UniTask.NetCoreTests/Linq/Joins.cs
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Joins
|
||||||
|
{
|
||||||
|
static int rd;
|
||||||
|
|
||||||
|
static UniTask<T> RandomRun<T>(T value)
|
||||||
|
{
|
||||||
|
if (Interlocked.Increment(ref rd) % 2 == 0)
|
||||||
|
{
|
||||||
|
return UniTask.Run(() => value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return UniTask.FromResult(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Join()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
|
||||||
|
var xs = await outer.ToUniTaskAsyncEnumerable().Join(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => (x, y)).ToArrayAsync();
|
||||||
|
var ys = outer.Join(inner, x => x, x => x, (x, y) => (x, y)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task JoinThrow()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = outer.ToUniTaskAsyncEnumerable().Join(item, x => x, x => x, (x, y) => x + y).ToArrayAsync();
|
||||||
|
var ys = item.Join(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => x + y).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task JoinAwait()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
|
||||||
|
var xs = await outer.ToUniTaskAsyncEnumerable().JoinAwait(inner.ToUniTaskAsyncEnumerable(), x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun((x, y))).ToArrayAsync();
|
||||||
|
var ys = outer.Join(inner, x => x, x => x, (x, y) => (x, y)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task JoinAwaitThrow()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = outer.ToUniTaskAsyncEnumerable().JoinAwait(item, x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun(x + y)).ToArrayAsync();
|
||||||
|
var ys = item.Join(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => x + y).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task JoinAwaitCt()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
|
||||||
|
var xs = await outer.ToUniTaskAsyncEnumerable().JoinAwaitWithCancellation(inner.ToUniTaskAsyncEnumerable(), (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun((x, y))).ToArrayAsync();
|
||||||
|
var ys = outer.Join(inner, x => x, x => x, (x, y) => (x, y)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task JoinAwaitCtThrow()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = outer.ToUniTaskAsyncEnumerable().JoinAwaitWithCancellation(item, (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x + y)).ToArrayAsync();
|
||||||
|
var ys = item.JoinAwaitWithCancellation(inner.ToUniTaskAsyncEnumerable(), (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x + y)).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GroupBy()
|
||||||
|
{
|
||||||
|
var arr = new[] { 1, 4, 10, 10, 4, 5, 10, 9 };
|
||||||
|
{
|
||||||
|
var xs = await arr.ToUniTaskAsyncEnumerable().GroupBy(x => x).ToArrayAsync();
|
||||||
|
var ys = arr.GroupBy(x => x).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await arr.ToUniTaskAsyncEnumerable().GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArrayAsync();
|
||||||
|
var ys = arr.GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.OrderBy(x => x.key).SelectMany(x => x.Item2).Should().BeEquivalentTo(ys.OrderBy(x => x.key).SelectMany(x => x.Item2));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwait(x => RandomRun(x)).ToArrayAsync();
|
||||||
|
var ys = arr.GroupBy(x => x).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwait(x => RandomRun(x), (key, xs) => RandomRun((key, xs.ToArray()))).ToArrayAsync();
|
||||||
|
var ys = arr.GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.OrderBy(x => x.key).SelectMany(x => x.Item2).Should().BeEquivalentTo(ys.OrderBy(x => x.key).SelectMany(x => x.Item2));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwaitWithCancellation((x, _) => RandomRun(x)).ToArrayAsync();
|
||||||
|
var ys = arr.GroupBy(x => x).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.OrderBy(x => x.Key).Should().BeEquivalentTo(ys.OrderBy(x => x.Key));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await arr.ToUniTaskAsyncEnumerable().GroupByAwaitWithCancellation((x, _) => RandomRun(x), (key, xs, _) => RandomRun((key, xs.ToArray()))).ToArrayAsync();
|
||||||
|
var ys = arr.GroupBy(x => x, (key, xs) => (key, xs.ToArray())).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.OrderBy(x => x.key).SelectMany(x => x.Item2).Should().BeEquivalentTo(ys.OrderBy(x => x.key).SelectMany(x => x.Item2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GroupByThrow()
|
||||||
|
{
|
||||||
|
var arr = new[] { 1, 4, 10, 10, 4, 5, 10, 9 };
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.GroupBy(x => x).ToArrayAsync();
|
||||||
|
var ys = item.GroupByAwait(x => RandomRun(x)).ToArrayAsync();
|
||||||
|
var zs = item.GroupByAwaitWithCancellation((x, _) => RandomRun(x)).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GroupJoin()
|
||||||
|
{
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 };
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 };
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await outer.ToUniTaskAsyncEnumerable().GroupJoin(inner.ToUniTaskAsyncEnumerable(), x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArrayAsync();
|
||||||
|
var ys = outer.GroupJoin(inner, x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await outer.ToUniTaskAsyncEnumerable().GroupJoinAwait(inner.ToUniTaskAsyncEnumerable(), x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun((x, string.Join(", ", y)))).ToArrayAsync();
|
||||||
|
var ys = outer.GroupJoin(inner, x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await outer.ToUniTaskAsyncEnumerable().GroupJoinAwaitWithCancellation(inner.ToUniTaskAsyncEnumerable(), (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun((x, string.Join(", ", y)))).ToArrayAsync();
|
||||||
|
var ys = outer.GroupJoin(inner, x => x, x => x, (x, y) => (x, string.Join(", ", y))).ToArray();
|
||||||
|
|
||||||
|
xs.Length.Should().Be(ys.Length);
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GroupJoinThrow()
|
||||||
|
{
|
||||||
|
|
||||||
|
var outer = new[] { 1, 2, 4, 5, 8, 10, 14, 4, 8, 1, 2, 10 }.ToUniTaskAsyncEnumerable();
|
||||||
|
var inner = new[] { 1, 2, 1, 2, 1, 14, 2 }.ToUniTaskAsyncEnumerable();
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = item.GroupJoin(outer, x => x, x => x, (x, y) => x).ToArrayAsync();
|
||||||
|
var ys = inner.GroupJoin(item, x => x, x => x, (x, y) => x).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.GroupJoinAwait(outer, x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun(x)).ToArrayAsync();
|
||||||
|
var ys = inner.GroupJoinAwait(item, x => RandomRun(x), x => RandomRun(x), (x, y) => RandomRun(x)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.GroupJoinAwaitWithCancellation(outer, (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x)).ToArrayAsync();
|
||||||
|
var ys = inner.GroupJoinAwaitWithCancellation(item, (x, _) => RandomRun(x), (x, _) => RandomRun(x), (x, y, _) => RandomRun(x)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
285
src/UniTask.NetCoreTests/Linq/Paging.cs
Normal file
285
src/UniTask.NetCoreTests/Linq/Paging.cs
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Paging
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(9, 0)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 5)]
|
||||||
|
[InlineData(9, 9)]
|
||||||
|
[InlineData(9, 15)]
|
||||||
|
public async Task Skip(int collection, int skipCount)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).Skip(skipCount).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).Skip(skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SkipException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Skip(5).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(9, 0)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 5)]
|
||||||
|
[InlineData(9, 9)]
|
||||||
|
[InlineData(9, 15)]
|
||||||
|
public async Task SkipLast(int collection, int skipCount)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipLast(skipCount).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipLast(skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SkipLastException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipLast(5).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(9, 0)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 5)]
|
||||||
|
[InlineData(9, 9)]
|
||||||
|
[InlineData(9, 15)]
|
||||||
|
public async Task TakeLast(int collection, int takeCount)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeLast(takeCount).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeLast(takeCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TakeLastException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeLast(5).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(9, 0)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 5)]
|
||||||
|
[InlineData(9, 9)]
|
||||||
|
[InlineData(9, 15)]
|
||||||
|
public async Task Take(int collection, int takeCount)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).Take(takeCount).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).Take(takeCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TakeException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Take(5).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(9, 0)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 5)]
|
||||||
|
[InlineData(9, 9)]
|
||||||
|
[InlineData(9, 15)]
|
||||||
|
public async Task SkipWhile(int collection, int skipCount)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwait(x => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwait((x, i) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipWhile(x => x < skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).SkipWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).SkipWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SkipWhileException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipWhile(x => x < 2).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipWhile((x, i) => x < 2).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipWhileAwait((x) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipWhileAwait((x, i) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SkipWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < 2)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(9, 0)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 5)]
|
||||||
|
[InlineData(9, 9)]
|
||||||
|
[InlineData(9, 15)]
|
||||||
|
public async Task TakeWhile(int collection, int skipCount)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwait(x => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwait((x, i) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < skipCount)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TakeWhileException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeWhile(x => x < 5).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeWhile((x, i) => x < 5).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeWhileAwait((x) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeWhileAwait((x, i) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.TakeWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < 5)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
323
src/UniTask.NetCoreTests/Linq/Projection.cs
Normal file
323
src/UniTask.NetCoreTests/Linq/Projection.cs
Normal file
@@ -0,0 +1,323 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Projection
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(0, 1)]
|
||||||
|
[InlineData(0, 2)]
|
||||||
|
[InlineData(0, 10)]
|
||||||
|
public async Task Reverse(int start, int count)
|
||||||
|
{
|
||||||
|
var xs = await Enumerable.Range(start, count).ToUniTaskAsyncEnumerable().Reverse().ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(start, count).Reverse().ToArray();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ReverseException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Reverse().ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0)]
|
||||||
|
[InlineData(1)]
|
||||||
|
[InlineData(9)]
|
||||||
|
public async Task Select(int count)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, count).Select(x => x * x).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, count).Select(x => x * x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
|
||||||
|
var zs = await UniTaskAsyncEnumerable.Range(1, count).SelectAwait((x) => UniTask.Run(() => x * x)).ToArrayAsync();
|
||||||
|
zs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, count).Select((x, i) => x * x * i).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, count).Select((x, i) => x * x * i).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
|
||||||
|
var zs = await UniTaskAsyncEnumerable.Range(1, count).SelectAwait((x, i) => UniTask.Run(() => x * x * i)).ToArrayAsync();
|
||||||
|
zs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SelectException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Select(x => UniTaskAsyncEnumerable.Range(0, 1)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// await
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SelectAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// cancel
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SelectAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 9)] // empty + exists
|
||||||
|
[InlineData(9, 0)] // exists + empty
|
||||||
|
[InlineData(9, 9)] // exists + exists
|
||||||
|
public async Task SelectMany(int leftCount, int rightCount)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount * x)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount * x), (x, y) => x * y).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (x, y) => x * y).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
// await
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (x, y) => x * y).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
// with cancel
|
||||||
|
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (x, y) => x * y).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SelectManyException()
|
||||||
|
{
|
||||||
|
// error + exists
|
||||||
|
// exists + error
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SelectMany(x => UniTaskAsyncEnumerable.Range(0, 1)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectMany(x => item).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// await
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectManyAwait(x => UniTask.Run(() => item)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// with c
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => item)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 9)] // empty + exists
|
||||||
|
[InlineData(9, 0)] // exists + empty
|
||||||
|
[InlineData(9, 9)] // same
|
||||||
|
[InlineData(9, 4)] // leftlong
|
||||||
|
[InlineData(4, 9)] // rightlong
|
||||||
|
public async Task Zip(int leftCount, int rightCount)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).Zip(UniTaskAsyncEnumerable.Range(99, rightCount)).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).ZipAwait(UniTaskAsyncEnumerable.Range(99, rightCount), (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).ZipAwaitWithCancellation(UniTaskAsyncEnumerable.Range(99, rightCount), (x, y, _) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||||
|
var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public async Task ZipException()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Zip(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(1, 10).Zip(item).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// a
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.ZipAwait(UniTaskAsyncEnumerable.Range(1, 10), (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(1, 10).ZipAwait(item, (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// c
|
||||||
|
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.ZipAwaitWithCancellation(UniTaskAsyncEnumerable.Range(1, 10), (x, y, c) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Range(1, 10).ZipAwaitWithCancellation(item, (x, y, c) => UniTask.Run(() => (x, y))).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
// [InlineData(0, 0)]
|
||||||
|
[InlineData(0, 3)]
|
||||||
|
[InlineData(9, 1)]
|
||||||
|
[InlineData(9, 2)]
|
||||||
|
[InlineData(9, 3)]
|
||||||
|
[InlineData(17, 3)]
|
||||||
|
[InlineData(17, 16)]
|
||||||
|
[InlineData(17, 17)]
|
||||||
|
[InlineData(17, 27)]
|
||||||
|
public async Task Buffer(int rangeCount, int bufferCount)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||||
|
var ys = await AsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
// [InlineData(0, 0)]
|
||||||
|
[InlineData(0, 3, 2)]
|
||||||
|
[InlineData(9, 1, 1)]
|
||||||
|
[InlineData(9, 2, 3)]
|
||||||
|
[InlineData(9, 3, 4)]
|
||||||
|
[InlineData(17, 3, 3)]
|
||||||
|
[InlineData(17, 16, 5)]
|
||||||
|
[InlineData(17, 17, 19)]
|
||||||
|
public async Task BufferSkip(int rangeCount, int bufferCount, int skipCount)
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount, skipCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||||
|
var ys = await AsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount, skipCount).Select(x => string.Join(",", x)).ToArrayAsync();
|
||||||
|
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BufferError()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Buffer(3).ToArrayAsync();
|
||||||
|
var ys = item.Buffer(3, 2).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
193
src/UniTask.NetCoreTests/Linq/Sets.cs
Normal file
193
src/UniTask.NetCoreTests/Linq/Sets.cs
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class Sets
|
||||||
|
{
|
||||||
|
public static IEnumerable<object[]> array1 = new object[][]
|
||||||
|
{
|
||||||
|
new object[] { new int[] { } }, // empty
|
||||||
|
new object[] { new int[] { 1, 2, 3 } }, // no dup
|
||||||
|
new object[] { new int[] { 1, 2, 3, 3, 4, 5, 2 } }, // dup
|
||||||
|
};
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> array2 = new object[][]
|
||||||
|
{
|
||||||
|
new object[] { new int[] { } }, // empty
|
||||||
|
new object[] { new int[] { 1, 2 } },
|
||||||
|
new object[] { new int[] { 1, 2, 4, 5, 9 } }, // dup
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(array1))]
|
||||||
|
public async Task Distinct(int[] array)
|
||||||
|
{
|
||||||
|
var ys = array.Distinct().ToArray();
|
||||||
|
{
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().Distinct().ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().Distinct(x => x).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().DistinctAwait(x => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().DistinctAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DistinctThrow()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = item.Distinct().ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.Distinct(x => x).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.DistinctAwait(x => UniTask.Run(() => x)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.DistinctAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(array1))]
|
||||||
|
public async Task DistinctUntilChanged(int[] array)
|
||||||
|
{
|
||||||
|
var ys = await array.ToAsyncEnumerable().DistinctUntilChanged().ToArrayAsync();
|
||||||
|
{
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChanged().ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChanged(x => x).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChangedAwait(x => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
(await array.ToUniTaskAsyncEnumerable().DistinctUntilChangedAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync()).Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DistinctUntilChangedThrow()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = item.DistinctUntilChanged().ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.DistinctUntilChanged(x => x).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.DistinctUntilChangedAwait(x => UniTask.Run(() => x)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = item.DistinctUntilChangedAwaitWithCancellation((x, _) => UniTask.Run(() => x)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Except()
|
||||||
|
{
|
||||||
|
foreach (var a1 in array1.First().Cast<int[]>())
|
||||||
|
{
|
||||||
|
foreach (var a2 in array2.First().Cast<int[]>())
|
||||||
|
{
|
||||||
|
var xs = await a1.ToUniTaskAsyncEnumerable().Except(a2.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||||
|
var ys = a1.Except(a2).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExceptThrow()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Except(UniTaskAsyncEnumerable.Return(10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Return(10).Except(item).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Intersect()
|
||||||
|
{
|
||||||
|
foreach (var a1 in array1.First().Cast<int[]>())
|
||||||
|
{
|
||||||
|
foreach (var a2 in array2.First().Cast<int[]>())
|
||||||
|
{
|
||||||
|
var xs = await a1.ToUniTaskAsyncEnumerable().Intersect(a2.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||||
|
var ys = a1.Intersect(a2).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task IntersectThrow()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Intersect(UniTaskAsyncEnumerable.Return(10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Return(10).Intersect(item).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Union()
|
||||||
|
{
|
||||||
|
foreach (var a1 in array1.First().Cast<int[]>())
|
||||||
|
{
|
||||||
|
foreach (var a2 in array2.First().Cast<int[]>())
|
||||||
|
{
|
||||||
|
var xs = await a1.ToUniTaskAsyncEnumerable().Union(a2.ToUniTaskAsyncEnumerable()).ToArrayAsync();
|
||||||
|
var ys = a1.Union(a2).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UnionThrow()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = item.Union(UniTaskAsyncEnumerable.Return(10)).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
var xs = UniTaskAsyncEnumerable.Return(10).Union(item).ToArrayAsync();
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
221
src/UniTask.NetCoreTests/Linq/Sort.cs
Normal file
221
src/UniTask.NetCoreTests/Linq/Sort.cs
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reactive.Concurrency;
|
||||||
|
using System.Reactive.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class SortCheck
|
||||||
|
{
|
||||||
|
public int Age { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return (Age, FirstName, LastName).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Sort
|
||||||
|
{
|
||||||
|
static int rd;
|
||||||
|
|
||||||
|
static UniTask<T> RandomRun<T>(T value)
|
||||||
|
{
|
||||||
|
if (Interlocked.Increment(ref rd) % 2 == 0)
|
||||||
|
{
|
||||||
|
return UniTask.Run(() => value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return UniTask.FromResult(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static UniTask<T> RandomRun<T>(T value, CancellationToken ct)
|
||||||
|
{
|
||||||
|
if (Interlocked.Increment(ref rd) % 2 == 0)
|
||||||
|
{
|
||||||
|
return UniTask.Run(() => value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return UniTask.FromResult(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OrderBy()
|
||||||
|
{
|
||||||
|
var array = new[] { 1, 99, 32, 4, 536, 7, 8 };
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x).ToArrayAsync();
|
||||||
|
var ys = array.OrderBy(x => x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x).ToArrayAsync();
|
||||||
|
var ys = array.OrderByDescending(x => x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().OrderByAwait(RandomRun).ToArrayAsync();
|
||||||
|
var ys = array.OrderBy(x => x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(RandomRun).ToArrayAsync();
|
||||||
|
var ys = array.OrderByDescending(x => x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||||
|
var ys = array.OrderBy(x => x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var xs = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||||
|
var ys = array.OrderByDescending(x => x).ToArray();
|
||||||
|
xs.Should().BeEquivalentTo(ys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ThenBy()
|
||||||
|
{
|
||||||
|
var array = new[]
|
||||||
|
{
|
||||||
|
new SortCheck { Age = 99, FirstName = "ABC", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 49, FirstName = "ABC", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 49, FirstName = "ABC", LastName = "ZKH" },
|
||||||
|
new SortCheck { Age = 12, FirstName = "ABC", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 49, FirstName = "ABC", LastName = "MEF" },
|
||||||
|
new SortCheck { Age = 12, FirstName = "QQQ", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 19, FirstName = "ZKN", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 39, FirstName = "APO", LastName = "REF" },
|
||||||
|
new SortCheck { Age = 59, FirstName = "ABC", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 99, FirstName = "DBC", LastName = "DEF" },
|
||||||
|
new SortCheck { Age = 99, FirstName = "DBC", LastName = "MEF" },
|
||||||
|
};
|
||||||
|
{
|
||||||
|
var a = array.OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||||
|
var b = array.OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||||
|
var c = array.OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||||
|
var d = array.OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||||
|
var e = array.OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||||
|
var f = array.OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||||
|
var g = array.OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArray();
|
||||||
|
var h = array.OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArray();
|
||||||
|
|
||||||
|
{
|
||||||
|
var a2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||||
|
var b2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||||
|
var c2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||||
|
var d2 = await array.ToUniTaskAsyncEnumerable().OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||||
|
var e2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||||
|
var f2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenBy(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||||
|
var g2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenBy(x => x.LastName).ToArrayAsync();
|
||||||
|
var h2 = await array.ToUniTaskAsyncEnumerable().OrderByDescending(x => x.Age).ThenByDescending(x => x.FirstName).ThenByDescending(x => x.LastName).ToArrayAsync();
|
||||||
|
|
||||||
|
a.Should().BeEquivalentTo(a2);
|
||||||
|
b.Should().BeEquivalentTo(b2);
|
||||||
|
c.Should().BeEquivalentTo(c2);
|
||||||
|
d.Should().BeEquivalentTo(d2);
|
||||||
|
e.Should().BeEquivalentTo(e2);
|
||||||
|
f.Should().BeEquivalentTo(f2);
|
||||||
|
g.Should().BeEquivalentTo(g2);
|
||||||
|
h.Should().BeEquivalentTo(h2);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var b2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var c2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var d2 = await array.ToUniTaskAsyncEnumerable().OrderByAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var e2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var f2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var g2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var h2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwait(x => RandomRun(x.Age)).ThenByDescendingAwait(x => RandomRun(x.FirstName)).ThenByDescendingAwait(x => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
|
||||||
|
a.Should().BeEquivalentTo(a2);
|
||||||
|
b.Should().BeEquivalentTo(b2);
|
||||||
|
c.Should().BeEquivalentTo(c2);
|
||||||
|
d.Should().BeEquivalentTo(d2);
|
||||||
|
e.Should().BeEquivalentTo(e2);
|
||||||
|
f.Should().BeEquivalentTo(f2);
|
||||||
|
g.Should().BeEquivalentTo(g2);
|
||||||
|
h.Should().BeEquivalentTo(h2);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var b2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var c2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var d2 = await array.ToUniTaskAsyncEnumerable().OrderByAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var e2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var f2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var g2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
var h2 = await array.ToUniTaskAsyncEnumerable().OrderByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.Age)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.FirstName)).ThenByDescendingAwaitWithCancellation((x, ct) => RandomRun(x.LastName)).ToArrayAsync();
|
||||||
|
|
||||||
|
a.Should().BeEquivalentTo(a2);
|
||||||
|
b.Should().BeEquivalentTo(b2);
|
||||||
|
c.Should().BeEquivalentTo(c2);
|
||||||
|
d.Should().BeEquivalentTo(d2);
|
||||||
|
e.Should().BeEquivalentTo(e2);
|
||||||
|
f.Should().BeEquivalentTo(f2);
|
||||||
|
g.Should().BeEquivalentTo(g2);
|
||||||
|
h.Should().BeEquivalentTo(h2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Throws()
|
||||||
|
{
|
||||||
|
foreach (var item in UniTaskTestException.Throws())
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var a = item.OrderBy(x => x).ToArrayAsync();
|
||||||
|
var b = item.OrderByDescending(x => x).ToArrayAsync();
|
||||||
|
var c = item.OrderBy(x => x).ThenBy(x => x).ToArrayAsync();
|
||||||
|
var d = item.OrderBy(x => x).ThenByDescending(x => x).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await a);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await b);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await c);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await d);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a = item.OrderByAwait(RandomRun).ToArrayAsync();
|
||||||
|
var b = item.OrderByDescendingAwait(RandomRun).ToArrayAsync();
|
||||||
|
var c = item.OrderByAwait(RandomRun).ThenByAwait(RandomRun).ToArrayAsync();
|
||||||
|
var d = item.OrderByAwait(RandomRun).ThenByDescendingAwait(RandomRun).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await a);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await b);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await c);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await d);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var a = item.OrderByAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||||
|
var b = item.OrderByDescendingAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||||
|
var c = item.OrderByAwaitWithCancellation(RandomRun).ThenByAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||||
|
var d = item.OrderByAwaitWithCancellation(RandomRun).ThenByDescendingAwaitWithCancellation(RandomRun).ToArrayAsync();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await a);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await b);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await c);
|
||||||
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
126
src/UniTask.NetCoreTests/Linq/_Exception.cs
Normal file
126
src/UniTask.NetCoreTests/Linq/_Exception.cs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.ExceptionServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace NetCoreTests.Linq
|
||||||
|
{
|
||||||
|
public class UniTaskTestException : Exception
|
||||||
|
{
|
||||||
|
public static IUniTaskAsyncEnumerable<int> ThrowImmediate()
|
||||||
|
{
|
||||||
|
return UniTaskAsyncEnumerable.Throw<int>(new UniTaskTestException());
|
||||||
|
}
|
||||||
|
public static IUniTaskAsyncEnumerable<int> ThrowAfter()
|
||||||
|
{
|
||||||
|
return new ThrowAfter<int>(new UniTaskTestException());
|
||||||
|
}
|
||||||
|
public static IUniTaskAsyncEnumerable<int> ThrowInMoveNext()
|
||||||
|
{
|
||||||
|
return new ThrowIn<int>(new UniTaskTestException());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static IEnumerable<IUniTaskAsyncEnumerable<int>> Throws(int count = 3)
|
||||||
|
{
|
||||||
|
yield return ThrowImmediate();
|
||||||
|
yield return ThrowAfter();
|
||||||
|
yield return ThrowInMoveNext();
|
||||||
|
yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowImmediate());
|
||||||
|
yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowAfter());
|
||||||
|
yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowInMoveNext());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class ThrowIn<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||||
|
{
|
||||||
|
readonly Exception exception;
|
||||||
|
|
||||||
|
public ThrowIn(Exception exception)
|
||||||
|
{
|
||||||
|
this.exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new Enumerator(exception, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||||
|
{
|
||||||
|
readonly Exception exception;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
public Enumerator(Exception exception, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.exception = exception;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TValue Current => default;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
ExceptionDispatchInfo.Capture(exception).Throw();
|
||||||
|
return new UniTask<bool>(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class ThrowAfter<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||||
|
{
|
||||||
|
readonly Exception exception;
|
||||||
|
|
||||||
|
public ThrowAfter(Exception exception)
|
||||||
|
{
|
||||||
|
this.exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new Enumerator(exception, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||||
|
{
|
||||||
|
readonly Exception exception;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
public Enumerator(Exception exception, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.exception = exception;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TValue Current => default;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
var tcs = new UniTaskCompletionSource<bool>();
|
||||||
|
|
||||||
|
var awaiter = UniTask.Yield().GetAwaiter();
|
||||||
|
awaiter.UnsafeOnCompleted(() =>
|
||||||
|
{
|
||||||
|
Thread.Sleep(1);
|
||||||
|
tcs.TrySetException(exception);
|
||||||
|
});
|
||||||
|
|
||||||
|
return tcs.Task;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
@@ -9,10 +9,20 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||||
<PackageReference Include="xunit" Version="2.4.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
|
||||||
<PackageReference Include="coverlet.collector" Version="1.0.1" />
|
<PackageReference Include="System.Linq.Async" Version="4.1.1" />
|
||||||
|
<PackageReference Include="System.Reactive" Version="4.4.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\UniTask.NetCore\UniTask.NetCore.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
using System;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace UniTask.NetCoreTests
|
|
||||||
{
|
|
||||||
public class UnitTest1
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void Test1()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,14 +12,7 @@ public static class PackageExporter
|
|||||||
public static void Export()
|
public static void Export()
|
||||||
{
|
{
|
||||||
var root = "Plugins/UniTask";
|
var root = "Plugins/UniTask";
|
||||||
var version = Environment.GetEnvironmentVariable("UNITY_PACKAGE_VERSION");
|
var version = GetVersion(root);
|
||||||
|
|
||||||
var versionJson = Path.Combine(Application.dataPath, root, "package.json");
|
|
||||||
if (File.Exists(versionJson))
|
|
||||||
{
|
|
||||||
var v = JsonUtility.FromJson<Version>(File.ReadAllText(versionJson));
|
|
||||||
version = v.version;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileName = string.IsNullOrEmpty(version) ? "UniTask.unitypackage" : $"UniTask.{version}.unitypackage";
|
var fileName = string.IsNullOrEmpty(version) ? "UniTask.unitypackage" : $"UniTask.{version}.unitypackage";
|
||||||
var exportPath = "./" + fileName;
|
var exportPath = "./" + fileName;
|
||||||
@@ -40,6 +33,37 @@ public static class PackageExporter
|
|||||||
UnityEngine.Debug.Log("Export complete: " + Path.GetFullPath(exportPath));
|
UnityEngine.Debug.Log("Export complete: " + Path.GetFullPath(exportPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string GetVersion(string root)
|
||||||
|
{
|
||||||
|
var version = Environment.GetEnvironmentVariable("UNITY_PACKAGE_VERSION");
|
||||||
|
var versionJson = Path.Combine(Application.dataPath, root, "package.json");
|
||||||
|
|
||||||
|
if (File.Exists(versionJson))
|
||||||
|
{
|
||||||
|
var v = JsonUtility.FromJson<Version>(File.ReadAllText(versionJson));
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(version))
|
||||||
|
{
|
||||||
|
if (v.version != version)
|
||||||
|
{
|
||||||
|
var msg = $"package.json and env version are mismatched. UNITY_PACKAGE_VERSION:{version}, package.json:{v.version}";
|
||||||
|
|
||||||
|
if (Application.isBatchMode)
|
||||||
|
{
|
||||||
|
Console.WriteLine(msg);
|
||||||
|
Application.Quit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception("package.json and env version are mismatched.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
version = v.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
public class Version
|
public class Version
|
||||||
{
|
{
|
||||||
public string version;
|
public string version;
|
||||||
|
|||||||
8
src/UniTask/Assets/Plugins/UniTask/Runtime.meta
Normal file
8
src/UniTask/Assets/Plugins/UniTask/Runtime.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: aa765154468d4b34eb34304100d39e64
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
public interface IUniTaskAsyncEnumerable<out T>
|
||||||
|
{
|
||||||
|
IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IUniTaskAsyncEnumerator<out T> : IUniTaskAsyncDisposable
|
||||||
|
{
|
||||||
|
T Current { get; }
|
||||||
|
UniTask<bool> MoveNextAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IUniTaskAsyncDisposable
|
||||||
|
{
|
||||||
|
UniTask DisposeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IUniTaskOrderedAsyncEnumerable<TElement> : IUniTaskAsyncEnumerable<TElement>
|
||||||
|
{
|
||||||
|
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
|
||||||
|
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
|
||||||
|
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
|
||||||
|
}
|
||||||
|
|
||||||
|
//public interface IUniTaskAsyncGrouping<out TKey, out TElement> : IUniTaskAsyncEnumerable<TElement>
|
||||||
|
//{
|
||||||
|
// TKey Key { get; }
|
||||||
|
//}
|
||||||
|
|
||||||
|
public static class UniTaskAsyncEnumerableExtensions
|
||||||
|
{
|
||||||
|
public static UniTaskCancelableAsyncEnumerable<T> WithCancellation<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new UniTaskCancelableAsyncEnumerable<T>(source, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public readonly struct UniTaskCancelableAsyncEnumerable<T>
|
||||||
|
{
|
||||||
|
private readonly IUniTaskAsyncEnumerable<T> enumerable;
|
||||||
|
private readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
internal UniTaskCancelableAsyncEnumerable(IUniTaskAsyncEnumerable<T> enumerable, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.enumerable = enumerable;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumerator GetAsyncEnumerator()
|
||||||
|
{
|
||||||
|
return new Enumerator(enumerable.GetAsyncEnumerator(cancellationToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public readonly struct Enumerator
|
||||||
|
{
|
||||||
|
private readonly IUniTaskAsyncEnumerator<T> enumerator;
|
||||||
|
|
||||||
|
internal Enumerator(IUniTaskAsyncEnumerator<T> enumerator)
|
||||||
|
{
|
||||||
|
this.enumerator = enumerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Current => enumerator.Current;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
return enumerator.MoveNextAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b20cf9f02ac585948a4372fa4ee06504
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -20,6 +20,24 @@ namespace Cysharp.Threading.Tasks.Internal
|
|||||||
throw new ArgumentNullException(paramName);
|
throw new ArgumentNullException(paramName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Exception ArgumentOutOfRange(string paramName)
|
||||||
|
{
|
||||||
|
return new ArgumentOutOfRangeException(paramName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Exception NoElements()
|
||||||
|
{
|
||||||
|
return new InvalidOperationException("Source sequence doesn't contain any elements.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Exception MoreThanOneElement()
|
||||||
|
{
|
||||||
|
return new InvalidOperationException("Source sequence contains more than one element.");
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
public static void ThrowArgumentException<T>(string message)
|
public static void ThrowArgumentException<T>(string message)
|
||||||
{
|
{
|
||||||
@@ -4,12 +4,14 @@ using System.Threading;
|
|||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Internal
|
namespace Cysharp.Threading.Tasks.Internal
|
||||||
{
|
{
|
||||||
internal interface IPromisePoolItem
|
// public, allow to user create custom operator with pool.
|
||||||
|
|
||||||
|
public interface IPromisePoolItem
|
||||||
{
|
{
|
||||||
void Reset();
|
void Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class PromisePool<T>
|
public class PromisePool<T>
|
||||||
where T : class, IPromisePoolItem
|
where T : class, IPromisePoolItem
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
8
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq.meta
Normal file
8
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4cc94a232b1c1154b8084bdda29c3484
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
318
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Aggregate.cs
Normal file
318
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Aggregate.cs
Normal file
@@ -0,0 +1,318 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static UniTask<TSource> AggregateAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, accumulator, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, seed, accumulator, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(resultSelector));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, seed, accumulator, resultSelector, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TSource> AggregateAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, UniTask<TSource>> accumulator, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, accumulator, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TAccumulate> AggregateAwaitAsync<TSource, TAccumulate>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, seed, accumulator, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TResult> AggregateAwaitAsync<TSource, TAccumulate, TResult>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, Func<TAccumulate, UniTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(resultSelector));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, seed, accumulator, resultSelector, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TSource> AggregateAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, UniTask<TSource>> accumulator, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, accumulator, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TAccumulate> AggregateAwaitWithCancellationAsync<TSource, TAccumulate>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, seed, accumulator, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<TResult> AggregateAwaitWithCancellationAsync<TSource, TAccumulate, TResult>(this IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, UniTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(accumulator));
|
||||||
|
Error.ThrowArgumentNullException(accumulator, nameof(resultSelector));
|
||||||
|
|
||||||
|
return Aggregate.InvokeAsync(source, seed, accumulator, resultSelector, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static class Aggregate
|
||||||
|
{
|
||||||
|
internal static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TSource value;
|
||||||
|
if (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = e.Current;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw Error.NoElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = accumulator(value, e.Current);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<TAccumulate> InvokeAsync<TSource, TAccumulate>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TAccumulate value = seed;
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = accumulator(value, e.Current);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<TResult> InvokeAsync<TSource, TAccumulate, TResult>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TAccumulate value = seed;
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = accumulator(value, e.Current);
|
||||||
|
}
|
||||||
|
return resultSelector(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// with async
|
||||||
|
|
||||||
|
internal static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, UniTask<TSource>> accumulator, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TSource value;
|
||||||
|
if (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = e.Current;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw Error.NoElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = await accumulator(value, e.Current);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<TAccumulate> InvokeAsync<TSource, TAccumulate>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TAccumulate value = seed;
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = await accumulator(value, e.Current);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<TResult> InvokeAsync<TSource, TAccumulate, TResult>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, UniTask<TAccumulate>> accumulator, Func<TAccumulate, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TAccumulate value = seed;
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = await accumulator(value, e.Current);
|
||||||
|
}
|
||||||
|
return await resultSelector(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// with cancellation
|
||||||
|
|
||||||
|
internal static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, UniTask<TSource>> accumulator, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TSource value;
|
||||||
|
if (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = e.Current;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw Error.NoElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = await accumulator(value, e.Current, cancellationToken);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<TAccumulate> InvokeAsync<TSource, TAccumulate>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TAccumulate value = seed;
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = await accumulator(value, e.Current, cancellationToken);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<TResult> InvokeAsync<TSource, TAccumulate, TResult>(IUniTaskAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, UniTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TAccumulate value = seed;
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
value = await accumulator(value, e.Current, cancellationToken);
|
||||||
|
}
|
||||||
|
return await resultSelector(value, cancellationToken);
|
||||||
|
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5dc68c05a4228c643937f6ebd185bcca
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
108
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/All.cs
Normal file
108
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/All.cs
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static UniTask<Boolean> AllAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||||
|
|
||||||
|
return All.InvokeAsync(source, predicate, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<Boolean> AllAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||||
|
|
||||||
|
return All.InvokeAsync(source, predicate, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<Boolean> AllAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||||
|
|
||||||
|
return All.InvokeAsync(source, predicate, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static class All
|
||||||
|
{
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
if (!predicate(e.Current))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
if (!await predicate(e.Current))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
if (!await predicate(e.Current, cancellationToken))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/All.cs.meta
Normal file
11
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/All.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7271437e0033af2448b600ee248924dd
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
136
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Any.cs
Normal file
136
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Any.cs
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static UniTask<Boolean> AnyAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
|
||||||
|
return Any.InvokeAsync(source, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<Boolean> AnyAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||||
|
|
||||||
|
return Any.InvokeAsync(source, predicate, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<Boolean> AnyAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||||
|
|
||||||
|
return Any.InvokeAsync(source, predicate, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<Boolean> AnyAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
Error.ThrowArgumentNullException(predicate, nameof(predicate));
|
||||||
|
|
||||||
|
return Any.InvokeAsync(source, predicate, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static class Any
|
||||||
|
{
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
if (predicate(e.Current))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
if (await predicate(e.Current))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async UniTask<bool> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var e = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await e.MoveNextAsync())
|
||||||
|
{
|
||||||
|
if (await predicate(e.Current, cancellationToken))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (e != null)
|
||||||
|
{
|
||||||
|
await e.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Any.cs.meta
Normal file
11
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Any.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e2b2e65745263994fbe34f3e0ec8eb12
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
148
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/AppendPrepend.cs
Normal file
148
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/AppendPrepend.cs
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static IUniTaskAsyncEnumerable<TSource> Append<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource element)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
|
||||||
|
return new AppendPrepend<TSource>(source, element, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<TSource> Prepend<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource element)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
|
||||||
|
return new AppendPrepend<TSource>(source, element, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class AppendPrepend<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||||
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
readonly TSource element;
|
||||||
|
readonly bool append; // or prepend
|
||||||
|
|
||||||
|
public AppendPrepend(IUniTaskAsyncEnumerable<TSource> source, TSource element, bool append)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
this.element = element;
|
||||||
|
this.append = append;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new Enumerator(source, element, append, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
|
{
|
||||||
|
enum State : byte
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
RequirePrepend,
|
||||||
|
RequireAppend,
|
||||||
|
Completed
|
||||||
|
}
|
||||||
|
|
||||||
|
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||||
|
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
readonly TSource element;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
State state;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
|
||||||
|
public Enumerator(IUniTaskAsyncEnumerable<TSource> source, TSource element, bool append, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
this.element = element;
|
||||||
|
this.state = append ? State.RequireAppend : State.RequirePrepend;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
completionSource.Reset();
|
||||||
|
|
||||||
|
if (enumerator == null)
|
||||||
|
{
|
||||||
|
if (state == State.RequireAppend)
|
||||||
|
{
|
||||||
|
Current = element;
|
||||||
|
state = State.None;
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == State.Completed)
|
||||||
|
{
|
||||||
|
return CompletedTasks.False;
|
||||||
|
}
|
||||||
|
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
MoveNextCoreDelegate(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MoveNextCore(object state)
|
||||||
|
{
|
||||||
|
var self = (Enumerator)state;
|
||||||
|
|
||||||
|
if (self.TryGetResult(self.awaiter, out var result))
|
||||||
|
{
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
self.Current = self.enumerator.Current;
|
||||||
|
self.completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (self.state == State.RequireAppend)
|
||||||
|
{
|
||||||
|
self.state = State.Completed;
|
||||||
|
self.Current = self.element;
|
||||||
|
self.completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.state = State.Completed;
|
||||||
|
self.completionSource.TrySetResult(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3268ec424b8055f45aa2a26d17c80468
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 69866e262589ea643bbc62a1d696077a
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -0,0 +1,410 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public abstract class MoveNextSource : IUniTaskSource<bool>
|
||||||
|
{
|
||||||
|
protected UniTaskCompletionSourceCore<bool> completionSource;
|
||||||
|
|
||||||
|
public bool GetResult(short token)
|
||||||
|
{
|
||||||
|
return completionSource.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return completionSource.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
completionSource.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return completionSource.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
void IUniTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
completionSource.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool TryGetResult<T>(UniTask<T>.Awaiter awaiter, out T result)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = awaiter.GetResult();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool TryGetResult(UniTask.Awaiter awaiter)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
awaiter.GetResult();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class AsyncEnumeratorBase<TSource, TResult> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
|
{
|
||||||
|
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
||||||
|
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
protected CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter sourceMoveNext;
|
||||||
|
|
||||||
|
public AsyncEnumeratorBase(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
// abstract
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If return value is false, continue source.MoveNext.
|
||||||
|
/// </summary>
|
||||||
|
protected abstract bool TryMoveNextCore(bool sourceHasCurrent, out bool result);
|
||||||
|
|
||||||
|
// Util
|
||||||
|
protected TSource SourceCurrent => enumerator.Current;
|
||||||
|
|
||||||
|
// IUniTaskAsyncEnumerator<T>
|
||||||
|
|
||||||
|
public TResult Current { get; protected set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
if (enumerator == null)
|
||||||
|
{
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
if (!OnFirstIteration())
|
||||||
|
{
|
||||||
|
SourceMoveNext();
|
||||||
|
}
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual bool OnFirstIteration()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void SourceMoveNext()
|
||||||
|
{
|
||||||
|
CONTINUE:
|
||||||
|
sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (sourceMoveNext.IsCompleted)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!TryMoveNextCore(sourceMoveNext.GetResult(), out result))
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
completionSource.TrySetCanceled(cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MoveNextCallBack(object state)
|
||||||
|
{
|
||||||
|
var self = (AsyncEnumeratorBase<TSource, TResult>)state;
|
||||||
|
bool result;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result))
|
||||||
|
{
|
||||||
|
self.SourceMoveNext();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetCanceled(self.cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if require additional resource to dispose, override and call base.DisposeAsync.
|
||||||
|
public virtual UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
|
{
|
||||||
|
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
||||||
|
static readonly Action<object> setCurrentCallbackDelegate = SetCurrentCallBack;
|
||||||
|
|
||||||
|
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
protected CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter sourceMoveNext;
|
||||||
|
|
||||||
|
UniTask<TAwait>.Awaiter resultAwaiter;
|
||||||
|
|
||||||
|
public AsyncEnumeratorAwaitSelectorBase(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
// abstract
|
||||||
|
|
||||||
|
protected abstract UniTask<TAwait> TransformAsync(TSource sourceCurrent);
|
||||||
|
protected abstract bool TrySetCurrentCore(TAwait awaitResult, out bool terminateIteration);
|
||||||
|
|
||||||
|
// Util
|
||||||
|
protected TSource SourceCurrent { get; private set; }
|
||||||
|
|
||||||
|
protected (bool waitCallback, bool requireNextIteration) ActionCompleted(bool trySetCurrentResult, out bool moveNextResult)
|
||||||
|
{
|
||||||
|
if (trySetCurrentResult)
|
||||||
|
{
|
||||||
|
moveNextResult = true;
|
||||||
|
return (false, false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
moveNextResult = default;
|
||||||
|
return (false, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected (bool waitCallback, bool requireNextIteration) WaitAwaitCallback(out bool moveNextResult) { moveNextResult = default; return (true, false); }
|
||||||
|
protected (bool waitCallback, bool requireNextIteration) IterateFinished(out bool moveNextResult) { moveNextResult = false; return (false, false); }
|
||||||
|
|
||||||
|
// IUniTaskAsyncEnumerator<T>
|
||||||
|
|
||||||
|
public TResult Current { get; protected set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
if (enumerator == null)
|
||||||
|
{
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
SourceMoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void SourceMoveNext()
|
||||||
|
{
|
||||||
|
CONTINUE:
|
||||||
|
sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (sourceMoveNext.IsCompleted)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
(bool waitCallback, bool requireNextIteration) = TryMoveNextCore(sourceMoveNext.GetResult(), out result);
|
||||||
|
|
||||||
|
if (waitCallback)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requireNextIteration)
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(bool waitCallback, bool requireNextIteration) TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
||||||
|
{
|
||||||
|
if (sourceHasCurrent)
|
||||||
|
{
|
||||||
|
SourceCurrent = enumerator.Current;
|
||||||
|
var task = TransformAsync(SourceCurrent);
|
||||||
|
if (UnwarapTask(task, out var taskResult))
|
||||||
|
{
|
||||||
|
var currentResult = TrySetCurrentCore(taskResult, out var terminateIteration);
|
||||||
|
if (terminateIteration)
|
||||||
|
{
|
||||||
|
return IterateFinished(out result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ActionCompleted(currentResult, out result);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return WaitAwaitCallback(out result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IterateFinished(out result);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool UnwarapTask(UniTask<TAwait> taskResult, out TAwait result)
|
||||||
|
{
|
||||||
|
resultAwaiter = taskResult.GetAwaiter();
|
||||||
|
|
||||||
|
if (resultAwaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
result = resultAwaiter.GetResult();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
resultAwaiter.SourceOnCompleted(setCurrentCallbackDelegate, this);
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MoveNextCallBack(object state)
|
||||||
|
{
|
||||||
|
var self = (AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait>)state;
|
||||||
|
bool result = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
(bool waitCallback, bool requireNextIteration) = self.TryMoveNextCore(self.sourceMoveNext.GetResult(), out result);
|
||||||
|
|
||||||
|
if (waitCallback)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requireNextIteration)
|
||||||
|
{
|
||||||
|
self.SourceMoveNext();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetCurrentCallBack(object state)
|
||||||
|
{
|
||||||
|
var self = (AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait>)state;
|
||||||
|
|
||||||
|
bool doneSetCurrent;
|
||||||
|
bool terminateIteration;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = self.resultAwaiter.GetResult();
|
||||||
|
doneSetCurrent = self.TrySetCurrentCore(result, out terminateIteration);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetCanceled(self.cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (doneSetCurrent)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (terminateIteration)
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetResult(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.SourceMoveNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if require additional resource to dispose, override and call base.DisposeAsync.
|
||||||
|
public virtual UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 01ba1d3b17e13fb4c95740131c7e6e19
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user