Skip to main content

grommet_macros/
lib.rs

1//! SQLite-style coverage accounting with zero production abstraction cost.
2//!
3//! `always!` and `never!` are three separately compiled definitions: asserted
4//! in debug, folded out of coverage, and literal pass-throughs in release.
5//! Arguments must be side-effect free because coverage builds discard them.
6
7#![cfg_attr(not(coverage), no_std)]
8
9#[cfg(coverage)]
10#[macro_export]
11macro_rules! always {
12    ($condition:expr) => {{
13        let _ = &$condition;
14        true
15    }};
16}
17
18#[cfg(coverage)]
19#[macro_export]
20macro_rules! never {
21    ($condition:expr) => {{
22        let _ = &$condition;
23        false
24    }};
25}
26
27#[cfg(all(not(coverage), debug_assertions))]
28#[macro_export]
29macro_rules! always {
30    ($condition:expr) => {{
31        let value = $condition;
32        debug_assert!(value, "ALWAYS violated: {}", stringify!($condition));
33        value
34    }};
35}
36
37#[cfg(all(not(coverage), debug_assertions))]
38#[macro_export]
39macro_rules! never {
40    ($condition:expr) => {{
41        let value = $condition;
42        debug_assert!(!value, "NEVER violated: {}", stringify!($condition));
43        value
44    }};
45}
46
47#[cfg(all(not(coverage), not(debug_assertions)))]
48#[macro_export]
49macro_rules! always {
50    ($condition:expr) => {
51        $condition
52    };
53}
54
55#[cfg(all(not(coverage), not(debug_assertions)))]
56#[macro_export]
57macro_rules! never {
58    ($condition:expr) => {
59        $condition
60    };
61}
62
63#[cfg(coverage)]
64#[macro_export]
65macro_rules! testcase {
66    ($condition:expr) => {{
67        let value = $condition;
68        $crate::coverage::record(file!(), line!(), value);
69        value
70    }};
71}
72
73#[cfg(not(coverage))]
74#[macro_export]
75macro_rules! testcase {
76    ($condition:expr) => {
77        $condition
78    };
79}
80
81#[cfg(coverage)]
82pub mod coverage {
83    use std::collections::BTreeMap;
84    use std::sync::{Mutex, OnceLock};
85
86    const SAW_TRUE: u8 = 0b01;
87    const SAW_FALSE: u8 = 0b10;
88    type Table = BTreeMap<(&'static str, u32), u8>;
89
90    fn table() -> &'static Mutex<Table> {
91        static TABLE: OnceLock<Mutex<Table>> = OnceLock::new();
92        TABLE.get_or_init(|| Mutex::new(Table::new()))
93    }
94
95    pub fn record(file: &'static str, line: u32, value: bool) {
96        let bit = if value { SAW_TRUE } else { SAW_FALSE };
97        *table().lock().expect("coverage table poisoned").entry((file, line)).or_insert(0) |= bit;
98    }
99
100    pub fn assert_all_covered() {
101        let table = table().lock().expect("coverage table poisoned");
102        let incomplete: Vec<_> = table
103            .iter()
104            .filter(|(_, mask)| **mask != SAW_TRUE | SAW_FALSE)
105            .map(|(&(file, line), &mask)| {
106                let missing = if mask & SAW_TRUE == 0 { "true" } else { "false" };
107                format!("{file}:{line} never observed {missing}")
108            })
109            .collect();
110        assert!(
111            incomplete.is_empty(),
112            "incomplete testcase! coverage:\n  {}",
113            incomplete.join("\n  ")
114        );
115    }
116}
117
118#[cfg(all(fault_injection, not(debug_assertions), not(sim)))]
119compile_error!(
120    "fault injection is enabled in an optimized non-sim build; also pass --cfg sim for an intentional simulation"
121);