Example #1
0
        public void Enumerator_Dispose()
        {
            var collection = new GrowingCollection <string>();

            for (int i = 0; i < 5000; i++)
            {
                collection.Add(i.ToString());
            }

            GrowingCollection <string> .Enumerator enumerator = collection.GetEnumerator();

            for (int i = 4999; i >= 0; i--)
            {
                enumerator.MoveNext();
                Assert.AreEqual(i.ToString(), enumerator.Current);

                enumerator.Dispose();  // Expects No-Op.
            }

            enumerator.Reset();

            for (int i = 4999; i >= 0; i--)
            {
                enumerator.MoveNext();
                Assert.AreEqual(i.ToString(), enumerator.Current);

                enumerator.Dispose();  // Expects No-Op.
            }

            Assert.AreEqual(5000, enumerator.Count);
        }
Example #2
0
        public void Enumerator_Reset()
        {
            var collection = new GrowingCollection <string>();

            for (int i = 0; i < 5000; i++)
            {
                collection.Add(i.ToString());
            }

            GrowingCollection <string> .Enumerator enumerator = collection.GetEnumerator();

            Assert.AreEqual(5000, enumerator.Count);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => enumerator.Current);

            for (int i = 4999; i >= 0; i--)
            {
                bool canMove = enumerator.MoveNext();
                Assert.IsTrue(canMove);

                Assert.AreEqual(i.ToString(), enumerator.Current);
            }

            {
                Assert.AreEqual(5000, enumerator.Count);

                bool canMove = enumerator.MoveNext();
                Assert.IsFalse(canMove);

                Assert.AreEqual("0", enumerator.Current);
                Assert.AreEqual(5000, enumerator.Count);
            }

            enumerator.Reset();

            Assert.AreEqual(5000, enumerator.Count);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => enumerator.Current);

            for (int i = 4999; i >= 0; i--)
            {
                bool canMove = enumerator.MoveNext();
                Assert.IsTrue(canMove);

                Assert.AreEqual(i.ToString(), enumerator.Current);
            }

            {
                Assert.AreEqual(5000, enumerator.Count);

                bool canMove = enumerator.MoveNext();
                Assert.IsFalse(canMove);

                Assert.AreEqual("0", enumerator.Current);
                Assert.AreEqual(5000, enumerator.Count);
            }
        }
Example #3
0
        private static async Task CheckEnumeratorUnderConcurrencyAsync <T>(GrowingCollection <T> .Enumerator enumerator, int expectedCount)
        {
            Assert.IsNotNull(enumerator);
            Assert.IsTrue(expectedCount > 0);

            Random rnd = new Random();

            await Task.Delay(rnd.Next(10)).ConfigureAwait(continueOnCapturedContext: false);

            Assert.AreEqual(expectedCount, enumerator.Count);

            for (int i = expectedCount - 1; i >= 0; i--)
            {
                Assert.IsTrue(enumerator.MoveNext());
                Assert.AreEqual(i.ToString(), enumerator.Current);
                await Task.Delay(rnd.Next(2)).ConfigureAwait(continueOnCapturedContext: false);
            }

            {
                Assert.IsFalse(enumerator.MoveNext());
            }

            enumerator.Reset();

            Assert.AreEqual(expectedCount, enumerator.Count);

            for (int i = expectedCount - 1; i >= 0; i--)
            {
                Assert.IsTrue(enumerator.MoveNext());
                Assert.AreEqual(i.ToString(), enumerator.Current);
                await Task.Delay(rnd.Next(2)).ConfigureAwait(continueOnCapturedContext: false);
            }

            {
                Assert.IsFalse(enumerator.MoveNext());
            }
        }