-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
646 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System; | ||
using Xunit; | ||
using async_enumerable_dotnet; | ||
using System.Threading.Tasks; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
public class GroupByTest | ||
{ | ||
[Fact] | ||
public async void Normal_Same_Group() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.GroupBy(k => 1) | ||
.FlatMap(v => v.ToList()) | ||
.AssertResult( | ||
ListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Normal_Distinct_Group() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.GroupBy(k => k) | ||
.FlatMap(v => v.ToList()) | ||
.AssertResultSet( | ||
ListComparer<int>.Default, | ||
ListOf(1), | ||
ListOf(2), | ||
ListOf(3), | ||
ListOf(4), | ||
ListOf(5), | ||
ListOf(6), | ||
ListOf(7), | ||
ListOf(8), | ||
ListOf(9), | ||
ListOf(10) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Normal_Mixed() | ||
{ | ||
var disposed = 0; | ||
|
||
await AsyncEnumerable.Range(1, 10) | ||
.DoOnDispose(() => disposed++) | ||
.GroupBy(k => k % 2) | ||
.FlatMap(v => v.ToList()) | ||
.AssertResultSet( | ||
ListComparer<int>.Default, | ||
ListOf(1, 3, 5, 7, 9), | ||
ListOf(2, 4, 6, 8, 10) | ||
); | ||
|
||
Assert.Equal(1, disposed); | ||
} | ||
|
||
[Fact] | ||
public async void Normal_Ordered() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.GroupBy(k => k < 6) | ||
.FlatMap(v => v.ToList()) | ||
.AssertResultSet( | ||
ListComparer<int>.Default, | ||
ListOf(1, 2, 3, 4, 5), | ||
ListOf(6, 7, 8, 9, 10) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Take_2_Groups() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.GroupBy(k => k % 3) | ||
.Take(2) | ||
.FlatMap(v => v.ToList()) | ||
.AssertResultSet( | ||
ListComparer<int>.Default, | ||
ListOf(1, 4, 7, 10), | ||
ListOf(2, 5, 8) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Take_1_Of_Each_Group() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.GroupBy(k => k) | ||
.FlatMap(v => v.Take(1)) | ||
.AssertResultSet( | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Take_1_Of_Each_Group_Two_Groups_Total() | ||
{ | ||
var disposed = 0; | ||
|
||
await AsyncEnumerable.Range(1, 10) | ||
.DoOnDispose(() => disposed++) | ||
.GroupBy(k => k) | ||
.Take(2) | ||
.FlatMap(v => v.Take(1)) | ||
.AssertResultSet( | ||
1, 2 | ||
); | ||
|
||
Assert.Equal(1, disposed); | ||
} | ||
|
||
static List<int> ListOf(params int[] values) | ||
{ | ||
return new List<int>(values); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
internal sealed class ListComparer<T> : IEqualityComparer<IList<T>> | ||
{ | ||
internal static readonly ListComparer<T> Default = new ListComparer<T>(); | ||
|
||
public bool Equals(IList<T> x, IList<T> y) | ||
{ | ||
return x.SequenceEqual(y); | ||
} | ||
|
||
public int GetHashCode(IList<T> obj) | ||
{ | ||
var hash = 19; | ||
|
||
unchecked | ||
{ | ||
foreach (var t in obj) | ||
{ | ||
hash = hash * 31 + (t != null ? t.GetHashCode() : 0); | ||
} | ||
} | ||
|
||
return hash; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using Xunit; | ||
using async_enumerable_dotnet; | ||
using System.Threading.Tasks; | ||
using System.Collections.Generic; | ||
using static async_enumerable_dotnet_test.GroupByTest; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
public class TestHelperTest | ||
{ | ||
[Fact] | ||
public async void AssertResultSet() | ||
{ | ||
await AsyncEnumerable.Range(1, 3) | ||
.AssertResultSet(1, 2, 3); | ||
} | ||
|
||
[Fact] | ||
public async void AssertResultSet_List() | ||
{ | ||
await AsyncEnumerable.FromArray(new List<int>(new [] { 1, 2, 3 })) | ||
.AssertResultSet( | ||
ListComparer<int>.Default, | ||
new List<int>(new[] { 1, 2, 3 })); | ||
} | ||
|
||
[Fact] | ||
public void HashSet_Contains() | ||
{ | ||
var set = new HashSet<IList<int>>(ListComparer<int>.Default); | ||
|
||
set.Add(new List<int>(new[] { 1, 2, 3 })); | ||
|
||
Assert.Contains(new List<int>(new[] { 1, 2, 3 }), set); | ||
|
||
Assert.True(set.Remove(new List<int>(new[] { 1, 2, 3 }))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using Xunit; | ||
using async_enumerable_dotnet; | ||
using System.Threading.Tasks; | ||
using System.Collections.Generic; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
public class ToListTest | ||
{ | ||
[Fact] | ||
public async void Normal() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.ToList() | ||
.AssertResult(new List<int>(new[] { 1, 2, 3, 4, 5 })); | ||
} | ||
|
||
[Fact] | ||
public async void Empty() | ||
{ | ||
await AsyncEnumerable.Empty<int>() | ||
.ToList() | ||
.AssertResult(new List<int>()); | ||
} | ||
|
||
[Fact] | ||
public async void Error() | ||
{ | ||
await AsyncEnumerable.Error<int>(new InvalidOperationException()) | ||
.ToList() | ||
.AssertFailure(typeof(InvalidOperationException)); | ||
} | ||
} | ||
} |
Oops, something went wrong.