-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathNodeStackTest.java
73 lines (63 loc) · 2.59 KB
/
NodeStackTest.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
package com.thealgorithms.datastructures.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class NodeStackTest {
@Test
void testPush() {
NodeStack<Integer> stack = new NodeStack<>();
stack.push(10);
stack.push(20);
assertEquals(20, stack.peek(), "Top element should be 20 after pushing 10 and 20.");
}
@Test
void testPop() {
NodeStack<String> stack = new NodeStack<>();
stack.push("First");
stack.push("Second");
assertEquals("Second", stack.pop(), "Pop should return 'Second', the last pushed element.");
assertEquals("First", stack.pop(), "Pop should return 'First' after 'Second' is removed.");
}
@Test
void testPopOnEmptyStack() {
NodeStack<Double> stack = new NodeStack<>();
assertThrows(IllegalStateException.class, stack::pop, "Popping an empty stack should throw IllegalStateException.");
}
@Test
void testPeek() {
NodeStack<Integer> stack = new NodeStack<>();
stack.push(5);
stack.push(15);
assertEquals(15, stack.peek(), "Peek should return 15, the top element.");
stack.pop();
assertEquals(5, stack.peek(), "Peek should return 5 after 15 is popped.");
}
@Test
void testPeekOnEmptyStack() {
NodeStack<String> stack = new NodeStack<>();
assertThrows(IllegalStateException.class, stack::peek, "Peeking an empty stack should throw IllegalStateException.");
}
@Test
void testIsEmpty() {
NodeStack<Character> stack = new NodeStack<>();
assertTrue(stack.isEmpty(), "Newly initialized stack should be empty.");
stack.push('A');
assertFalse(stack.isEmpty(), "Stack should not be empty after a push operation.");
stack.pop();
assertTrue(stack.isEmpty(), "Stack should be empty after popping the only element.");
}
@Test
void testSize() {
NodeStack<Integer> stack = new NodeStack<>();
assertEquals(0, stack.size(), "Size of empty stack should be 0.");
stack.push(3);
stack.push(6);
assertEquals(2, stack.size(), "Size should be 2 after pushing two elements.");
stack.pop();
assertEquals(1, stack.size(), "Size should be 1 after popping one element.");
stack.pop();
assertEquals(0, stack.size(), "Size should be 0 after popping all elements.");
}
}