Пример #1
0
        public static void TestGetEnumerator(int count)
        {
            Stack stack1 = Helpers.CreateIntStack(count);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                IEnumerator enumerator1 = stack2.GetEnumerator();
                IEnumerator enumerator2 = stack2.GetEnumerator();

                IEnumerator[] enumerators = { enumerator1, enumerator2 };
                foreach (IEnumerator enumerator in enumerators)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        int counter = 0;
                        while (enumerator.MoveNext())
                        {
                            counter++;
                            Assert.NotNull(enumerator.Current);
                        }
                        Assert.Equal(count, counter);

                        enumerator.Reset();
                    }
                }
            });
        }
Пример #2
0
        public static void Clear()
        {
            Stack stack1 = Helpers.CreateIntStack(100);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                stack2.Clear();
                Assert.Equal(0, stack2.Count);

                stack2.Clear();
                Assert.Equal(0, stack2.Count);
            });
        }
Пример #3
0
        public static void Pop(int pushCount, int popCount)
        {
            Stack stack1 = Helpers.CreateIntStack(pushCount);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                for (int i = 0; i < popCount; i++)
                {
                    Assert.Equal(pushCount - i - 1, stack2.Pop());
                    Assert.Equal(pushCount - i - 1, stack2.Count);
                }
                Assert.Equal(pushCount - popCount, stack2.Count);
            });
        }
Пример #4
0
        public static void ToArray(int count)
        {
            Stack stack1 = Helpers.CreateIntStack(count);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                object[] array = stack2.ToArray();

                Assert.Equal(stack2.Count, array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    Assert.Equal(stack2.Pop(), array[i]);
                }
            });
        }
Пример #5
0
        public static void CopyTo_Invalid()
        {
            Stack stack1 = Helpers.CreateIntStack(100);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                AssertExtensions.Throws <ArgumentNullException>("array", () => stack2.CopyTo(null, 0));                   // Array is null
                AssertExtensions.Throws <ArgumentException>("array", null, () => stack2.CopyTo(new object[10, 10], 0));   // Array is multidimensional

                AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => stack2.CopyTo(new object[100], -1)); // Index < 0
                AssertExtensions.Throws <ArgumentException>(null, () => stack2.CopyTo(new object[0], 0));                 // Index >= array.Count
                AssertExtensions.Throws <ArgumentException>(null, () => stack2.CopyTo(new object[100], 1));               // Index + array.Count > stack.Count
                AssertExtensions.Throws <ArgumentException>(null, () => stack2.CopyTo(new object[150], 51));              // Index + array.Count > stack.Count
            });
        }
Пример #6
0
        public static void CopyTo_IntArray(int count, int index)
        {
            Stack stack1 = Helpers.CreateIntStack(count);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                int[] iArray = new int[index + count];
                stack2.CopyTo(iArray, index);
                Assert.Equal(index + count, iArray.Length);
                for (int i = index; i < count; i++)
                {
                    Assert.Equal(stack2.Pop(), iArray[i]);
                }
            });
        }
Пример #7
0
        public static void Clone(int count)
        {
            Stack stack1 = Helpers.CreateIntStack(count);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                Stack stackClone = (Stack)stack2.Clone();

                Assert.Equal(stack2.Count, stackClone.Count);
                Assert.Equal(stack2.IsSynchronized, stackClone.IsSynchronized);

                for (int i = 0; i < stackClone.Count; i++)
                {
                    Assert.Equal(stack2.Pop(), stackClone.Pop());
                }
            });
        }
Пример #8
0
        public static void Peek()
        {
            int   count  = 100;
            Stack stack1 = Helpers.CreateIntStack(count);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                for (int i = 0; i < count; i++)
                {
                    int peek1 = (int)stack2.Peek();
                    int peek2 = (int)stack2.Peek();

                    Assert.Equal(peek1, peek2);
                    Assert.Equal(stack2.Pop(), peek1);
                    Assert.Equal(count - i - 1, peek1);
                }
            });
        }
Пример #9
0
        public static void GetEnumerator_Invalid()
        {
            Stack stack1 = Helpers.CreateIntStack(100);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                IEnumerator enumerator = stack2.GetEnumerator();

                // Index < 0
                Assert.Throws <InvalidOperationException>(() => enumerator.Current);

                // Index > dictionary.Count
                while (enumerator.MoveNext())
                {
                    ;
                }
                Assert.False(enumerator.MoveNext());
                Assert.Throws <InvalidOperationException>(() => enumerator.Current);

                // Current throws after resetting
                enumerator.Reset();
                Assert.True(enumerator.MoveNext());

                enumerator.Reset();
                Assert.Throws <InvalidOperationException>(() => enumerator.Current);

                // MoveNext and Reset throws after stack is modified, but current doesn't
                enumerator = stack2.GetEnumerator();
                enumerator.MoveNext();
                stack2.Push("hi");
                Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
                Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
                Assert.NotNull(enumerator.Current);

                enumerator = stack2.GetEnumerator();
                enumerator.MoveNext();
                stack2.Pop();
                Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
                Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
                Assert.NotNull(enumerator.Current);
            });
        }
Пример #10
0
        public static void GetEnumerator(int count)
        {
            Stack stack1 = Helpers.CreateIntStack(count);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                Assert.NotSame(stack2.GetEnumerator(), stack2.GetEnumerator());
                IEnumerator enumerator = stack2.GetEnumerator();
                for (int i = 0; i < 2; i++)
                {
                    int counter = 0;
                    while (enumerator.MoveNext())
                    {
                        counter++;
                        Assert.NotNull(enumerator.Current);
                    }
                    Assert.Equal(count, counter);
                    enumerator.Reset();
                }
            });
        }
Пример #11
0
        public static void Contains()
        {
            Stack stack1 = Helpers.CreateIntStack(100);

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                for (int i = 0; i < stack2.Count; i++)
                {
                    Assert.True(stack2.Contains(i));
                }

                Assert.False(stack2.Contains(101));
                Assert.False(stack2.Contains("hello"));
                Assert.False(stack2.Contains(null));

                stack2.Push(null);
                Assert.True(stack2.Contains(null));

                Assert.False(stack2.Contains(-1)); // We have a null item in the list, so the algorithm may use a different branch
            });
        }
Пример #12
0
        public static void GetEnumerator_InMiddleOfEnumeration_Clone()
        {
            Stack stack = Helpers.CreateIntStack(10);

            IEnumerator enumerator = stack.GetEnumerator();

            enumerator.MoveNext();
            ICloneable cloneableEnumerator = (ICloneable)enumerator;

            // Cloned and original enumerators should start at the same spot, even
            // if the original is in the middle of enumeration.
            IEnumerator clonedEnumerator = (IEnumerator)cloneableEnumerator.Clone();

            Assert.Equal(enumerator.Current, clonedEnumerator.Current);

            for (int i = 0; i < stack.Count - 1; i++)
            {
                Assert.True(clonedEnumerator.MoveNext());
            }

            Assert.False(clonedEnumerator.MoveNext());
        }
Пример #13
0
        public static void GetEnumerator_StartOfEnumeration_Clone()
        {
            Stack stack = Helpers.CreateIntStack(10);

            IEnumerator enumerator          = stack.GetEnumerator();
            ICloneable  cloneableEnumerator = (ICloneable)enumerator;

            IEnumerator clonedEnumerator = (IEnumerator)cloneableEnumerator.Clone();

            Assert.NotSame(enumerator, clonedEnumerator);

            // Cloned and original enumerators should enumerate separately.
            Assert.True(enumerator.MoveNext());
            Assert.Equal(stack.Count - 1, enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => clonedEnumerator.Current);

            Assert.True(clonedEnumerator.MoveNext());
            Assert.Equal(stack.Count - 1, enumerator.Current);
            Assert.Equal(stack.Count - 1, clonedEnumerator.Current);

            // Cloned and original enumerators should enumerate in the same sequence.
            for (int i = 1; i < stack.Count; i++)
            {
                Assert.True(enumerator.MoveNext());
                Assert.NotEqual(enumerator.Current, clonedEnumerator.Current);

                Assert.True(clonedEnumerator.MoveNext());
                Assert.Equal(enumerator.Current, clonedEnumerator.Current);
            }

            Assert.False(enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Equal(0, clonedEnumerator.Current);

            Assert.False(clonedEnumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => clonedEnumerator.Current);
        }