Scan.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 Scan : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Scan_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Scan(default(IAsyncEnumerable<int>), 3, (x, y) => x + y));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Scan(Return42, 3, default(Func<int, int, int>)));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Scan(default(IAsyncEnumerable<int>), (x, y) => x + y));
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Scan(Return42, default(Func<int, int, int>)));
  20. }
  21. [Fact]
  22. public async Task Scan1Async()
  23. {
  24. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, (x, y) => x + y);
  25. var e = xs.GetAsyncEnumerator();
  26. await HasNextAsync(e, 9);
  27. await HasNextAsync(e, 11);
  28. await HasNextAsync(e, 14);
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task Scan2Async()
  33. {
  34. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan((x, y) => x + y);
  35. var e = xs.GetAsyncEnumerator();
  36. await HasNextAsync(e, 3);
  37. await HasNextAsync(e, 6);
  38. await NoNextAsync(e);
  39. }
  40. [Fact]
  41. public async Task Scan3()
  42. {
  43. var ex = new Exception("Bang!");
  44. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, new Func<int, int, int>((x, y) => { throw ex; }));
  45. var e = xs.GetAsyncEnumerator();
  46. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  47. }
  48. [Fact]
  49. public async Task Scan4()
  50. {
  51. var ex = new Exception("Bang!");
  52. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(new Func<int, int, int>((x, y) => { throw ex; }));
  53. var e = xs.GetAsyncEnumerator();
  54. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  55. }
  56. [Fact]
  57. public async Task Scan5()
  58. {
  59. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, (x, y) => x + y);
  60. await SequenceIdentity(xs);
  61. }
  62. [Fact]
  63. public async Task Scan6()
  64. {
  65. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan((x, y) => x + y);
  66. await SequenceIdentity(xs);
  67. }
  68. }
  69. }