@@ -21,11 +21,11 @@ void testPushAndPop() {
2121 stack .push (4 );
2222 stack .push (5 );
2323
24- Assertions .assertEquals (5 , stack .pop ()); // Stack follows LIFO, so 5 should be popped first
25- Assertions .assertEquals (4 , stack .pop ()); // Next, 4 should be popped
26- Assertions .assertEquals (3 , stack .pop ()); // Followed by 3
27- Assertions .assertEquals (2 , stack .pop ()); // Then 2
28- Assertions .assertEquals (1 , stack .pop ()); // Finally 1
24+ Assertions .assertEquals (5 , stack .pop ());
25+ Assertions .assertEquals (4 , stack .pop ());
26+ Assertions .assertEquals (3 , stack .pop ());
27+ Assertions .assertEquals (2 , stack .pop ());
28+ Assertions .assertEquals (1 , stack .pop ());
2929 }
3030
3131 @ Test
@@ -34,34 +34,34 @@ void testPeek() {
3434 stack .push (20 );
3535 stack .push (30 );
3636
37- Assertions .assertEquals (30 , stack .peek ()); // Peek should return 30, the top of the stack
38- Assertions .assertEquals (3 , stack .size ()); // Size should remain 3 after peek
37+ Assertions .assertEquals (30 , stack .peek ());
38+ Assertions .assertEquals (3 , stack .size ());
3939
4040 stack .pop ();
41- Assertions .assertEquals (20 , stack .peek ()); // After popping, peek should return 20
41+ Assertions .assertEquals (20 , stack .peek ());
4242 }
4343
4444 @ Test
4545 void testIsEmpty () {
46- Assertions .assertTrue (stack .isEmpty ()); // Initially, the stack should be empty
46+ Assertions .assertTrue (stack .isEmpty ());
4747 stack .push (42 );
48- Assertions .assertFalse (stack .isEmpty ()); // After pushing an element, the stack should not be empty
48+ Assertions .assertFalse (stack .isEmpty ());
4949 stack .pop ();
50- Assertions .assertTrue (stack .isEmpty ()); // After popping the only element, the stack should be empty again
50+ Assertions .assertTrue (stack .isEmpty ());
5151 }
5252
5353 @ Test
5454 void testResizeOnPush () {
55- StackArray <Integer > smallStack = new StackArray <>(2 ); // Start with a small stack size
55+ StackArray <Integer > smallStack = new StackArray <>(2 );
5656 smallStack .push (1 );
5757 smallStack .push (2 );
58- Assertions .assertTrue (smallStack .isFull ()); // Initially, the stack should be full
58+ Assertions .assertTrue (smallStack .isFull ());
5959
60- smallStack .push (3 ); // This push should trigger a resize
61- Assertions .assertFalse (smallStack .isFull ()); // The stack should no longer be full after resize
62- Assertions .assertEquals (3 , smallStack .size ()); // Size should be 3 after pushing 3 elements
60+ smallStack .push (3 );
61+ Assertions .assertFalse (smallStack .isFull ());
62+ Assertions .assertEquals (3 , smallStack .size ());
6363
64- Assertions .assertEquals (3 , smallStack .pop ()); // LIFO behavior check
64+ Assertions .assertEquals (3 , smallStack .pop ());
6565 Assertions .assertEquals (2 , smallStack .pop ());
6666 Assertions .assertEquals (1 , smallStack .pop ());
6767 }
@@ -74,13 +74,13 @@ void testResizeOnPop() {
7474 stack .push (3 );
7575 stack .push (4 );
7676
77- stack .pop (); // Removing elements should trigger a resize when less than 1/4 of the stack is used
7877 stack .pop ();
7978 stack .pop ();
80- Assertions .assertEquals (1 , stack .size ()); // After popping, only one element should remain
79+ stack .pop ();
80+ Assertions .assertEquals (1 , stack .size ());
8181
8282 stack .pop ();
83- Assertions .assertTrue (stack .isEmpty ()); // The stack should be empty now
83+ Assertions .assertTrue (stack .isEmpty ());
8484 }
8585
8686 @ Test
@@ -90,32 +90,40 @@ void testMakeEmpty() {
9090 stack .push (3 );
9191 stack .makeEmpty ();
9292
93- Assertions .assertTrue (stack .isEmpty ()); // The stack should be empty after calling makeEmpty
94- Assertions .assertThrows (IllegalStateException .class , stack ::pop ); // Popping from empty stack should throw exception
93+ Assertions .assertTrue (stack .isEmpty ());
94+ Assertions .assertThrows (IllegalStateException .class , stack ::pop );
9595 }
9696
9797 @ Test
9898 void testPopEmptyStackThrowsException () {
99- Assertions .assertThrows (IllegalStateException .class , stack ::pop ); // Popping from an empty stack should throw an exception
99+ Assertions .assertThrows (IllegalStateException .class , stack ::pop );
100100 }
101101
102102 @ Test
103103 void testPeekEmptyStackThrowsException () {
104- Assertions .assertThrows (IllegalStateException .class , stack ::peek ); // Peeking into an empty stack should throw an exception
104+ Assertions .assertThrows (IllegalStateException .class , stack ::peek );
105105 }
106106
107107 @ Test
108108 void testConstructorWithInvalidSizeThrowsException () {
109- Assertions .assertThrows (IllegalArgumentException .class , () -> new StackArray <>(0 )); // Size 0 is invalid
110- Assertions .assertThrows (IllegalArgumentException .class , () -> new StackArray <>(-5 )); // Negative size is invalid
109+ Assertions .assertThrows (IllegalArgumentException .class , () -> new StackArray <>(0 ));
110+ Assertions .assertThrows (IllegalArgumentException .class , () -> new StackArray <>(-5 ));
111111 }
112112
113113 @ Test
114114 void testDefaultConstructor () {
115- StackArray <Integer > defaultStack = new StackArray <>(); // Using default constructor
116- Assertions .assertEquals (0 , defaultStack .size ()); // Initially, size should be 0
115+ StackArray <Integer > defaultStack = new StackArray <>();
116+ Assertions .assertEquals (0 , defaultStack .size ());
117117
118118 defaultStack .push (1 );
119- Assertions .assertEquals (1 , defaultStack .size ()); // After pushing, size should be 1
119+ Assertions .assertEquals (1 , defaultStack .size ());
120+ }
121+
122+ @ Test
123+ void testToString () {
124+ stack .push (1 );
125+ stack .push (2 );
126+ stack .push (3 );
127+ Assertions .assertEquals ("StackArray [1, 2, 3]" , stack .toString ());
120128 }
121129}
0 commit comments