Select.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.Linq;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Select : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Select_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Select<int, int>(default, x => x));
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Select<int, int>(default, (x, i) => x));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Select(Return42, default(Func<int, int>)));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Select(Return42, default(Func<int, int, int>)));
  19. }
  20. [Fact]
  21. public async Task Select1()
  22. {
  23. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  24. var ys = xs.Select(x => (char)('a' + x));
  25. var e = ys.GetAsyncEnumerator();
  26. await HasNextAsync(e, 'a');
  27. await HasNextAsync(e, 'b');
  28. await HasNextAsync(e, 'c');
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task Select2()
  33. {
  34. var xs = new[] { 8, 5, 7 }.ToAsyncEnumerable();
  35. var ys = xs.Select((x, i) => (char)('a' + i));
  36. var e = ys.GetAsyncEnumerator();
  37. await HasNextAsync(e, 'a');
  38. await HasNextAsync(e, 'b');
  39. await HasNextAsync(e, 'c');
  40. await NoNextAsync(e);
  41. }
  42. [Fact]
  43. public async Task Select3Async()
  44. {
  45. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  46. var ys = xs.Select(x => 1 / x);
  47. var e = ys.GetAsyncEnumerator();
  48. await AssertThrowsAsync<DivideByZeroException>(e.MoveNextAsync().AsTask());
  49. }
  50. [Fact]
  51. public async Task Select4Async()
  52. {
  53. var xs = new[] { 8, 5, 7 }.ToAsyncEnumerable();
  54. var ys = xs.Select((x, i) => 1 / i);
  55. var e = ys.GetAsyncEnumerator();
  56. await AssertThrowsAsync<DivideByZeroException>(e.MoveNextAsync().AsTask());
  57. }
  58. [Fact]
  59. public async Task Select5()
  60. {
  61. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  62. var ys = xs.Select(i => i + 3).Select(x => (char)('a' + x));
  63. var e = ys.GetAsyncEnumerator();
  64. await HasNextAsync(e, 'd');
  65. await HasNextAsync(e, 'e');
  66. await HasNextAsync(e, 'f');
  67. await NoNextAsync(e);
  68. }
  69. [Fact]
  70. public async Task Select7()
  71. {
  72. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  73. var ys = xs.Select(x => (char)('a' + x));
  74. await SequenceIdentity(ys);
  75. }
  76. [Fact]
  77. public async Task Select8()
  78. {
  79. var xs = new[] { 8, 5, 7 }.ToAsyncEnumerable();
  80. var ys = xs.Select((x, i) => (char)('a' + i));
  81. await SequenceIdentity(ys);
  82. }
  83. }
  84. }