First, Last, Single, ElementAt

master
neuecc 2020-05-09 23:22:51 +09:00
parent dd6a8da96f
commit aa8cb80866
14 changed files with 1002 additions and 6315 deletions

View File

@ -1,775 +1,58 @@
namespace Cysharp.Threading.Tasks.Linq using Cysharp.Threading.Tasks.Internal;
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{ {
internal sealed class ElementAt public static partial class UniTaskAsyncEnumerable
{ {
public static UniTask<TSource> ElementAtAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return ElementAt.InvokeAsync(source, index, cancellationToken, false);
} }
public static UniTask<TSource> ElementAtOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return ElementAt.InvokeAsync(source, index, cancellationToken, true);
}
} }
internal static class ElementAt
{
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
int i = 0;
while (await e.MoveNextAsync())
{
if (i++ == index)
{
return e.Current;
}
}
if (defaultIfEmpty)
{
return default;
}
else
{
throw Error.ArgumentOutOfRange(nameof(index));
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
}
}

View File

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

View File

@ -1,775 +1,200 @@
namespace Cysharp.Threading.Tasks.Linq using Cysharp.Threading.Tasks.Internal;
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{ {
internal sealed class First public static partial class UniTaskAsyncEnumerable
{ {
public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return First.InvokeAsync(source, cancellationToken, false);
} }
public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return First.InvokeAsync(source, predicate, cancellationToken, false);
} }
public static UniTask<TSource> FirstAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return First.InvokeAsync(source, predicate, cancellationToken, false);
}
public static UniTask<TSource> FirstAwaitWithCancellationAsync<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 First.InvokeAsync(source, predicate, cancellationToken, false);
}
public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return First.InvokeAsync(source, cancellationToken, true);
}
public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return First.InvokeAsync(source, predicate, cancellationToken, true);
}
public static UniTask<TSource> FirstOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return First.InvokeAsync(source, predicate, cancellationToken, true);
}
public static UniTask<TSource> FirstOrDefaultAwaitWithCancellationAsync<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 First.InvokeAsync(source, predicate, cancellationToken, true);
}
}
internal static class First
{
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
if (await e.MoveNextAsync())
{
return e.Current;
}
else
{
if (defaultIfEmpty)
{
return default;
}
else
{
throw Error.NoElements();
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
var v = e.Current;
if (predicate(v))
{
return v;
}
}
if (defaultIfEmpty)
{
return default;
}
else
{
throw Error.NoElements();
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
var v = e.Current;
if (await predicate(v))
{
return v;
}
}
if (defaultIfEmpty)
{
return default;
}
else
{
throw Error.NoElements();
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
while (await e.MoveNextAsync())
{
var v = e.Current;
if (await predicate(v, cancellationToken))
{
return v;
}
}
if (defaultIfEmpty)
{
return default;
}
else
{
throw Error.NoElements();
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
}
}

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -1,776 +1,230 @@
namespace Cysharp.Threading.Tasks.Linq using Cysharp.Threading.Tasks.Internal;
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{ {
// avoid conflicts with System.Single public static partial class UniTaskAsyncEnumerable
internal sealed class SingleOperator
{ {
public static UniTask<TSource> SingleAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return SingleOperator.InvokeAsync(source, cancellationToken, false);
} }
public static UniTask<TSource> SingleAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return SingleOperator.InvokeAsync(source, predicate, cancellationToken, false);
} }
public static UniTask<TSource> SingleAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return SingleOperator.InvokeAsync(source, predicate, cancellationToken, false);
}
public static UniTask<TSource> SingleAwaitWithCancellationAsync<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 SingleOperator.InvokeAsync(source, predicate, cancellationToken, false);
}
public static UniTask<TSource> SingleOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
return SingleOperator.InvokeAsync(source, cancellationToken, true);
}
public static UniTask<TSource> SingleOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return SingleOperator.InvokeAsync(source, predicate, cancellationToken, true);
}
public static UniTask<TSource> SingleOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
Error.ThrowArgumentNullException(source, nameof(source));
Error.ThrowArgumentNullException(predicate, nameof(predicate));
return SingleOperator.InvokeAsync(source, predicate, cancellationToken, true);
}
public static UniTask<TSource> SingleOrDefaultAwaitWithCancellationAsync<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 SingleOperator.InvokeAsync(source, predicate, cancellationToken, true);
}
}
internal static class SingleOperator
{
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
if (await e.MoveNextAsync())
{
var v = e.Current;
if (!await e.MoveNextAsync())
{
return v;
}
throw Error.MoreThanOneElement();
}
else
{
if (defaultIfEmpty)
{
return default;
}
else
{
throw Error.NoElements();
}
}
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
TSource value = default;
bool found = false;
while (await e.MoveNextAsync())
{
var v = e.Current;
if (predicate(v))
{
if (found)
{
throw Error.MoreThanOneElement();
}
else
{
found = true;
value = v;
}
}
}
if (found || defaultIfEmpty)
{
return value;
}
throw Error.NoElements();
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
TSource value = default;
bool found = false;
while (await e.MoveNextAsync())
{
var v = e.Current;
if (await predicate(v))
{
if (found)
{
throw Error.MoreThanOneElement();
}
else
{
found = true;
value = v;
}
}
}
if (found || defaultIfEmpty)
{
return value;
}
throw Error.NoElements();
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
public static async UniTask<TSource> InvokeAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
{
var e = source.GetAsyncEnumerator(cancellationToken);
try
{
TSource value = default;
bool found = false;
while (await e.MoveNextAsync())
{
var v = e.Current;
if (await predicate(v, cancellationToken))
{
if (found)
{
throw Error.MoreThanOneElement();
}
else
{
found = true;
value = v;
}
}
}
if (found || defaultIfEmpty)
{
return value;
}
throw Error.NoElements();
}
finally
{
if (e != null)
{
await e.DisposeAsync();
}
}
}
}
}

View File

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

View File

@ -223,6 +223,8 @@ namespace Cysharp.Threading.Tasks.Linq
CancellationToken cancellationToken; CancellationToken cancellationToken;
bool useCachedCurrent;
T current;
bool subscribeCompleted; bool subscribeCompleted;
readonly Queue<T> queuedResult; readonly Queue<T> queuedResult;
Exception error; Exception error;
@ -250,7 +252,14 @@ namespace Cysharp.Threading.Tasks.Linq
ExceptionDispatchInfo.Capture(error).Throw(); ExceptionDispatchInfo.Capture(error).Throw();
} }
return queuedResult.Dequeue(); if (useCachedCurrent)
{
return current;
}
current = queuedResult.Dequeue();
useCachedCurrent = true;
return current;
} }
} }
@ -258,6 +267,8 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
lock (queuedResult) lock (queuedResult)
{ {
useCachedCurrent = false;
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
return UniTask.FromCanceled<bool>(cancellationToken); return UniTask.FromCanceled<bool>(cancellationToken);

View File

@ -172,45 +172,7 @@ namespace ___Dummy
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> FirstOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IUniTaskAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer) public static IUniTaskAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IUniTaskAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
@ -403,45 +365,6 @@ namespace ___Dummy
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static UniTask<TSource> LastAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> LastOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<Int64> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) public static UniTask<Int64> LongCountAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{ {
@ -611,46 +534,6 @@ namespace ___Dummy
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static UniTask<TSource> SingleAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static UniTask<TSource> SingleOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public static IUniTaskAsyncEnumerable<TSource> Skip<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count) public static IUniTaskAsyncEnumerable<TSource> Skip<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
{ {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@ -1,4 +1,6 @@
using System; #pragma warning disable 0649
using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading.Tasks.Sources; using System.Threading.Tasks.Sources;
@ -75,7 +77,6 @@ namespace Cysharp.Threading.Tasks
struct UniTaskToValueTask<T> struct UniTaskToValueTask<T>
{ {
public IUniTaskSource<T> source; public IUniTaskSource<T> source;
public T result; public T result;
public short token; public short token;

View File

@ -34,12 +34,13 @@ namespace NetCoreSandbox
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
var x = await new[] { 110, 50, 200 }.ToUniTaskAsyncEnumerable().MinAsync(); var xs = new[] { 1, 10, 100 }.GetEnumerator();
Console.WriteLine(x); while (xs.MoveNext())
// new object[] { }.Min( {
// AsyncEnumerable.MinAwaitAsync( }
Console.WriteLine(xs.MoveNext());
} }

View 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);
}
}
}
}
}

View File

@ -20,16 +20,22 @@ namespace Cysharp.Threading.Tasks.Internal
throw new ArgumentNullException(paramName); throw new ArgumentNullException(paramName);
} }
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Exception ArgumentOutOfRange(string paramName) public static Exception ArgumentOutOfRange(string paramName)
{ {
return new ArgumentOutOfRangeException(paramName); return new ArgumentOutOfRangeException(paramName);
} }
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Exception NoElements() public static Exception NoElements()
{ {
throw new InvalidOperationException("Source sequence doesn't contain any elements."); 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)]