Code example for Stack

Methods: peekpoppushsize

0
  } 
 
  /** Tests pop and peek. */ 
  public void testPopAndPeek() { 
    Stack<String> testStack = createStack();
    int x = testStack.size();
    Object item = testStack.peek();
    assertTrue(testStack.pop() == item);
    assertEquals(x - 1, testStack.size());
  } 
 
  /** Tests push and peek. */ 
  public void testPushAndPeek() { 
    Stack<String> testStack = createStack();
    int x = testStack.size();
    String item = "4";
    testStack.push(item);
    assertEquals(x + 1, testStack.size());
    assertTrue(testStack.peek() == item);
  } 
 
  /** 
   * Tests all combinations of search for a stack that attains a max size of 
   * TEST_SEARCH_SIZE. 
   */ 
  public void testSearch() { 
    Stack<Integer> searchStack = new Stack<Integer>();
    checkEmptiness(searchStack);
    for (int stackSizeIncreasing = 0; stackSizeIncreasing < TEST_SEARCH_SIZE;
        stackSizeIncreasing++) {
      for (int theInt = 0; theInt < stackSizeIncreasing; theInt++) {
        assertEquals( 
            "Increasing search found", searchStack.search(theInt), searchStack.size() - theInt);
      } 
      for (int theInt = stackSizeIncreasing; theInt < TEST_SEARCH_SIZE; theInt++) {
        assertEquals("Increasing not found", searchStack.search(theInt), -1);
      } 
      searchStack.push(stackSizeIncreasing);
    } 
    for (int stackSizeDecreasing = TEST_SEARCH_SIZE - 1; stackSizeDecreasing >= 0;
        stackSizeDecreasing--) {
      for (int theInt = TEST_SEARCH_SIZE - 1; theInt > stackSizeDecreasing; theInt--) {
        assertEquals("Search decreasing not found", searchStack.search(theInt), -1);
      } 
      for (int theInt = stackSizeDecreasing; theInt >= 0; theInt--) {
        assertEquals( 
            "Search decreasing found", searchStack.search(theInt), searchStack.size() - theInt);
      } 
      searchStack.pop();
    } 
    checkEmptiness(searchStack);
  } 
 
  private void checkLargeStack(Stack<Integer> stack, int offset) {
    for (int i = TEST_SIZE - 1; i >= 0; i--) {
      int theObtainedInt = stack.pop();
      assertEquals(i + offset, theObtainedInt);
    } 
    checkEmptiness(stack);
  } 
 
  private Stack<Integer> createLargeStack() {
    Stack<Integer> large = new Stack<Integer>();
    for (int i = 0; i < TEST_SIZE; i++) {
      large.push(i);
    } 
    return large;
  } 
   
  private Stack<String> createStack() {
    Stack<String> stack = new Stack<String>();
    stack.push("1");
    stack.push("2");
    stack.push("3");
    return stack;
  } 
 
  /** Checks for emptiness of stack by peeking and popping. */ 
  private void checkEmptiness(Stack<?> s) {