Skip to content

Commit b68711c

Browse files
committed
Add test for drop behavior
1 parent f8ec8e2 commit b68711c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

library/alloc/tests/vec.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,41 @@ fn test_vec_macro_repeat_const() {
23332333
assert_eq!(vec![const { ITEM }; 3], vec![ITEM, ITEM, ITEM]);
23342334
}
23352335

2336+
#[test]
2337+
fn test_vec_macro_repeat_const_drop_behavior() {
2338+
thread_local! { static DROP_COUNT: Cell<usize> = const { Cell::new(0) } };
2339+
2340+
fn with_drop_count_scope(count: usize, f: impl FnOnce()) {
2341+
struct RestoreOldDropCount(usize);
2342+
2343+
impl Drop for RestoreOldDropCount {
2344+
fn drop(&mut self) {
2345+
DROP_COUNT.set(self.0);
2346+
}
2347+
}
2348+
2349+
let _restore_old_drop_count = RestoreOldDropCount(DROP_COUNT.replace(count));
2350+
2351+
f();
2352+
}
2353+
2354+
struct DropCounter;
2355+
2356+
impl Drop for DropCounter {
2357+
fn drop(&mut self) {
2358+
DROP_COUNT.set(DROP_COUNT.get().checked_add(1).unwrap());
2359+
}
2360+
}
2361+
2362+
for n in 0..10 {
2363+
with_drop_count_scope(0, || {
2364+
_ = vec![const { DropCounter }; n];
2365+
2366+
assert_eq!(DROP_COUNT.get(), n);
2367+
});
2368+
}
2369+
}
2370+
23362371
#[test]
23372372
fn test_vec_swap() {
23382373
let mut a: Vec<isize> = vec![0, 1, 2, 3, 4, 5, 6];

0 commit comments

Comments
 (0)