示例#1
0
        public void CopyTo()
        {
            int[] array = new int[5];
              var deque = new Deque<int>(new[] { 2, 3, 4 });
              deque.CopyTo(array, 2);
              Assert.AreEqual(0, array[0]);
              Assert.AreEqual(0, array[1]);
              Assert.AreEqual(2, array[2]);
              Assert.AreEqual(3, array[3]);
              Assert.AreEqual(4, array[4]);

              array = new int[5];
              deque = new Deque<int>();
              deque.EnqueueHead(3);
              deque.EnqueueHead(2);
              deque.EnqueueTail(4);
              deque.CopyTo(array, 2);
              Assert.AreEqual(0, array[0]);
              Assert.AreEqual(0, array[1]);
              Assert.AreEqual(2, array[2]);
              Assert.AreEqual(3, array[3]);
              Assert.AreEqual(4, array[4]);

              Assert.That(() => deque.CopyTo(null, 0), Throws.TypeOf<ArgumentNullException>());
              Assert.That(() => deque.CopyTo(new int[5], -1), Throws.TypeOf<ArgumentOutOfRangeException>());
              Assert.That(() => deque.CopyTo(new int[2], 0), Throws.TypeOf<ArgumentException>());
              Assert.That(() => deque.CopyTo(new int[5], 5), Throws.TypeOf<ArgumentException>());
              Assert.That(() => deque.CopyTo(new int[5], 4), Throws.TypeOf<ArgumentException>());
        }
示例#2
0
 public void CopyToShouldNotThrowIfEmpty()
 {
     int[] array = new int[0];
       var deque = new Deque<int>();
       deque.CopyTo(array, 0);
 }
示例#3
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);
            }
        }