private static void Last_Empty() { var lf = new LastFactory { OutputType = typeof(int) }; var ff = new SinkFactory { OutputType = typeof(int) }; var chain = new IFusionOperator[] { lf, // .Last() ff }; var ivt = Compile(typeof(int), chain); var res = new List <int>(); var err = default(Exception); var fin = false; var otp = Observer.Create <int>(res.Add, ex => err = ex, () => fin = true); var dsp = System.Reactive.Disposables.Disposable.Empty; var iv = (IObserver <int>)Activator.CreateInstance(ivt, new object[] { otp, dsp }); iv.OnCompleted(); Assert(res.Count == 0); Assert(err is InvalidOperationException); Assert(!fin); }
private static void DistinctUntilChanged() { var df = new DistinctUntilChangedFactory { OutputType = typeof(int) }; var ff = new SinkFactory { OutputType = typeof(int) }; var chain = new IFusionOperator[] { df, // .DistinctUntilChanged() ff }; var ivt = Compile(typeof(int), chain); var res = new List <int>(); var err = default(Exception); var fin = false; var otp = Observer.Create <int>(res.Add, ex => err = ex, () => fin = true); var dsp = System.Reactive.Disposables.Disposable.Empty; var iv = (IObserver <int>)Activator.CreateInstance(ivt, new object[] { otp, dsp }); iv.OnNext(0); iv.OnNext(1); iv.OnNext(1); iv.OnNext(1); iv.OnNext(2); iv.OnNext(2); iv.OnNext(1); iv.OnNext(3); iv.OnCompleted(); Assert(res.SequenceEqual(new[] { 0, 1, 2, 1, 3 })); Assert(err == null); Assert(fin); }
private static void Where() { var wf = new WhereFactory { Predicate = (Expression <Func <int, bool> >)(x => x > 0) }; var ff = new SinkFactory { OutputType = typeof(int) }; var chain = new IFusionOperator[] { wf, // .Where(x => x > 0) ff }; var ivt = Compile(typeof(int), chain); var res = new List <int>(); var err = default(Exception); var fin = false; var otp = Observer.Create <int>(res.Add, ex => err = ex, () => fin = true); var dsp = System.Reactive.Disposables.Disposable.Empty; var iv = (IObserver <int>)Activator.CreateInstance(ivt, new object[] { otp, dsp }); iv.OnNext(-1); iv.OnNext(0); iv.OnNext(-2); iv.OnNext(1); iv.OnNext(2); iv.OnNext(-3); iv.OnNext(4); iv.OnCompleted(); Assert(res.SequenceEqual(new[] { 1, 2, 4 })); Assert(err == null); Assert(fin); }
private static void SelectIndexed() { var sf = new SelectIndexedFactory { Selector = (Expression <Func <int, int, int> >)((x, i) => x * x + i) }; var ff = new SinkFactory { OutputType = typeof(int) }; var chain = new IFusionOperator[] { sf, // .Select((x, i) => x * x + i) ff }; var ivt = Compile(typeof(int), chain); var res = new List <int>(); var err = default(Exception); var fin = false; var otp = Observer.Create <int>(res.Add, ex => err = ex, () => fin = true); var dsp = System.Reactive.Disposables.Disposable.Empty; var iv = (IObserver <int>)Activator.CreateInstance(ivt, new object[] { otp, dsp }); iv.OnNext(1); iv.OnNext(2); iv.OnNext(3); iv.OnNext(4); iv.OnCompleted(); Assert(res.SequenceEqual(new[] { 1 + 0, 4 + 1, 9 + 2, 16 + 3 })); Assert(err == null); Assert(fin); }
private static void LongCount() { var cf = new LongCountFactory(); var ff = new SinkFactory { OutputType = typeof(long) }; var chain = new IFusionOperator[] { cf, // .LongCount() ff }; var ivt = Compile(typeof(long), chain); var res = new List <long>(); var err = default(Exception); var fin = false; var otp = Observer.Create <long>(res.Add, ex => err = ex, () => fin = true); var dsp = System.Reactive.Disposables.Disposable.Empty; var iv = (IObserver <long>)Activator.CreateInstance(ivt, new object[] { otp, dsp }); iv.OnNext(0); iv.OnNext(1); iv.OnNext(2); iv.OnNext(3); iv.OnCompleted(); Assert(res.SequenceEqual(new[] { 4L })); Assert(err == null); Assert(fin); }
private static void Old() { // TODO: implement a few operators in terms of others and see fusion emit the same code // e.g. xs.Last() == xs.Aggreggate((a, x) => x) // // Aggregate // - Count/LongCount // Average/Sum/Min/Max // Any/All/IsEmpty // - First // - Last // Single // Contains // ElementAt // - IgnoreElements // Scan // Sample // - Take // Skip // TakeWhile/SkipWhile // Finally // // CombineLatest // SequenceEqual // TakeUntil // // SkipUntil // StartWith // // Merge // SelectMany // Switch // var wf1 = new WhereFactory { Predicate = (Expression <Func <int, bool> >)(x => x > 0) }; var wf2 = new WhereIndexedFactory { Predicate = (Expression <Func <int, int, bool> >)((x, i) => x != 0 && i < int.MaxValue) }; var df = new DistinctUntilChangedFactory { OutputType = typeof(int) }; var sf1 = new SelectFactory { Selector = (Expression <Func <int, int> >)(x => x * 2) }; var sf2 = new SelectIndexedFactory { Selector = (Expression <Func <int, int, int> >)((x, i) => x * 3 + i) }; var tf1 = new TakeFactory { OutputType = typeof(int), Count = 1000 }; var tf2 = new TakeFactory { OutputType = typeof(int), Count = 2000 }; var af = new SumFactory(typeof(int)); var ff = new SinkFactory { OutputType = typeof(int) }; var chain = new IFusionOperator[] { wf1, // .Where(x => x > 0) wf2, // .Where((x, i) => x != 0 && i < int.MaxValue) df, // .DistinctUntilChanged() sf1, // .Select(x => x * 2) sf2, // .Select((x, i) => x * 3 + i) tf1, // .Take(1000) tf2, // .Take(2000) af, // .Sum() ff }; var ivt = Compile(typeof(int), chain); Test(ivt); }