Exemplo n.º 1
0
        public void EmptyExceptions()
        {
            Deque <double> d = new Deque <double>();

            try {
                d.GetAtFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.GetAtBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            d.AddToBack(2.3);
            d.RemoveFromFront();

            try {
                d.GetAtFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.GetAtBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }
        }
Exemplo n.º 2
0
        public void RandomAddRemoveFrontBack()
        {
            const int ITER = 2000, LOOP = 15;
            Random    rand = new Random(13);

            for (int loop = 0; loop < LOOP; ++loop)
            {
                Deque <int> deque = new Deque <int>();
                List <int>  list  = new List <int>();
                for (int iter = 0; iter < ITER; ++iter)
                {
                    //Console.Write("Loop {0}, Iteration {1}: ", loop, iter);
                    if (rand.Next(100) < 45 && list.Count > 0)
                    {
                        int r = rand.Next(10);
                        if (r < 4)
                        {
                            //Console.WriteLine("RemoveFront()");
                            int removedList = list[0];
                            list.RemoveAt(0);
                            int removedDeque = deque.RemoveFromFront();
                            Assert.AreEqual(removedList, removedDeque);
                        }
                        else if (r < 8)
                        {
                            //Console.WriteLine("RemoveBack()");
                            int removedList = list[list.Count - 1];
                            list.RemoveAt(list.Count - 1);
                            int removedDeque = deque.RemoveFromBack();
                            Assert.AreEqual(removedList, removedDeque);
                        }
                        else
                        {
                            int index = rand.Next(list.Count);
                            //Console.WriteLine("RemoveAt({0})", index);
                            list.RemoveAt(index);
                            deque.RemoveAt(index);
                        }
                    }
                    else
                    {
                        int r    = rand.Next(10);
                        int item = rand.Next(1, 1000);
                        if (r < 4)
                        {
                            //Console.WriteLine("AddFront({0})", item);
                            list.Insert(0, item);
                            deque.AddToFront(item);
                        }
                        else if (r < 8)
                        {
                            //Console.WriteLine("AddBack({0})", item);
                            list.Add(item);
                            deque.AddToBack(item);
                        }
                        else
                        {
                            // Add an item.
                            int index = rand.Next(list.Count + 1);
                            //Console.WriteLine("Insert({0}, {1})", index, item);
                            list.Insert(index, item);
                            deque.Insert(index, item);
                        }
                    }

                    //deque.Print();
                    CheckListAndDeque(list, deque);
                }

                InterfaceTests.TestReadWriteList <int>(deque, list.ToArray());
            }
        }