Example #1
0
        public void Docs_Ctor_RandomValues()
        {
            #region RandomValues
            var values = new Random();
            // Note: wrapping an infinite sequence
            var randSeq = new CachedSequence <int> (EnumerableTest.RandomValues(values, 10));
            Assert.IsTrue(
                randSeq.Take(20).SelectFromEach(randSeq.Take(10),
                                                (l, r) => l + r)
                .All(x => x % 2 == 0));
            #endregion

            // The above implicitly terminates the infinite sequence (as the
            // enumerator was disposed); there should be ~10 elements in it.
            Assert.AreEqual(11, randSeq.Count());
        }
Example #2
0
        public void GetEnumerator_Dispose_Is_Forwarded()
        {
            var d = new DisposedCounter();
            var s = new CachedSequence <int> (d.Values(100));
            var q = s.Take(1);
            int n = 0;

            foreach (var e in q)
            {
                Assert.AreEqual(n, e);
                ++n;
            }
            Assert.AreEqual(1, n);

            // When the CachedSequence<T> enumerator is disposed,
            // the underlying enumerator should also be disposed.
            Assert.AreEqual(1, d.Disposed);

            // Note that we've "lost" 99 elements
            Assert.AreEqual(2, s.Count());
        }