about summary refs log tree commit diff
path: root/library/alloc/tests
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2022-12-08 01:42:45 -0800
committerScott McMurray <scottmcm@users.noreply.github.com>2022-12-08 01:42:45 -0800
commit58e60ac211aa7555b949016cb795989ccedd8939 (patch)
tree617eee19d24e01e14bbae99d08c9da71481148db /library/alloc/tests
parentf5418b09e84883c4de2e652a147ab9faff4eee29 (diff)
Make `VecDeque::from_iter` O(1) from `vec(_deque)::IntoIter`
Diffstat (limited to 'library/alloc/tests')
-rw-r--r--library/alloc/tests/vec_deque.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs
index d04de5a074b..823061d400f 100644
--- a/library/alloc/tests/vec_deque.rs
+++ b/library/alloc/tests/vec_deque.rs
@@ -1736,3 +1736,29 @@ fn test_resize_keeps_reserved_space_from_item() {
     d.resize(1, v);
     assert_eq!(d[0].capacity(), 1234);
 }
+
+#[test]
+fn test_collect_from_into_iter_keeps_allocation() {
+    let mut v = Vec::with_capacity(13);
+    v.extend(0..7);
+    check(v.into_iter());
+
+    let mut v = VecDeque::with_capacity(13);
+    v.extend(0..7);
+    check(v.into_iter());
+
+    fn check(mut it: impl Iterator<Item = i32>) {
+        assert_eq!(it.next(), Some(0));
+        assert_eq!(it.next(), Some(1));
+        let mut v: VecDeque<i32> = it.collect();
+        assert_eq!(v.capacity(), 13);
+        assert_eq!(v.as_slices(), ([2, 3, 4, 5, 6].as_slice(), [].as_slice()));
+        v.push_front(7);
+        assert_eq!(v.as_slices(), ([7, 2, 3, 4, 5, 6].as_slice(), [].as_slice()));
+        v.push_front(8);
+        assert_eq!(v.as_slices(), ([8, 7, 2, 3, 4, 5, 6].as_slice(), [].as_slice()));
+        v.push_front(9);
+        assert_eq!(v.as_slices(), ([9].as_slice(), [8, 7, 2, 3, 4, 5, 6].as_slice()));
+        assert_eq!(v.capacity(), 13);
+    }
+}