public override IEnumerator GetEnumerator() { IEnumerator e; lock (root) { e = deque.GetEnumerator(); } return(e); }
public void Enumerator() { var deque = new Deque<int>(new[] { 2, 3, 4 }); Assert.IsNotNull(((IEnumerable<int>)deque).GetEnumerator()); Assert.IsNotNull(((IEnumerable)deque).GetEnumerator()); Deque<int>.Enumerator enumerator = deque.GetEnumerator(); Assert.AreEqual(0, enumerator.Current); Assert.That(() => ((IEnumerator)enumerator).Current, Throws.TypeOf<InvalidOperationException>()); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(2, enumerator.Current); Assert.AreEqual(2, ((IEnumerator)enumerator).Current); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(3, enumerator.Current); Assert.AreEqual(3, ((IEnumerator)enumerator).Current); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(4, enumerator.Current); Assert.AreEqual(4, ((IEnumerator)enumerator).Current); Assert.IsFalse(enumerator.MoveNext()); Assert.AreEqual(0, enumerator.Current); Assert.That(() => ((IEnumerator)enumerator).Current, Throws.TypeOf<InvalidOperationException>()); enumerator.Reset(); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(2, enumerator.Current); enumerator.Dispose(); Assert.AreEqual(0, enumerator.Current); Assert.IsFalse(enumerator.MoveNext()); Assert.That(() => ((IEnumerator)enumerator).Current, Throws.TypeOf<InvalidOperationException>()); enumerator = deque.GetEnumerator(); enumerator.MoveNext(); deque.EnqueueTail(5); Assert.That(() => enumerator.MoveNext(), Throws.TypeOf<InvalidOperationException>()); Assert.That(() => enumerator.Reset(), Throws.TypeOf<InvalidOperationException>()); }
private static void TestEnumerator(Deque deque) { deque.Clear(); PopulateDequePushBack(deque); IEnumerator e = deque.GetEnumerator(); try { object obj = e.Current; Debug.Fail("Exception failed"); } catch(Exception ex) { Console.WriteLine(ex.Message); } try { foreach(object obj in deque) { Debug.Assert(e.MoveNext()); } Debug.Assert(!e.MoveNext()); object o = e.Current; Debug.Fail("Exception failed"); } catch(Exception ex) { Console.WriteLine(ex.Message); } try { e.Reset(); foreach(object obj in deque) { Debug.Assert(e.MoveNext()); } Debug.Assert(!e.MoveNext()); object o = e.Current; Debug.Fail("Exception failed"); } catch(Exception ex) { Console.WriteLine(ex.Message); } try { deque.PushBack(deque.Count); e.Reset(); Debug.Fail("Exception failed"); } catch(Exception ex) { Console.WriteLine(ex.Message); } try { e.MoveNext(); Debug.Fail("Exception failed"); } catch(Exception ex) { Console.WriteLine(ex.Message); } }