@@ -59,3 +59,241 @@ pub unconstrained fn quicksort<T, let N: u32, Env>(
59
59
}
60
60
arr
61
61
}
62
+
63
+ mod test {
64
+ use crate::cmp::Ord ;
65
+ use super::quicksort ;
66
+
67
+ // helper functions for testing
68
+ fn ascending <T >(a : T , b : T ) -> bool
69
+ where
70
+ T : Ord ,
71
+ {
72
+ a < b
73
+ }
74
+
75
+ fn descending <T >(a : T , b : T ) -> bool
76
+ where
77
+ T : Ord ,
78
+ {
79
+ a > b
80
+ }
81
+
82
+ #[test]
83
+ fn test_empty_array () {
84
+ let arr : [u32 ; 0 ] = [];
85
+ // Safety: test
86
+ let result = unsafe { quicksort (arr , ascending ) };
87
+ assert_eq (result , arr );
88
+ }
89
+
90
+ #[test]
91
+ fn test_single_element () {
92
+ let arr : [u32 ; 1 ] = [42 ];
93
+ // Safety: test
94
+ let result = unsafe { quicksort (arr , ascending ) };
95
+ assert_eq (result , arr );
96
+ }
97
+
98
+ #[test]
99
+ fn test_two_elements_ascending () {
100
+ let arr : [u32 ; 2 ] = [2 , 1 ];
101
+ // Safety: test
102
+ let result = unsafe { quicksort (arr , ascending ) };
103
+ assert_eq (result , [1 , 2 ]);
104
+ }
105
+
106
+ #[test]
107
+ fn test_two_elements_descending () {
108
+ let arr : [u32 ; 2 ] = [1 , 2 ];
109
+ // Safety: test
110
+ let result = unsafe { quicksort (arr , descending ) };
111
+ assert_eq (result , [2 , 1 ]);
112
+ }
113
+
114
+ #[test]
115
+ fn test_already_sorted_ascending () {
116
+ let arr : [u32 ; 5 ] = [1 , 2 , 3 , 4 , 5 ];
117
+ // Safety: test
118
+ let result = unsafe { quicksort (arr , ascending ) };
119
+ assert_eq (result , [1 , 2 , 3 , 4 , 5 ]);
120
+ }
121
+
122
+ #[test]
123
+ fn test_already_sorted_ascending_with_one_out_of_order () {
124
+ let mut arr : [u32 ; 1000 ] = [0 ; 1000 ];
125
+ for i in 0 ..1000 {
126
+ arr [i ] = i as u32 ;
127
+ }
128
+ arr [0 ] = 2 ;
129
+ // Safety: test
130
+ let result = unsafe { quicksort (arr , ascending ) };
131
+ arr [0 ] = 1 ;
132
+ arr [1 ] = 2 ;
133
+ assert_eq (result , arr );
134
+ }
135
+
136
+ #[test]
137
+ fn test_already_sorted_descending () {
138
+ let arr : [u32 ; 5 ] = [5 , 4 , 3 , 2 , 1 ];
139
+ // Safety: test
140
+ let result = unsafe { quicksort (arr , descending ) };
141
+ assert_eq (result , [5 , 4 , 3 , 2 , 1 ]);
142
+ }
143
+
144
+ #[test]
145
+ fn test_reverse_sorted_ascending () {
146
+ let arr : [u32 ; 5 ] = [5 , 4 , 3 , 2 , 1 ];
147
+ // Safety: test
148
+ let result = unsafe { quicksort (arr , ascending ) };
149
+ assert_eq (result , [1 , 2 , 3 , 4 , 5 ]);
150
+ }
151
+
152
+ #[test]
153
+ fn test_reverse_sorted_descending () {
154
+ let arr : [u32 ; 5 ] = [1 , 2 , 3 , 4 , 5 ];
155
+ // Safety: test
156
+ let result = unsafe { quicksort (arr , descending ) };
157
+ assert_eq (result , [5 , 4 , 3 , 2 , 1 ]);
158
+ }
159
+
160
+ #[test]
161
+ fn test_random_array_ascending () {
162
+ let arr : [u32 ; 8 ] = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 ];
163
+ // Safety: test
164
+ let result = unsafe { quicksort (arr , ascending ) };
165
+ assert_eq (result , [1 , 1 , 2 , 3 , 4 , 5 , 6 , 9 ]);
166
+ }
167
+
168
+ #[test]
169
+ fn test_random_array_descending () {
170
+ let arr : [u32 ; 8 ] = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 ];
171
+ // Safety: test
172
+ let result = unsafe { quicksort (arr , descending ) };
173
+ assert_eq (result , [9 , 6 , 5 , 4 , 3 , 2 , 1 , 1 ]);
174
+ }
175
+
176
+ #[test]
177
+ fn test_duplicate_elements () {
178
+ let arr : [u32 ; 11 ] = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
179
+ // Safety: test
180
+ let result = unsafe { quicksort (arr , ascending ) };
181
+ assert_eq (result , [1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 5 , 6 , 9 ]);
182
+ }
183
+
184
+ #[test]
185
+ fn test_all_same_elements () {
186
+ let arr : [u32 ; 5 ] = [42 , 42 , 42 , 42 , 42 ];
187
+ // Safety: test
188
+ let result = unsafe { quicksort (arr , ascending ) };
189
+ assert_eq (result , [42 , 42 , 42 , 42 , 42 ]);
190
+ }
191
+
192
+ #[test]
193
+ fn test_large_array () {
194
+ let arr : [u32 ; 10 ] = [9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ];
195
+ // Safety: test
196
+ let result = unsafe { quicksort (arr , ascending ) };
197
+ assert_eq (result , [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]);
198
+ }
199
+
200
+ #[test]
201
+ fn test_negative_numbers () {
202
+ let arr : [i32 ; 8 ] = [-3 , 1 , -4 , 1 , -5 , 9 , -2 , 6 ];
203
+ // Safety: test
204
+ let result = unsafe { quicksort (arr , ascending ) };
205
+ assert_eq (result , [-5 , -4 , -3 , -2 , 1 , 1 , 6 , 9 ]);
206
+ }
207
+
208
+ #[test]
209
+ fn test_zero_values () {
210
+ let arr : [u32 ; 6 ] = [0 , 1 , 0 , 2 , 0 , 3 ];
211
+ // Safety: test
212
+ let result = unsafe { quicksort (arr , ascending ) };
213
+ assert_eq (result , [0 , 0 , 0 , 1 , 2 , 3 ]);
214
+ }
215
+
216
+ #[test]
217
+ fn test_edge_case_near_sorted () {
218
+ let arr : [u32 ; 8 ] = [1 , 2 , 3 , 5 , 4 , 6 , 7 , 8 ];
219
+ // Safety: test
220
+ let result = unsafe { quicksort (arr , ascending ) };
221
+ assert_eq (result , [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]);
222
+ }
223
+
224
+ #[test]
225
+ fn test_edge_case_near_reverse_sorted () {
226
+ let arr : [u32 ; 8 ] = [8 , 7 , 6 , 4 , 5 , 3 , 2 , 1 ];
227
+ // Safety: test
228
+ let result = unsafe { quicksort (arr , ascending ) };
229
+ assert_eq (result , [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]);
230
+ }
231
+
232
+ fn abs_compare (a : i32 , b : i32 ) -> bool {
233
+ let abs_a = if a < 0 { -a } else { a };
234
+ let abs_b = if b < 0 { -b } else { b };
235
+ abs_a < abs_b
236
+ }
237
+
238
+ #[test]
239
+ fn test_custom_comparison () {
240
+ // sort by absolute value
241
+
242
+ let arr : [i32 ; 8 ] = [-3 , 1 , -4 , 1 , -5 , 9 , -2 , 6 ];
243
+ // Safety: test
244
+ let result = unsafe { quicksort (arr , abs_compare ) };
245
+ assert_eq (result , [1 , 1 , -2 , -3 , -4 , -5 , 6 , 9 ]);
246
+ }
247
+
248
+ #[test]
249
+ fn test_equivalent_same_relative_order () {
250
+ // sort by absolute value
251
+ let arr : [i32 ; 11 ] = [-3 , 1 , -4 , 1 , -5 , 5 , -5 , 5 , 9 , -2 , 6 ];
252
+ // Safety: test
253
+ let result = unsafe { quicksort (arr , abs_compare ) };
254
+ assert_eq (result , [1 , 1 , -2 , -3 , -4 , -5 , 5 , -5 , 5 , 6 , 9 ]);
255
+ }
256
+
257
+ #[test]
258
+ fn test_maximum_values () {
259
+ let arr : [u32 ; 4 ] = [2 ^ 32 - 1 , 1 , 0 , 2 ^ 32 - 2 ];
260
+ // Safety: test
261
+ let result = unsafe { quicksort (arr , ascending ) };
262
+ assert_eq (result , [0 , 1 , 2 ^ 32 - 2 , 2 ^ 32 - 1 ]);
263
+ }
264
+
265
+ #[test]
266
+ fn test_alternating_pattern () {
267
+ let arr : [u32 ; 9 ] = [1 , 9 , 2 , 8 , 3 , 7 , 4 , 6 , 5 ];
268
+ // Safety: test
269
+ let result = unsafe { quicksort (arr , ascending ) };
270
+ assert_eq (result , [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]);
271
+ }
272
+
273
+ #[test]
274
+ fn test_multiple_sorts_same_array () {
275
+ let arr : [u32 ; 8 ] = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 ];
276
+ // Safety: test
277
+ let result1 = unsafe { quicksort (arr , ascending ) };
278
+ // Safety: test
279
+ let result2 = unsafe { quicksort (arr , ascending ) };
280
+ // Safety: test
281
+ let result3 = unsafe { quicksort (arr , ascending ) };
282
+
283
+ assert_eq (result1 , result2 );
284
+ assert_eq (result2 , result3 );
285
+ assert_eq (result1 , [1 , 1 , 2 , 3 , 4 , 5 , 6 , 9 ]);
286
+ }
287
+
288
+ #[test]
289
+ fn test_descending_then_ascending () {
290
+ let arr : [u32 ; 8 ] = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 ];
291
+ // Safety: test
292
+ let desc_result = unsafe { quicksort (arr , descending ) };
293
+ assert_eq (desc_result , [9 , 6 , 5 , 4 , 3 , 2 , 1 , 1 ]);
294
+
295
+ // Safety: test
296
+ let asc_result : [u32 ; 8 ] = unsafe { quicksort (desc_result , ascending ) };
297
+ assert_eq (asc_result , [1 , 1 , 2 , 3 , 4 , 5 , 6 , 9 ]);
298
+ }
299
+ }
0 commit comments