Select.cs 3.2 KB

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