Esempio n. 1
0
        public void TestLoop()
        {
            (Cell <int> c, CellStreamSink <int> s) = Transaction.Run(() =>
            {
                CellLoop <int> loop         = Cell.CreateLoop <int>();
                Cell <int> cLocal           = loop.Map(v => v * 5);
                CellStreamSink <int> sLocal = Cell.CreateStreamSink <int>();
                loop.Loop(sLocal.Hold(3));
                return(cLocal, sLocal);
            });

            List <int> output1 = new List <int>();
            List <int> output2 = new List <int>();
            IListener  l       = c.Listen(output1.Add);
            IListener  l2      = c.Updates().Listen(output2.Add);

            s.Send(5);
            s.Send(7);

            l2.Unlisten();
            l.Unlisten();

            CollectionAssert.AreEqual(new[] { 15, 25, 35 }, output1);
            CollectionAssert.AreEqual(new[] { 25, 35 }, output2);
        }
Esempio n. 2
0
            public void TestSwitchCDeferredLoopWithBetterApi()
            {
                CellStreamSink <IReadOnlyList <TestObject> > streamSink = Cell.CreateStreamSink <IReadOnlyList <TestObject> >();
                Cell <IReadOnlyList <TestObject> >           cell       = Transaction.Run(() =>
                {
                    CellLoop <IReadOnlyList <TestObject> > cellLoop = new CellLoop <IReadOnlyList <TestObject> >();
                    Cell <IReadOnlyList <TestObject> > cellLocal    = streamSink
                                                                      .OrElse(cellLoop.Map(oo => oo.Select(o => o.Output).Lift(vv => vv.Sum())).SwitchCWithDeferredValues().Filter(sum => sum < 50).Snapshot(cellLoop, (_, items) => (IReadOnlyList <TestObject>)items.Concat(new[] { new TestObject() }).ToArray()))
                                                                      .Hold(Enumerable.Range(1, 10).Select(_ => new TestObject()).ToArray());
                    cellLoop.Loop(cellLocal);
                    return(cellLocal);
                });

                List <int> objectCounts = new List <int>();

                objectCounts.Add(-1);
                cell.Listen(vv => objectCounts.Add(vv.Count));
                objectCounts.Add(-1);
                cell.Sample()[2].Input1.Send(1);
                objectCounts.Add(-1);
                cell.Sample()[1].Input1.Send(-20);
                objectCounts.Add(-1);
                streamSink.Send(new TestObject[0]);
                objectCounts.Add(-1);

                // Ideal result, likely not achievable.
                //CollectionAssert.AreEquivalent(new[] { -1, 10, -1, 11, -1, 15, -1, 10, -1 }, objectCounts);

                // Glitchy result, but correct otherwise.
                CollectionAssert.AreEquivalent(new[] { -1, 10, -1, 11, -1, 12, 13, 14, 15, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1 }, objectCounts);
            }
Esempio n. 3
0
            public void TestSwitchSValuesLoop()
            {
                CellStreamSink <IReadOnlyList <TestObject> > streamSink = Cell.CreateStreamSink <IReadOnlyList <TestObject> >();
                Cell <IReadOnlyList <TestObject> >           cell       = Transaction.Run(() =>
                {
                    CellLoop <IReadOnlyList <TestObject> > cellLoop = new CellLoop <IReadOnlyList <TestObject> >();
                    Cell <IReadOnlyList <TestObject> > cellLocal    = streamSink.Map(v => (Func <IReadOnlyList <TestObject>, IReadOnlyList <TestObject> >)(_ => v))
                                                                      .Merge(cellLoop.Map(oo => oo.Select(o => o.Output).Lift(vv => vv.Sum()).Values()).SwitchS().Filter(sum => sum < 50).MapTo((Func <IReadOnlyList <TestObject>, IReadOnlyList <TestObject> >)(v => v.Concat(new[] { new TestObject() }).ToArray())), (f, g) => v => g(f(v)))
                                                                      .Snapshot(cellLoop, (f, v) => f(v))
                                                                      .Hold(Enumerable.Range(1, 10).Select(_ => new TestObject()).ToArray());
                    cellLoop.Loop(cellLocal);
                    return(cellLocal);
                });

                List <int> objectCounts = new List <int>();

                objectCounts.Add(-1);
                cell.Listen(vv => objectCounts.Add(vv.Count));
                objectCounts.Add(-1);
                cell.Sample()[2].Input1.Send(1);
                objectCounts.Add(-1);
                cell.Sample()[1].Input1.Send(-20);
                objectCounts.Add(-1);
                streamSink.Send(new TestObject[0]);
                objectCounts.Add(-1);

                // Ideal result, likely not achievable.
                //CollectionAssert.AreEquivalent(new[] { -1, 10, -1, 11, -1, 15, -1, 10, -1 }, objectCounts);

                // Glitchy result, also not returned by this method.
                //CollectionAssert.AreEquivalent(new[] { -1, 10, -1, 11, -1, 12, 13, 14, 15, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1 }, objectCounts);

                // Incorrect result we will see.
                CollectionAssert.AreEquivalent(new[] { -1, 10, -1, 11, -1, 12, -1, 0, -1 }, objectCounts);
            }
Esempio n. 4
0
        public void TestMerge()
        {
            StreamSink <Unit> s = Stream.CreateSink <Unit>();
            var obj             = Transaction.Run(() =>
            {
                StreamLoop <bool> loop  = Stream.CreateLoop <bool>();
                CellStreamSink <int> s1 = new CellStreamSink <int>();
                CellStreamSink <int> s2 = new CellStreamSink <int>();
                TestObject[] l          = Enumerable.Range(0, 5000).Select(_ => new TestObject(loop, s1, s2)).ToArray();
                loop.Loop(s.Snapshot(l.Select(o => o.Cell).Lift()).Map(o => o.All(v => v == 0)));
                return(l);
            });

            int[] values = obj.Select(v => v.CurrentValue).ToArray();
        }
Esempio n. 5
0
            public void TestSwitchCLoop()
            {
                Exception actual = null;

                try
                {
                    CellStreamSink <IReadOnlyList <TestObject> > streamSink = Cell.CreateStreamSink <IReadOnlyList <TestObject> >();
                    Cell <IReadOnlyList <TestObject> >           cell       = Transaction.Run(() =>
                    {
                        CellLoop <IReadOnlyList <TestObject> > cellLoop = new CellLoop <IReadOnlyList <TestObject> >();
                        Cell <IReadOnlyList <TestObject> > cellLocal    = streamSink.Map(v => (Func <IReadOnlyList <TestObject>, IReadOnlyList <TestObject> >)(_ => v))
                                                                          .Merge(cellLoop.Map(oo => oo.Select(o => o.Output).Lift(vv => vv.Sum())).SwitchC().Updates().Filter(sum => sum < 50).MapTo((Func <IReadOnlyList <TestObject>, IReadOnlyList <TestObject> >)(v => v.Concat(new[] { new TestObject() }).ToArray())), (f, g) => v => g(f(v)))
                                                                          .Snapshot(cellLoop, (f, v) => f(v))
                                                                          .Hold(Enumerable.Range(1, 10).Select(_ => new TestObject()).ToArray());
                        cellLoop.Loop(cellLocal);
                        return(cellLocal);
                    });

                    List <int> objectCounts = new List <int>();
                    objectCounts.Add(-1);
                    cell.Listen(vv => objectCounts.Add(vv.Count));
                    objectCounts.Add(-1);
                    cell.Sample()[2].Input1.Send(1);
                    objectCounts.Add(-1);
                    cell.Sample()[1].Input1.Send(-20);
                    objectCounts.Add(-1);
                    streamSink.Send(new TestObject[0]);
                    objectCounts.Add(-1);
                }
                catch (AggregateException e)
                {
                    actual = e.InnerExceptions.FirstOrDefault(ex => ex.Message == "A dependency cycle was detected.");
                }
                catch (Exception e)
                {
                    actual = e;
                }

                Assert.IsNotNull(actual);
                Assert.AreEqual("A dependency cycle was detected.", actual.Message);
            }
Esempio n. 6
0
 /// <summary>
 ///     Return a reference to this <see cref="CellStreamSink{T}" /> as a <see cref="StreamSink{T}" />.
 /// </summary>
 /// <typeparam name="T">The type of the cell stream sink.</typeparam>
 /// <param name="c">The cell stream sink.</param>
 /// <returns>A reference to this <see cref="CellStreamSink{T}" /> as a <see cref="StreamSink{T}" />.</returns>
 public static StreamSink <T> AsStreamSink <T>(this CellStreamSink <T> c) => c;