コード例 #1
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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.AddToBack(1);
            deque.AddToBack(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.AddToBack(1);
            deque.AddToBack(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.AddToFront(1);
            deque.AddToFront(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.AddToFront(1);
            deque.AddToFront(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);
        }
コード例 #2
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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.AddToBack(1);
            deque.AddToBack(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.AddToBack(1);
            deque.AddToBack(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.AddToFront(1);
            deque.AddToFront(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.AddToFront(1);
            deque.AddToFront(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);
        }
コード例 #3
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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);
        }
コード例 #4
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        public void Constructor_CapacityOf0_PermitsAdd()
        {
            var deque = new Deque <int>(0);

            deque.AddToBack(13);
            Assert.Equal(new[] { 13 }, deque);
        }
コード例 #5
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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.AddToBack(0);
            Assert.Equal(deque1, deque2);
        }
コード例 #6
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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.AddToBack(3);
            Assert.Equal(deque1, deque2);
        }
コード例 #7
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        public void Capacity_Set_WhenSplit_PreservesData()
        {
            var deque = new Deque <int>(new int[] { 1, 2, 3 });

            deque.RemoveFromFront();
            deque.AddToBack(4);
            Assert.Equal(3, deque.Capacity);
            deque.Capacity = 7;
            Assert.Equal(7, deque.Capacity);
            Assert.Equal(new[] { 2, 3, 4 }, deque);
        }
コード例 #8
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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));
        }
コード例 #9
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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);
        }
コード例 #10
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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.RemoveFromFront();
                        deque.AddToBack(item);
                    }

                    // Do the insert.
                    deque.InsertRange(index, items);

                    // Ensure the results are as expected.
                    Assert.Equal(result, deque);
                }
            }
        }
コード例 #11
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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.RemoveFromFront();
                            deque.AddToBack(item);
                        }

                        // Do the remove.
                        deque.RemoveRange(index, count);

                        // Ensure the results are as expected.
                        Assert.Equal(result, deque);
                    }
                }
            }
        }
コード例 #12
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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));
        }
コード例 #13
0
ファイル: DequeTests.cs プロジェクト: dora-BYR/Fenix
        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);
        }