1 16 package org.apache.commons.collections; 17 18 import java.util.EmptyStackException ; 19 import java.util.List ; 20 21 import junit.framework.Test; 22 23 30 public class TestArrayStack extends TestArrayList { 31 32 protected ArrayStack stack = null; 33 34 public TestArrayStack(String testName) { 35 super(testName); 36 } 37 38 public static Test suite() { 39 return BulkTest.makeSuite(TestArrayStack.class); 40 } 41 42 public static void main(String args[]) { 43 String [] testCaseName = { TestArrayStack.class.getName() }; 44 junit.textui.TestRunner.main(testCaseName); 45 } 46 47 public List makeEmptyList() { 48 return new ArrayStack(); 49 } 50 51 public void setUp() { 52 stack = (ArrayStack) makeEmptyList(); 53 list = stack; 54 } 55 56 public void testNewStack() { 58 59 assertTrue("New stack is empty", stack.empty()); 60 assertEquals("New stack has size zero", stack.size(), 0); 61 62 try { 63 stack.peek(); 64 fail("peek() should have thrown EmptyStackException"); 65 } catch (EmptyStackException e) { 66 ; } 68 69 try { 70 stack.pop(); 71 fail("pop() should have thrown EmptyStackException"); 72 } catch (EmptyStackException e) { 73 ; } 75 76 } 77 78 public void testPushPeekPop() { 79 80 stack.push("First Item"); 81 assertTrue("Stack is not empty", !stack.empty()); 82 assertEquals("Stack size is one", stack.size(), 1); 83 assertEquals("Top item is 'First Item'", 84 (String ) stack.peek(), "First Item"); 85 assertEquals("Stack size is one", stack.size(), 1); 86 87 stack.push("Second Item"); 88 assertEquals("Stack size is two", stack.size(), 2); 89 assertEquals("Top item is 'Second Item'", 90 (String ) stack.peek(), "Second Item"); 91 assertEquals("Stack size is two", stack.size(), 2); 92 93 assertEquals("Popped item is 'Second Item'", 94 (String ) stack.pop(), "Second Item"); 95 assertEquals("Top item is 'First Item'", 96 (String ) stack.peek(), "First Item"); 97 assertEquals("Stack size is one", stack.size(), 1); 98 99 assertEquals("Popped item is 'First Item'", 100 (String ) stack.pop(), "First Item"); 101 assertEquals("Stack size is zero", stack.size(), 0); 102 103 } 104 105 public void testSearch() { 106 107 stack.push("First Item"); 108 stack.push("Second Item"); 109 assertEquals("Top item is 'Second Item'", 110 stack.search("Second Item"), 1); 111 assertEquals("Next Item is 'First Item'", 112 stack.search("First Item"), 2); 113 assertEquals("Cannot find 'Missing Item'", 114 stack.search("Missing Item"), -1); 115 116 } 117 118 } 119 | Popular Tags |