@@ -30,18 +30,18 @@ void testPeek() {
30
30
stack .push (10 );
31
31
stack .push (20 );
32
32
33
- Assertions .assertEquals (20 , stack .peek ());
34
- stack .pop (); // Remove 20
35
- Assertions .assertEquals (10 , stack .peek ());
33
+ Assertions .assertEquals (20 , stack .peek ()); // Peek should return the top element
34
+ stack .pop (); // Remove top element
35
+ Assertions .assertEquals (10 , stack .peek ()); // Peek should now return the new top element
36
36
}
37
37
38
38
@ Test
39
39
void testIsEmpty () {
40
- Assertions .assertTrue (stack .isEmpty ());
40
+ Assertions .assertTrue (stack .isEmpty ()); // Stack should initially be empty
41
41
stack .push (1 );
42
- Assertions .assertFalse (stack .isEmpty ());
42
+ Assertions .assertFalse (stack .isEmpty ()); // After pushing, stack should not be empty
43
43
stack .pop ();
44
- Assertions .assertTrue (stack .isEmpty ());
44
+ Assertions .assertTrue (stack .isEmpty ()); // After popping, stack should be empty again
45
45
}
46
46
47
47
@ Test
@@ -50,27 +50,58 @@ void testMakeEmpty() {
50
50
stack .push (2 );
51
51
stack .push (3 );
52
52
stack .makeEmpty ();
53
- Assertions .assertTrue (stack .isEmpty ());
54
- Assertions .assertEquals (0 , stack .size ());
53
+ Assertions .assertTrue (stack .isEmpty ()); // Stack should be empty after makeEmpty is called
54
+ Assertions .assertEquals (0 , stack .size ()); // Size should be 0 after makeEmpty
55
55
}
56
56
57
57
@ Test
58
58
void testSize () {
59
- Assertions .assertEquals (0 , stack .size ());
59
+ Assertions .assertEquals (0 , stack .size ()); // Initial size should be 0
60
60
stack .push (1 );
61
61
stack .push (2 );
62
- Assertions .assertEquals (2 , stack .size ());
62
+ Assertions .assertEquals (2 , stack .size ()); // Size should reflect number of elements added
63
63
stack .pop ();
64
- Assertions .assertEquals (1 , stack .size ());
64
+ Assertions .assertEquals (1 , stack .size ()); // Size should decrease with elements removed
65
65
}
66
66
67
67
@ Test
68
68
void testPopEmptyStackThrowsException () {
69
- Assertions .assertThrows (EmptyStackException .class , stack ::pop );
69
+ Assertions .assertThrows (EmptyStackException .class , stack ::pop ); // Popping from an empty stack should throw an exception
70
70
}
71
71
72
72
@ Test
73
73
void testPeekEmptyStackThrowsException () {
74
- Assertions .assertThrows (EmptyStackException .class , stack ::peek );
74
+ Assertions .assertThrows (EmptyStackException .class , stack ::peek ); // Peeking into an empty stack should throw an exception
75
+ }
76
+
77
+ @ Test
78
+ void testMixedOperations () {
79
+ // Testing a mix of push, pop, peek, and size operations in sequence
80
+ stack .push (5 );
81
+ stack .push (10 );
82
+ stack .push (15 );
83
+
84
+ Assertions .assertEquals (3 , stack .size ()); // Size should reflect number of elements
85
+ Assertions .assertEquals (15 , stack .peek ()); // Peek should show last element added
86
+
87
+ stack .pop (); // Remove top element
88
+ Assertions .assertEquals (10 , stack .peek ()); // New top should be 10
89
+ Assertions .assertEquals (2 , stack .size ()); // Size should reflect removal
90
+
91
+ stack .push (20 ); // Add a new element
92
+ Assertions .assertEquals (20 , stack .peek ()); // Top should be the last added element
93
+ }
94
+
95
+ @ Test
96
+ void testMultipleMakeEmptyCalls () {
97
+ // Ensures calling makeEmpty multiple times does not throw errors or misbehave
98
+ stack .push (1 );
99
+ stack .push (2 );
100
+ stack .makeEmpty ();
101
+ Assertions .assertTrue (stack .isEmpty ());
102
+
103
+ stack .makeEmpty ();
104
+ Assertions .assertTrue (stack .isEmpty ());
105
+ Assertions .assertEquals (0 , stack .size ());
75
106
}
76
107
}
0 commit comments