public void DequeVsQueue() { var list = new List <int>(); var queue = new Queue <int>(new[] { 1, 2, 3, 4 }); queue.Enqueue(5); Assert.Equal(new[] { 1, 2, 3, 4, 5 }, queue); queue = new Queue <int>(); queue.Enqueue(1); queue.Enqueue(2); Assert.Equal(new[] { 1, 2 }, queue); Assert.Equal(new[] { 1, 2 }, queue.ToArray()); var deque = new Deque <int>(); deque.AddLast(1); deque.AddLast(2); Assert.Equal(new[] { 1, 2 }, deque); Assert.Equal(new[] { 1, 2 }, deque.ToArray()); Assert.Equal(queue, deque); list.Clear(); deque.ForEach((item, lst) => lst.Add(item), list); Assert.Equal(queue, list); deque = new Deque <int>(true); deque.AddLast(1); deque.AddLast(2); Assert.Equal(new[] { 2, 1 }, deque); Assert.Equal(new[] { 1, 2 }, deque.ToArray()); list.Clear(); deque.ForEach((item, lst) => lst.Add(item), list); Assert.Equal(queue, list); deque = new Deque <int>(); deque.AddFirst(1); deque.AddFirst(2); Assert.Equal(new[] { 2, 1 }, deque); Assert.Equal(new[] { 2, 1 }, deque.ToArray()); list.Clear(); deque.Reverse((item, lst) => lst.Add(item), list); Assert.Equal(queue, list); deque = new Deque <int>(true); deque.AddFirst(1); deque.AddFirst(2); Assert.Equal(new[] { 1, 2 }, deque); Assert.Equal(new[] { 2, 1 }, deque.ToArray()); list.Clear(); deque.Reverse((item, lst) => lst.Add(item), list); Assert.Equal(queue, list); }
public void DequeVsStack() { var list = new List <int>(); var stack = new Stack <int>(new[] { 1, 2, 3, 4 }); stack.Push(5); Assert.Equal(new[] { 5, 4, 3, 2, 1 }, stack); stack = new Stack <int>(); stack.Push(1); stack.Push(2); Assert.Equal(new[] { 2, 1 }, stack); Assert.Equal(new[] { 2, 1 }, stack.ToArray()); var deque = new Deque <int>(); deque.AddLast(1); deque.AddLast(2); Assert.Equal(new[] { 1, 2 }, deque); Assert.Equal(new[] { 1, 2 }, deque.ToArray()); list.Clear(); deque.Reverse((item, lst) => lst.Add(item), list); Assert.Equal(stack, list); deque = new Deque <int>(true); deque.AddLast(1); deque.AddLast(2); Assert.Equal(new[] { 2, 1 }, deque); Assert.Equal(new[] { 1, 2 }, deque.ToArray()); list.Clear(); deque.Reverse((item, lst) => lst.Add(item), list); Assert.Equal(stack, list); deque = new Deque <int>(); deque.AddFirst(1); deque.AddFirst(2); Assert.Equal(new[] { 2, 1 }, deque); Assert.Equal(new[] { 2, 1 }, deque.ToArray()); list.Clear(); deque.ForEach((item, lst) => lst.Add(item), list); Assert.Equal(stack, list); deque = new Deque <int>(true); deque.AddFirst(1); deque.AddFirst(2); Assert.Equal(new[] { 1, 2 }, deque); Assert.Equal(new[] { 2, 1 }, deque.ToArray()); list.Clear(); deque.ForEach((item, lst) => lst.Add(item), list); Assert.Equal(stack, list); }
public void SetItem_Split_WritesElements() { var deque = new Deque <int>(new[] { 1, 2, 3 }); deque.RemoveLast(); deque.RemoveFirst(); deque.RemoveFirst(); deque.AddFirst(0); deque.AddLast(1); deque.AddLast(2); deque[0] = 7; deque[1] = 11; deque[2] = 13; Assert.Equal(new[] { 7, 11, 13 }, deque); }
public void Constructor_CapacityOf0_PermitsAdd() { var deque = new Deque <int>(0); deque.AddLast(13); Assert.Equal(new[] { 13 }, deque); }
public void Insert_AtCount_IsSameAsAddToBack() { var deque1 = new Deque <int>(new[] { 1, 2 }); var deque2 = new Deque <int>(new[] { 1, 2 }); deque1.Insert(deque1.Count, 0); deque2.AddLast(0); Assert.Equal(deque1, deque2); }
public void Add_IsAddToBack() { var deque1 = new Deque <int>(new[] { 1, 2 }); var deque2 = new Deque <int>(new[] { 1, 2 }); ((ICollection <int>)deque1).Add(3); deque2.AddLast(3); Assert.Equal(deque1, deque2); }
public void Capacity_Set_WhenSplit_PreservesData() { var deque = new Deque <int>(new int[] { 1, 2, 3 }); deque.RemoveFirst(); deque.AddLast(4); Assert.Equal(3, deque.Capacity); deque.Capacity = 7; Assert.Equal(7, deque.Capacity); Assert.Equal(new[] { 2, 3, 4 }, deque); }
public void RemoveAll() { var deque = new Deque <int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }); Assert.Equal(4, deque.RemoveAll(_ => _ >= 4 && _ <= 7)); Assert.Equal(4, deque.Count); Assert.Equal(new[] { 1, 2, 3, 8 }, deque); deque = new Deque <int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); Assert.Equal(9, deque.RemoveLast()); deque.AddFirst(0); Assert.Equal(5, deque.RemoveAll(_ => _ >= 4 && _ <= 7 || _ == 1)); Assert.Equal(4, deque.Count); Assert.Equal(new[] { 0, 2, 3, 8 }, deque); deque = new Deque <int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }); deque.RemoveFirst(); deque.AddLast(9); Assert.Equal(8, deque.Capacity); Assert.True(deque.IsSplit); Assert.Equal(4, deque.RemoveAll(_ => _ >= 4 && _ <= 7)); Assert.Equal(4, deque.Count); Assert.Equal(new[] { 2, 3, 8, 9 }, deque); Assert.False(deque.IsSplit); var list = new List <int>(); deque = new Deque <int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }); var result = deque.TryRemoveFirst(list, 4); Assert.True(result); Assert.Equal(new[] { 1, 2, 3, 4 }, list); Assert.Equal(new[] { 5, 6, 7, 8 }, deque); list.Clear(); result = deque.TryRemoveLast(list, 3); Assert.True(result); Assert.Equal(new[] { 8, 7, 6 }, list); Assert.Single(deque); result = deque.TryRemoveLast(list, 4); Assert.True(result); Assert.Equal(new[] { 8, 7, 6, 5 }, list); Assert.Empty(deque); Assert.False(deque.TryRemoveFirst(list, 10)); Assert.False(deque.TryRemoveLast(list, 10)); }
public void ToArray_CopiesToNewArray() { var deque = new Deque <int>(new[] { 0, 1 }); deque.AddLast(13); var result = deque.ToArray(); Assert.Equal(new[] { 0, 1, 13 }, result); result = deque.ToList().ToArray(); Assert.Equal(new[] { 0, 1, 13 }, result); deque = new Deque <int>(); deque.AddFirst(1); deque.AddFirst(2); deque.AddFirst(3); result = deque.ToArray(); Assert.Equal(new[] { 3, 2, 1 }, result); result = deque.ToList().ToArray(); Assert.Equal(new[] { 3, 2, 1 }, result); }
private void InsertTest(IReadOnlyCollection <int> initial, IReadOnlyCollection <int> items) #endif { var totalCapacity = initial.Count + items.Count; for (int rotated = 0; rotated <= totalCapacity; ++rotated) { for (int index = 0; index <= initial.Count; ++index) { // Calculate the expected result using the slower List<int>. var result = new List <int>(initial); for (int i = 0; i != rotated; ++i) { var item = result[0]; result.RemoveAt(0); result.Add(item); } result.InsertRange(index, items); // First, start off the deque with the initial items. var deque = new Deque <int>(initial); // Ensure there's enough room for the inserted items. deque.Capacity += items.Count; // Rotate the existing items. for (int i = 0; i != rotated; ++i) { var item = deque[0]; deque.RemoveFirst(); deque.AddLast(item); } // Do the insert. deque.InsertRange(index, items); // Ensure the results are as expected. Assert.Equal(result, deque); } } }
private void RemoveTest(IReadOnlyCollection <int> initial) #endif { for (int count = 0; count <= initial.Count; ++count) { for (int rotated = 0; rotated <= initial.Count; ++rotated) { for (int index = 0; index <= initial.Count - count; ++index) { // Calculated the expected result using the slower List<int>. var result = new List <int>(initial); for (int i = 0; i != rotated; ++i) { var item = result[0]; result.RemoveAt(0); result.Add(item); } result.RemoveRange(index, count); // First, start off the deque with the initial items. var deque = new Deque <int>(initial); // Rotate the existing items. for (int i = 0; i != rotated; ++i) { var item = deque[0]; deque.RemoveFirst(); deque.AddLast(item); } // Do the remove. deque.RemoveRange(index, count); // Ensure the results are as expected. Assert.Equal(result, deque); } } } }
public void IndexOf_ItemPresentAndSplit_ReturnsItemIndex() { var deque = new Deque <int>(new[] { 1, 2, 3 }); Assert.Equal(3, deque.RemoveLast()); deque.AddFirst(0); Assert.Equal(0, deque.IndexOf(0)); Assert.Equal(1, deque.IndexOf(1)); Assert.Equal(2, deque.IndexOf(2)); deque = new Deque <int>(new[] { 1, 2, 3 }); deque.TryRemoveLast(out var item); Assert.Equal(3, item); deque.AddFirst(0); Assert.Equal(0, deque.IndexOf(0)); Assert.Equal(1, deque.IndexOf(1)); Assert.Equal(2, deque.IndexOf(2)); deque = new Deque <int>(new[] { 1, 2, 3 }); deque.TryRemoveLastIf(_ => true, out var item1); Assert.Equal(3, item1); deque.AddFirst(0); Assert.Equal(0, deque.IndexOf(0)); Assert.Equal(1, deque.IndexOf(1)); Assert.Equal(2, deque.IndexOf(2)); deque = new Deque <int>(new[] { 1, 2, 3 }); Assert.Equal(1, deque.RemoveFirst()); deque.AddLast(4); Assert.Equal(0, deque.IndexOf(2)); Assert.Equal(1, deque.IndexOf(3)); Assert.Equal(2, deque.IndexOf(4)); deque = new Deque <int>(new[] { 1, 2, 3 }); deque.TryRemoveFirst(out var item2); Assert.Equal(1, item2); deque.AddLast(4); Assert.Equal(0, deque.IndexOf(2)); Assert.Equal(1, deque.IndexOf(3)); Assert.Equal(2, deque.IndexOf(4)); deque = new Deque <int>(new[] { 1, 2, 3 }); deque.TryRemoveFirstIf(_ => true, out var item3); Assert.Equal(1, item3); deque.AddLast(4); Assert.Equal(0, deque.IndexOf(2)); Assert.Equal(1, deque.IndexOf(3)); Assert.Equal(2, deque.IndexOf(4)); deque = new Deque <int>(new[] { 1, 2, 3 }); Assert.Equal(1, deque.GetFirst()); Assert.Equal(3, deque.GetLast()); deque.TryGetFirst(out var first); Assert.Equal(1, first); deque.TryGetLast(out var last); Assert.Equal(3, last); deque.RemoveLast(); deque.RemoveFirst(); Assert.Equal(2, deque.GetFirst()); Assert.Equal(2, deque.GetLast()); deque.TryGetFirst(out first); Assert.Equal(2, first); deque.TryGetLast(out last); Assert.Equal(2, last); deque = new Deque <int>(new[] { 1, 2, 3, 4 }); deque.TryRemoveFirstUntil(_ => _ == 2, out first); Assert.Equal(2, first); Assert.Equal(1, deque.GetFirst()); Assert.Equal(4, deque.GetLast()); Assert.Equal(3, deque.Count); Assert.False(deque.TryRemoveFirstUntil(_ => _ == 10, out first)); deque = new Deque <int>(new[] { 1, 2, 3, 4 }); deque.TryRemoveLastUntil(_ => _ == 3, out last); Assert.Equal(3, last); Assert.Equal(1, deque.GetFirst()); Assert.Equal(4, deque.GetLast()); Assert.Equal(3, deque.Count); Assert.False(deque.TryRemoveLastUntil(_ => _ == 10, out first)); }
public void DequeVsList() { var deque = new Deque <int>(new[] { 1, 2, 3, 4 }); deque.AddLast(5); Assert.Equal(new[] { 1, 2, 3, 4, 5 }, deque); var queue = new Queue <int>(new[] { 1, 2, 3, 4 }); queue.Enqueue(5); Assert.Equal(new[] { 1, 2, 3, 4, 5 }, queue); var stack = new Stack <int>(new[] { 1, 2, 3, 4 }); stack.Push(5); Assert.Equal(new[] { 5, 4, 3, 2, 1 }, stack); var lstComparer = new List <int>(); deque.ForEach((item, lst) => lst.Add(item), lstComparer); Assert.Equal(new[] { 1, 2, 3, 4, 5 }, lstComparer); lstComparer.Clear(); deque.Reverse((item, lst) => lst.Add(item), lstComparer); Assert.Equal(new[] { 5, 4, 3, 2, 1 }, lstComparer); lstComparer = queue.ToList(); Assert.Equal(new[] { 1, 2, 3, 4, 5 }, lstComparer); lstComparer = queue.Reverse().ToList(); Assert.Equal(new[] { 5, 4, 3, 2, 1 }, lstComparer); lstComparer = stack.ToList(); Assert.Equal(new[] { 5, 4, 3, 2, 1 }, lstComparer); lstComparer = stack.Reverse().ToList(); Assert.Equal(new[] { 1, 2, 3, 4, 5 }, lstComparer); var list = new List <int>(new[] { 1, 2 }); Assert.Equal(new[] { 1, 2 }, list); Assert.Equal(new[] { 1, 2 }, list.ToArray()); deque = new Deque <int>(); deque.AddLast(1); deque.AddLast(2); Assert.Equal(new[] { 1, 2 }, deque); Assert.Equal(new[] { 1, 2 }, deque.ToArray()); Assert.Equal(list, deque); deque = new Deque <int>(true); deque.AddLast(1); deque.AddLast(2); Assert.Equal(new[] { 2, 1 }, deque); Assert.Equal(new[] { 1, 2 }, deque.ToArray()); deque = new Deque <int>(); deque.AddFirst(1); deque.AddFirst(2); Assert.Equal(new[] { 2, 1 }, deque); Assert.Equal(new[] { 2, 1 }, deque.ToArray()); deque = new Deque <int>(true); deque.AddFirst(1); deque.AddFirst(2); Assert.Equal(new[] { 1, 2 }, deque); Assert.Equal(new[] { 2, 1 }, deque.ToArray()); Assert.Equal(list, deque); }