RepeatTest.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.Text;
  7. using System.Linq;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class RepeatTest : Tests
  12. {
  13. [Fact]
  14. public void RepeatElementInfinite()
  15. {
  16. var xs = EnumerableEx.Repeat(42).Take(1000);
  17. Assert.True(xs.All(x => x == 42));
  18. Assert.True(xs.Count() == 1000);
  19. }
  20. [Fact]
  21. public void RepeatSequence_Arguments()
  22. {
  23. AssertThrows<ArgumentNullException>(() => EnumerableEx.Repeat<int>(null));
  24. AssertThrows<ArgumentNullException>(() => EnumerableEx.Repeat<int>(null, 5));
  25. AssertThrows<ArgumentOutOfRangeException>(() => EnumerableEx.Repeat<int>(new[] { 1 }, -1));
  26. }
  27. [Fact]
  28. public void RepeatSequence1()
  29. {
  30. var i = 0;
  31. var xs = new[] { 1, 2 }.Do(_ => i++).Repeat();
  32. var res = xs.Take(10).ToList();
  33. Assert.Equal(10, res.Count);
  34. Assert.True(res.Buffer(2).Select(b => b.Sum()).All(x => x == 3));
  35. Assert.Equal(10, i);
  36. }
  37. [Fact]
  38. public void RepeatSequence2()
  39. {
  40. var i = 0;
  41. var xs = new[] { 1, 2 }.Do(_ => i++).Repeat(5);
  42. var res = xs.ToList();
  43. Assert.Equal(10, res.Count);
  44. Assert.True(res.Buffer(2).Select(b => b.Sum()).All(x => x == 3));
  45. Assert.Equal(10, i);
  46. }
  47. }
  48. }