Skip to content

Commit eca1e18

Browse files
committed
Add stability test for sort_by_cached_key
1 parent 785e3c3 commit eca1e18

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/liballoc/tests/slice.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -485,24 +485,29 @@ fn test_sort_stability() {
485485
// the second item represents which occurrence of that
486486
// number this element is, i.e. the second elements
487487
// will occur in sorted order.
488-
let mut v: Vec<_> = (0..len)
488+
let mut orig: Vec<_> = (0..len)
489489
.map(|_| {
490490
let n = thread_rng().gen::<usize>() % 10;
491491
counts[n] += 1;
492492
(n, counts[n])
493493
})
494494
.collect();
495495

496-
// only sort on the first element, so an unstable sort
496+
let mut v = orig.clone();
497+
// Only sort on the first element, so an unstable sort
497498
// may mix up the counts.
498499
v.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
499500

500-
// this comparison includes the count (the second item
501+
// This comparison includes the count (the second item
501502
// of the tuple), so elements with equal first items
502503
// will need to be ordered with increasing
503504
// counts... i.e. exactly asserting that this sort is
504505
// stable.
505506
assert!(v.windows(2).all(|w| w[0] <= w[1]));
507+
508+
let mut v = orig.clone();
509+
v.sort_by_cached_key(|&(x, _)| x);
510+
assert!(v.windows(2).all(|w| w[0] <= w[1]));
506511
}
507512
}
508513
}

0 commit comments

Comments
 (0)