public void RandomInsertDeleteRange() { const int ITER = 2000, LOOP = 15; Random rand = new Random(13); for (int loop = 0; loop < LOOP; ++loop) { Deque <int> deque = new Deque <int>(); List <int> list = new List <int>(); for (int iter = 0; iter < ITER; ++iter) { //Console.Write("Loop {0}, Iteration {1}: ", loop, iter); if (rand.Next(100) < 45) { // remove a range. if (list.Count > 0) { int index = rand.Next(list.Count); int count = rand.Next(list.Count - index); //Console.WriteLine("RemoveAt({0}, {1})", index, count); list.RemoveRange(index, count); deque.RemoveRange(index, count); } } else { // Add an range. int index = rand.Next(list.Count + 1); int count = rand.Next(10); int[] items = new int[count]; for (int i = 0; i < count; ++i) { items[i] = rand.Next(1000); } /*Console.Write("Insert({0}, {{", index); * for (int i = 0; i < count; ++i) { * if (i > 0) * Console.Write(", "); * Console.Write(items[i]); * } * Console.WriteLine("})"); */ IEnumerable <int> e = (rand.Next(2) == 0) ? AlgorithmsTests.EnumerableFromArray(items) : items; list.InsertRange(index, e); deque.InsertRange(index, e); } //deque.Print(); CheckListAndDeque(list, deque); } InterfaceTests.TestReadWriteList <int>(deque, list.ToArray()); } }
public void RemoveRangeExceptions() { Deque <int> deque1 = new Deque <int>(); for (int i = 0; i < 100; ++i) { deque1.AddToBack(i); } try { deque1.RemoveRange(3, 98); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(-1, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(0, int.MaxValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(1, int.MinValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(45, int.MinValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(0, 101); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(100, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(int.MinValue, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName); } try { deque1.RemoveRange(int.MaxValue, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName); } }
public void FailFastEnumerator() { Deque <string> deque1 = new Deque <string>(new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }); int i = 0; try { foreach (string s in deque1) { ++i; Assert.IsTrue(i < 4); if (i == 3) { deque1.AddToBack("hi"); } } Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } i = 0; try { foreach (string s in deque1) { ++i; Assert.IsTrue(i < 4); if (i == 3) { deque1.AddToFront("hi"); } } Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } i = 0; try { foreach (string s in deque1) { ++i; Assert.IsTrue(i < 4); if (i == 3) { deque1.RemoveRange(2, 4); } } Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } i = 0; try { foreach (string s in deque1) { ++i; Assert.IsTrue(i < 4); if (i == 3) { deque1[5] = "hi"; } } Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } i = 0; try { foreach (string s in deque1) { ++i; Assert.IsTrue(i < 4); if (i == 3) { deque1.Clear(); } } Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } }