File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -383,9 +383,11 @@ fn test_reverse() {
383
383
384
384
#[ test]
385
385
fn test_sort ( ) {
386
+ let mut rng = thread_rng ( ) ;
387
+
386
388
for len in ( 2 ..25 ) . chain ( 500 ..510 ) {
387
389
for _ in 0 ..100 {
388
- let mut v: Vec < _ > = thread_rng ( ) . gen_iter :: < i32 > ( ) . take ( len) . collect ( ) ;
390
+ let mut v: Vec < _ > = rng . gen_iter :: < i32 > ( ) . take ( len) . collect ( ) ;
389
391
let mut v1 = v. clone ( ) ;
390
392
391
393
v. sort ( ) ;
@@ -399,6 +401,18 @@ fn test_sort() {
399
401
}
400
402
}
401
403
404
+ // Sort using a completely random comparison function.
405
+ // This will reorder the elements *somehow*, but won't panic.
406
+ let mut v = [ 0 ; 500 ] ;
407
+ for i in 0 ..v. len ( ) {
408
+ v[ i] = i as i32 ;
409
+ }
410
+ v. sort_by ( |_, _| * rng. choose ( & [ Less , Equal , Greater ] ) . unwrap ( ) ) ;
411
+ v. sort ( ) ;
412
+ for i in 0 ..v. len ( ) {
413
+ assert_eq ! ( v[ i] , i as i32 ) ;
414
+ }
415
+
402
416
// Should not panic.
403
417
[ 0i32 ; 0 ] . sort ( ) ;
404
418
[ ( ) ; 10 ] . sort ( ) ;
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use core:: cmp:: Ordering :: { Equal , Greater , Less } ;
11
12
use core:: slice:: heapsort;
12
13
use core:: result:: Result :: { Ok , Err } ;
13
14
use rand:: { Rng , XorShiftRng } ;
@@ -268,6 +269,17 @@ fn sort_unstable() {
268
269
}
269
270
}
270
271
272
+ // Sort using a completely random comparison function.
273
+ // This will reorder the elements *somehow*, but won't panic.
274
+ for i in 0 ..v. len ( ) {
275
+ v[ i] = i as i32 ;
276
+ }
277
+ v. sort_unstable_by ( |_, _| * rng. choose ( & [ Less , Equal , Greater ] ) . unwrap ( ) ) ;
278
+ v. sort_unstable ( ) ;
279
+ for i in 0 ..v. len ( ) {
280
+ assert_eq ! ( v[ i] , i as i32 ) ;
281
+ }
282
+
271
283
// Should not panic.
272
284
[ 0i32 ; 0 ] . sort_unstable ( ) ;
273
285
[ ( ) ; 10 ] . sort_unstable ( ) ;
You can’t perform that action at this time.
0 commit comments