public void RemoveAt() { Deque <string> d = new Deque <string>(); d.Insert(0, "a"); d.Insert(1, "b"); d.Insert(0, "c"); d.Insert(2, "d"); d.Insert(1, "e"); d.Insert(4, "f"); d.Insert(3, "g"); d.Insert(2, "h"); d.Insert(0, "i"); d.Insert(2, "j"); d.Insert(4, "k"); d.RemoveAt(4); d.RemoveAt(3); d.RemoveAt(2); d.RemoveAt(5); d.RemoveAt(2); InterfaceTests.TestReadWriteListGeneric(d, new string[] { "i", "c", "a", "g", "f", "b" }); d.Clear(); d.AddToBack("f"); d.AddToBack("g"); d.AddToFront("e"); d.AddToFront("d"); d.AddToFront("c"); d.AddToFront("b"); d.AddToFront("a"); d.RemoveAt(3); d.RemoveAt(4); d.RemoveAt(4); InterfaceTests.TestReadWriteListGeneric(d, new string[] { "a", "b", "c", "e" }); }
public void IListInterface() { Deque <string> d = new Deque <string>(); d.AddToFront("foo"); d.AddToBack("world"); d.AddToFront("hello"); d.AddToBack("elvis"); d.AddToFront("elvis"); d.AddToBack(null); d.AddToFront("cool"); InterfaceTests.TestReadWriteList <string>((IList)d, new string[] { "cool", "elvis", "hello", "foo", "world", "elvis", null }); }
/// <summary> /// Makes a deep clone of this Deque. A new Deque is created with a clone of /// each element of this set, by calling ICloneable.Clone on each element. If T is /// a value type, then each element is copied as if by simple assignment. /// </summary> /// <remarks><para>If T is a reference type, it must implement /// ICloneable. Otherwise, an InvalidOperationException is thrown.</para> /// <para>Cloning the Deque takes time O(N), where N is the number of items in the Deque.</para></remarks> /// <returns>The cloned Deque.</returns> /// <exception cref="InvalidOperationException">T is a reference type that does not implement ICloneable.</exception> public Deque <T> CloneContents() { bool itemIsValueType; if (Util.IsCloneableType(typeof(T), out itemIsValueType) == false) { throw new InvalidOperationException(string.Format(Strings.TypeNotCloneable, typeof(T).FullName)); } var clone = new Deque <T>(); // Clone each item, and add it to the new ordered set. foreach (var item in this) { T itemClone; if (itemIsValueType) { itemClone = item; } else { itemClone = Equals(item, null) ? default(T) : (T)(((ICloneable)item).Clone()); } clone.AddToBack(itemClone); } return(clone); }
public void Indexer() { Deque <string> d = new Deque <string>(); d.AddToFront("c"); d.AddToFront("b"); d.AddToFront("a"); d.AddToBack("d"); d.AddToBack("e"); d.AddToBack("f"); Assert.AreEqual("b", d[1]); Assert.AreEqual("e", d[4]); d[1] = "q"; d[4] = "r"; Assert.AreEqual("q", d[1]); Assert.AreEqual("r", d[4]); InterfaceTests.TestReadWriteListGeneric(d, new string[] { "a", "q", "c", "d", "r", "f" }); d.Clear(); d.AddToBack("a"); d.AddToBack("b"); d.AddToBack("c"); d.AddToBack("d"); Assert.AreEqual("b", d[1]); Assert.AreEqual("d", d[3]); d[1] = "q"; d[3] = "r"; Assert.AreEqual("q", d[1]); Assert.AreEqual("r", d[3]); InterfaceTests.TestReadWriteListGeneric(d, new string[] { "a", "q", "c", "r" }); }
public void RemoveAtExceptions() { Deque <string> d = new Deque <string>(); d.AddToFront("foo"); d.AddToBack("world"); d.AddToFront("hello"); d.AddToBack("elvis"); d.AddToFront("elvis"); d.AddToBack(null); d.AddToFront("cool"); try { d.RemoveAt(-1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d.RemoveAt(7); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d.RemoveAt(int.MinValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d.RemoveAt(int.MaxValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } }
public void SerializeStrings() { Deque <string> d = new Deque <string>(); d.AddToFront("foo"); d.AddToBack("world"); d.AddToFront("hello"); d.AddToBack("elvis"); d.AddToFront("elvis"); d.AddToBack(null); d.AddToFront("cool"); d.AddManyToFront(new string[] { "1", "2", "3", "4", "5", "6" }); d.AddManyToBack(new string[] { "7", "8", "9", "10", "11", "12" }); Deque <string> result = (Deque <string>)InterfaceTests.SerializeRoundTrip(d); InterfaceTests.TestReadWriteListGeneric <string>((IList <string>)result, new string[] { "1", "2", "3", "4", "5", "6", "cool", "elvis", "hello", "foo", "world", "elvis", null, "7", "8", "9", "10", "11", "12" }); }
public void Clone() { Deque <int> deque1, deque2, deque3, deque4; deque1 = new Deque <int>(); deque2 = deque1.Clone(); deque3 = new Deque <int>(deque1); //(Deque<int>)(((ICloneable)deque1).Clone()); deque4 = new Deque <int>(deque1); InterfaceTests.TestListGeneric <int>(deque2, new int[0], null); InterfaceTests.TestListGeneric <int>(deque3, new int[0], null); InterfaceTests.TestListGeneric <int>(deque4, new int[0], null); deque1.Add(5); InterfaceTests.TestListGeneric <int>(deque2, new int[0], null); InterfaceTests.TestListGeneric <int>(deque3, new int[0], null); InterfaceTests.TestListGeneric <int>(deque4, new int[0], null); int[] array = new int[100]; for (int i = 0; i < 100; ++i) { array[i] = i; } deque1.Clear(); for (int i = 63; i < 100; ++i) { deque1.AddToBack(i); } for (int i = 62; i >= 0; --i) { deque1.AddToFront(i); } deque2 = deque1.Clone(); deque3 = new Deque <int>(deque1); //(Deque<int>)(((ICloneable)deque1).Clone()); deque4 = new Deque <int>(deque1); InterfaceTests.TestListGeneric <int>(deque2, array, null); InterfaceTests.TestListGeneric <int>(deque3, array, null); InterfaceTests.TestListGeneric <int>(deque4, array, null); deque4.Clear(); InterfaceTests.TestListGeneric <int>(deque1, array, null); InterfaceTests.TestListGeneric <int>(deque2, array, null); InterfaceTests.TestListGeneric <int>(deque3, array, null); }
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); } }
public void EmptyExceptions() { Deque <double> d = new Deque <double>(); try { d.GetAtFront(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } try { d.GetAtBack(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } try { d.RemoveFromFront(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } try { d.RemoveFromBack(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } d.AddToBack(2.3); d.RemoveFromFront(); try { d.GetAtFront(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } try { d.GetAtBack(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } try { d.RemoveFromFront(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } try { d.RemoveFromBack(); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is InvalidOperationException); } }
public void IndexerExceptions() { string s = "foo"; Deque <string> d = new Deque <string>(); d.AddToFront("c"); d.AddToFront("b"); d.AddToFront("a"); d.AddToBack("d"); d.AddToBack("e"); d.AddToBack("f"); try { s = d[-1]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { s = d[6]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { s = d[int.MaxValue]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { s = d[int.MinValue]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[-1] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[6] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[int.MaxValue] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[int.MinValue] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } d.Clear(); d.AddToBack("a"); d.AddToBack("b"); d.AddToBack("c"); d.AddToBack("d"); try { s = d[-1]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { s = d[4]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { s = d[int.MaxValue]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { s = d[int.MinValue]; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[-1] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[4] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[int.MaxValue] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } try { d[int.MinValue] = s; Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); } }
public void RandomAddRemoveFrontBack() { 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 && list.Count > 0) { int r = rand.Next(10); if (r < 4) { //Console.WriteLine("RemoveFront()"); int removedList = list[0]; list.RemoveAt(0); int removedDeque = deque.RemoveFromFront(); Assert.AreEqual(removedList, removedDeque); } else if (r < 8) { //Console.WriteLine("RemoveBack()"); int removedList = list[list.Count - 1]; list.RemoveAt(list.Count - 1); int removedDeque = deque.RemoveFromBack(); Assert.AreEqual(removedList, removedDeque); } else { int index = rand.Next(list.Count); //Console.WriteLine("RemoveAt({0})", index); list.RemoveAt(index); deque.RemoveAt(index); } } else { int r = rand.Next(10); int item = rand.Next(1, 1000); if (r < 4) { //Console.WriteLine("AddFront({0})", item); list.Insert(0, item); deque.AddToFront(item); } else if (r < 8) { //Console.WriteLine("AddBack({0})", item); list.Add(item); deque.AddToBack(item); } else { // Add an item. int index = rand.Next(list.Count + 1); //Console.WriteLine("Insert({0}, {1})", index, item); list.Insert(index, item); deque.Insert(index, item); } } //deque.Print(); CheckListAndDeque(list, deque); } InterfaceTests.TestReadWriteList <int>(deque, list.ToArray()); } }
public void RangeExceptions() { Deque <int> deque = new Deque <int>(); IList <int> range; for (int i = 0; i < 50; ++i) { deque.AddToFront(i); } for (int i = 0; i < 50; ++i) { deque.AddToBack(i); } try { range = deque.Range(3, 98); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(-1, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("start", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(0, int.MaxValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(1, int.MinValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(45, int.MinValue); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(0, 101); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(100, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("start", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(int.MinValue, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("start", ((ArgumentOutOfRangeException)e).ParamName); } try { range = deque.Range(int.MaxValue, 1); Assert.Fail("should throw"); } catch (Exception e) { Assert.IsTrue(e is ArgumentOutOfRangeException); Assert.AreEqual("start", ((ArgumentOutOfRangeException)e).ParamName); } }