// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #if !NO_TPL using System; using System.Collections.Generic; using System.Linq; using System.Text; #if NUNIT using NUnit.Framework; using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; using TestMethodAttribute = NUnit.Framework.TestAttribute; using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; #else using Microsoft.VisualStudio.TestTools.UnitTesting; #endif using System.Threading; namespace Tests { public partial class AsyncTests { [TestMethod] public void Select_Null() { AssertThrows(() => AsyncEnumerable.Select(null, x => x)); AssertThrows(() => AsyncEnumerable.Select(null, (x, i) => x)); AssertThrows(() => AsyncEnumerable.Select(AsyncEnumerable.Return(42), default(Func))); AssertThrows(() => AsyncEnumerable.Select(AsyncEnumerable.Return(42), default(Func))); } [TestMethod] public void Select1() { var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable(); var ys = xs.Select(x => (char)('a' + x)); var e = ys.GetEnumerator(); HasNext(e, 'a'); HasNext(e, 'b'); HasNext(e, 'c'); NoNext(e); } [TestMethod] public void Select2() { var xs = new[] { 8, 5, 7 }.ToAsyncEnumerable(); var ys = xs.Select((x, i) => (char)('a' + i)); var e = ys.GetEnumerator(); HasNext(e, 'a'); HasNext(e, 'b'); HasNext(e, 'c'); NoNext(e); } [TestMethod] public void Select3() { var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable(); var ys = xs.Select(x => 1 / x); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is DivideByZeroException); } [TestMethod] public void Select4() { var xs = new[] { 8, 5, 7 }.ToAsyncEnumerable(); var ys = xs.Select((x, i) => 1 / i); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is DivideByZeroException); } [TestMethod] public void Where_Null() { AssertThrows(() => AsyncEnumerable.Where(null, x => true)); AssertThrows(() => AsyncEnumerable.Where(null, (x, i) => true)); AssertThrows(() => AsyncEnumerable.Where(AsyncEnumerable.Return(42), default(Func))); AssertThrows(() => AsyncEnumerable.Where(AsyncEnumerable.Return(42), default(Func))); } [TestMethod] public void Where1() { var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable(); var ys = xs.Where(x => x % 2 == 0); var e = ys.GetEnumerator(); HasNext(e, 8); HasNext(e, 4); HasNext(e, 6); HasNext(e, 2); HasNext(e, 0); NoNext(e); } [TestMethod] public void Where2() { var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable(); var ys = xs.Where((x, i) => i % 2 == 0); var e = ys.GetEnumerator(); HasNext(e, 8); HasNext(e, 7); HasNext(e, 6); HasNext(e, 2); HasNext(e, 0); NoNext(e); } [TestMethod] public void Where3() { var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable(); var ex = new Exception("Bang"); var ys = xs.Where(x => { if (x == 4) throw ex; return true; }); var e = ys.GetEnumerator(); HasNext(e, 8); HasNext(e, 5); HasNext(e, 7); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Where4() { var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable(); var ex = new Exception("Bang"); var ys = xs.Where((x, i) => { if (i == 3) throw ex; return true; }); var e = ys.GetEnumerator(); HasNext(e, 8); HasNext(e, 5); HasNext(e, 7); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Where5() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Where(x => true); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Where6() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Where((x, i) => true); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany_Null() { AssertThrows(() => AsyncEnumerable.SelectMany(null, x => null)); AssertThrows(() => AsyncEnumerable.SelectMany(null, (x, i) => null)); AssertThrows(() => AsyncEnumerable.SelectMany(AsyncEnumerable.Return(42), default(Func>))); AssertThrows(() => AsyncEnumerable.SelectMany(AsyncEnumerable.Return(42), default(Func>))); AssertThrows(() => AsyncEnumerable.SelectMany(null, x => null, (x, y) => x)); AssertThrows(() => AsyncEnumerable.SelectMany(null, (x, i) => null, (x, y) => x)); AssertThrows(() => AsyncEnumerable.SelectMany(AsyncEnumerable.Return(42), default(Func>), (x, y) => x)); AssertThrows(() => AsyncEnumerable.SelectMany(AsyncEnumerable.Return(42), default(Func>), (x, y) => x)); AssertThrows(() => AsyncEnumerable.SelectMany(AsyncEnumerable.Return(42), x => null, default(Func))); AssertThrows(() => AsyncEnumerable.SelectMany(AsyncEnumerable.Return(42), (x, i) => null, default(Func))); } [TestMethod] public void SelectMany1() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany(x => Enumerable.Range(0, x).ToAsyncEnumerable()); var e = ys.GetEnumerator(); HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); HasNext(e, 0); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void SelectMany2() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany(x => { if (x < 3) return Enumerable.Range(0, x).ToAsyncEnumerable(); else return AsyncEnumerable.Throw(ex); }); var e = ys.GetEnumerator(); HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany3() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.SelectMany(x => Enumerable.Range(0, x).ToAsyncEnumerable()); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany4() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany(x => { if (x < 3) return Enumerable.Range(0, x).ToAsyncEnumerable(); else throw ex; }); var e = ys.GetEnumerator(); HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany5() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany((x, i) => Enumerable.Range(i + 5, x).ToAsyncEnumerable()); var e = ys.GetEnumerator(); HasNext(e, 5); HasNext(e, 6); HasNext(e, 7); HasNext(e, 7); HasNext(e, 8); HasNext(e, 9); NoNext(e); } [TestMethod] public void SelectMany6() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany((x, i) => { if (i < 2) return Enumerable.Range(0, x).ToAsyncEnumerable(); else return AsyncEnumerable.Throw(ex); }); var e = ys.GetEnumerator(); HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany7() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.SelectMany((x, i) => Enumerable.Range(0, x).ToAsyncEnumerable()); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany8() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany((x, i) => { if (i < 2) return Enumerable.Range(0, x).ToAsyncEnumerable(); else throw ex; }); var e = ys.GetEnumerator(); HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany9() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany(x => Enumerable.Range(3, x).ToAsyncEnumerable(), (x, y) => x * y); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 6); HasNext(e, 8); HasNext(e, 9); HasNext(e, 12); HasNext(e, 15); NoNext(e); } [TestMethod] public void SelectMany10() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany((x, i) => Enumerable.Range(i + 3, x).ToAsyncEnumerable(), (x, y) => x * y); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 8); HasNext(e, 10); HasNext(e, 15); HasNext(e, 18); HasNext(e, 21); NoNext(e); } [TestMethod] public void SelectMany11() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany(x => Enumerable.Range(3, x).ToAsyncEnumerable(), (x, y) => { if (x * y > 10) throw ex; return x * y; }); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 6); HasNext(e, 8); HasNext(e, 9); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SelectMany12() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.SelectMany((x, i) => Enumerable.Range(i + 3, x).ToAsyncEnumerable(), (x, y) => { if (x * y > 10) throw ex; return x * y; }); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 8); HasNext(e, 10); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void OfType_Null() { AssertThrows(() => AsyncEnumerable.OfType(null)); } [TestMethod] public void OfType() { var xs = new object[] { 1, 1.2, true, 4, "" }.ToAsyncEnumerable(); var ys = xs.OfType(); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 4); NoNext(e); } [TestMethod] public void Cast_Null() { AssertThrows(() => AsyncEnumerable.Cast(null)); } [TestMethod] public void Cast() { var xs = new object[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Cast(); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void Do_Null() { AssertThrows(() => AsyncEnumerable.Do(null, x => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), default(Action))); AssertThrows(() => AsyncEnumerable.Do(null, x => { }, () => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), default(Action), () => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), x => { }, default(Action))); AssertThrows(() => AsyncEnumerable.Do(null, x => { }, ex => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), default(Action), ex => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), x => { }, default(Action))); AssertThrows(() => AsyncEnumerable.Do(null, x => { }, ex => { }, () => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), default(Action), ex => { }, () => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), x => { }, default(Action), () => { })); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), x => { }, ex => { }, default(Action))); AssertThrows(() => AsyncEnumerable.Do(null, new MyObs())); AssertThrows(() => AsyncEnumerable.Do(AsyncEnumerable.Return(42), default(IObserver))); } class MyObs : IObserver { public void OnCompleted() { throw new NotImplementedException(); } public void OnError(Exception error) { throw new NotImplementedException(); } public void OnNext(int value) { throw new NotImplementedException(); } } [TestMethod] public void Do1() { var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Do(x => sum += x); var e = ys.GetEnumerator(); HasNext(e, 1); Assert.AreEqual(1, sum); HasNext(e, 2); Assert.AreEqual(3, sum); HasNext(e, 3); Assert.AreEqual(6, sum); HasNext(e, 4); Assert.AreEqual(10, sum); NoNext(e); } [TestMethod] public void Do2() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Do(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Do3() { var sum = 0; var fail = false; var done = false; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Do(x => sum += x, ex => { fail = true; }, () => { done = true; }); var e = ys.GetEnumerator(); HasNext(e, 1); Assert.AreEqual(1, sum); HasNext(e, 2); Assert.AreEqual(3, sum); HasNext(e, 3); Assert.AreEqual(6, sum); HasNext(e, 4); Assert.AreEqual(10, sum); NoNext(e); Assert.IsFalse(fail); Assert.IsTrue(done); } [TestMethod] public void Do4() { var sum = 0; var done = false; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Do(x => sum += x, () => { done = true; }); var e = ys.GetEnumerator(); HasNext(e, 1); Assert.AreEqual(1, sum); HasNext(e, 2); Assert.AreEqual(3, sum); HasNext(e, 3); Assert.AreEqual(6, sum); HasNext(e, 4); Assert.AreEqual(10, sum); NoNext(e); Assert.IsTrue(done); } [TestMethod] public void Do5() { var ex = new Exception("Bang"); var exa = default(Exception); var done = false; var hasv = false; var xs = AsyncEnumerable.Throw(ex); var ys = xs.Do(x => { hasv = true; }, exx => { exa = exx; }, () => { done = true; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); Assert.IsFalse(hasv); Assert.IsFalse(done); Assert.AreSame(((AggregateException)exa).Flatten().InnerExceptions.Single(), ex); } [TestMethod] public void Do6() { var ex = new Exception("Bang"); var exa = default(Exception); var hasv = false; var xs = AsyncEnumerable.Throw(ex); var ys = xs.Do(x => { hasv = true; }, exx => { exa = exx; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); Assert.IsFalse(hasv); Assert.AreSame(((AggregateException)exa).Flatten().InnerExceptions.Single(), ex); } [TestMethod] public void ForEachAsync_Null() { AssertThrows(() => AsyncEnumerable.ForEachAsync(null, x => { })); AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action))); AssertThrows(() => AsyncEnumerable.ForEachAsync(null, (x, i) => { })); AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action))); AssertThrows(() => AsyncEnumerable.ForEachAsync(null, x => { }, CancellationToken.None)); AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); AssertThrows(() => AsyncEnumerable.ForEachAsync(null, (x, i) => { }, CancellationToken.None)); AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); } [TestMethod] public void ForEach_Null() { AssertThrows(() => AsyncEnumerable.ForEach(null, x => { })); AssertThrows(() => AsyncEnumerable.ForEach(AsyncEnumerable.Return(42), default(Action))); AssertThrows(() => AsyncEnumerable.ForEach(null, (x, i) => { })); AssertThrows(() => AsyncEnumerable.ForEach(AsyncEnumerable.Return(42), default(Action))); AssertThrows(() => AsyncEnumerable.ForEach(null, x => { }, CancellationToken.None)); AssertThrows(() => AsyncEnumerable.ForEach(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); AssertThrows(() => AsyncEnumerable.ForEach(null, (x, i) => { }, CancellationToken.None)); AssertThrows(() => AsyncEnumerable.ForEach(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); } [TestMethod] public void ForEachAsync1() { var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); xs.ForEachAsync(x => sum += x).Wait(); Assert.AreEqual(10, sum); } [TestMethod] public void ForEach1() { var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); xs.ForEach(x => sum += x); Assert.AreEqual(10, sum); } [TestMethod] public void ForEachAsync2() { var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); xs.ForEach((x, i) => sum += x * i); Assert.AreEqual(1 * 0 + 2 * 1 + 3 * 2 + 4 * 3, sum); } [TestMethod] public void ForEach2() { var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); xs.ForEachAsync((x, i) => sum += x * i).Wait(); Assert.AreEqual(1 * 0 + 2 * 1 + 3 * 2 + 4 * 3, sum); } [TestMethod] public void ForEachAsync3() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); AssertThrows(() => xs.ForEachAsync(x => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEach3() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); AssertThrows(() => xs.ForEach(x => { throw ex; }), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEachAsync4() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); AssertThrows(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEach4() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); AssertThrows(() => xs.ForEach((x, i) => { throw ex; }), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEachAsync5() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); AssertThrows(() => xs.ForEachAsync(x => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEach5() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); AssertThrows(() => xs.ForEach(x => { throw ex; }), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEachAsync6() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); AssertThrows(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ForEach6() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); AssertThrows(() => xs.ForEach((x, i) => { throw ex; }), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Take_Null() { AssertThrows(() => AsyncEnumerable.Take(null, 5)); AssertThrows(() => AsyncEnumerable.Take(AsyncEnumerable.Return(42), -1)); } [TestMethod] public void Take1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Take(2); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void Take2() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Take(10); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void Take3() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Take(0); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void Take4() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Take(2); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void TakeWhile_Null() { AssertThrows(() => AsyncEnumerable.TakeWhile(null, x => true)); AssertThrows(() => AsyncEnumerable.TakeWhile(null, (x, i) => true)); AssertThrows(() => AsyncEnumerable.TakeWhile(AsyncEnumerable.Return(42), default(Func))); AssertThrows(() => AsyncEnumerable.TakeWhile(AsyncEnumerable.Return(42), default(Func))); } [TestMethod] public void TakeWhile1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => x < 3); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void TakeWhile2() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => false); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void TakeWhile3() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => true); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void TakeWhile4() { var xs = new[] { 1, 2, 3, 4, 3, 2, 1 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => x < 3); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void TakeWhile5() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void TakeWhile6() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => i < 2); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void TakeWhile7() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => false); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void TakeWhile8() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => true); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void TakeWhile9() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Skip_Null() { AssertThrows(() => AsyncEnumerable.Skip(null, 5)); AssertThrows(() => AsyncEnumerable.Skip(AsyncEnumerable.Return(42), -1)); } [TestMethod] public void Skip1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Skip(2); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void Skip2() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Skip(10); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void Skip3() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Skip(0); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void Skip4() { var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Skip(2); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SkipWhile_Null() { AssertThrows(() => AsyncEnumerable.SkipWhile(null, x => true)); AssertThrows(() => AsyncEnumerable.SkipWhile(null, (x, i) => true)); AssertThrows(() => AsyncEnumerable.SkipWhile(AsyncEnumerable.Return(42), default(Func))); AssertThrows(() => AsyncEnumerable.SkipWhile(AsyncEnumerable.Return(42), default(Func))); } [TestMethod] public void SkipWhile1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile(x => x < 3); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void SkipWhile2() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile(x => false); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void SkipWhile3() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile(x => true); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void SkipWhile4() { var xs = new[] { 1, 2, 3, 4, 3, 2, 1 }.ToAsyncEnumerable(); var ys = xs.SkipWhile(x => x < 3); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 4); HasNext(e, 3); HasNext(e, 2); HasNext(e, 1); NoNext(e); } [TestMethod] public void SkipWhile5() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void SkipWhile6() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile((x, i) => i < 2); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void SkipWhile7() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile((x, i) => false); var e = ys.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void SkipWhile8() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile((x, i) => true); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void SkipWhile9() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.SkipWhile((x, i) => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void DefaultIfEmpty_Null() { AssertThrows(() => AsyncEnumerable.DefaultIfEmpty(null)); AssertThrows(() => AsyncEnumerable.DefaultIfEmpty(null, 42)); } [TestMethod] public void DefaultIfEmpty1() { var xs = AsyncEnumerable.Empty().DefaultIfEmpty(); var e = xs.GetEnumerator(); HasNext(e, 0); NoNext(e); } [TestMethod] public void DefaultIfEmpty2() { var xs = AsyncEnumerable.Empty().DefaultIfEmpty(42); var e = xs.GetEnumerator(); HasNext(e, 42); NoNext(e); } [TestMethod] public void DefaultIfEmpty3() { var xs = AsyncEnumerable.Return(42).DefaultIfEmpty(); var e = xs.GetEnumerator(); HasNext(e, 42); NoNext(e); } [TestMethod] public void DefaultIfEmpty4() { var xs = AsyncEnumerable.Return(42).DefaultIfEmpty(24); var e = xs.GetEnumerator(); HasNext(e, 42); NoNext(e); } [TestMethod] public void DefaultIfEmpty5() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().DefaultIfEmpty(); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void DefaultIfEmpty6() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().DefaultIfEmpty(24); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void DefaultIfEmpty7() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex).DefaultIfEmpty(); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void DefaultIfEmpty8() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex).DefaultIfEmpty(24); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Distinct_Null() { AssertThrows(() => AsyncEnumerable.Distinct(null)); AssertThrows(() => AsyncEnumerable.Distinct(null, new Eq())); AssertThrows(() => AsyncEnumerable.Distinct(AsyncEnumerable.Return(42), null)); } [TestMethod] public void Distinct1() { var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 5); HasNext(e, 4); NoNext(e); } [TestMethod] public void Distinct2() { var xs = new[] { 1, -2, -1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(new Eq()); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, -2); HasNext(e, 3); HasNext(e, 5); HasNext(e, 4); NoNext(e); } [TestMethod] public void Reverse_Null() { AssertThrows(() => AsyncEnumerable.Reverse(null)); } [TestMethod] public void Reverse1() { var xs = AsyncEnumerable.Empty(); var ys = xs.Reverse(); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void Reverse2() { var xs = AsyncEnumerable.Return(42); var ys = xs.Reverse(); var e = ys.GetEnumerator(); HasNext(e, 42); NoNext(e); } [TestMethod] public void Reverse3() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.Reverse(); var e = ys.GetEnumerator(); HasNext(e, 3); HasNext(e, 2); HasNext(e, 1); NoNext(e); } [TestMethod] public void Reverse4() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Reverse(); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void OrderBy_Null() { AssertThrows(() => AsyncEnumerable.OrderBy(null, x => x)); AssertThrows(() => AsyncEnumerable.OrderBy(AsyncEnumerable.Return(42), null)); AssertThrows(() => AsyncEnumerable.OrderBy(null, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderBy(AsyncEnumerable.Return(42), null, Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderBy(AsyncEnumerable.Return(42), x => x, null)); AssertThrows(() => AsyncEnumerable.OrderByDescending(null, x => x)); AssertThrows(() => AsyncEnumerable.OrderByDescending(AsyncEnumerable.Return(42), null)); AssertThrows(() => AsyncEnumerable.OrderByDescending(null, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderByDescending(AsyncEnumerable.Return(42), null, Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderByDescending(AsyncEnumerable.Return(42), x => x, null)); var xs = AsyncEnumerable.Return(42).OrderBy(x => x); AssertThrows(() => AsyncEnumerable.ThenBy(null, x => x)); AssertThrows(() => AsyncEnumerable.ThenBy(xs, null)); AssertThrows(() => AsyncEnumerable.ThenBy(null, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenBy(xs, null, Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenBy(xs, x => x, null)); AssertThrows(() => AsyncEnumerable.ThenByDescending(null, x => x)); AssertThrows(() => AsyncEnumerable.ThenByDescending(xs, null)); AssertThrows(() => AsyncEnumerable.ThenByDescending(null, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenByDescending(xs, null, Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenByDescending(xs, x => x, null)); } [TestMethod] public void OrderBy1() { var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x); var e = ys.GetEnumerator(); for (int i = 0; i < 10; i++) HasNext(e, i); NoNext(e); } [TestMethod] public void OrderBy2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ThenBy2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x).ThenBy(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void OrderByDescending1() { var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderByDescending(x => x); var e = ys.GetEnumerator(); for (int i = 9; i >= 0; i--) HasNext(e, i); NoNext(e); } [TestMethod] public void OrderByDescending2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderByDescending(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void ThenByDescending2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x).ThenByDescending(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void OrderByThenBy1() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderBy(x => x.Name).ThenBy(x => x.Age); var resa = ys.OrderBy(x => x.Name).ThenBy(x => x.Age); Assert.IsTrue(ress.SequenceEqual(resa.ToEnumerable())); } [TestMethod] public void OrderByThenBy2() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderBy(x => x.Name).ThenByDescending(x => x.Age); var resa = ys.OrderBy(x => x.Name).ThenByDescending(x => x.Age); Assert.IsTrue(ress.SequenceEqual(resa.ToEnumerable())); } [TestMethod] public void OrderByThenBy3() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderByDescending(x => x.Name).ThenBy(x => x.Age); var resa = ys.OrderByDescending(x => x.Name).ThenBy(x => x.Age); Assert.IsTrue(ress.SequenceEqual(resa.ToEnumerable())); } [TestMethod] public void OrderByThenBy4() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderByDescending(x => x.Name).ThenByDescending(x => x.Age); var resa = ys.OrderByDescending(x => x.Name).ThenByDescending(x => x.Age); Assert.IsTrue(ress.SequenceEqual(resa.ToEnumerable())); } [TestMethod] public void GroupBy_Null() { AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(IEqualityComparer))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, x => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), x => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(Func))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, x => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), x => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(Func), EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, x => x, default(IEqualityComparer))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, (x, ys) => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), (x, ys) => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(Func, int>))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, (x, ys) => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), (x, ys) => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(Func, int>), EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, (x, ys) => x, default(IEqualityComparer))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, x => x, (x, ys) => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), x => x, (x, ys) => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(Func), (x, ys) => x)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, x => x, default(Func, int>))); AssertThrows(() => AsyncEnumerable.GroupBy(null, x => x, x => x, (x, ys) => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), default(Func), x => x, (x, ys) => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, default(Func), (x, ys) => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, x => x, default(Func, int>), EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.GroupBy(AsyncEnumerable.Return(42), x => x, x => x, (x, ys) => x, default(IEqualityComparer))); } [TestMethod] public void GroupBy1() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var res = ys.GroupBy(x => x.Age / 10); var e = res.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); Assert.AreEqual(e.Current.Key, 2); var g1 = e.Current.GetEnumerator(); HasNext(g1, xs[0]); HasNext(g1, xs[2]); HasNext(g1, xs[4]); HasNext(g1, xs[5]); NoNext(g1); Assert.IsTrue(e.MoveNext().Result); Assert.AreEqual(e.Current.Key, 6); var g2 = e.Current.GetEnumerator(); HasNext(g2, xs[1]); NoNext(g2); Assert.IsTrue(e.MoveNext().Result); Assert.AreEqual(e.Current.Key, 1); var g3 = e.Current.GetEnumerator(); HasNext(g3, xs[3]); NoNext(g3); Assert.IsTrue(e.MoveNext().Result); Assert.AreEqual(e.Current.Key, 4); var g4 = e.Current.GetEnumerator(); HasNext(g4, xs[6]); NoNext(g4); NoNext(e); } [TestMethod] public void GroupBy2() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var res = ys.GroupBy(x => x.Age / 10); var e = res.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 2); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 6); Assert.IsTrue(e.MoveNext().Result); var g3 = e.Current; Assert.AreEqual(g3.Key, 1); Assert.IsTrue(e.MoveNext().Result); var g4 = e.Current; Assert.AreEqual(g4.Key, 4); NoNext(e); var g1e = g1.GetEnumerator(); HasNext(g1e, xs[0]); HasNext(g1e, xs[2]); HasNext(g1e, xs[4]); HasNext(g1e, xs[5]); NoNext(g1e); var g2e = g2.GetEnumerator(); HasNext(g2e, xs[1]); NoNext(g2e); var g3e = g3.GetEnumerator(); HasNext(g3e, xs[3]); NoNext(g3e); var g4e = g4.GetEnumerator(); HasNext(g4e, xs[6]); NoNext(g4e); } [TestMethod] public void GroupBy3() { var xs = AsyncEnumerable.Empty(); var ys = xs.GroupBy(x => x); var e = ys.GetEnumerator(); NoNext(e); } [TestMethod] public void GroupBy4() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.GroupBy(x => x); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void GroupBy5() { var xs = GetXs().ToAsyncEnumerable(); var ys = xs.GroupBy(x => x); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 42); var g1e = g1.GetEnumerator(); HasNext(g1e, 42); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 43); var g2e = g2.GetEnumerator(); HasNext(g2e, 43); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); AssertThrows(() => g1e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); AssertThrows(() => g2e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } [TestMethod] public void GroupBy6() { var xs = GetXs().ToAsyncEnumerable(); var ys = xs.GroupBy(x => x); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 42); var g1e = g1.GetEnumerator(); HasNext(g1e, 42); AssertThrows(() => g1e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } static IEnumerable GetXs() { yield return 42; yield return 43; throw new Exception("Bang!"); } [TestMethod] public void GroupBy7() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Return(42); var ys = xs.GroupBy(x => { throw ex; }); var e = ys.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void GroupBy8() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable(); var ys = xs.GroupBy(x => { if (x == 3) throw ex; return x; }); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 1); var g1e = g1.GetEnumerator(); HasNext(g1e, 1); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 2); var g2e = g2.GetEnumerator(); HasNext(g2e, 2); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); AssertThrows(() => g1e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); AssertThrows(() => g2e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void GroupBy9() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x % 3, x => (char)('a' + x)); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 0); var g1e = g1.GetEnumerator(); HasNext(g1e, 'a'); HasNext(g1e, 'd'); HasNext(g1e, 'g'); HasNext(g1e, 'j'); NoNext(g1e); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 1); var g2e = g2.GetEnumerator(); HasNext(g2e, 'b'); HasNext(g2e, 'e'); HasNext(g2e, 'h'); NoNext(g2e); Assert.IsTrue(e.MoveNext().Result); var g3 = e.Current; Assert.AreEqual(g3.Key, 2); var g3e = g3.GetEnumerator(); HasNext(g3e, 'c'); HasNext(g3e, 'f'); HasNext(g3e, 'i'); NoNext(g3e); NoNext(e); } [TestMethod] public void GroupBy10() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x % 3, x => (char)('a' + x), (k, cs) => k + " - " + cs.Aggregate("", (a, c) => a + c).Result); var e = ys.GetEnumerator(); HasNext(e, "0 - adgj"); HasNext(e, "1 - beh"); HasNext(e, "2 - cfi"); NoNext(e); } [TestMethod] public void GroupBy11() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x % 3, (k, cs) => k + " - " + cs.Aggregate("", (a, c) => a + c).Result); var e = ys.GetEnumerator(); HasNext(e, "0 - 0369"); HasNext(e, "1 - 147"); HasNext(e, "2 - 258"); NoNext(e); } [TestMethod] public void GroupBy12() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, new EqMod(3)); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 0); var g1e = g1.GetEnumerator(); HasNext(g1e, 0); HasNext(g1e, 3); HasNext(g1e, 6); HasNext(g1e, 9); NoNext(g1e); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 1); var g2e = g2.GetEnumerator(); HasNext(g2e, 1); HasNext(g2e, 4); HasNext(g2e, 7); NoNext(g2e); Assert.IsTrue(e.MoveNext().Result); var g3 = e.Current; Assert.AreEqual(g3.Key, 2); var g3e = g3.GetEnumerator(); HasNext(g3e, 2); HasNext(g3e, 5); HasNext(g3e, 8); NoNext(g3e); NoNext(e); } [TestMethod] public void GroupBy13() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, x => (char)('a' + x), new EqMod(3)); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 0); var g1e = g1.GetEnumerator(); HasNext(g1e, 'a'); HasNext(g1e, 'd'); HasNext(g1e, 'g'); HasNext(g1e, 'j'); NoNext(g1e); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 1); var g2e = g2.GetEnumerator(); HasNext(g2e, 'b'); HasNext(g2e, 'e'); HasNext(g2e, 'h'); NoNext(g2e); Assert.IsTrue(e.MoveNext().Result); var g3 = e.Current; Assert.AreEqual(g3.Key, 2); var g3e = g3.GetEnumerator(); HasNext(g3e, 'c'); HasNext(g3e, 'f'); HasNext(g3e, 'i'); NoNext(g3e); NoNext(e); } [TestMethod] public void GroupBy14() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, x => (char)('a' + x), (k, cs) => k + " - " + cs.Aggregate("", (a, c) => a + c).Result, new EqMod(3)); var e = ys.GetEnumerator(); HasNext(e, "0 - adgj"); HasNext(e, "1 - beh"); HasNext(e, "2 - cfi"); NoNext(e); } [TestMethod] public void GroupBy15() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, (k, cs) => k + " - " + cs.Aggregate("", (a, c) => a + c).Result, new EqMod(3)); var e = ys.GetEnumerator(); HasNext(e, "0 - 0369"); HasNext(e, "1 - 147"); HasNext(e, "2 - 258"); NoNext(e); } [TestMethod] public void GroupBy16() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, x => (char)('a' + x), new EqMod(3)); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 0); var g1e = g1.GetEnumerator(); HasNext(g1e, 'a'); HasNext(g1e, 'd'); HasNext(g1e, 'g'); HasNext(g1e, 'j'); NoNext(g1e); g1e.Dispose(); Assert.IsTrue(e.MoveNext().Result); var g2 = e.Current; Assert.AreEqual(g2.Key, 1); var g2e = g2.GetEnumerator(); HasNext(g2e, 'b'); HasNext(g2e, 'e'); HasNext(g2e, 'h'); NoNext(g2e); g2e.Dispose(); Assert.IsTrue(e.MoveNext().Result); var g3 = e.Current; Assert.AreEqual(g3.Key, 2); var g3e = g3.GetEnumerator(); HasNext(g3e, 'c'); HasNext(g3e, 'f'); HasNext(g3e, 'i'); NoNext(g3e); g3e.Dispose(); NoNext(e); e.Dispose(); } [TestMethod] public void GroupBy17() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, x => (char)('a' + x), new EqMod(3)); var e = ys.GetEnumerator(); e.Dispose(); Assert.IsFalse(e.MoveNext().Result); } [TestMethod] public void GroupBy18() { var xs = AsyncEnumerable.Range(0, 10); var ys = xs.GroupBy(x => x, x => (char)('a' + x), new EqMod(3)); var e = ys.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); var g1 = e.Current; Assert.AreEqual(g1.Key, 0); var g1e = g1.GetEnumerator(); HasNext(g1e, 'a'); e.Dispose(); HasNext(g1e, 'd'); HasNext(g1e, 'g'); HasNext(g1e, 'j'); NoNext(g1e); g1e.Dispose(); Assert.IsFalse(e.MoveNext().Result); } class EqMod : IEqualityComparer { private readonly int _d; public EqMod(int d) { _d = d; } public bool Equals(int x, int y) { return EqualityComparer.Default.Equals(x % _d, y % _d); } public int GetHashCode(int obj) { return EqualityComparer.Default.GetHashCode(obj % _d); } } [TestMethod] public void AsAsyncEnumerable_Null() { AssertThrows(() => AsyncEnumerable.AsAsyncEnumerable(null)); } [TestMethod] public void AsAsyncEnumerable1() { var xs = AsyncEnumerable.Return(42); var ys = xs.AsAsyncEnumerable(); Assert.AreNotSame(xs, ys); var e = xs.GetEnumerator(); HasNext(e, 42); NoNext(e); } [TestMethod] public void RepeatSeq_Null() { AssertThrows(() => AsyncEnumerable.Repeat(default(IAsyncEnumerable))); AssertThrows(() => AsyncEnumerable.Repeat(default(IAsyncEnumerable), 3)); AssertThrows(() => AsyncEnumerable.Repeat(AsyncEnumerable.Return(42), -1)); } [TestMethod] public void RepeatSeq1() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat(); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); } [TestMethod] public void RepeatSeq2() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat(3); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); NoNext(e); } [TestMethod] public void RepeatSeq3() { var i = 0; var xs = RepeatXs(() => i++).ToAsyncEnumerable().Repeat(3); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 1); HasNext(e, 2); HasNext(e, 1); HasNext(e, 2); NoNext(e); Assert.AreEqual(3, i); } static IEnumerable RepeatXs(Action started) { started(); yield return 1; yield return 2; } [TestMethod] public void RepeatSeq4() { var xs = new FailRepeat().ToAsyncEnumerable().Repeat(); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); } [TestMethod] public void RepeatSeq5() { var xs = new FailRepeat().ToAsyncEnumerable().Repeat(3); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); } class FailRepeat : IEnumerable { public IEnumerator GetEnumerator() { throw new NotImplementedException(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } [TestMethod] public void IgnoreElements_Null() { AssertThrows(() => AsyncEnumerable.IgnoreElements(default(IAsyncEnumerable))); } [TestMethod] public void IgnoreElements1() { var xs = AsyncEnumerable.Empty().IgnoreElements(); var e = xs.GetEnumerator(); NoNext(e); AssertThrows(() => { var ignored = e.Current; }); } [TestMethod] public void IgnoreElements2() { var xs = AsyncEnumerable.Return(42).IgnoreElements(); var e = xs.GetEnumerator(); NoNext(e); AssertThrows(() => { var ignored = e.Current; }); } [TestMethod] public void IgnoreElements3() { var xs = AsyncEnumerable.Range(0, 10).IgnoreElements(); var e = xs.GetEnumerator(); NoNext(e); AssertThrows(() => { var ignored = e.Current; }); } [TestMethod] public void IgnoreElements4() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex).IgnoreElements(); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void StartWith_Null() { AssertThrows(() => AsyncEnumerable.StartWith(default(IAsyncEnumerable), new[] { 1 })); AssertThrows(() => AsyncEnumerable.StartWith(AsyncEnumerable.Return(42), null)); } [TestMethod] public void StartWith1() { var xs = AsyncEnumerable.Empty().StartWith(1, 2); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void StartWith2() { var xs = AsyncEnumerable.Return(0).StartWith(1, 2); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 0); NoNext(e); } [TestMethod] public void StartWith3() { var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex).StartWith(1, 2); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Buffer_Null() { AssertThrows(() => AsyncEnumerable.Buffer(default(IAsyncEnumerable), 1)); AssertThrows(() => AsyncEnumerable.Buffer(default(IAsyncEnumerable), 1, 1)); AssertThrows(() => AsyncEnumerable.Buffer(AsyncEnumerable.Return(42), -1)); AssertThrows(() => AsyncEnumerable.Buffer(AsyncEnumerable.Return(42), -1, 1)); AssertThrows(() => AsyncEnumerable.Buffer(AsyncEnumerable.Return(42), 1, -1)); } [TestMethod] public void Buffer1() { var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(2); var e = xs.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 1, 2 })); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 3, 4 })); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 5 })); Assert.IsFalse(e.MoveNext().Result); } [TestMethod] public void Buffer2() { var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(3, 2); var e = xs.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 1, 2, 3 })); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 3, 4, 5 })); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 5 })); Assert.IsFalse(e.MoveNext().Result); } [TestMethod] public void Buffer3() { var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Buffer(2, 3); var e = xs.GetEnumerator(); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 1, 2 })); Assert.IsTrue(e.MoveNext().Result); Assert.IsTrue(e.Current.SequenceEqual(new[] { 4, 5 })); Assert.IsFalse(e.MoveNext().Result); } [TestMethod] public void DistinctUntilChanged_Null() { AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(default(IAsyncEnumerable))); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(default(IAsyncEnumerable), EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(AsyncEnumerable.Return(42), default(IEqualityComparer))); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(default(IAsyncEnumerable), x => x)); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(AsyncEnumerable.Return(42), default(Func))); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(default(IAsyncEnumerable), x => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(AsyncEnumerable.Return(42), default(Func), EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.DistinctUntilChanged(AsyncEnumerable.Return(42), x => x, default(IEqualityComparer))); } [TestMethod] public void DistinctUntilChanged1() { var xs = new[] { 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 7, 3, 2, 2, 1, 1 }.ToAsyncEnumerable().DistinctUntilChanged(); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); HasNext(e, 5); HasNext(e, 6); HasNext(e, 7); HasNext(e, 3); HasNext(e, 2); HasNext(e, 1); NoNext(e); } [TestMethod] public void DistinctUntilChanged2() { var xs = new[] { 1, 2, 3, 4, 3, 5, 2 }.ToAsyncEnumerable().DistinctUntilChanged(x => (x + 1) / 2); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 3); HasNext(e, 5); HasNext(e, 2); NoNext(e); } [TestMethod] public void DistinctUntilChanged3() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4, 3, 5, 2 }.ToAsyncEnumerable().DistinctUntilChanged(x => { if (x == 4) throw ex; return x; }); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Expand_Null() { AssertThrows(() => AsyncEnumerable.Expand(default(IAsyncEnumerable), x => null)); AssertThrows(() => AsyncEnumerable.Expand(AsyncEnumerable.Return(42), null)); } [TestMethod] public void Expand1() { var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => AsyncEnumerable.Return(x - 1).Repeat(x - 1)); var e = xs.GetEnumerator(); HasNext(e, 2); HasNext(e, 3); HasNext(e, 1); HasNext(e, 2); HasNext(e, 2); HasNext(e, 1); HasNext(e, 1); NoNext(e); } [TestMethod] public void Expand2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => { throw ex; }); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Expand3() { var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => null); var e = xs.GetEnumerator(); HasNext(e, 2); HasNext(e, 3); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NullReferenceException); } [TestMethod] public void Scan_Null() { AssertThrows(() => AsyncEnumerable.Scan(default(IAsyncEnumerable), 3, (x, y) => x + y)); AssertThrows(() => AsyncEnumerable.Scan(AsyncEnumerable.Return(42), 3, null)); AssertThrows(() => AsyncEnumerable.Scan(default(IAsyncEnumerable), (x, y) => x + y)); AssertThrows(() => AsyncEnumerable.Scan(AsyncEnumerable.Return(42), null)); } [TestMethod] public void Scan1() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, (x, y) => x + y); var e = xs.GetEnumerator(); HasNext(e, 9); HasNext(e, 11); HasNext(e, 14); NoNext(e); } [TestMethod] public void Scan2() { var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan((x, y) => x + y); var e = xs.GetEnumerator(); HasNext(e, 3); HasNext(e, 6); NoNext(e); } [TestMethod] public void Scan3() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, (x, y) => { throw ex; }); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void Scan4() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan((x, y) => { throw ex; }); var e = xs.GetEnumerator(); AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [TestMethod] public void DistinctKey_Null() { AssertThrows(() => AsyncEnumerable.Distinct(default(IAsyncEnumerable), x => x)); AssertThrows(() => AsyncEnumerable.Distinct(AsyncEnumerable.Return(42), null)); AssertThrows(() => AsyncEnumerable.Distinct(default(IAsyncEnumerable), x => x, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.Distinct(AsyncEnumerable.Return(42), null, EqualityComparer.Default)); AssertThrows(() => AsyncEnumerable.Distinct(AsyncEnumerable.Return(42), x => x, null)); } [TestMethod] public void DistinctKey1() { var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Distinct(x => x / 2); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 4); NoNext(e); } [TestMethod] public void TakeLast_Null() { AssertThrows(() => AsyncEnumerable.TakeLast(default(IAsyncEnumerable), 5)); AssertThrows(() => AsyncEnumerable.TakeLast(AsyncEnumerable.Return(42), -1)); } [TestMethod] public void TakeLast1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(2); var e = xs.GetEnumerator(); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void TakeLast2() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(5); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); HasNext(e, 4); NoNext(e); } [TestMethod] public void SkipLast_Null() { AssertThrows(() => AsyncEnumerable.SkipLast(default(IAsyncEnumerable), 5)); AssertThrows(() => AsyncEnumerable.SkipLast(AsyncEnumerable.Return(42), -1)); } [TestMethod] public void SkipLast1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().SkipLast(2); var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); NoNext(e); } [TestMethod] public void SkipLast2() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().SkipLast(5); var e = xs.GetEnumerator(); NoNext(e); } } } #endif