about summary refs log tree commit diff
path: root/library/alloctests/tests/vec.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/alloctests/tests/vec.rs')
-rw-r--r--library/alloctests/tests/vec.rs36
1 files changed, 26 insertions, 10 deletions
diff --git a/library/alloctests/tests/vec.rs b/library/alloctests/tests/vec.rs
index 00f640cd17e..ea334ab0f14 100644
--- a/library/alloctests/tests/vec.rs
+++ b/library/alloctests/tests/vec.rs
@@ -15,7 +15,7 @@ use std::ops::Bound::*;
 use std::panic::{AssertUnwindSafe, catch_unwind};
 use std::rc::Rc;
 use std::sync::atomic::{AtomicU32, Ordering};
-use std::vec::{Drain, IntoIter};
+use std::vec::{Drain, IntoIter, PeekMut};
 
 use crate::testing::macros::struct_with_counted_drop;
 
@@ -631,6 +631,21 @@ fn test_swap_remove_empty() {
 }
 
 #[test]
+fn test_try_remove() {
+    let mut vec = vec![1, 2, 3];
+    // We are attempting to remove vec[0] which contains 1
+    assert_eq!(vec.try_remove(0), Some(1));
+    // Now `vec` looks like: [2, 3]
+    // We will now try to remove vec[2] which does not exist
+    // This should return `None`
+    assert_eq!(vec.try_remove(2), None);
+
+    // We will try the same thing with an empty vector
+    let mut v: Vec<u8> = vec![];
+    assert!(v.try_remove(0).is_none());
+}
+
+#[test]
 fn test_move_items() {
     let vec = vec![1, 2, 3];
     let mut vec2 = vec![];
@@ -2643,15 +2658,16 @@ fn test_peek_mut() {
     assert!(vec.peek_mut().is_none());
     vec.push(1);
     vec.push(2);
-    if let Some(mut p) = vec.peek_mut() {
-        assert_eq!(*p, 2);
-        *p = 0;
-        assert_eq!(*p, 0);
-        p.pop();
-        assert_eq!(vec.len(), 1);
-    } else {
-        unreachable!()
-    }
+    let mut p = vec.peek_mut().unwrap();
+    assert_eq!(*p, 2);
+    *p = 0;
+    assert_eq!(*p, 0);
+    drop(p);
+    assert_eq!(vec, vec![1, 0]);
+    let p = vec.peek_mut().unwrap();
+    let p = PeekMut::pop(p);
+    assert_eq!(p, 0);
+    assert_eq!(vec, vec![1]);
 }
 
 /// This assortment of tests, in combination with miri, verifies we handle UB on fishy arguments