diff --git a/src/runtime2/store/tests.rs b/src/runtime2/store/tests.rs new file mode 100644 index 0000000000000000000000000000000000000000..7b720a6de059a165a379cb29676b5250216828d3 --- /dev/null +++ b/src/runtime2/store/tests.rs @@ -0,0 +1,50 @@ +pub use std::sync::Arc; +pub use std::sync::atomic::{AtomicU64, Ordering}; + +// Little wrapper for the two atomic ctor/dtor counters +#[derive(Clone)] +pub struct Counters { + pub ctor: Arc, + pub dtor: Arc, +} + +impl Counters { + pub fn new() -> Self { + return Self{ + ctor: Arc::new(AtomicU64::new(0)), + dtor: Arc::new(AtomicU64::new(0)), + }; + } +} + +macro_rules! assert_ctor_eq { + ($counters:expr, $count:expr) => { + assert_eq!($counters.ctor.load(Ordering::Acquire), $count); + } + } + +macro_rules! assert_dtor_eq { + ($counters:expr, $count:expr) => { + assert_eq!($counters.dtor.load(Ordering::Acquire), $count); + } + } + +// Utility resource structure that counts the number of constructors and +// destructor calls. +pub struct Resource { + dtor: Arc, + pub val: u64, +} + +impl Resource { + pub fn new(counters: &Counters, val: u64) -> Self { + counters.ctor.fetch_add(1, Ordering::SeqCst); + return Self{ dtor: counters.dtor.clone(), val }; + } +} + +impl Drop for Resource { + fn drop(&mut self) { + self.dtor.fetch_add(1, Ordering::SeqCst); + } +} \ No newline at end of file