Range.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Linq;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Range : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Range_Null()
  14. {
  15. Assert.Throws<ArgumentOutOfRangeException>(() => AsyncEnumerable.Range(0, -1));
  16. Assert.Throws<ArgumentOutOfRangeException>(() => AsyncEnumerable.Range(1024, int.MaxValue - 1022));
  17. }
  18. [Fact]
  19. public async Task Range_Simple()
  20. {
  21. var xs = AsyncEnumerable.Range(2, 5);
  22. var e = xs.GetAsyncEnumerator();
  23. await HasNextAsync(e, 2);
  24. await HasNextAsync(e, 3);
  25. await HasNextAsync(e, 4);
  26. await HasNextAsync(e, 5);
  27. await HasNextAsync(e, 6);
  28. await NoNextAsync(e);
  29. }
  30. [Fact]
  31. public async Task Range_Simple_IAsyncPartition()
  32. {
  33. var xs = AsyncEnumerable.Range(2, 5);
  34. int[] expected = [2, 3, 4, 5, 6];
  35. Assert.Equal(5, await xs.CountAsync());
  36. Assert.Equal(2, await xs.FirstAsync());
  37. Assert.Equal(6, await xs.LastAsync());
  38. Assert.Equal(2, await xs.ElementAtAsync(0));
  39. Assert.Equal(3, await xs.ElementAtAsync(1));
  40. Assert.Equal(4, await xs.ElementAtAsync(2));
  41. Assert.Equal(5, await xs.ElementAtAsync(3));
  42. Assert.Equal(6, await xs.ElementAtAsync(4));
  43. await AssertThrowsAsync<ArgumentOutOfRangeException>(xs.ElementAtAsync(5).AsTask());
  44. Assert.Equal(2, await xs.Skip(0).FirstAsync());
  45. Assert.Equal(6, await xs.Skip(0).LastAsync());
  46. Assert.Equal(3, await xs.Skip(1).FirstAsync());
  47. Assert.Equal(6, await xs.Skip(1).LastAsync());
  48. Assert.Equal(0, await xs.Skip(5).CountAsync());
  49. Assert.Equal(0, await xs.Skip(1024).CountAsync());
  50. Assert.Equal(2, await xs.Take(4).FirstAsync());
  51. Assert.Equal(5, await xs.Take(4).LastAsync());
  52. Assert.Equal(2, await xs.Take(5).FirstAsync());
  53. Assert.Equal(6, await xs.Take(5).LastAsync());
  54. Assert.Equal(2, await xs.Take(1024).FirstAsync());
  55. Assert.Equal(6, await xs.Take(1024).LastAsync());
  56. Assert.Equal(expected, await xs.ToArrayAsync());
  57. Assert.Equal(expected, await xs.ToListAsync());
  58. }
  59. [Fact]
  60. public async Task Range_Empty()
  61. {
  62. var xs = AsyncEnumerable.Range(2, 0);
  63. var e = xs.GetAsyncEnumerator();
  64. await NoNextAsync(e);
  65. }
  66. [Fact]
  67. public async Task Range_Empty_IAsyncPartition()
  68. {
  69. var xs = AsyncEnumerable.Range(2, 0);
  70. Assert.Equal(0, await xs.CountAsync());
  71. await AssertThrowsAsync<InvalidOperationException>(xs.FirstAsync().AsTask());
  72. await AssertThrowsAsync<InvalidOperationException>(xs.LastAsync().AsTask());
  73. await AssertThrowsAsync<ArgumentOutOfRangeException>(xs.ElementAtAsync(0).AsTask());
  74. Assert.Empty(await xs.ToArrayAsync());
  75. Assert.Empty(await xs.ToListAsync());
  76. }
  77. }
  78. }