Buffer.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Buffer : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Buffer_Null()
  15. {
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Buffer(default(IAsyncEnumerable<int>), 1));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Buffer(default(IAsyncEnumerable<int>), 1, 1));
  18. AssertThrows<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Buffer(Return42, -1));
  19. AssertThrows<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Buffer(Return42, -1, 1));
  20. AssertThrows<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Buffer(Return42, 1, -1));
  21. }
  22. [Fact]
  23. public void Buffer1()
  24. {
  25. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(2);
  26. var e = xs.GetAsyncEnumerator();
  27. Assert.True(e.MoveNextAsync().Result);
  28. Assert.True(e.Current.SequenceEqual(new[] { 1, 2 }));
  29. Assert.True(e.MoveNextAsync().Result);
  30. Assert.True(e.Current.SequenceEqual(new[] { 3, 4 }));
  31. Assert.True(e.MoveNextAsync().Result);
  32. Assert.True(e.Current.SequenceEqual(new[] { 5 }));
  33. Assert.False(e.MoveNextAsync().Result);
  34. }
  35. [Fact]
  36. public void Buffer2()
  37. {
  38. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(3, 2);
  39. var e = xs.GetAsyncEnumerator();
  40. Assert.True(e.MoveNextAsync().Result);
  41. Assert.True(e.Current.SequenceEqual(new[] { 1, 2, 3 }));
  42. Assert.True(e.MoveNextAsync().Result);
  43. Assert.True(e.Current.SequenceEqual(new[] { 3, 4, 5 }));
  44. Assert.True(e.MoveNextAsync().Result);
  45. Assert.True(e.Current.SequenceEqual(new[] { 5 }));
  46. Assert.False(e.MoveNextAsync().Result);
  47. }
  48. [Fact]
  49. public void Buffer3()
  50. {
  51. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(2, 3);
  52. var e = xs.GetAsyncEnumerator();
  53. Assert.True(e.MoveNextAsync().Result);
  54. Assert.True(e.Current.SequenceEqual(new[] { 1, 2 }));
  55. Assert.True(e.MoveNextAsync().Result);
  56. Assert.True(e.Current.SequenceEqual(new[] { 4, 5 }));
  57. Assert.False(e.MoveNextAsync().Result);
  58. }
  59. [Fact]
  60. public async Task Buffer4()
  61. {
  62. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(3, 2);
  63. await SequenceIdentity(xs);
  64. }
  65. }
  66. }