/
ArrayBasedStackTest.java
200 lines (177 loc) · 3.56 KB
/
ArrayBasedStackTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cmsc256;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class ArrayBasedStackTest {
ArrayBasedStack<String> stack;
private String n1,n2,n3;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
stack = new ArrayBasedStack<>();
n1 = "Able";
n2 = "Baker";
n3 = "Charlie";
}
public void pushAllElements() {
stack.push(n1);
stack.push(n2);
stack.push(n3);
}
/**
* Test push onto an empty stack
*/
@Test
public final void testPush1() {
stack.push("Rodney Ram");
assertEquals(stack.peek(), "Rodney Ram");
}
/**
* Test push onto a stack with elements
*/
@Test
public final void testPush2() {
pushAllElements();
stack.push("Rodney Ram");
assertEquals(stack.peek(), "Rodney Ram");
}
/**
* Test push onto a full stack
*/
@Test
public final void testPush3() {
pushAllElements();
stack.push("Alan Turing");
stack.push("Ada Lovelace");
stack.push("Adm. Grace Hopper");
}
/**
* Test pushing a null element
*/
@Test
public final void testPush4() {
pushAllElements();
stack.push(null);
assertEquals(null, stack.peek());
}
/**
* Test method for pop() an empty stack
*/
@Test (expected = EmptyStackException.class)
public final void testPop1() {
stack.pop();
}
/**
* Test method for pop() after push
*/
@Test
public final void testPop2() {
pushAllElements();
assertEquals("Charlie", stack.pop());
}
/**
* Test method for pop() after clear()
*/
@Test (expected = EmptyStackException.class)
public final void testPop3() {
pushAllElements();
stack.clear();
stack.pop();
}
/**
* Test peek() before pushing anything
*/
@Test (expected = EmptyStackException.class)
public final void testPeek1() {
stack.peek();
}
/**
* Test peek() after push
*/
@Test
public final void testPeek2() {
pushAllElements();
assertEquals("Charlie", stack.peek());
}
/**
* Test peek() after push & pop
*/
@Test
public final void testPeek3() {
pushAllElements();
stack.pop();
assertEquals("Baker", stack.peek());
}
/**
* Test peak after clear
*/
@Test (expected = EmptyStackException.class)
public final void testPeek4() {
pushAllElements();
stack.clear();
stack.peek();
}
/**
* Test method for isEmpty() initially
*/
@Test
public final void testIsEmpty1() {
assertTrue(stack.isEmpty());
}
/**
* Test method for isEmpty() after push
*/
@Test
public final void testIsEmpty2() {
pushAllElements();
assertFalse(stack.isEmpty());
}
/**
* Test method for isEmpty() after push & pop
*/
@Test
public final void testIsEmpty3() {
pushAllElements();
stack.pop();
assertFalse(stack.isEmpty());
}
/**
* Test method for isEmpty() after push & pops
*/
@Test
public final void testIsEmpty4() {
pushAllElements();
stack.pop();
stack.pop();
stack.pop();
assertTrue(stack.isEmpty());
}
/**
* Test method for isEmpty() after clear
*/
@Test
public final void testIsEmpty5() {
pushAllElements();
stack.clear();
assertTrue(stack.isEmpty());
}
/**
* Test method for clear() after initialization
*/
@Test
public final void testClear1() {
stack.clear();
assertTrue(stack.isEmpty());
}
/**
* Test method for clear() after pushing
*/
@Test
public final void testClear2() {
pushAllElements();
stack.clear();
assertTrue(stack.isEmpty());
}
}