From 16a0238dc52836f4707be368be207a50a271c995 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 6 Nov 2018 20:51:44 -0500 Subject: [PATCH] Add some more tests --- src/lib.rs | 10 ++++++++- tests/simple.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ef83620..b8251f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,15 @@ static LOG_LEVEL: RwLock = RwLock::new(Level::Debug); pub struct QADAPT; pub fn set_panic(b: bool) { - *DO_PANIC.write() = b; + let mut val = DO_PANIC.write(); + if *val == b { + let level = LOG_LEVEL.read(); + if log_enabled!(*level) { + log!(*level, "Panic flag was already {}, potential data race", b) + } + } + + *val = b; } pub fn set_log_level(level: Level) { diff --git a/tests/simple.rs b/tests/simple.rs index 86a225a..bc09bb1 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -27,4 +27,63 @@ fn test_allocate() { set_panic(true); let _x = Box::new(12); set_panic(false); +} + +fn unit_result(b: bool) -> Result<(), ()> { + if b { + Ok(()) + } else { + Err(()) + } +} + +#[test] +fn test_unit_result() { + set_panic(true); + #[allow(unused)] + { black_box(unit_result(true)); } + black_box(unit_result(true)).unwrap(); + #[allow(unused)] + { black_box(unit_result(false)); } + black_box(unit_result(false)).unwrap_err(); + set_panic(false); +} + +#[test] +#[should_panic] +fn test_vec_push() { + let mut v = Vec::new(); + set_panic(true); + v.push(0); +} + +#[test] +fn test_vec_push_capacity() { + let mut v = Vec::with_capacity(1); + set_panic(true); + v.push(0); + v.pop(); + v.push(0); + set_panic(false); +} + +#[test] +fn test_vec_with_zero() { + set_panic(true); + let _v: Vec = black_box(Vec::with_capacity(0)); + set_panic(false); +} + +#[test] +fn test_vec_new() { + set_panic(true); + let _v: Vec = black_box(Vec::new()); + set_panic(false); +} + +#[test] +#[should_panic] +fn test_vec_with_one() { + set_panic(true); + let _v: Vec = Vec::with_capacity(1); } \ No newline at end of file