Range.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Assert.Equal(5, await xs.CountAsync());
  35. Assert.Equal(2, await xs.FirstAsync());
  36. Assert.Equal(6, await xs.LastAsync());
  37. Assert.Equal(2, await xs.ElementAtAsync(0));
  38. Assert.Equal(3, await xs.ElementAtAsync(1));
  39. Assert.Equal(4, await xs.ElementAtAsync(2));
  40. Assert.Equal(5, await xs.ElementAtAsync(3));
  41. Assert.Equal(6, await xs.ElementAtAsync(4));
  42. await AssertThrowsAsync<ArgumentOutOfRangeException>(xs.ElementAtAsync(5).AsTask());
  43. Assert.Equal(2, await xs.Skip(0).FirstAsync());
  44. Assert.Equal(6, await xs.Skip(0).LastAsync());
  45. Assert.Equal(3, await xs.Skip(1).FirstAsync());
  46. Assert.Equal(6, await xs.Skip(1).LastAsync());
  47. Assert.Equal(0, await xs.Skip(5).CountAsync());
  48. Assert.Equal(0, await xs.Skip(1024).CountAsync());
  49. Assert.Equal(2, await xs.Take(4).FirstAsync());
  50. Assert.Equal(5, await xs.Take(4).LastAsync());
  51. Assert.Equal(2, await xs.Take(5).FirstAsync());
  52. Assert.Equal(6, await xs.Take(5).LastAsync());
  53. Assert.Equal(2, await xs.Take(1024).FirstAsync());
  54. Assert.Equal(6, await xs.Take(1024).LastAsync());
  55. Assert.Equal([2, 3, 4, 5, 6], await xs.ToArrayAsync());
  56. Assert.Equal(new[] { 2, 3, 4, 5, 6 }, await xs.ToListAsync());
  57. }
  58. [Fact]
  59. public async Task Range_Empty()
  60. {
  61. var xs = AsyncEnumerable.Range(2, 0);
  62. var e = xs.GetAsyncEnumerator();
  63. await NoNextAsync(e);
  64. }
  65. [Fact]
  66. public async Task Range_Empty_IAsyncPartition()
  67. {
  68. var xs = AsyncEnumerable.Range(2, 0);
  69. Assert.Equal(0, await xs.CountAsync());
  70. await AssertThrowsAsync<InvalidOperationException>(xs.FirstAsync().AsTask());
  71. await AssertThrowsAsync<InvalidOperationException>(xs.LastAsync().AsTask());
  72. await AssertThrowsAsync<ArgumentOutOfRangeException>(xs.ElementAtAsync(0).AsTask());
  73. Assert.Empty(await xs.ToArrayAsync());
  74. Assert.Empty(await xs.ToListAsync());
  75. }
  76. }
  77. }