Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
0
        public void Enumerator_MoveNextAndCurrent()
        {
            var collection = new GrowingCollection <string>();

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

            collection.Add(null);
            collection.Add("");

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

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => enumerator.Current);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => ((IEnumerator)enumerator).Current);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => ((IEnumerator <string>)enumerator).Current);

            {
                bool canMove = enumerator.MoveNext();
                Assert.IsTrue(canMove);

                Assert.AreEqual("", enumerator.Current);
                Assert.AreEqual("", ((IEnumerator)enumerator).Current);
                Assert.AreEqual("", ((IEnumerator <string>)enumerator).Current);
            }
            {
                bool canMove = enumerator.MoveNext();
                Assert.IsTrue(canMove);

                Assert.IsNull(enumerator.Current);
                Assert.IsNull(((IEnumerator)enumerator).Current);
                Assert.IsNull(((IEnumerator <string>)enumerator).Current);
            }

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

                Assert.AreEqual(i.ToString(), enumerator.Current);
                Assert.AreEqual(i.ToString(), ((IEnumerator)enumerator).Current);
                Assert.AreEqual(i.ToString(), ((IEnumerator <string>)enumerator).Current);
            }

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

                Assert.AreEqual("0", enumerator.Current);
                Assert.AreEqual("0", ((IEnumerator)enumerator).Current);
                Assert.AreEqual("0", ((IEnumerator <string>)enumerator).Current);
            }
        }
Exemplo n.º 4
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());
            }
        }