Skip to content

Commit 265e3a6

Browse files
committed
Unit test Iterator::partition_in_place and is_partitioned
1 parent 0492f97 commit 265e3a6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/libcore/tests/iter.rs

+36
Original file line numberDiff line numberDiff line change
@@ -2460,3 +2460,39 @@ fn test_is_sorted() {
24602460
assert!(!["c", "bb", "aaa"].iter().is_sorted());
24612461
assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len()));
24622462
}
2463+
2464+
#[test]
2465+
fn test_partition() {
2466+
fn check(xs: &mut [i32], ref p: impl Fn(&i32) -> bool, expected: usize) {
2467+
let i = xs.iter_mut().partition_in_place(p);
2468+
assert_eq!(expected, i);
2469+
assert!(xs[..i].iter().all(p));
2470+
assert!(!xs[i..].iter().any(p));
2471+
assert!(xs.iter().is_partitioned(p));
2472+
if i == 0 || i == xs.len() {
2473+
assert!(xs.iter().rev().is_partitioned(p));
2474+
} else {
2475+
assert!(!xs.iter().rev().is_partitioned(p));
2476+
}
2477+
}
2478+
2479+
check(&mut [], |_| true, 0);
2480+
check(&mut [], |_| false, 0);
2481+
2482+
check(&mut [0], |_| true, 1);
2483+
check(&mut [0], |_| false, 0);
2484+
2485+
check(&mut [-1, 1], |&x| x > 0, 1);
2486+
check(&mut [-1, 1], |&x| x < 0, 1);
2487+
2488+
let ref mut xs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
2489+
check(xs, |_| true, 10);
2490+
check(xs, |_| false, 0);
2491+
check(xs, |&x| x % 2 == 0, 5); // evens
2492+
check(xs, |&x| x % 2 == 1, 5); // odds
2493+
check(xs, |&x| x % 3 == 0, 4); // multiple of 3
2494+
check(xs, |&x| x % 4 == 0, 3); // multiple of 4
2495+
check(xs, |&x| x % 5 == 0, 2); // multiple of 5
2496+
check(xs, |&x| x < 3, 3); // small
2497+
check(xs, |&x| x > 6, 3); // large
2498+
}

src/libcore/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#![feature(slice_partition_dedup)]
3232
#![feature(int_error_matching)]
3333
#![feature(const_fn)]
34+
#![feature(iter_partition_in_place)]
35+
#![feature(iter_is_partitioned)]
3436
#![warn(rust_2018_idioms)]
3537

3638
extern crate test;

0 commit comments

Comments
 (0)