forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTest.java
More file actions
50 lines (39 loc) · 1.25 KB
/
StackTest.java
File metadata and controls
50 lines (39 loc) · 1.25 KB
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
package com.examplehub.datastructures.stack;
import static org.junit.jupiter.api.Assertions.*;
import java.util.EmptyStackException;
import org.junit.jupiter.api.Test;
class StackTest {
@Test
void testStack() {
Stack<String> stack = new Stack<>();
assertEquals(stack.size(), 0);
assertTrue(stack.empty());
try {
stack.pop();
fail(); /* this will not happen */
} catch (EmptyStackException e) {
assertTrue(true); /* this will happen */
}
try {
stack.peek();
fail(); /* this will not happen */
} catch (EmptyStackException e) {
assertTrue(true); /* this will happen */
}
assertEquals("Java", stack.push("Java"));
assertEquals("Python", stack.push("Python"));
assertEquals("C", stack.push("C"));
assertEquals("Shell", stack.push("Shell"));
assertEquals("[Java, Python, C, Shell]", stack.toString());
assertEquals("Shell", stack.peek());
assertEquals("Shell", stack.pop());
assertEquals("C", stack.pop());
assertFalse(stack.empty());
assertEquals(2, stack.size());
assertEquals("[Java, Python]", stack.toString());
stack.clear();
assertTrue(stack.empty());
assertEquals(0, stack.size());
assertEquals("[]", stack.toString());
}
}