Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        public void RemoveAt_RemovesElementAtIndex()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3 });

            deque.RemoveLast();
            deque.AddFirst​(0);
            deque.RemoveAt(1);
            Assert.Equal(new[] { 0, 2 }, deque);
        }
Exemplo n.º 4
0
        public void Insert_AtIndex0_IsSameAsAddToFront()
        {
            var deque1 = new Deque <int>(new[] { 1, 2 });
            var deque2 = new Deque <int>(new[] { 1, 2 });

            deque1.Insert(0, 0);
            deque2.AddFirst​(0);
            Assert.Equal(deque1, deque2);
        }
Exemplo n.º 5
0
        public void GetItem_Split_ReadsElements()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3 });

            deque.RemoveLast();
            deque.AddFirst​(0);
            Assert.Equal(0, deque[0]);
            Assert.Equal(1, deque[1]);
            Assert.Equal(2, deque[2]);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void NongenericIndexOf_ItemPresentAndSplit_ReturnsItemIndex()
        {
            var deque_ = new Deque <int>(new[] { 1, 2, 3 });

            deque_.RemoveLast();
            deque_.AddFirst​(0);
            var deque = deque_ as IList;

            Assert.Equal(0, deque.IndexOf(0));
            Assert.Equal(1, deque.IndexOf(1));
            Assert.Equal(2, deque.IndexOf(2));
        }
Exemplo n.º 8
0
        public void NongenericContains_ItemPresentAndSplit_ReturnsTrue()
        {
            var deque_ = new Deque <int>(new[] { 1, 2, 3 });

            deque_.RemoveLast();
            deque_.AddFirst​(0);
            var deque = deque_ as IList;

            Assert.True(deque.Contains(0));
            Assert.True(deque.Contains(1));
            Assert.True(deque.Contains(2));
            Assert.False(deque.Contains(3));
        }
Exemplo n.º 9
0
        public void Contains_ItemPresentAndSplit_ReturnsTrue()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3 });

            deque.RemoveLast();
            deque.AddFirst​(0);
            var deq = deque as ICollection <int>;

            Assert.True(deq.Contains(0));
            Assert.True(deq.Contains(1));
            Assert.True(deq.Contains(2));
            Assert.False(deq.Contains(3));
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        public void UpdateAll()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 });

            Assert.Equal(4, deque.UpdateAll(_ => _ >= 4 && _ <= 7, _ => _ * 2));
            Assert.Equal(8, deque.Count);
            Assert.Equal(new[] { 1, 2, 3, 8, 10, 12, 14, 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(4, deque.UpdateAll(_ => _ % 2 == 1, _ => _ * 5));
            Assert.Equal(9, deque.Count);
            Assert.Equal(new[] { 0, 5, 2, 15, 4, 25, 6, 35, 8 }, deque);
        }
Exemplo n.º 12
0
        public void ConvertAll()
        {
            var deque     = new Deque <int>(new[] { 1, 2, 3, 4 });
            var deque_str = deque.ConvertAll(_ => _ + "");

            Assert.Equal(4, deque_str.Count);
            Assert.Equal(new[] { "1", "2", "3", "4" }, deque_str);

            deque = new Deque <int>(new[] { 1, 2, 3, 4 });
            Assert.Equal(4, deque.RemoveLast());
            deque.AddFirst​(0);
            Assert.True(deque.IsSplit);
            deque_str = deque.ConvertAll(_ => _ + "");
            Assert.Equal(4, deque_str.Count);
            Assert.Equal(new[] { "0", "1", "2", "3" }, deque_str);
        }
Exemplo n.º 13
0
        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));
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
        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);
        }