public void CanEnumerateFixedQueue() { var pq1 = new TestPriorityQueue <char>(5); // fixed capacity pq1.AddWithOverflow('f'); pq1.AddWithOverflow('u'); pq1.AddWithOverflow('b'); pq1.AddWithOverflow('a'); pq1.AddWithOverflow('r'); var enumerator = pq1.GetEnumerator(); var list1 = Iterate(enumerator); Assert.AreEqual("abfru", new string(list1.OrderBy(c => c).ToArray())); pq1.Clear(); pq1.Add('f'); pq1.Add('o'); pq1.Add('o'); enumerator.Reset(); var list2 = Iterate(enumerator); Assert.AreEqual("foo", new string(list2.OrderBy(c => c).ToArray())); enumerator.Dispose(); }
public void CannotEnumerateChangingQueue() { const string items = "hello"; var pq = new TestPriorityQueue <char>(items.Length + 1); foreach (var c in items) { pq.Add(c); } var iter = pq.GetEnumerator(); Assert.True(iter.MoveNext()); pq.Add('x'); // modify (will fill up capacity) var ex1 = Assert.Catch <InvalidOperationException>(() => iter.MoveNext()); Console.WriteLine("Expected after Add: {0}", ex1.Message); iter.Reset(); Assert.True(iter.MoveNext()); pq.AddWithOverflow('y'); // modify (will overflow) var ex2 = Assert.Catch <InvalidOperationException>(() => iter.MoveNext()); Console.WriteLine("Expected after AddWithOverflow: {0}", ex2.Message); iter.Reset(); Assert.True(iter.MoveNext()); pq.Pop(); // modify var ex3 = Assert.Catch <InvalidOperationException>(() => iter.MoveNext()); Console.WriteLine("Expected after Pop: {0}", ex3.Message); iter.Reset(); Assert.True(iter.MoveNext()); pq.Clear(); // modify var ex4 = Assert.Catch <InvalidOperationException>(() => iter.MoveNext()); Console.WriteLine("Expected after Clear: {0}", ex4.Message); iter.Dispose(); }
public void CannotEnumerateChangingQueue() { const string items = "hello"; var pq = new TestPriorityQueue <char>(items.Length + 1); foreach (var c in items) { pq.Add(c); } var iter = pq.GetEnumerator(); Assert.True(iter.MoveNext()); pq.Add('x'); // modify (will fill up capacity) Assert.Throws <InvalidOperationException>(() => iter.MoveNext()); iter.Reset(); Assert.True(iter.MoveNext()); pq.AddWithOverflow('y'); // modify (will overflow) Assert.Throws <InvalidOperationException>(() => iter.MoveNext()); iter.Reset(); Assert.True(iter.MoveNext()); pq.Pop(); // modify Assert.Throws <InvalidOperationException>(() => iter.MoveNext()); iter.Reset(); Assert.True(iter.MoveNext()); pq.Clear(); // modify Assert.Throws <InvalidOperationException>(() => iter.MoveNext()); iter.Dispose(); }