about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2019-07-09 12:39:19 -0700
committerJosh Stone <jistone@redhat.com>2019-07-09 12:39:25 -0700
commit265e3a6230364455dfe55378b59833be93e17084 (patch)
tree3668e1411c6ab26685ca1c3275f2fd2280a107c9 /src/libcore
parent0492f972c7751daaa819a937c75ceadd0cf5326e (diff)
downloadrust-265e3a6230364455dfe55378b59833be93e17084.tar.gz
rust-265e3a6230364455dfe55378b59833be93e17084.zip
Unit test Iterator::partition_in_place and is_partitioned
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/tests/iter.rs36
-rw-r--r--src/libcore/tests/lib.rs2
2 files changed, 38 insertions, 0 deletions
diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs
index 4d840ef24c8..b7b0849e212 100644
--- a/src/libcore/tests/iter.rs
+++ b/src/libcore/tests/iter.rs
@@ -2460,3 +2460,39 @@ fn test_is_sorted() {
     assert!(!["c", "bb", "aaa"].iter().is_sorted());
     assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len()));
 }
+
+#[test]
+fn test_partition() {
+    fn check(xs: &mut [i32], ref p: impl Fn(&i32) -> bool, expected: usize) {
+        let i = xs.iter_mut().partition_in_place(p);
+        assert_eq!(expected, i);
+        assert!(xs[..i].iter().all(p));
+        assert!(!xs[i..].iter().any(p));
+        assert!(xs.iter().is_partitioned(p));
+        if i == 0 || i == xs.len() {
+            assert!(xs.iter().rev().is_partitioned(p));
+        } else {
+            assert!(!xs.iter().rev().is_partitioned(p));
+        }
+    }
+
+    check(&mut [], |_| true, 0);
+    check(&mut [], |_| false, 0);
+
+    check(&mut [0], |_| true, 1);
+    check(&mut [0], |_| false, 0);
+
+    check(&mut [-1, 1], |&x| x > 0, 1);
+    check(&mut [-1, 1], |&x| x < 0, 1);
+
+    let ref mut xs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
+    check(xs, |_| true, 10);
+    check(xs, |_| false, 0);
+    check(xs, |&x| x % 2 == 0, 5); // evens
+    check(xs, |&x| x % 2 == 1, 5); // odds
+    check(xs, |&x| x % 3 == 0, 4); // multiple of 3
+    check(xs, |&x| x % 4 == 0, 3); // multiple of 4
+    check(xs, |&x| x % 5 == 0, 2); // multiple of 5
+    check(xs, |&x| x < 3, 3); // small
+    check(xs, |&x| x > 6, 3); // large
+}
diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs
index 4b48d122590..cbb6423d710 100644
--- a/src/libcore/tests/lib.rs
+++ b/src/libcore/tests/lib.rs
@@ -31,6 +31,8 @@
 #![feature(slice_partition_dedup)]
 #![feature(int_error_matching)]
 #![feature(const_fn)]
+#![feature(iter_partition_in_place)]
+#![feature(iter_is_partitioned)]
 #![warn(rust_2018_idioms)]
 
 extern crate test;