Ejemplo n.º 1
0
        public void RemoveAt_RemovesElementAtIndex()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3 });

            deque.RemoveFromBack();
            deque.AddToFront(0);
            deque.RemoveAt(1);
            Assert.Equal(new[] { 0, 2 }, deque);
        }
Ejemplo n.º 2
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.AddToFront(0);
            Assert.Equal(deque1, deque2);
        }
Ejemplo n.º 3
0
        public void GetItem_Split_ReadsElements()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3 });

            deque.RemoveFromBack();
            deque.AddToFront(0);
            Assert.Equal(0, deque[0]);
            Assert.Equal(1, deque[1]);
            Assert.Equal(2, deque[2]);
        }
Ejemplo n.º 4
0
        public void ToArray_CopiesToNewArray()
        {
            var deque = new Deque <int>(new[] { 0, 1 });

            deque.AddToBack(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.AddToFront(1);
            deque.AddToFront(2);
            deque.AddToFront(3);
            result = deque.ToArray();
            Assert.Equal(new[] { 3, 2, 1 }, result);
            result = deque.ToList().ToArray();
            Assert.Equal(new[] { 3, 2, 1 }, result);
        }
Ejemplo n.º 5
0
        public void NongenericIndexOf_ItemPresentAndSplit_ReturnsItemIndex()
        {
            var deque_ = new Deque <int>(new[] { 1, 2, 3 });

            deque_.RemoveFromBack();
            deque_.AddToFront(0);
            var deque = deque_ as IList;

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

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

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

            deque.RemoveFromBack();
            deque.RemoveFromFront();
            deque.RemoveFromFront();
            deque.AddToFront(0);
            deque.AddToBack(1);
            deque.AddToBack(2);
            deque[0] = 7;
            deque[1] = 11;
            deque[2] = 13;
            Assert.Equal(new[] { 7, 11, 13 }, deque);
        }
Ejemplo n.º 9
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.RemoveFromBack());
            deque.AddToFront(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);
        }
Ejemplo n.º 10
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.RemoveFromBack());
            deque.AddToFront(0);
            Assert.True(deque.IsSplit);
            deque_str = deque.ConvertAll(_ => _ + "");
            Assert.Equal(4, deque_str.Count);
            Assert.Equal(new[] { "0", "1", "2", "3" }, deque_str);
        }
Ejemplo n.º 11
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.RemoveFromBack());
            deque.AddToFront(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.RemoveFromFront();
            deque.AddToBack(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.TryRemoveFromFront(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.TryRemoveFromBack(list, 3);
            Assert.True(result);
            Assert.Equal(new[] { 8, 7, 6 }, list);
            Assert.Single(deque);
            result = deque.TryRemoveFromBack(list, 4);
            Assert.True(result);
            Assert.Equal(new[] { 8, 7, 6, 5 }, list);
            Assert.Empty(deque);
            Assert.False(deque.TryRemoveFromFront(list, 10));
            Assert.False(deque.TryRemoveFromBack(list, 10));
        }
Ejemplo n.º 12
0
        public void IndexOf_ItemPresentAndSplit_ReturnsItemIndex()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3 });

            Assert.Equal(3, deque.RemoveFromBack());
            deque.AddToFront(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.TryRemoveFromBack(out var item);
            Assert.Equal(3, item);
            deque.AddToFront(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.TryRemoveFromBackIf(_ => true, out var item1);
            Assert.Equal(3, item1);
            deque.AddToFront(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.RemoveFromFront());
            deque.AddToBack(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.TryRemoveFromFront(out var item2);
            Assert.Equal(1, item2);
            deque.AddToBack(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.TryRemoveFromFrontIf(_ => true, out var item3);
            Assert.Equal(1, item3);
            deque.AddToBack(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.PeekFromFront());
            Assert.Equal(3, deque.PeekFromBack());
            deque.TryPeekFromFront(out var first);
            Assert.Equal(1, first);
            deque.TryPeekFromBack(out var last);
            Assert.Equal(3, last);
            deque.RemoveFromBack();
            deque.RemoveFromFront();
            Assert.Equal(2, deque.PeekFromFront());
            Assert.Equal(2, deque.PeekFromBack());
            deque.TryPeekFromFront(out first);
            Assert.Equal(2, first);
            deque.TryPeekFromBack(out last);
            Assert.Equal(2, last);

            deque = new Deque <int>(new[] { 1, 2, 3, 4 });
            deque.TryRemoveFromFrontUntil(_ => _ == 2, out first);
            Assert.Equal(2, first);
            Assert.Equal(1, deque.PeekFromFront());
            Assert.Equal(4, deque.PeekFromBack());
            Assert.Equal(3, deque.Count);;
            Assert.False(deque.TryRemoveFromFrontUntil(_ => _ == 10, out first));

            deque = new Deque <int>(new[] { 1, 2, 3, 4 });
            deque.TryRemoveFromBackUntil(_ => _ == 3, out last);
            Assert.Equal(3, last);
            Assert.Equal(1, deque.PeekFromFront());
            Assert.Equal(4, deque.PeekFromBack());
            Assert.Equal(3, deque.Count);;
            Assert.False(deque.TryRemoveFromBackUntil(_ => _ == 10, out first));
        }
Ejemplo n.º 13
0
        public void DequeVsList()
        {
            var deque = new Deque <int>(new[] { 1, 2, 3, 4 });

            deque.AddToBack(5);
            Assert.Equal(new[] { 1, 2, 3, 4, 5 }, deque);

            var queue = new QueueX <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.Clear();
            queue.ForEach((item, lst) => lst.Add(item), lstComparer);
            Assert.Equal(new[] { 1, 2, 3, 4, 5 }, lstComparer);

            lstComparer.Clear();
            queue.Reverse((item, lst) => lst.Add(item), lstComparer);
            Assert.Equal(new[] { 5, 4, 3, 2, 1 }, lstComparer);

            //lstComparer.Clear();
            //stack.ForEach((item, lst) => lst.Add(item), lstComparer);
            //Assert.Equal(new[] { 5, 4, 3, 2, 1 }, lstComparer);

            //lstComparer.Clear();
            //stack.Reverse((item, lst) => lst.Add(item), lstComparer);
            //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.AddToBack(1);
            deque.AddToBack(2);
            Assert.Equal(new[] { 1, 2 }, deque);
            Assert.Equal(new[] { 1, 2 }, deque.ToArray());
            Assert.Equal(list, deque);

            deque = new Deque <int>(true);
            deque.AddToBack(1);
            deque.AddToBack(2);
            Assert.Equal(new[] { 2, 1 }, deque);
            Assert.Equal(new[] { 1, 2 }, deque.ToArray());

            deque = new Deque <int>();
            deque.AddToFront(1);
            deque.AddToFront(2);
            Assert.Equal(new[] { 2, 1 }, deque);
            Assert.Equal(new[] { 2, 1 }, deque.ToArray());

            deque = new Deque <int>(true);
            deque.AddToFront(1);
            deque.AddToFront(2);
            Assert.Equal(new[] { 1, 2 }, deque);
            Assert.Equal(new[] { 2, 1 }, deque.ToArray());
            Assert.Equal(list, deque);
        }