예제 #1
0
        public void NongenericContains_ItemNotPresent_ReturnsFalse()
        {
            var deque = new Deque <int>(new[] { 1, 2 }) as IList;

            Assert.False(deque.Contains(3));
        }
예제 #2
0
        public void NongenericIsReadOnly_ReturnsFalse()
        {
            var deque = new Deque <int>() as IList;

            Assert.False(deque.IsReadOnly);
        }
예제 #3
0
        public void IsReadOnly_ReturnsFalse()
        {
            var deque = new Deque <int>();

            Assert.False(((ICollection <int>)deque).IsReadOnly);
        }
예제 #4
0
        public void NongenericContains_ItemPresent_ReturnsTrue()
        {
            var deque = new Deque <int>(new[] { 1, 2 }) as IList;

            Assert.True(deque.Contains(2));
        }
예제 #5
0
        public void Contains_ItemPresent_ReturnsTrue()
        {
            var deque = new Deque <int>(new[] { 1, 2 }) as ICollection <int>;

            Assert.True(deque.Contains(2));
        }
예제 #6
0
        public void Contains_ItemNotPresent_ReturnsFalse()
        {
            var deque = new Deque <int>(new[] { 1, 2 }) as ICollection <int>;

            Assert.False(deque.Contains(3));
        }
예제 #7
0
        public void Constructor_FromEmptySequence_UsesDefaultCapacity()
        {
            var deque = new Deque <int>(new int[] { });

            Assert.Equal(DefaultCapacity, deque.Capacity);
        }
예제 #8
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));
        }
예제 #9
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);
        }
예제 #10
0
        public void Constructor_CapacityOf1_UsesSpecifiedCapacity()
        {
            var deque = new Deque <int>(1);

            Assert.Equal(1, deque.Capacity);
        }
예제 #11
0
        public void NongenericSyncRoot_IsSelf()
        {
            var deque = new Deque <int>() as IList;

            Assert.Same(deque, deque.SyncRoot);
        }
예제 #12
0
        public void NongenericIsSynchronized_IsFalse()
        {
            var deque = new Deque <int>() as IList;

            Assert.False(deque.IsSynchronized);
        }
예제 #13
0
        public void NongenericIsFixedSize_IsFalse()
        {
            var deque = new Deque <int>() as IList;

            Assert.False(deque.IsFixedSize);
        }
예제 #14
0
        public void Constructor_WithoutExplicitCapacity_UsesDefaultCapacity()
        {
            var deque = new Deque <int>();

            Assert.Equal(DefaultCapacity, deque.Capacity);
        }
예제 #15
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));
        }