ToDictionary.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class ToDictionary : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task ToDictionary_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int>(default, x => 0));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync(Return42, default(Func<int, int>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int>(default, x => 0, EqualityComparer<int>.Default));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync(Return42, default(Func<int, int>), EqualityComparer<int>.Default));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(default, x => 0, x => 0));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(Return42, default, x => 0));
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(Return42, x => 0, default));
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(default, x => 0, x => 0, EqualityComparer<int>.Default));
  25. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync(Return42, default, x => 0, EqualityComparer<int>.Default));
  26. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(Return42, x => 0, default, EqualityComparer<int>.Default));
  27. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int>(default, x => 0, CancellationToken.None));
  28. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync(Return42, default(Func<int, int>), CancellationToken.None));
  29. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int>(default, x => 0, EqualityComparer<int>.Default, CancellationToken.None));
  30. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync(Return42, default(Func<int, int>), EqualityComparer<int>.Default, CancellationToken.None));
  31. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(default, x => 0, x => 0, CancellationToken.None));
  32. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(Return42, default, x => 0, CancellationToken.None));
  33. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(Return42, x => 0, default, CancellationToken.None));
  34. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(default, x => 0, x => 0, EqualityComparer<int>.Default, CancellationToken.None));
  35. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync(Return42, default, x => 0, EqualityComparer<int>.Default, CancellationToken.None));
  36. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToDictionaryAsync<int, int, int>(Return42, x => 0, default, EqualityComparer<int>.Default, CancellationToken.None));
  37. }
  38. [Fact]
  39. public async Task ToDictionary1Async()
  40. {
  41. var xs = new[] { 1, 4 }.ToAsyncEnumerable();
  42. var res = await xs.ToDictionaryAsync(x => x % 2);
  43. Assert.True(res[0] == 4);
  44. Assert.True(res[1] == 1);
  45. }
  46. [Fact]
  47. public async Task ToDictionary2Async()
  48. {
  49. var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
  50. await AssertThrowsAsync<ArgumentException>(xs.ToDictionaryAsync(x => x % 2));
  51. }
  52. [Fact]
  53. public async Task ToDictionary3Async()
  54. {
  55. var xs = new[] { 1, 4 }.ToAsyncEnumerable();
  56. var res = await xs.ToDictionaryAsync(x => x % 2, x => x + 1);
  57. Assert.True(res[0] == 5);
  58. Assert.True(res[1] == 2);
  59. }
  60. [Fact]
  61. public async Task ToDictionary4Async()
  62. {
  63. var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
  64. await AssertThrowsAsync<ArgumentException>(xs.ToDictionaryAsync(x => x % 2, x => x + 1));
  65. }
  66. [Fact]
  67. public async Task ToDictionary5Async()
  68. {
  69. var xs = new[] { 1, 4 }.ToAsyncEnumerable();
  70. var res = await xs.ToDictionaryAsync(x => x % 2, new Eq());
  71. Assert.True(res[0] == 4);
  72. Assert.True(res[1] == 1);
  73. }
  74. [Fact]
  75. public async Task ToDictionary6Async()
  76. {
  77. var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable();
  78. await AssertThrowsAsync<ArgumentException>(xs.ToDictionaryAsync(x => x % 2, new Eq()));
  79. }
  80. [Fact]
  81. public async Task ToDictionary7Async()
  82. {
  83. var xs = new[] { 1, 4 }.ToAsyncEnumerable();
  84. var res = await xs.ToDictionaryAsync(x => x % 2, x => x, new Eq());
  85. Assert.True(res[0] == 4);
  86. Assert.True(res[1] == 1);
  87. }
  88. private sealed class Eq : IEqualityComparer<int>
  89. {
  90. public bool Equals(int x, int y)
  91. {
  92. return EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));
  93. }
  94. public int GetHashCode(int obj)
  95. {
  96. return EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
  97. }
  98. }
  99. }
  100. }