@@ -118,4 +118,104 @@ public void testSequentialPushAndPop() {
118
118
}
119
119
assertTrue (stack .isEmpty (), "Stack should be empty after popping all elements" );
120
120
}
121
+
122
+ @ Test
123
+ public void testPushZeroAndNegativeValues () {
124
+ stack .push (0 );
125
+ stack .push (-1 );
126
+ stack .push (-1 );
127
+
128
+ assertEquals (-1 , stack .pop (), "Should handle negative values correctly" );
129
+ assertEquals (-1 , stack .pop (), "Should handle negative values correctly" );
130
+ assertEquals (0 , stack .pop (), "Should handle zero value correctly" );
131
+ }
132
+
133
+ @ Test
134
+ public void testPushDuplicateValues () {
135
+ stack .push (1 );
136
+ stack .push (1 );
137
+ stack .push (1 );
138
+
139
+ assertEquals (3 , stack .getSize (), "Should allow duplicate values" );
140
+ assertEquals (1 , stack .pop ());
141
+ assertEquals (1 , stack .pop ());
142
+ assertEquals (1 , stack .pop ());
143
+ }
144
+
145
+ @ Test
146
+ public void testPushAfterEmptyingStack () {
147
+ stack .push (1 );
148
+ stack .push (2 );
149
+ stack .pop ();
150
+ stack .pop ();
151
+
152
+ assertTrue (stack .isEmpty (), "Stack should be empty" );
153
+
154
+ stack .push (10 );
155
+ assertEquals (10 , stack .peek (), "Should work correctly after emptying and refilling" );
156
+ assertEquals (1 , stack .getSize (), "Size should be correct after refilling" );
157
+ }
158
+
159
+ @ Test
160
+ public void testPeekDoesNotModifyStack () {
161
+ stack .push (1 );
162
+
163
+ int firstPeek = stack .peek ();
164
+ int secondPeek = stack .peek ();
165
+ int thirdPeek = stack .peek ();
166
+
167
+ assertEquals (firstPeek , secondPeek , "Multiple peeks should return same value" );
168
+ assertEquals (secondPeek , thirdPeek , "Multiple peeks should return same value" );
169
+ assertEquals (1 , stack .getSize (), "Peek should not modify stack size" );
170
+ assertEquals (1 , stack .pop (), "Element should still be poppable after peeking" );
171
+ }
172
+
173
+ @ Test
174
+ public void testAlternatingPushAndPop () {
175
+ stack .push (1 );
176
+ assertEquals (1 , stack .pop ());
177
+
178
+ stack .push (2 );
179
+ stack .push (3 );
180
+ assertEquals (3 , stack .pop ());
181
+
182
+ stack .push (4 );
183
+ assertEquals (4 , stack .pop ());
184
+ assertEquals (2 , stack .pop ());
185
+
186
+ assertTrue (stack .isEmpty (), "Stack should be empty after alternating operations" );
187
+ }
188
+
189
+ @ Test
190
+ public void testToStringWithSingleElement () {
191
+ stack .push (42 );
192
+ assertEquals ("42" , stack .toString (), "String representation with single element should not have arrows" );
193
+ }
194
+
195
+ @ Test
196
+ public void testStackIntegrity () {
197
+ // Test that internal state remains consistent
198
+ for (int i = 0 ; i < 10 ; i ++) {
199
+ stack .push (i );
200
+ assertEquals (i + 1 , stack .getSize (), "Size should be consistent during pushes" );
201
+ assertEquals (i , stack .peek (), "Peek should return last pushed value" );
202
+ }
203
+
204
+ for (int i = 9 ; i >= 0 ; i --) {
205
+ assertEquals (i , stack .peek (), "Peek should return correct value before pop" );
206
+ assertEquals (i , stack .pop (), "Pop should return values in LIFO order" );
207
+ assertEquals (i , stack .getSize (), "Size should be consistent during pops" );
208
+ }
209
+ }
210
+
211
+ @ Test
212
+ public void testMixedDataTypes () {
213
+ // If your stack supports Object types, test with different data types
214
+
215
+ stack .push (1 );
216
+ stack .push (2 );
217
+
218
+ assertEquals (Integer .valueOf (2 ), stack .pop ());
219
+ assertEquals (Integer .valueOf (1 ), stack .pop ());
220
+ }
121
221
}
0 commit comments