1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import java.util.EmptyStackException;
20
21 import junit.framework.Test;
22
23
24
25
26
27 @SuppressWarnings("deprecation")
28 public class ArrayStackTest<E> extends AbstractArrayListTest<E> {
29
30 public ArrayStackTest(final String testName) {
31 super(testName);
32 }
33
34 public static Test suite() {
35 return BulkTest.makeSuite(ArrayStackTest.class);
36 }
37
38 @Override
39 public ArrayStack<E> makeObject() {
40 return new ArrayStack<>();
41 }
42
43
44 public void testNewStack() {
45 final ArrayStack<E> stack = makeObject();
46 assertTrue("New stack is empty", stack.empty());
47 assertEquals("New stack has size zero", 0, stack.size());
48
49 try {
50 stack.peek();
51 fail("peek() should have thrown EmptyStackException");
52 } catch (final EmptyStackException e) {
53
54 }
55
56 try {
57 stack.pop();
58 fail("pop() should have thrown EmptyStackException");
59 } catch (final EmptyStackException e) {
60
61 }
62
63 }
64
65 @SuppressWarnings("unchecked")
66 public void testPushPeekPop() {
67 final ArrayStack<E> stack = makeObject();
68
69 stack.push((E) "First Item");
70 assertTrue("Stack is not empty", !stack.empty());
71 assertEquals("Stack size is one", 1, stack.size());
72 assertEquals("Top item is 'First Item'",
73 "First Item", (String) stack.peek());
74 assertEquals("Stack size is one", 1, stack.size());
75
76 stack.push((E) "Second Item");
77 assertEquals("Stack size is two", 2, stack.size());
78 assertEquals("Top item is 'Second Item'",
79 "Second Item", (String) stack.peek());
80 assertEquals("Stack size is two", 2, stack.size());
81
82 assertEquals("Popped item is 'Second Item'",
83 "Second Item", (String) stack.pop());
84 assertEquals("Top item is 'First Item'",
85 "First Item", (String) stack.peek());
86 assertEquals("Stack size is one", 1, stack.size());
87
88 assertEquals("Popped item is 'First Item'",
89 "First Item", (String) stack.pop());
90 assertEquals("Stack size is zero", 0, stack.size());
91
92 }
93
94 @Override
95 @SuppressWarnings("unchecked")
96 public void testSearch() {
97 final ArrayStack<E> stack = makeObject();
98
99 stack.push((E) "First Item");
100 stack.push((E) "Second Item");
101 assertEquals("Top item is 'Second Item'",
102 1, stack.search("Second Item"));
103 assertEquals("Next Item is 'First Item'",
104 2, stack.search("First Item"));
105 assertEquals("Cannot find 'Missing Item'",
106 -1, stack.search("Missing Item"));
107
108 }
109
110 @Override
111 public String getCompatibilityVersion() {
112 return "4";
113 }
114
115
116
117
118
119
120
121
122 }