|
| 1 | +use std::fmt::Debug; |
| 2 | +use std::ops::{Range, RangeBounds}; |
| 3 | +use std::slice; |
| 4 | + |
| 5 | +fn check_is_partial_sorted<T: Ord + Clone + Debug, R: RangeBounds<usize>>(v: &mut [T], range: R) { |
| 6 | + let Range { start, end } = slice::range(range, ..v.len()); |
| 7 | + v.partial_sort_unstable(start..end); |
| 8 | + |
| 9 | + let max_before = v[..start].iter().max().into_iter(); |
| 10 | + let sorted_range = v[start..end].into_iter(); |
| 11 | + let min_after = v[end..].iter().min().into_iter(); |
| 12 | + let seq = max_before.chain(sorted_range).chain(min_after); |
| 13 | + assert!(seq.is_sorted()); |
| 14 | +} |
| 15 | + |
| 16 | +#[test] |
| 17 | +fn basic_impl() { |
| 18 | + check_is_partial_sorted::<i32, _>(&mut [], ..); |
| 19 | + check_is_partial_sorted::<(), _>(&mut [], ..); |
| 20 | + check_is_partial_sorted::<(), _>(&mut [()], ..); |
| 21 | + check_is_partial_sorted::<(), _>(&mut [(), ()], ..); |
| 22 | + check_is_partial_sorted::<(), _>(&mut [(), (), ()], ..); |
| 23 | + check_is_partial_sorted::<i32, _>(&mut [], ..); |
| 24 | + |
| 25 | + check_is_partial_sorted::<i32, _>(&mut [77], ..); |
| 26 | + check_is_partial_sorted::<i32, _>(&mut [2, 3], ..); |
| 27 | + check_is_partial_sorted::<i32, _>(&mut [2, 3, 6], ..); |
| 28 | + check_is_partial_sorted::<i32, _>(&mut [2, 3, 99, 6], ..); |
| 29 | + check_is_partial_sorted::<i32, _>(&mut [2, 7709, 400, 90932], ..); |
| 30 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], ..); |
| 31 | + |
| 32 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 0..0); |
| 33 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 0..1); |
| 34 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 0..5); |
| 35 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 0..7); |
| 36 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 7..7); |
| 37 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 6..7); |
| 38 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 5..7); |
| 39 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 5..5); |
| 40 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 4..5); |
| 41 | + check_is_partial_sorted::<i32, _>(&mut [15, -1, 3, -1, -3, -1, 7], 4..6); |
| 42 | +} |
0 commit comments