about summary refs log tree commit diff
path: root/library/alloc/tests
diff options
context:
space:
mode:
authorPavel Grigorenko <GrigorenkoPV@ya.ru>2025-01-22 19:57:19 +0300
committerPavel Grigorenko <GrigorenkoPV@ya.ru>2025-01-22 20:04:36 +0300
commited7cc3486cbd0a059fa5f7d7f9198ee1c5144c01 (patch)
tree8f41eba7ed8176b53ef5e91015eb6324e7c46598 /library/alloc/tests
parentb2728d5426bab1d8c39709768c7e22b7f66dde5d (diff)
Implement `VecDeque::pop_front_if` & `VecDeque::pop_back_if`
Diffstat (limited to 'library/alloc/tests')
-rw-r--r--library/alloc/tests/lib.rs1
-rw-r--r--library/alloc/tests/vec_deque.rs39
2 files changed, 40 insertions, 0 deletions
diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs
index 393bdfe48b7..d8364d750fa 100644
--- a/library/alloc/tests/lib.rs
+++ b/library/alloc/tests/lib.rs
@@ -38,6 +38,7 @@
 #![feature(str_as_str)]
 #![feature(strict_provenance_lints)]
 #![feature(vec_pop_if)]
+#![feature(vec_deque_pop_if)]
 #![feature(unique_rc_arc)]
 #![feature(macro_metavar_expr_concat)]
 #![allow(internal_features)]
diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs
index 4b8d3c735f7..1b03c29e5bd 100644
--- a/library/alloc/tests/vec_deque.rs
+++ b/library/alloc/tests/vec_deque.rs
@@ -81,6 +81,45 @@ fn test_parameterized<T: Clone + PartialEq + Debug>(a: T, b: T, c: T, d: T) {
 }
 
 #[test]
+fn test_pop_if() {
+    let mut deq: VecDeque<_> = vec![0, 1, 2, 3, 4].into();
+    let pred = |x: &mut i32| *x % 2 == 0;
+
+    assert_eq!(deq.pop_front_if(pred), Some(0));
+    assert_eq!(deq, [1, 2, 3, 4]);
+
+    assert_eq!(deq.pop_front_if(pred), None);
+    assert_eq!(deq, [1, 2, 3, 4]);
+
+    assert_eq!(deq.pop_back_if(pred), Some(4));
+    assert_eq!(deq, [1, 2, 3]);
+
+    assert_eq!(deq.pop_back_if(pred), None);
+    assert_eq!(deq, [1, 2, 3]);
+}
+
+#[test]
+fn test_pop_if_empty() {
+    let mut deq = VecDeque::<i32>::new();
+    assert_eq!(deq.pop_front_if(|_| true), None);
+    assert_eq!(deq.pop_back_if(|_| true), None);
+    assert!(deq.is_empty());
+}
+
+#[test]
+fn test_pop_if_mutates() {
+    let mut v: VecDeque<_> = vec![-1, 1].into();
+    let pred = |x: &mut i32| {
+        *x *= 2;
+        false
+    };
+    assert_eq!(v.pop_front_if(pred), None);
+    assert_eq!(v, [-2, 1]);
+    assert_eq!(v.pop_back_if(pred), None);
+    assert_eq!(v, [-2, 2]);
+}
+
+#[test]
 fn test_push_front_grow() {
     let mut deq = VecDeque::new();
     for i in 0..66 {