@@ -30,18 +30,18 @@ void testPeek() {
3030 stack .push (10 );
3131 stack .push (20 );
3232
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
3636 }
3737
3838 @ Test
3939 void testIsEmpty () {
40- Assertions .assertTrue (stack .isEmpty ());
40+ Assertions .assertTrue (stack .isEmpty ()); // Stack should initially be empty
4141 stack .push (1 );
42- Assertions .assertFalse (stack .isEmpty ());
42+ Assertions .assertFalse (stack .isEmpty ()); // After pushing, stack should not be empty
4343 stack .pop ();
44- Assertions .assertTrue (stack .isEmpty ());
44+ Assertions .assertTrue (stack .isEmpty ()); // After popping, stack should be empty again
4545 }
4646
4747 @ Test
@@ -50,27 +50,58 @@ void testMakeEmpty() {
5050 stack .push (2 );
5151 stack .push (3 );
5252 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
5555 }
5656
5757 @ Test
5858 void testSize () {
59- Assertions .assertEquals (0 , stack .size ());
59+ Assertions .assertEquals (0 , stack .size ()); // Initial size should be 0
6060 stack .push (1 );
6161 stack .push (2 );
62- Assertions .assertEquals (2 , stack .size ());
62+ Assertions .assertEquals (2 , stack .size ()); // Size should reflect number of elements added
6363 stack .pop ();
64- Assertions .assertEquals (1 , stack .size ());
64+ Assertions .assertEquals (1 , stack .size ()); // Size should decrease with elements removed
6565 }
6666
6767 @ Test
6868 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
7070 }
7171
7272 @ Test
7373 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 ());
75106 }
76107}
0 commit comments