You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
assertEquals(3, stack.peek(), "Peek should return the last pushed value");
27
+
assertEquals(3, stack.getSize(), "Size should reflect the number of elements");
28
+
}
29
+
30
+
@Test
31
+
publicvoidtestPop() {
32
+
stack.push(1);
33
+
stack.push(2);
34
+
stack.push(3);
35
+
36
+
assertEquals(3, stack.pop(), "Pop should return the last pushed value");
37
+
assertEquals(2, stack.pop(), "Pop should return the next last pushed value");
38
+
assertEquals(1, stack.pop(), "Pop should return the first pushed value");
39
+
assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements");
40
+
}
41
+
42
+
@Test
43
+
publicvoidtestPopEmptyStack() {
44
+
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.pop(), "Popping from an empty stack should throw NoSuchElementException");
45
+
}
46
+
47
+
@Test
48
+
publicvoidtestPeekEmptyStack() {
49
+
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.peek(), "Peeking into an empty stack should throw NoSuchElementException");
50
+
}
51
+
52
+
@Test
53
+
publicvoidtestIsEmpty() {
54
+
assertTrue(stack.isEmpty(), "Newly created stack should be empty");
55
+
56
+
stack.push(1);
57
+
assertFalse(stack.isEmpty(), "Stack should not be empty after pushing an element");
58
+
59
+
stack.pop();
60
+
assertTrue(stack.isEmpty(), "Stack should be empty after popping the only element");
61
+
}
62
+
63
+
@Test
64
+
publicvoidtestToString() {
65
+
stack.push(1);
66
+
stack.push(2);
67
+
stack.push(3);
68
+
69
+
assertEquals("3->2->1", stack.toString(), "String representation of stack should match the expected format");
70
+
}
71
+
72
+
@Test
73
+
publicvoidtestMultiplePushesAndPops() {
74
+
stack.push(5);
75
+
stack.push(10);
76
+
stack.push(15);
77
+
78
+
assertEquals(15, stack.pop(), "Pop should return the last pushed value");
79
+
assertEquals(10, stack.peek(), "Peek should return the new top value after popping");
80
+
assertEquals(10, stack.pop(), "Pop should return the next last pushed value");
81
+
assertEquals(5, stack.pop(), "Pop should return the first pushed value");
82
+
assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements");
83
+
}
84
+
85
+
@Test
86
+
publicvoidtestGetSize() {
87
+
assertEquals(0, stack.getSize(), "Size of an empty stack should be zero");
88
+
stack.push(1);
89
+
stack.push(2);
90
+
assertEquals(2, stack.getSize(), "Size should reflect the number of elements");
91
+
stack.pop();
92
+
assertEquals(1, stack.getSize(), "Size should decrease with each pop");
93
+
}
94
+
95
+
@Test
96
+
publicvoidtestSizeAfterClearingStack() {
97
+
stack.push(1);
98
+
stack.push(2);
99
+
stack.push(3);
100
+
101
+
// Manually clear the stack
102
+
while (!stack.isEmpty()) {
103
+
stack.pop();
104
+
}
105
+
assertTrue(stack.isEmpty(), "Stack should be empty after clearing");
106
+
assertEquals(0, stack.getSize(), "Size should be zero after clearing the stack");
107
+
}
108
+
109
+
@Test
110
+
publicvoidtestSequentialPushAndPop() {
111
+
for (inti = 1; i <= 100; i++) {
112
+
stack.push(i);
113
+
}
114
+
assertEquals(100, stack.getSize(), "Size should be 100 after pushing 100 elements");
115
+
116
+
for (inti = 100; i >= 1; i--) {
117
+
assertEquals(i, stack.pop(), "Popping should return values in LIFO order");
118
+
}
119
+
assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements");
0 commit comments