示例#1
0
        public void DequeList_AllMethodsIsolated()
        {
            Random                r      = new Random();
            DequeList <int>       list   = new DequeList <int>();
            DequeListTester <int> tester = new DequeListTester <int>();

            int    mismatchIndex;
            string errorMessage;

            int size = 1000;

            for (int i = 0; i < size; i++)
            {
                int item = r.Next(0, 2 * size);
                list.Add(item);
                tester.Add(item);
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            for (mismatchIndex = size - 1; mismatchIndex >= 0; mismatchIndex--)
            {
                int item1 = list.PopRight();
                int item2 = tester.PopRight();
                Assert.AreEqual <int>(item1, item2, String.Format("Mismatch during PopRight of popped items at index {0}", mismatchIndex));
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            for (int i = 0; i < size; i++)
            {
                int item = r.Next(0, 2 * size);
                list.PushRight(item);
                tester.PushRight(item);
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            for (mismatchIndex = size - 1; mismatchIndex >= 0; mismatchIndex--)
            {
                int item1 = list.PopRight();
                int item2 = tester.PopRight();
                Assert.AreEqual <int>(item1, item2, String.Format("Mismatch during PopRight of popped items at index {0}", mismatchIndex));
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            for (int i = 0; i < size; i++)
            {
                int item = r.Next(0, 2 * size);
                list.PushLeft(item);
                tester.PushLeft(item);
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            for (mismatchIndex = size - 1; mismatchIndex >= 0; mismatchIndex--)
            {
                int item1 = list.PopLeft();
                int item2 = tester.PopLeft();
                Assert.AreEqual <int>(item1, item2, String.Format("Mismatch during PopLeft of popped items at index {0}", mismatchIndex));
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            for (int i = 0; i < size; i++)
            {
                int item = r.Next(0, 2 * size);
                list.PushLeft(item);
                tester.PushLeft(item);
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }

            while (list.Count > 0)
            {
                int x     = r.Next(0, list.Count);
                int item1 = list[x];
                int item2 = tester[x];
                list.RemoveAt(x);
                tester.RemoveAt(x);
                Assert.AreEqual <int>(item1, item2, String.Format("Mismatch during RemoveAt of popped items at index {0}", mismatchIndex));
                Assert.IsTrue(TestEquality(list, tester, Comparer <int> .Default, out mismatchIndex, out errorMessage), errorMessage);
            }
        }
示例#2
0
        private ActionPair[] MakeActionList <T>(Func <T> generator, DequeList <T> list, DequeListTester <T> tester)
        {
            List <ActionPair> actionList = new List <ActionPair>();
            Random            r          = new Random();

            actionList.Add(new ActionPair("PushLeft", () =>
            {
                T x = generator();
                list.PushLeft(x);
                tester.PushLeft(x);
                return(true);
            }));

            actionList.Add(new ActionPair("PushRight", () =>
            {
                T x = generator();
                list.PushRight(x);
                tester.PushRight(x);
                return(true);
            }));

            actionList.Add(new ActionPair("PopLeft", () =>
            {
                T x1    = default(T), x2 = default(T);
                bool e1 = false, e2 = false;

                try { x1 = list.PopLeft(); }
                catch { e1 = true; }
                try { x2 = tester.PopLeft(); }
                catch { e2 = true; }

                if (!e1 && !e2)
                {
                    return(x1.Equals(x2));
                }
                else if (e1 != e2)
                {
                    return(false);
                }

                return(true);
            }));

            actionList.Add(new ActionPair("PopRight", () =>
            {
                T x1    = default(T), x2 = default(T);
                bool e1 = false, e2 = false;

                try { x1 = list.PopRight(); }
                catch { e1 = true; }
                try { x2 = tester.PopRight(); }
                catch { e2 = true; }

                if (!e1 && !e2)
                {
                    return(x1.Equals(x2));
                }
                else if (e1 != e2)
                {
                    return(false);
                }

                return(true);
            }));

            actionList.Add(new ActionPair("Contains", () =>
            {
                if (r.Next(0, 2) == 0) // check for an actually contained item
                {
                    if (list.Count > 0)
                    {
                        int index = r.Next(0, list.Count);
                        T elem    = list[index];
                        return(list.Contains(elem) && tester.Contains(elem));
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    T elem = generator();
                    return(list.Contains(elem) == tester.Contains(elem));
                }
            }));

            actionList.Add(new ActionPair("Remove", () =>
            {
                if (r.Next(0, 2) == 0) // check for an actually contained item
                {
                    if (list.Count > 0)
                    {
                        int index = r.Next(0, list.Count);
                        T elem    = list[index];
                        return(list.Remove(elem) == true && tester.Remove(elem));
                    }
                    else
                    {
                        return(!list.Remove(generator()));
                    }
                }
                else
                {
                    T elem = generator();
                    return(list.Remove(elem) == tester.Remove(elem));
                }
            }));

            actionList.Add(new ActionPair("IndexOf", () =>
            {
                if (r.Next(0, 2) == 0) // check for an actually contained item
                {
                    if (list.Count > 0)
                    {
                        int index = r.Next(0, list.Count);
                        T elem    = list[index];
                        return(list.IndexOf(elem) == index && tester.IndexOf(elem) == index);
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    T elem = generator();
                    return(list.Contains(elem) == tester.Contains(elem));
                }
            }));

            actionList.Add(new ActionPair("RemoveAt", () =>
            {
                if (list.Count > 0)
                {
                    int index = r.Next(0, list.Count);
                    list.RemoveAt(index);
                    tester.RemoveAt(index);
                }

                return(true);
            }));

            actionList.Add(new ActionPair("Insert", () =>
            {
                int index = r.Next(0, list.Count);
                T elem    = generator();
                try { list.Insert(index, elem); }
                catch (Exception ex) { throw new Exception(String.Format("The index being inserted to was {0}. Element = {1}. Original exception message = {2}.", index, elem, ex.Message), ex); }

                tester.Insert(index, elem);
                return(true);
            }));

            actionList.Add(new ActionPair("CopyTo", () =>
            {
                T[] cpy1 = new T[list.Count];
                T[] cpy2 = new T[tester.Count];
                list.CopyTo(cpy1, 0);
                tester.CopyTo(cpy2, 0);
                int mismatchIndex;
                string message;
                return(TestEquality <T>(cpy1, cpy2, Comparer <T> .Default, out mismatchIndex, out message));
            }));

            return(actionList.ToArray());
        }