@@ -21,11 +21,11 @@ void testPushAndPop() {
21
21
stack .push (4 );
22
22
stack .push (5 );
23
23
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 ());
29
29
}
30
30
31
31
@ Test
@@ -34,34 +34,34 @@ void testPeek() {
34
34
stack .push (20 );
35
35
stack .push (30 );
36
36
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 ());
39
39
40
40
stack .pop ();
41
- Assertions .assertEquals (20 , stack .peek ()); // After popping, peek should return 20
41
+ Assertions .assertEquals (20 , stack .peek ());
42
42
}
43
43
44
44
@ Test
45
45
void testIsEmpty () {
46
- Assertions .assertTrue (stack .isEmpty ()); // Initially, the stack should be empty
46
+ Assertions .assertTrue (stack .isEmpty ());
47
47
stack .push (42 );
48
- Assertions .assertFalse (stack .isEmpty ()); // After pushing an element, the stack should not be empty
48
+ Assertions .assertFalse (stack .isEmpty ());
49
49
stack .pop ();
50
- Assertions .assertTrue (stack .isEmpty ()); // After popping the only element, the stack should be empty again
50
+ Assertions .assertTrue (stack .isEmpty ());
51
51
}
52
52
53
53
@ Test
54
54
void testResizeOnPush () {
55
- StackArray <Integer > smallStack = new StackArray <>(2 ); // Start with a small stack size
55
+ StackArray <Integer > smallStack = new StackArray <>(2 );
56
56
smallStack .push (1 );
57
57
smallStack .push (2 );
58
- Assertions .assertTrue (smallStack .isFull ()); // Initially, the stack should be full
58
+ Assertions .assertTrue (smallStack .isFull ());
59
59
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 ());
63
63
64
- Assertions .assertEquals (3 , smallStack .pop ()); // LIFO behavior check
64
+ Assertions .assertEquals (3 , smallStack .pop ());
65
65
Assertions .assertEquals (2 , smallStack .pop ());
66
66
Assertions .assertEquals (1 , smallStack .pop ());
67
67
}
@@ -74,13 +74,13 @@ void testResizeOnPop() {
74
74
stack .push (3 );
75
75
stack .push (4 );
76
76
77
- stack .pop (); // Removing elements should trigger a resize when less than 1/4 of the stack is used
78
77
stack .pop ();
79
78
stack .pop ();
80
- Assertions .assertEquals (1 , stack .size ()); // After popping, only one element should remain
79
+ stack .pop ();
80
+ Assertions .assertEquals (1 , stack .size ());
81
81
82
82
stack .pop ();
83
- Assertions .assertTrue (stack .isEmpty ()); // The stack should be empty now
83
+ Assertions .assertTrue (stack .isEmpty ());
84
84
}
85
85
86
86
@ Test
@@ -90,32 +90,40 @@ void testMakeEmpty() {
90
90
stack .push (3 );
91
91
stack .makeEmpty ();
92
92
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 );
95
95
}
96
96
97
97
@ Test
98
98
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 );
100
100
}
101
101
102
102
@ Test
103
103
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 );
105
105
}
106
106
107
107
@ Test
108
108
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 ));
111
111
}
112
112
113
113
@ Test
114
114
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 ());
117
117
118
118
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 ());
120
128
}
121
129
}
0 commit comments