() > 0 {
+ assert!(a.data.capacity() < initial_len);
+ assert!(a.data.capacity() >= a.len());
+ }
+ assert_eq!(a.data.capacity(), new_capacity);
+ }
+
+ let a = Array::from_iter(0..56)
+ .into_shape_with_order((8, 7))
+ .unwrap();
+ assert_shrink_before_after(a, Slice::new(1, Some(-1), 1), Slice::new(0, None, 2), 42);
+
+ let a = Array::from_iter(0..56)
+ .into_shape_with_order((8, 7))
+ .unwrap();
+ assert_shrink_before_after(a, Slice::new(1, Some(-1), -1), Slice::new(0, None, -1), 42);
+
+ let a = Array::from_iter(0..56)
+ .into_shape_with_order((8, 7))
+ .unwrap();
+ assert_shrink_before_after(a, Slice::new(1, Some(3), 1), Slice::new(1, None, -2), 12);
+
+ // empty but still has some allocation to allow offsetting along each stride
+ let a = Array::from_iter(0..56)
+ .into_shape_with_order((8, 7))
+ .unwrap();
+ assert_shrink_before_after(a, Slice::new(1, Some(1), 1), Slice::new(1, None, 1), 6);
+
+ // Test ZST
+ let a = Array::from_iter((0..56).map(|_| ()))
+ .into_shape_with_order((8, 7))
+ .unwrap();
+ assert_shrink_before_after(a, Slice::new(1, Some(3), 1), Slice::new(1, None, -2), usize::MAX);
+ }
+}
diff --git a/tests/assign.rs b/tests/assign.rs
index 29a6b851a..4fbcabe2c 100644
--- a/tests/assign.rs
+++ b/tests/assign.rs
@@ -232,6 +232,39 @@ fn move_into()
}
}
+#[test]
+fn shrink_to_fit_slicing()
+{
+ // Count correct number of drops when using shrink_to_fit and discontiguous arrays (with holes).
+ for &use_f_order in &[false, true] {
+ for &invert_axis in &[0b00, 0b01, 0b10, 0b11] {
+ // bitmask for axis to invert
+ let counter = DropCounter::default();
+ {
+ let (m, n) = (5, 4);
+
+ let mut a = Array::from_shape_fn((m, n).set_f(use_f_order), |_idx| counter.element());
+ a.slice_collapse(s![1..-1, ..;2]);
+ if invert_axis & 0b01 != 0 {
+ a.invert_axis(Axis(0));
+ }
+ if invert_axis & 0b10 != 0 {
+ a.invert_axis(Axis(1));
+ }
+
+ a.shrink_to_fit();
+
+ let total = m * n;
+ let dropped_1 = if use_f_order { n * 2 - 1 } else { m * 2 - 1 };
+ assert_eq!(counter.created(), total);
+ assert_eq!(counter.dropped(), dropped_1 as usize);
+ drop(a);
+ }
+ counter.assert_drop_count();
+ }
+ }
+}
+
/// This counter can create elements, and then count and verify
/// the number of which have actually been dropped again.
#[derive(Default)]
@@ -241,6 +274,7 @@ struct DropCounter
dropped: AtomicUsize,
}
+#[derive(Debug)]
struct Element<'a>(&'a AtomicUsize);
impl DropCounter
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy