@@ -74,4 +74,187 @@ public void testSortWithDuplicateElements() {
74
74
assertEquals (2 , (int ) stack .pop ());
75
75
assertEquals (1 , (int ) stack .pop ());
76
76
}
77
+
78
+ @ Test
79
+ public void testSortReverseSortedStack () {
80
+ // Test worst case scenario - completely reverse sorted
81
+ stack .push (5 );
82
+ stack .push (4 );
83
+ stack .push (3 );
84
+ stack .push (2 );
85
+ stack .push (1 );
86
+ SortStack .sortStack (stack );
87
+
88
+ assertEquals (5 , stack .size ());
89
+ assertEquals (5 , (int ) stack .pop ());
90
+ assertEquals (4 , (int ) stack .pop ());
91
+ assertEquals (3 , (int ) stack .pop ());
92
+ assertEquals (2 , (int ) stack .pop ());
93
+ assertEquals (1 , (int ) stack .pop ());
94
+ }
95
+
96
+ @ Test
97
+ public void testSortWithAllSameElements () {
98
+ // Test stack with all identical elements
99
+ stack .push (7 );
100
+ stack .push (7 );
101
+ stack .push (7 );
102
+ stack .push (7 );
103
+ SortStack .sortStack (stack );
104
+
105
+ assertEquals (4 , stack .size ());
106
+ assertEquals (7 , (int ) stack .pop ());
107
+ assertEquals (7 , (int ) stack .pop ());
108
+ assertEquals (7 , (int ) stack .pop ());
109
+ assertEquals (7 , (int ) stack .pop ());
110
+ }
111
+
112
+ @ Test
113
+ public void testSortWithNegativeNumbers () {
114
+ // Test with negative numbers
115
+ stack .push (-3 );
116
+ stack .push (1 );
117
+ stack .push (-5 );
118
+ stack .push (2 );
119
+ stack .push (-1 );
120
+ SortStack .sortStack (stack );
121
+
122
+ assertEquals (5 , stack .size ());
123
+ assertEquals (2 , (int ) stack .pop ());
124
+ assertEquals (1 , (int ) stack .pop ());
125
+ assertEquals (-1 , (int ) stack .pop ());
126
+ assertEquals (-3 , (int ) stack .pop ());
127
+ assertEquals (-5 , (int ) stack .pop ());
128
+ }
129
+
130
+ @ Test
131
+ public void testSortWithAllNegativeNumbers () {
132
+ // Test with only negative numbers
133
+ stack .push (-10 );
134
+ stack .push (-5 );
135
+ stack .push (-15 );
136
+ stack .push (-1 );
137
+ SortStack .sortStack (stack );
138
+
139
+ assertEquals (4 , stack .size ());
140
+ assertEquals (-1 , (int ) stack .pop ());
141
+ assertEquals (-5 , (int ) stack .pop ());
142
+ assertEquals (-10 , (int ) stack .pop ());
143
+ assertEquals (-15 , (int ) stack .pop ());
144
+ }
145
+
146
+ @ Test
147
+ public void testSortWithZero () {
148
+ // Test with zero included
149
+ stack .push (3 );
150
+ stack .push (0 );
151
+ stack .push (-2 );
152
+ stack .push (1 );
153
+ SortStack .sortStack (stack );
154
+
155
+ assertEquals (4 , stack .size ());
156
+ assertEquals (3 , (int ) stack .pop ());
157
+ assertEquals (1 , (int ) stack .pop ());
158
+ assertEquals (0 , (int ) stack .pop ());
159
+ assertEquals (-2 , (int ) stack .pop ());
160
+ }
161
+
162
+ @ Test
163
+ public void testSortLargerStack () {
164
+ // Test with a larger number of elements
165
+ int [] values = {15 , 3 , 9 , 1 , 12 , 6 , 18 , 4 , 11 , 8 };
166
+ for (int value : values ) {
167
+ stack .push (value );
168
+ }
169
+
170
+ SortStack .sortStack (stack );
171
+
172
+ assertEquals (10 , stack .size ());
173
+
174
+ // Verify sorted order (largest to smallest when popping)
175
+ int [] expectedOrder = {18 , 15 , 12 , 11 , 9 , 8 , 6 , 4 , 3 , 1 };
176
+ for (int expected : expectedOrder ) {
177
+ assertEquals (expected , (int ) stack .pop ());
178
+ }
179
+ }
180
+
181
+ @ Test
182
+ public void testSortTwoElements () {
183
+ // Test edge case with exactly two elements
184
+ stack .push (5 );
185
+ stack .push (2 );
186
+ SortStack .sortStack (stack );
187
+
188
+ assertEquals (2 , stack .size ());
189
+ assertEquals (5 , (int ) stack .pop ());
190
+ assertEquals (2 , (int ) stack .pop ());
191
+ }
192
+
193
+ @ Test
194
+ public void testSortTwoElementsAlreadySorted () {
195
+ // Test two elements already in correct order
196
+ stack .push (2 );
197
+ stack .push (5 );
198
+ SortStack .sortStack (stack );
199
+
200
+ assertEquals (2 , stack .size ());
201
+ assertEquals (5 , (int ) stack .pop ());
202
+ assertEquals (2 , (int ) stack .pop ());
203
+ }
204
+
205
+ @ Test
206
+ public void testSortStackWithMinAndMaxValues () {
207
+ // Test with Integer.MAX_VALUE and Integer.MIN_VALUE
208
+ stack .push (0 );
209
+ stack .push (Integer .MAX_VALUE );
210
+ stack .push (Integer .MIN_VALUE );
211
+ stack .push (100 );
212
+ SortStack .sortStack (stack );
213
+
214
+ assertEquals (4 , stack .size ());
215
+ assertEquals (Integer .MAX_VALUE , (int ) stack .pop ());
216
+ assertEquals (100 , (int ) stack .pop ());
217
+ assertEquals (0 , (int ) stack .pop ());
218
+ assertEquals (Integer .MIN_VALUE , (int ) stack .pop ());
219
+ }
220
+
221
+ @ Test
222
+ public void testSortWithManyDuplicates () {
223
+ // Test with multiple sets of duplicates
224
+ stack .push (3 );
225
+ stack .push (1 );
226
+ stack .push (3 );
227
+ stack .push (1 );
228
+ stack .push (2 );
229
+ stack .push (2 );
230
+ stack .push (3 );
231
+ SortStack .sortStack (stack );
232
+
233
+ assertEquals (7 , stack .size ());
234
+ assertEquals (3 , (int ) stack .pop ());
235
+ assertEquals (3 , (int ) stack .pop ());
236
+ assertEquals (3 , (int ) stack .pop ());
237
+ assertEquals (2 , (int ) stack .pop ());
238
+ assertEquals (2 , (int ) stack .pop ());
239
+ assertEquals (1 , (int ) stack .pop ());
240
+ assertEquals (1 , (int ) stack .pop ());
241
+ }
242
+
243
+ @ Test
244
+ public void testOriginalStackIsModified () {
245
+ // Verify that the original stack is modified, not a copy
246
+ Stack <Integer > originalReference = stack ;
247
+ stack .push (3 );
248
+ stack .push (1 );
249
+ stack .push (2 );
250
+
251
+ SortStack .sortStack (stack );
252
+
253
+ // Verify it's the same object reference
254
+ assertTrue (stack == originalReference );
255
+ assertEquals (3 , stack .size ());
256
+ assertEquals (3 , (int ) stack .pop ());
257
+ assertEquals (2 , (int ) stack .pop ());
258
+ assertEquals (1 , (int ) stack .pop ());
259
+ }
77
260
}
0 commit comments