Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ToImmutable*Async extension methods #1545

Merged
merged 11 commits into from
Aug 28, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Tests
{
public class ToImmutableArray : AsyncEnumerableTests
{
[Fact]
public async Task ToImmutableArray_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableArrayAsync<int>(default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableArrayAsync<int>(default, CancellationToken.None).AsTask());
}

[Fact]
public async Task ToImmutableArray_IAsyncIListProvider_Simple()
{
var xs = new[] { 42, 25, 39 };
var res = xs.ToAsyncEnumerable().ToImmutableArrayAsync();
Assert.True((await res).SequenceEqual(xs));
}

[Fact]
public async Task ToImmutableArray_IAsyncIListProvider_Empty1()
{
var xs = new int[0];
var res = xs.ToAsyncEnumerable().ToImmutableArrayAsync();
Assert.True((await res).SequenceEqual(xs));
}

[Fact]
public async Task ToImmutableArray_IAsyncIListProvider_Empty2()
{
var xs = new HashSet<int>();
var res = xs.ToAsyncEnumerable().ToImmutableArrayAsync();
Assert.True((await res).SequenceEqual(xs));
}

[Fact]
public async Task ToImmutableArray_Empty()
{
var xs = AsyncEnumerable.Empty<int>();
var res = xs.ToImmutableArrayAsync();
Assert.True((await res).Length == 0);
}

[Fact]
public async Task ToImmutableArray_Throw()
{
var ex = new Exception("Bang!");
var res = Throw<int>(ex).ToImmutableArrayAsync();
await AssertThrowsAsync(res, ex);
}

[Fact]
public async Task ToImmutableArray_Query()
{
var xs = await AsyncEnumerable.Range(5, 50).Take(10).ToImmutableArrayAsync();
var ex = new[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };

Assert.True(ex.SequenceEqual(xs));
}

[Fact]
public async Task ToImmutableArray_Set()
{
var res = new[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
var xs = new HashSet<int>(res);

var arr = await xs.ToAsyncEnumerable().ToImmutableArrayAsync();

Assert.True(res.SequenceEqual(arr));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Tests
{
public class ToImmutableDictionary : AsyncEnumerableTests
{
[Fact]
public async Task ToImmutableDictionary_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int>(default, x => 0).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync(Return42, default(Func<int, int>)).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int>(default, x => 0, EqualityComparer<int>.Default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync(Return42, default, EqualityComparer<int>.Default).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(default, x => 0, x => 0).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(Return42, default, x => 0).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(Return42, x => 0, default).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(default, x => 0, x => 0, EqualityComparer<int>.Default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync(Return42, default, x => 0, EqualityComparer<int>.Default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(Return42, x => 0, default, EqualityComparer<int>.Default).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int>(default, x => 0, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync(Return42, default(Func<int, int>), CancellationToken.None).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int>(default, x => 0, EqualityComparer<int>.Default, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync(Return42, default, EqualityComparer<int>.Default, CancellationToken.None).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(default, x => 0, x => 0, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(Return42, default, x => 0, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(Return42, x => 0, default, CancellationToken.None).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(default, x => 0, x => 0, EqualityComparer<int>.Default, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync(Return42, default, x => 0, EqualityComparer<int>.Default, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableDictionaryAsync<int, int, int>(Return42, x => 0, default, EqualityComparer<int>.Default, CancellationToken.None).AsTask());
}

[Fact]
public async Task ToImmutableDictionary1Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableDictionaryAsync(x => x % 2);
Assert.True(res[0] == 4);
Assert.True(res[1] == 1);
}

[Fact]
public async Task ToImmutableDictionary2Async()
{
var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
await AssertThrowsAsync<ArgumentException>(xs.ToImmutableDictionaryAsync(x => x % 2).AsTask());
}

[Fact]
public async Task ToImmutableDictionary3Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableDictionaryAsync(x => x % 2, x => x + 1);
Assert.True(res[0] == 5);
Assert.True(res[1] == 2);
}

[Fact]
public async Task ToImmutableDictionary4Async()
{
var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
await AssertThrowsAsync<ArgumentException>(xs.ToImmutableDictionaryAsync(x => x % 2, x => x + 1).AsTask());
}

[Fact]
public async Task ToImmutableDictionary5Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableDictionaryAsync(x => x % 2, new Eq());
Assert.True(res[0] == 4);
Assert.True(res[1] == 1);
}

[Fact]
public async Task ToImmutableDictionary6Async()
{
var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
await AssertThrowsAsync<ArgumentException>(xs.ToImmutableDictionaryAsync(x => x % 2, new Eq()).AsTask());
}

[Fact]
public async Task ToImmutableDictionary7Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableDictionaryAsync(x => x % 2, x => x, new Eq());
Assert.True(res[0] == 4);
Assert.True(res[1] == 1);
}

private sealed class Eq : IEqualityComparer<int>
{
public bool Equals(int x, int y) => EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));

public int GetHashCode(int obj) => EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Tests
{
public class ToImmutableHashSet : AsyncEnumerableTests
{
[Fact]
public async Task ToImmutableHashSet_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableHashSetAsync<int>(default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableHashSetAsync<int>(default, CancellationToken.None).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableHashSetAsync(default, EqualityComparer<int>.Default, CancellationToken.None).AsTask());
}

[Fact]
public async Task ToImmutableHashSet_Simple()
{
var xs = new[] { 1, 2, 1, 2, 3, 4, 1, 2, 3, 4 };
var res = xs.ToAsyncEnumerable().ToImmutableHashSetAsync();
Assert.True((await res).OrderBy(x => x).SequenceEqual(new[] { 1, 2, 3, 4 }));
}

[Fact]
public async Task ToImmutableHashSet_Comparer()
{
var xs = new[] { 1, 12, 11, 2, 3, 14, 1, 12, 13, 4 };
var res = xs.ToAsyncEnumerable().ToImmutableHashSetAsync(new Eq());
Assert.True((await res).OrderBy(x => x).SequenceEqual(new[] { 1, 3, 12, 14 }));
}

private class Eq : IEqualityComparer<int>
{
public bool Equals(int x, int y) => x % 10 == y % 10;

public int GetHashCode(int obj) => obj % 10;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Tests
{
public class ToImmutableList : AsyncEnumerableTests
{
[Fact]
public async Task ToImmutableList_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableListAsync<int>(default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableListAsync<int>(default, CancellationToken.None).AsTask());
}

[Fact]
public async Task ToImmutableList_Simple()
{
var xs = new[] { 42, 25, 39 };
var res = xs.ToAsyncEnumerable().ToImmutableListAsync();
Assert.True((await res).SequenceEqual(xs));
}

[Fact]
public async Task ToImmutableList_Empty()
{
var xs = AsyncEnumerable.Empty<int>();
var res = xs.ToImmutableListAsync();
Assert.True((await res).Count == 0);
}

[Fact]
public async Task ToImmutableList_Throw()
{
var ex = new Exception("Bang!");
var res = Throw<int>(ex).ToImmutableListAsync();
await AssertThrowsAsync(res, ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Tests
{
public class ToImmutableSortedDictionary : AsyncEnumerableTests
{
[Fact]
public async Task ToImmutableSortedDictionary_Null()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(default, x => 0, x => 0).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(Return42, default, x => 0).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(Return42, x => 0, default).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(default, x => 0, x => 0, Comparer<int>.Default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync(Return42, default, x => 0, Comparer<int>.Default).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(Return42, x => 0, default, Comparer<int>.Default).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(default, x => 0, x => 0, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(Return42, default, x => 0, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(Return42, x => 0, default, CancellationToken.None).AsTask());

await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(default, x => 0, x => 0, Comparer<int>.Default, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync(Return42, default, x => 0, Comparer<int>.Default, CancellationToken.None).AsTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToImmutableSortedDictionaryAsync<int, int, int>(Return42, x => 0, default, Comparer<int>.Default, CancellationToken.None).AsTask());
}

[Fact]
public async Task ToImmutableSortedDictionary1Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x);
Assert.True(res[0] == 4);
Assert.True(res[1] == 1);
}

[Fact]
public async Task ToImmutableSortedDictionary2Async()
{
var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
await AssertThrowsAsync<ArgumentException>(xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x).AsTask());
}

[Fact]
public async Task ToImmutableSortedDictionary3Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x + 1);
Assert.True(res[0] == 5);
Assert.True(res[1] == 2);
}

[Fact]
public async Task ToImmutableSortedDictionary4Async()
{
var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
await AssertThrowsAsync<ArgumentException>(xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x + 1).AsTask());
}

[Fact]
public async Task ToImmutableSortedDictionary5Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x, new Eq());
Assert.True(res[0] == 4);
Assert.True(res[1] == 1);
}

[Fact]
public async Task ToImmutableSortedDictionary6Async()
{
var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
await AssertThrowsAsync<ArgumentException>(xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x, new Eq()).AsTask());
}

[Fact]
public async Task ToImmutableSortedDictionary7Async()
{
var xs = new[] { 1, 4 }.ToAsyncEnumerable();
var res = await xs.ToImmutableSortedDictionaryAsync(x => x % 2, x => x, new Eq());
Assert.True(res[0] == 4);
Assert.True(res[1] == 1);
}

[Fact]
public async Task ToImmutableSortedDictionary8Async()
{
var xs = new[] { 5, 8, 7, 1, 9 }.ToAsyncEnumerable();
var res = await xs.ToImmutableSortedDictionaryAsync(x => x, x => x);
Assert.Equal(new[] { 1, 5, 7, 8, 9 }, res.Keys);
}

private sealed class Eq : IComparer<int>
{
public int Compare(int x, int y) => Comparer<int>.Default.Compare(Math.Abs(x), Math.Abs(y));
}
}
}
Loading