ToArray.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class ToArray : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task ToArray_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToArrayAsync<int>(default).AsTask());
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToArrayAsync<int>(default, CancellationToken.None).AsTask());
  19. }
  20. [Fact]
  21. public async Task ToArray_IAsyncIListProvider_Simple()
  22. {
  23. var xs = new[] { 42, 25, 39 };
  24. var res = xs.ToAsyncEnumerable().ToArrayAsync();
  25. Assert.True((await res).SequenceEqual(xs));
  26. }
  27. [Fact]
  28. public async Task ToArray_IAsyncIListProvider_Empty1()
  29. {
  30. var xs = Array.Empty<int>();
  31. var res = xs.ToAsyncEnumerable().ToArrayAsync();
  32. Assert.True((await res).SequenceEqual(xs));
  33. }
  34. [Fact]
  35. public async Task ToArray_IAsyncIListProvider_Empty2()
  36. {
  37. var xs = new HashSet<int>();
  38. var res = xs.ToAsyncEnumerable().ToArrayAsync();
  39. Assert.True((await res).SequenceEqual(xs));
  40. }
  41. [Fact]
  42. public async Task ToArray_Empty()
  43. {
  44. var xs = AsyncEnumerable.Empty<int>();
  45. var res = xs.ToArrayAsync();
  46. Assert.True((await res).Length == 0);
  47. }
  48. [Fact]
  49. public async Task ToArray_Throw()
  50. {
  51. var ex = new Exception("Bang!");
  52. var res = Throw<int>(ex).ToArrayAsync();
  53. await AssertThrowsAsync(res, ex);
  54. }
  55. [Fact]
  56. public async Task ToArray_Query()
  57. {
  58. var xs = await AsyncEnumerable.Range(5, 50).Take(10).ToArrayAsync();
  59. var ex = new[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
  60. Assert.True(ex.SequenceEqual(xs));
  61. }
  62. [Fact]
  63. public async Task ToArray_Set()
  64. {
  65. var res = new[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
  66. var xs = new HashSet<int>(res);
  67. var arr = await xs.ToAsyncEnumerable().ToArrayAsync();
  68. Assert.True(res.SequenceEqual(arr));
  69. }
  70. }
  71. }