PerformActionOnAllStackWrappers() public static method

public static PerformActionOnAllStackWrappers ( Stack stack, Action action ) : void
stack Stack
action Action
return void
Example #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();
                    }
                }
            });
        }
Example #2
0
        public static void Pop_EmptyStack_ThrowsInvalidOperationException()
        {
            var stack1 = new Stack();

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                Assert.Throws <InvalidOperationException>(() => stack2.Pop()); // Empty stack
            });
        }
Example #3
0
        public static void TestPop_Invalid()
        {
            var stack1 = new Stack();

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                Assert.Throws <InvalidOperationException>(() => stack2.Pop());
            });
        }
Example #4
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);
            });
        }
Example #5
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);
            });
        }
Example #6
0
        public static void Push(int count)
        {
            var stack1 = new Stack();

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                for (int i = 0; i < count; i++)
                {
                    stack2.Push(i);
                    Assert.Equal(i + 1, stack2.Count);
                }
                Assert.Equal(count, stack2.Count);
            });
        }
Example #7
0
        public static void Push_Null()
        {
            var stack1 = new Stack();

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                stack2.Push(null);
                stack2.Push(-1);
                stack2.Push(null);

                Assert.True(stack2.Contains(null));
                Assert.True(stack2.Contains(-1));
            });
        }
Example #8
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
            });
        }
Example #9
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]);
                }
            });
        }
Example #10
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]);
                }
            });
        }
Example #11
0
        public static void Pop_Null()
        {
            var stack1 = new Stack();

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                stack2.Push(null);
                stack2.Push(-1);
                stack2.Push(null);

                Assert.Null(stack2.Pop());
                Assert.Equal(-1, stack2.Pop());
                Assert.Null(stack2.Pop());
            });
        }
Example #12
0
        public static void Clone_IsShallowCopy()
        {
            var stack1 = new Stack();

            stack1.Push(new Foo(10));
            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                Stack stackClone = (Stack)stack2.Clone();

                Foo a1      = (Foo)stack2.Pop();
                a1.IntValue = 50;

                Foo a2 = (Foo)stackClone.Pop();
                Assert.Equal(50, a1.IntValue);
            });
        }
Example #13
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());
                }
            });
        }
Example #14
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);
                }
            });
        }
Example #15
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);
            });
        }
Example #16
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
            });
        }
Example #17
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();
                }
            });
        }
Example #18
0
        public static void Peek_EmptyStack_ThrowsInvalidOperationException()
        {
            var stack1 = new Stack();

            Helpers.PerformActionOnAllStackWrappers(stack1, stack2 =>
            {
                Assert.Throws <InvalidOperationException>(() => stack2.Peek()); // Empty stack

                for (int i = 0; i < 1000; i++)
                {
                    stack2.Push(i);
                }

                for (int i = 0; i < 1000; i++)
                {
                    stack2.Pop();
                }

                Assert.Throws <InvalidOperationException>(() => stack2.Peek()); // Empty stack
            });
        }