From 31b788a2c9f919a5559303a7c9c6d612d24bbb30 Mon Sep 17 00:00:00 2001 From: neuecc Date: Sun, 10 May 2020 00:07:51 +0900 Subject: [PATCH] All, ANy, Contains, SequeuceEqual --- src/UniTask.NetCore/Linq/All.cs | 875 +++------------------ src/UniTask.NetCore/Linq/Any.cs | 903 ++++------------------ src/UniTask.NetCore/Linq/Contains.cs | 817 ++------------------ src/UniTask.NetCore/Linq/Count.cs | 18 +- src/UniTask.NetCore/Linq/LongCount.cs | 18 +- src/UniTask.NetCore/Linq/SequenceEqual.cs | 854 ++------------------ src/UniTask.NetCore/Linq/_FileMaker.cs | 52 -- src/UniTask.NetCoreTests/Linq/AllAny.cs | 112 +++ 8 files changed, 495 insertions(+), 3154 deletions(-) create mode 100644 src/UniTask.NetCoreTests/Linq/AllAny.cs diff --git a/src/UniTask.NetCore/Linq/All.cs b/src/UniTask.NetCore/Linq/All.cs index bc15cec..b8fb003 100644 --- a/src/UniTask.NetCore/Linq/All.cs +++ b/src/UniTask.NetCore/Linq/All.cs @@ -1,775 +1,108 @@ -namespace Cysharp.Threading.Tasks.Linq +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq { - internal sealed class All + public static partial class UniTaskAsyncEnumerable { + public static UniTask AllAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return All.InvokeAsync(source, predicate, cancellationToken); + } + + public static UniTask AllAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return All.InvokeAsync(source, predicate, cancellationToken); + } + + public static UniTask AllAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> 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 InvokeAsync(IUniTaskAsyncEnumerable source, Func 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 InvokeAsync(IUniTaskAsyncEnumerable source, Func> 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 InvokeAsync(IUniTaskAsyncEnumerable source, Func> 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(); + } + } + } + } +} \ No newline at end of file diff --git a/src/UniTask.NetCore/Linq/Any.cs b/src/UniTask.NetCore/Linq/Any.cs index b614c81..cf3cbfa 100644 --- a/src/UniTask.NetCore/Linq/Any.cs +++ b/src/UniTask.NetCore/Linq/Any.cs @@ -1,775 +1,136 @@ -namespace Cysharp.Threading.Tasks.Linq +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq { - internal sealed class Any + public static partial class UniTaskAsyncEnumerable { + public static UniTask AnyAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + + return Any.InvokeAsync(source, cancellationToken); + } + + public static UniTask AnyAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Any.InvokeAsync(source, predicate, cancellationToken); + } + + public static UniTask AnyAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(predicate, nameof(predicate)); + + return Any.InvokeAsync(source, predicate, cancellationToken); + } + + public static UniTask AnyAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> 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 InvokeAsync(IUniTaskAsyncEnumerable 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 InvokeAsync(IUniTaskAsyncEnumerable source, Func 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 InvokeAsync(IUniTaskAsyncEnumerable source, Func> 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 InvokeAsync(IUniTaskAsyncEnumerable source, Func> 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(); + } + } + } + } +} \ No newline at end of file diff --git a/src/UniTask.NetCore/Linq/Contains.cs b/src/UniTask.NetCore/Linq/Contains.cs index b9d1b90..e38601c 100644 --- a/src/UniTask.NetCore/Linq/Contains.cs +++ b/src/UniTask.NetCore/Linq/Contains.cs @@ -1,775 +1,50 @@ -namespace Cysharp.Threading.Tasks.Linq +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq { - internal sealed class Contains + public static partial class UniTaskAsyncEnumerable { + public static UniTask ContainsAsync(this IUniTaskAsyncEnumerable source, TSource value, CancellationToken cancellationToken = default) + { + return ContainsAsync(source, value, EqualityComparer.Default, cancellationToken); + } + + public static UniTask ContainsAsync(this IUniTaskAsyncEnumerable source, TSource value, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(source, nameof(source)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return Contains.InvokeAsync(source, value, comparer, cancellationToken); + } } - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + internal static class Contains + { + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, TSource value, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var e = source.GetAsyncEnumerator(cancellationToken); + try + { + while (await e.MoveNextAsync()) + { + if (comparer.Equals(value, e.Current)) + { + return true; + } + } + + return false; + } + finally + { + if (e != null) + { + await e.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/src/UniTask.NetCore/Linq/Count.cs b/src/UniTask.NetCore/Linq/Count.cs index a6ec863..1cdcda3 100644 --- a/src/UniTask.NetCore/Linq/Count.cs +++ b/src/UniTask.NetCore/Linq/Count.cs @@ -10,7 +10,7 @@ namespace Cysharp.Threading.Tasks.Linq { Error.ThrowArgumentNullException(source, nameof(source)); - return Count.InvokeAsync(source, cancellationToken); + return Count.InvokeAsync(source, cancellationToken); } public static UniTask CountAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) @@ -18,7 +18,7 @@ namespace Cysharp.Threading.Tasks.Linq Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); - return Count.InvokeAsync(source, predicate, cancellationToken); + return Count.InvokeAsync(source, predicate, cancellationToken); } public static UniTask CountAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) @@ -26,7 +26,7 @@ namespace Cysharp.Threading.Tasks.Linq Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); - return Count.InvokeAsync(source, predicate, cancellationToken); + return Count.InvokeAsync(source, predicate, cancellationToken); } public static UniTask CountAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) @@ -34,13 +34,13 @@ namespace Cysharp.Threading.Tasks.Linq Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); - return Count.InvokeAsync(source, predicate, cancellationToken); + return Count.InvokeAsync(source, predicate, cancellationToken); } } - internal static class Count + internal static class Count { - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) { var count = 0; @@ -63,7 +63,7 @@ namespace Cysharp.Threading.Tasks.Linq return count; } - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { var count = 0; @@ -89,7 +89,7 @@ namespace Cysharp.Threading.Tasks.Linq return count; } - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { var count = 0; @@ -115,7 +115,7 @@ namespace Cysharp.Threading.Tasks.Linq return count; } - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { var count = 0; diff --git a/src/UniTask.NetCore/Linq/LongCount.cs b/src/UniTask.NetCore/Linq/LongCount.cs index 13b8c0f..4460061 100644 --- a/src/UniTask.NetCore/Linq/LongCount.cs +++ b/src/UniTask.NetCore/Linq/LongCount.cs @@ -10,7 +10,7 @@ namespace Cysharp.Threading.Tasks.Linq { Error.ThrowArgumentNullException(source, nameof(source)); - return LongCount.InvokeAsync(source, cancellationToken); + return LongCount.InvokeAsync(source, cancellationToken); } public static UniTask LongCountAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) @@ -18,7 +18,7 @@ namespace Cysharp.Threading.Tasks.Linq Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); - return LongCount.InvokeAsync(source, predicate, cancellationToken); + return LongCount.InvokeAsync(source, predicate, cancellationToken); } public static UniTask LongCountAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) @@ -26,7 +26,7 @@ namespace Cysharp.Threading.Tasks.Linq Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); - return LongCount.InvokeAsync(source, predicate, cancellationToken); + return LongCount.InvokeAsync(source, predicate, cancellationToken); } public static UniTask LongCountAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) @@ -34,13 +34,13 @@ namespace Cysharp.Threading.Tasks.Linq Error.ThrowArgumentNullException(source, nameof(source)); Error.ThrowArgumentNullException(predicate, nameof(predicate)); - return LongCount.InvokeAsync(source, predicate, cancellationToken); + return LongCount.InvokeAsync(source, predicate, cancellationToken); } } - internal static class LongCount + internal static class LongCount { - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) { long count = 0; @@ -63,7 +63,7 @@ namespace Cysharp.Threading.Tasks.Linq return count; } - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { long count = 0; @@ -89,7 +89,7 @@ namespace Cysharp.Threading.Tasks.Linq return count; } - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { long count = 0; @@ -115,7 +115,7 @@ namespace Cysharp.Threading.Tasks.Linq return count; } - internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { long count = 0; diff --git a/src/UniTask.NetCore/Linq/SequenceEqual.cs b/src/UniTask.NetCore/Linq/SequenceEqual.cs index a297a45..ccef8b6 100644 --- a/src/UniTask.NetCore/Linq/SequenceEqual.cs +++ b/src/UniTask.NetCore/Linq/SequenceEqual.cs @@ -1,775 +1,87 @@ -namespace Cysharp.Threading.Tasks.Linq +using Cysharp.Threading.Tasks.Internal; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq { - internal sealed class SequenceEqual + public static partial class UniTaskAsyncEnumerable { + public static UniTask SequenceEqualAsync(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, CancellationToken cancellationToken = default) + { + return SequenceEqualAsync(first, second, EqualityComparer.Default, cancellationToken); + } + + public static UniTask SequenceEqualAsync(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken = default) + { + Error.ThrowArgumentNullException(first, nameof(first)); + Error.ThrowArgumentNullException(second, nameof(second)); + Error.ThrowArgumentNullException(comparer, nameof(comparer)); + + return SequenceEqual.InvokeAsync(first, second, comparer, cancellationToken); + } } - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + internal static class SequenceEqual + { + internal static async UniTask InvokeAsync(IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) + { + var e1 = first.GetAsyncEnumerator(cancellationToken); + try + { + var e2 = second.GetAsyncEnumerator(cancellationToken); + try + { + while (true) + { + if (await e1.MoveNextAsync()) + { + if (await e2.MoveNextAsync()) + { + if (comparer.Equals(e1.Current, e2.Current)) + { + continue; + } + else + { + return false; + } + } + else + { + // e2 is finished, but e1 has value + return false; + } + } + else + { + // e1 is finished, e2? + if (await e2.MoveNextAsync()) + { + return false; + } + else + { + return true; + } + } + } + } + finally + { + if (e2 != null) + { + await e2.DisposeAsync(); + } + } + } + finally + { + if (e1 != null) + { + await e1.DisposeAsync(); + } + } + } + } +} \ No newline at end of file diff --git a/src/UniTask.NetCore/Linq/_FileMaker.cs b/src/UniTask.NetCore/Linq/_FileMaker.cs index 7f5d3ea..902b263 100644 --- a/src/UniTask.NetCore/Linq/_FileMaker.cs +++ b/src/UniTask.NetCore/Linq/_FileMaker.cs @@ -66,41 +66,6 @@ namespace ___Dummy throw new NotImplementedException(); } - public static UniTask AllAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask AllAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask AllAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask AnyAsync(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask AnyAsync(this IUniTaskAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask AnyAwaitAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask AnyAwaitWithCancellationAsync(this IUniTaskAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - public static IUniTaskAsyncEnumerable Append(this IUniTaskAsyncEnumerable source, TSource element) { throw new NotImplementedException(); @@ -122,15 +87,7 @@ namespace ___Dummy throw new NotImplementedException(); } - public static UniTask ContainsAsync(this IUniTaskAsyncEnumerable source, TSource value, IEqualityComparer comparer, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - public static UniTask ContainsAsync(this IUniTaskAsyncEnumerable source, TSource value, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } public static IUniTaskAsyncEnumerable DefaultIfEmpty(this IUniTaskAsyncEnumerable source) { @@ -496,15 +453,6 @@ namespace ___Dummy throw new NotImplementedException(); } - public static UniTask SequenceEqualAsync(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - public static UniTask SequenceEqualAsync(this IUniTaskAsyncEnumerable first, IUniTaskAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } public static IUniTaskAsyncEnumerable Skip(this IUniTaskAsyncEnumerable source, Int32 count) { diff --git a/src/UniTask.NetCoreTests/Linq/AllAny.cs b/src/UniTask.NetCoreTests/Linq/AllAny.cs new file mode 100644 index 0000000..541c3db --- /dev/null +++ b/src/UniTask.NetCoreTests/Linq/AllAny.cs @@ -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(); + } + } +}