mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
vec_deque tests: remove static mut
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use std::cell::Cell;
|
||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||
use std::thread;
|
||||
|
||||
@@ -6,6 +5,7 @@
|
||||
|
||||
use super::*;
|
||||
use crate::testing::crash_test::{CrashTestDummy, Panic};
|
||||
use crate::testing::macros::struct_with_counted_drop;
|
||||
use crate::vec::Vec;
|
||||
|
||||
#[test]
|
||||
@@ -1010,22 +1010,6 @@ fn extract_if_drop_panic_leak() {
|
||||
assert_eq!(d7.dropped(), 1);
|
||||
}
|
||||
|
||||
macro_rules! struct_with_counted_drop {
|
||||
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
|
||||
thread_local! {static $drop_counter: Cell<u32> = Cell::new(0);}
|
||||
|
||||
struct $struct_name$(($elt_ty))?;
|
||||
|
||||
impl Drop for $struct_name {
|
||||
fn drop(&mut self) {
|
||||
$drop_counter.set($drop_counter.get() + 1);
|
||||
|
||||
$($drop_stmt(self))?
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
|
||||
fn extract_if_pred_panic_leak() {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
|
||||
#![allow(static_mut_refs)]
|
||||
|
||||
use core::iter::TrustedLen;
|
||||
|
||||
use super::*;
|
||||
use crate::testing::macros::struct_with_counted_drop;
|
||||
|
||||
#[bench]
|
||||
fn bench_push_back_100(b: &mut test::Bencher) {
|
||||
@@ -1086,36 +1084,24 @@ fn test_clone_from() {
|
||||
|
||||
#[test]
|
||||
fn test_vec_deque_truncate_drop() {
|
||||
static mut DROPS: u32 = 0;
|
||||
#[derive(Clone)]
|
||||
struct Elem(#[allow(dead_code)] i32);
|
||||
impl Drop for Elem {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
DROPS += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
struct_with_counted_drop!(Elem, DROPS);
|
||||
|
||||
let v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
|
||||
for push_front in 0..=v.len() {
|
||||
let v = v.clone();
|
||||
let mut tester = VecDeque::with_capacity(5);
|
||||
for (index, elem) in v.into_iter().enumerate() {
|
||||
const LEN: usize = 5;
|
||||
for push_front in 0..=LEN {
|
||||
let mut tester = VecDeque::with_capacity(LEN);
|
||||
for index in 0..LEN {
|
||||
if index < push_front {
|
||||
tester.push_front(elem);
|
||||
tester.push_front(Elem);
|
||||
} else {
|
||||
tester.push_back(elem);
|
||||
tester.push_back(Elem);
|
||||
}
|
||||
}
|
||||
assert_eq!(unsafe { DROPS }, 0);
|
||||
assert_eq!(DROPS.get(), 0);
|
||||
tester.truncate(3);
|
||||
assert_eq!(unsafe { DROPS }, 2);
|
||||
assert_eq!(DROPS.get(), 2);
|
||||
tester.truncate(0);
|
||||
assert_eq!(unsafe { DROPS }, 5);
|
||||
unsafe {
|
||||
DROPS = 0;
|
||||
}
|
||||
assert_eq!(DROPS.get(), 5);
|
||||
DROPS.set(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
macro_rules! struct_with_counted_drop {
|
||||
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
|
||||
thread_local! {static $drop_counter: ::core::cell::Cell<u32> = ::core::cell::Cell::new(0);}
|
||||
|
||||
struct $struct_name$(($elt_ty))?;
|
||||
|
||||
impl ::std::ops::Drop for $struct_name {
|
||||
fn drop(&mut self) {
|
||||
$drop_counter.set($drop_counter.get() + 1);
|
||||
|
||||
$($drop_stmt(self))?
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use struct_with_counted_drop;
|
||||
@@ -1,3 +1,4 @@
|
||||
pub(crate) mod crash_test;
|
||||
pub(crate) mod macros;
|
||||
pub(crate) mod ord_chaos;
|
||||
pub(crate) mod rng;
|
||||
|
||||
Reference in New Issue
Block a user