about summary refs log tree commit diff
path: root/src/libcore/tests
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-01-18 01:57:13 +0800
committerGitHub <noreply@github.com>2018-01-18 01:57:13 +0800
commit175dd84ed8969fe136a4da1c7e5fe1d9c7693f2d (patch)
treee273945750e0615538bc5314972aeee154425c71 /src/libcore/tests
parent283ee544586a0952a9a45782f42addc9dbac3392 (diff)
parent0b56ab0f7b0c5e01611b7ea6a28c77bc09c26275 (diff)
downloadrust-175dd84ed8969fe136a4da1c7e5fe1d9c7693f2d.tar.gz
rust-175dd84ed8969fe136a4da1c7e5fe1d9c7693f2d.zip
Rollup merge of #47333 - arthurprs:iter-position-bounds-check, r=dtolnay
Optimize slice.{r}position result bounds check

Second attempt of https://github.com/rust-lang/rust/pull/45501
Fixes https://github.com/rust-lang/rust/issues/45964

Demo: https://godbolt.org/g/N4mBHp
Diffstat (limited to 'src/libcore/tests')
-rw-r--r--src/libcore/tests/slice.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs
index f7a4a71e5cf..13740b95802 100644
--- a/src/libcore/tests/slice.rs
+++ b/src/libcore/tests/slice.rs
@@ -10,6 +10,25 @@
 
 use core::result::Result::{Ok, Err};
 
+
+#[test]
+fn test_position() {
+    let b = [1, 2, 3, 5, 5];
+    assert!(b.iter().position(|&v| v == 9) == None);
+    assert!(b.iter().position(|&v| v == 5) == Some(3));
+    assert!(b.iter().position(|&v| v == 3) == Some(2));
+    assert!(b.iter().position(|&v| v == 0) == None);
+}
+
+#[test]
+fn test_rposition() {
+    let b = [1, 2, 3, 5, 5];
+    assert!(b.iter().rposition(|&v| v == 9) == None);
+    assert!(b.iter().rposition(|&v| v == 5) == Some(4));
+    assert!(b.iter().rposition(|&v| v == 3) == Some(2));
+    assert!(b.iter().rposition(|&v| v == 0) == None);
+}
+
 #[test]
 fn test_binary_search() {
     let b: [i32; 0] = [];