Skip to content

Commit e5f75a9

Browse files
committed
test: unwind safety edge cases
Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
1 parent 39ba1b7 commit e5f75a9

1 file changed

Lines changed: 48 additions & 44 deletions

File tree

tests/unwind_safety.rs

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -98,77 +98,81 @@ fn fallible_pin_init(should_error: bool) -> impl PinInit<Counted, Error> {
9898
#[test]
9999
fn init_array_from_fn_drops_initialized_prefix_on_panic() {
100100
const N: usize = 10;
101-
const PANIC_AT: usize = 5;
102101

103-
let _g = LOCK.lock().unwrap();
104-
DROPS.store(0, Ordering::SeqCst);
102+
for panic_at in [0, 5, N - 1] {
103+
let _g = LOCK.lock().unwrap();
104+
DROPS.store(0, Ordering::SeqCst);
105105

106-
let func = AssertUnwindSafe(|| {
107-
let init = init_array_from_fn(|i| {
108-
let should_panic = i == PANIC_AT;
109-
maybe_panicking_init(should_panic)
106+
let func = AssertUnwindSafe(|| {
107+
let init = init_array_from_fn(|i| {
108+
let should_panic = i == panic_at;
109+
maybe_panicking_init(should_panic)
110+
});
111+
let _array: Result<Box<[Counted; N]>, _> = Box::init(init);
110112
});
111-
let _array: Result<Box<[Counted; N]>, _> = Box::init(init);
112-
});
113-
let result = catch_unwind(func);
113+
let result = catch_unwind(func);
114114

115-
assert!(result.is_err());
116-
assert_eq!(DROPS.load(Ordering::SeqCst), PANIC_AT);
115+
assert!(result.is_err());
116+
assert_eq!(DROPS.load(Ordering::SeqCst), panic_at);
117+
}
117118
}
118119

119120
#[test]
120121
fn pin_init_array_from_fn_drops_initialized_prefix_on_panic() {
121122
const N: usize = 10;
122-
const PANIC_AT: usize = 5;
123123

124-
let _g = LOCK.lock().unwrap();
125-
DROPS.store(0, Ordering::SeqCst);
124+
for panic_at in [0, 5, N - 1] {
125+
let _g = LOCK.lock().unwrap();
126+
DROPS.store(0, Ordering::SeqCst);
126127

127-
let func = AssertUnwindSafe(|| {
128-
let init = pin_init_array_from_fn(|i| {
129-
let should_panic = i == PANIC_AT;
130-
maybe_panicking_pin_init(should_panic)
128+
let func = AssertUnwindSafe(|| {
129+
let init = pin_init_array_from_fn(|i| {
130+
let should_panic = i == panic_at;
131+
maybe_panicking_pin_init(should_panic)
132+
});
133+
let _array: Result<Pin<Box<[Counted; N]>>, _> = Box::pin_init(init);
131134
});
132-
let _array: Result<Pin<Box<[Counted; N]>>, _> = Box::pin_init(init);
133-
});
134-
let result = catch_unwind(func);
135+
let result = catch_unwind(func);
135136

136-
assert!(result.is_err());
137-
assert_eq!(DROPS.load(Ordering::SeqCst), PANIC_AT);
137+
assert!(result.is_err());
138+
assert_eq!(DROPS.load(Ordering::SeqCst), panic_at);
139+
}
138140
}
139141

140142
#[test]
141143
fn init_array_from_fn_drops_initialized_prefix_on_error() {
142144
const N: usize = 10;
143-
const ERROR_AT: usize = 5;
144145

145-
let _g = LOCK.lock().unwrap();
146-
DROPS.store(0, Ordering::SeqCst);
146+
for error_at in [0, 5, N - 1] {
147+
let _g = LOCK.lock().unwrap();
148+
DROPS.store(0, Ordering::SeqCst);
147149

148-
let init = init_array_from_fn(|i| {
149-
let should_error = i == ERROR_AT;
150-
fallible_init(should_error)
151-
});
152-
let result: Result<Box<[Counted; N]>, _> = Box::try_init(init);
153-
assert!(result.is_err());
154-
assert_eq!(DROPS.load(Ordering::SeqCst), ERROR_AT);
150+
let init = init_array_from_fn(|i| {
151+
let should_error = i == error_at;
152+
fallible_init(should_error)
153+
});
154+
let result: Result<Box<[Counted; N]>, _> = Box::try_init(init);
155+
assert!(result.is_err());
156+
assert_eq!(DROPS.load(Ordering::SeqCst), error_at);
157+
}
155158
}
156159

157160
#[test]
158161
fn pin_init_array_from_fn_drops_initialized_prefix_on_error() {
159162
const N: usize = 10;
160-
const ERROR_AT: usize = 5;
161163

162-
let _g = LOCK.lock().unwrap();
163-
DROPS.store(0, Ordering::SeqCst);
164+
for error_at in [0, 5, N - 1] {
165+
let _g = LOCK.lock().unwrap();
166+
DROPS.store(0, Ordering::SeqCst);
164167

165-
let init = pin_init_array_from_fn(|i| {
166-
let should_error = i == ERROR_AT;
167-
fallible_pin_init(should_error)
168-
});
169-
let result: Result<Pin<Box<[Counted; N]>>, _> = Box::try_pin_init(init);
170-
assert!(result.is_err());
171-
assert_eq!(DROPS.load(Ordering::SeqCst), ERROR_AT);
168+
let init = pin_init_array_from_fn(|i| {
169+
let should_error = i == error_at;
170+
fallible_pin_init(should_error)
171+
});
172+
let result: Result<Pin<Box<[Counted; N]>>, _> = Box::try_pin_init(init);
173+
assert!(result.is_err());
174+
assert_eq!(DROPS.load(Ordering::SeqCst), error_at);
175+
}
172176
}
173177

174178
#[test]

0 commit comments

Comments
 (0)