public void PushAndPop_StackWithInitialCapacity1_ShouldWorkCorrectly() { //Arrange var stack = new ArrayStack <int>(1); //Assert Assert.AreEqual(0, stack.Count); //Act stack.Push(0); //Assert Assert.AreEqual(1, stack.Count); //Act stack.Push(1); //Assert Assert.AreEqual(2, stack.Count); //Act var secondElement = stack.Pop(); //Assert Assert.AreEqual(1, secondElement); Assert.AreEqual(1, stack.Count); //Act var firstElement = stack.Pop(); //Assert Assert.AreEqual(0, firstElement); Assert.AreEqual(0, stack.Count); }
public void PushAndPop_StackWith1000Elemnts_ShouldTestTheAutoGrowFunctionality() { //Arrange var stack = new ArrayStack <string>(); int i = 0; //Assert Assert.AreEqual(0, stack.Count); while (i != 1000) { //Act i++; stack.Push(i.ToString()); //Assert Assert.AreEqual(i, stack.Count); } while (i != 0) { //Act i--; string lastElement = stack.Pop(); //Assert Assert.AreEqual(i, stack.Count); Assert.AreEqual((i + 1).ToString(), lastElement); } }
private static void Main(string[] args) { var stack = new ArrayStack <int>(); for (int i = 0; i < 50; i++) { stack.Push(i); } Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Count); Console.WriteLine(stack.Pop()); int[] arr = stack.ToArray(); Console.WriteLine(arr[0]); Console.WriteLine(arr.Length); }
public void Pop_EmptyStack_ThrowsAnException() { //Arrange var stack = new ArrayStack <int>(); //Act stack.Pop(); //Assert: expect an exception }
public static void Main(string[] args) { var stack = new ArrayStack <int>(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); stack.Pop(); var array = stack.ToArray(); Console.WriteLine(stack.Peek()); }
public void PushAndPop_StackWith1Element_ShouldRemoveElement() { //Arrange var stack = new ArrayStack <string>(); var elementToBeAdded = "An element"; //Act stack.Push(elementToBeAdded); var removedElement = stack.Pop(); //Assert Assert.AreEqual(0, stack.Count); Assert.AreEqual(elementToBeAdded, removedElement); }
static void Main(string[] args) { //Create a Random Number Generator Random num = new Random(); //Create an ArrayList ArrayList myStack = new ArrayList(); //Create a Array stack instance ArrayStack theStack = new ArrayStack(myStack, 10); WriteLine("Try To Pop An Empty Stack"); theStack.Pop(); //Fill the stack WriteLine("\nFill The Stack With Values"); for (int i = 0; i < theStack.Capacity; i++) { theStack.Push(num.Next(0, 10)); }//End for loop //Print the stack for comparison PrintStack(theStack); //Try out the peek method WriteLine($"\nPeek Method Returned: {theStack.Peek().ToString()}"); //Try to push past capacity WriteLine("\nTry To Push Over Capacity"); theStack.Push(22); //Tryout the pop method WriteLine($"\nPop Method Removed: {theStack.Peek()} From The Stack"); theStack.Pop(); WriteLine($"\nPop Method Removed: {theStack.Peek()} From The Stack"); theStack.Pop(); //Print for comparison PrintStack(theStack); //Try to push again WriteLine("\nPushing Value '22' To The Stack"); theStack.Push(22); //Try to push again WriteLine("\nPushing Value '33' To The Stack"); theStack.Push(33); //Try to push again WriteLine("\nPushing Value '44' To The Stack"); theStack.Push(44); //Print for comparison PrintStack(theStack); //Try out the peek method WriteLine($"\nPeek Method Returned: {theStack.Peek().ToString()}"); //Readkey to stop the program ReadKey(); }