ToArray.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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;
  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));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToArrayAsync<int>(default, CancellationToken.None));
  19. }
  20. [Fact]
  21. public async Task ToArray1()
  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 ToArray2()
  29. {
  30. var xs = AsyncEnumerable.Empty<int>();
  31. var res = xs.ToArrayAsync();
  32. Assert.True((await res).Length == 0);
  33. }
  34. [Fact]
  35. public async Task ToArray3Async()
  36. {
  37. var ex = new Exception("Bang!");
  38. var res = Throw<int>(ex).ToArrayAsync();
  39. await AssertThrowsAsync(res, ex);
  40. }
  41. [Fact]
  42. public async Task ToArray4()
  43. {
  44. var xs = await AsyncEnumerable.Range(5, 50).Take(10).ToArrayAsync();
  45. var ex = new[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
  46. Assert.True(ex.SequenceEqual(xs));
  47. }
  48. [Fact]
  49. public async Task ToArray5()
  50. {
  51. var res = new[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
  52. var xs = new HashSet<int>(res);
  53. var arr = await xs.ToAsyncEnumerable().ToArrayAsync();
  54. Assert.True(res.SequenceEqual(arr));
  55. }
  56. }
  57. }