Example #1
0
        public void Clear()
        {
            var deque = new Deque<int>(new[] { 2, 3, 4, 5, 6 });
              deque.Clear();
              Assert.AreEqual(0, deque.Count);

              deque = new Deque<int>(new[] { 2, 3, 4, 5, 6 });
              deque.EnqueueHead(1);
              deque.EnqueueHead(0);
              deque.Clear();
              Assert.AreEqual(0, deque.Count);

              deque = new Deque<int>();
              deque.EnqueueHead(1);
              deque.EnqueueHead(0);
              deque.Clear();
              Assert.AreEqual(0, deque.Count);
        }
        private static void PopulateDequePushFront(Deque deque)
        {
            deque.Clear();

            for(int i = 0; i < ElementCount; i++)
            {
                deque.PushFront(i);
            }

            Debug.Assert(deque.Count == ElementCount);

            int j = ElementCount - 1;

            foreach(int i in deque)
            {
                Debug.Assert(i == j);
                j--;
            }
        }
        private static void TestClone(Deque deque)
        {
            deque.Clear();

            PopulateDequePushBack(deque);

            Deque deque2 = (Deque)deque.Clone();

            Debug.Assert(deque.Count == deque2.Count);

            IEnumerator d2 = deque2.GetEnumerator();

            d2.MoveNext();

            foreach(object obj in deque)
            {
                Debug.Assert(obj.Equals(d2.Current));

                d2.MoveNext();
            }
        }
Example #4
0
        public void TrimExcess()
        {
            var deque = new Deque<int>();
              deque.TrimExcess();

              deque = new Deque<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
              deque.TrimExcess();

              deque.Clear();
              deque.TrimExcess();
        }
Example #5
0
        public void Indexer()
        {
            var deque = new Deque<int>(new[] { 2, 3, 4, 5, 6 });
              Assert.AreEqual(5, deque.Count);
              Assert.AreEqual(2, deque[0]);
              Assert.AreEqual(3, deque[1]);
              Assert.AreEqual(4, deque[2]);
              Assert.AreEqual(5, deque[3]);
              Assert.AreEqual(6, deque[4]);

              deque[0] = 1;
              deque[4] = 4;
              Assert.AreEqual(1, deque[0]);
              Assert.AreEqual(4, deque[4]);

              Assert.That(() => { int i = deque[-1]; }, Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => { int i = deque[5]; }, Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => { deque[-1] = 0; }, Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => { deque[-5] = 0; }, Throws.TypeOf<ArgumentOutOfRangeException>());

              deque.Clear();
              Assert.That(() => { int i = deque[0]; }, Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => { int i = deque[0]; }, Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => { deque[0] = 0; }, Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => { deque[0] = 0; }, Throws.TypeOf<ArgumentOutOfRangeException>());
        }
Example #6
0
        public void Count()
        {
            var deque = new Deque<int>();
              Assert.AreEqual(0, deque.Count);

              deque.EnqueueHead(1);
              deque.EnqueueHead(0);
              deque.EnqueueTail(2);
              deque.EnqueueTail(3);
              deque.EnqueueTail(4);
              deque.DequeueHead();
              deque.DequeueTail();
              Assert.AreEqual(3, deque.Count);

              deque.Clear();
              Assert.AreEqual(0, deque.Count);
        }
        private static void TestContains(Deque deque)
        {
            deque.Clear();

            PopulateDequePushBack(deque);

            for(int i = 0; i < deque.Count; i++)
            {
                Debug.Assert(deque.Contains(i));
            }

            Debug.Assert(!deque.Contains(ElementCount));
        }
        private static void TestToArray(Deque deque)
        {
            deque.Clear();

            PopulateDequePushBack(deque);

            object[] array = deque.ToArray();
            int i = 0;

            foreach(object obj in deque)
            {
                Debug.Assert(obj.Equals(array[i]));
                i++;
            }
        }
        private static void TestPopFront(Deque deque)
        {
            deque.Clear();

            PopulateDequePushBack(deque);

            int j;

            for(int i = 0; i < ElementCount; i++)
            {
                j = (int)deque.PopFront();

                Debug.Assert(j == i);
            }

            Debug.Assert(deque.Count == 0);
        }
Example #10
0
        private static void TestEnumerator(Deque deque)
        {
            deque.Clear();

            PopulateDequePushBack(deque);

            IEnumerator e = deque.GetEnumerator();

            try
            {
                object obj = e.Current;

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                foreach(object obj in deque)
                {
                    Debug.Assert(e.MoveNext());
                }

                Debug.Assert(!e.MoveNext());

                object o = e.Current;

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                e.Reset();

                foreach(object obj in deque)
                {
                    Debug.Assert(e.MoveNext());
                }

                Debug.Assert(!e.MoveNext());

                object o = e.Current;

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                deque.PushBack(deque.Count);

                e.Reset();

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                e.MoveNext();

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #11
0
        private static void TestDeque(Deque deque)
        {
            deque.Clear();
            Debug.Assert(deque.Count == 0);

            PopulateDequePushFront(deque);
            PopulateDequePushBack(deque);
            TestPopFront(deque);
            TestPopBack(deque);
            TestContains(deque);
            TestCopyTo(deque);
            TestToArray(deque);
            TestClone(deque);
            TestEnumerator(deque);
        }
Example #12
0
        private static void TestCopyTo(Deque deque)
        {
            deque.Clear();

            PopulateDequePushBack(deque);

            int[] array = new int[deque.Count];

            deque.CopyTo(array, 0);

            foreach(int i in deque)
            {
                Debug.Assert(array[i] == i);
            }

            array = new int[deque.Count * 2];

            deque.CopyTo(array, deque.Count);

            foreach(int i in deque)
            {
                Debug.Assert(array[i + deque.Count] == i);
            }

            array = new int[deque.Count];

            try
            {
                deque.CopyTo(null, deque.Count);

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                deque.CopyTo(array, -1);

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                deque.CopyTo(array, deque.Count / 2);

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                deque.CopyTo(array, deque.Count);

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                deque.CopyTo(new int[10, 10], deque.Count);

                Debug.Fail("Exception failed");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }