puffin/
test_utils.rs

1use crate::algebra::term::TermType;
2use crate::algebra::{DYTerm, Term};
3use crate::execution::{ExecutionStatus, ForkError};
4use crate::graphviz::write_graphviz;
5use crate::protocol::ProtocolTypes;
6use crate::trace::{Action, Trace};
7
8impl<PT: ProtocolTypes> Trace<PT> {
9    #[must_use]
10    pub fn count_functions_by_name(&self, find_name: &'static str) -> usize {
11        self.steps
12            .iter()
13            .map(|step| match &step.action {
14                Action::Input(input) => input.recipe.count_functions_by_name(find_name),
15                Action::Output(_) => 0,
16            })
17            .sum()
18    }
19
20    #[must_use]
21    pub fn count_functions(&self) -> usize {
22        self.steps
23            .iter()
24            .filter_map(|step| match &step.action {
25                Action::Input(input) => Some(&input.recipe),
26                Action::Output(_) => None,
27            })
28            .map(|term| term.size())
29            .sum()
30    }
31
32    pub fn write_plots(&self, i: u16) {
33        write_graphviz(
34            format!("test_mutation{i}.svg").as_str(),
35            "svg",
36            self.dot_graph(true).as_str(),
37        )
38        .unwrap();
39    }
40}
41
42impl<PT: ProtocolTypes> Term<PT> {
43    pub fn count_functions_by_name(&self, find_name: &'static str) -> usize {
44        let mut found = 0;
45        for term in self {
46            if let DYTerm::Application(func, _) = &term.term {
47                if func.name() == find_name {
48                    found += 1;
49                }
50            }
51        }
52        found
53    }
54}
55
56pub trait AssertExecution {
57    fn expect_crash(self);
58}
59
60impl AssertExecution for Result<ExecutionStatus, ForkError> {
61    fn expect_crash(self) {
62        use ExecutionStatus as S;
63        match self {
64            Ok(S::Crashed) => (),
65            Ok(S::Failure(_)) => panic!("invalid trace"),
66            Ok(S::Timeout) => panic!("trace execution timed out"),
67            Ok(S::Interrupted) => panic!("trace execution interrupted"),
68            Ok(S::Success) => panic!("expected trace execution to crash, but succeeded"),
69            Err(reason) => panic!("trace execution error: {reason}"),
70        }
71    }
72}
73
74#[macro_export]
75macro_rules! test_puts {
76    // handle default arguments
77    ( $func:ident) => { test_puts!( $func, attrs = [], filter = all() ); };
78    ( $func:ident, puts = $puts:tt ) => { test_puts!( $func, puts = $puts, attrs = [], filter = all() ); };
79    ( $func:ident, puts = $puts:tt, attrs = $attrs:tt ) => { test_puts!( $func, puts = $puts, attrs = $attrs, filter = all() ); };
80    ( $func:ident, puts = $puts:tt, filter = $filter:meta ) => { test_puts!( $func, puts = $puts, attrs = [], filter = $filter ); };
81    ( $func:ident, attrs = $attrs:tt  ) => { test_puts!( $func, puts = all, attrs = $attrs, filter = all() ); };
82    ( $func:ident, filter = $filter:meta ) => { test_puts!( $func, puts = all, attrs = [], filter = $filter ); };
83    ( $func:ident, attrs = $attrs:tt, filter = $filter:meta ) => { test_puts!( $func, puts = all, attrs = $attrs, filter = $filter ); };
84
85    // put arguments in a canonical order
86    ( $func:ident, attrs = $attrs:tt, puts = $puts:tt, filter = $filter:meta ) => { test_puts!($func, puts = $puts, attrs = $attrs, filter = $filter); };
87    ( $func:ident, attrs = $attrs:tt, filter = $filter:meta, puts = $puts:tt ) => { test_puts!($func, puts = $puts, attrs = $attrs, filter = $filter); };
88    ( $func:ident, filter = $filter:meta, puts = $puts:tt, attrs = $attrs:tt ) => { test_puts!($func, puts = $puts, attrs = $attrs, filter = $filter); };
89    ( $func:ident, filter = $filter:meta, attrs = $attrs:tt, puts = $puts:tt ) => { test_puts!($func, puts = $puts, attrs = $attrs, filter = $filter); };
90    ( $func:ident, puts = $puts:tt, filter = $filter:meta, attrs = $attrs:tt ) => { test_puts!($func, puts = $puts, attrs = $attrs, filter = $filter); };
91
92    // expand `puts` argument when `all` was requested
93    ( $func:ident, puts = all, attrs = $attrs:tt, filter = $filter:meta ) => {
94        mod $func {
95            #![allow(unused_imports)]
96            #![allow(unexpected_cfgs)]
97
98            use super::$func;
99            use super::for_puts;
100            use super::test_puts;
101            use puffin_macros::expand_cfg;
102
103            for_puts!(
104                test_puts!(@expand-one $func, put = __PUT__:__PUTSTR__, attrs = $attrs, filter = $filter);
105            );
106        }
107    };
108
109    // actual expansion with canonical arguments
110    ( $func:ident, puts = [ $($put:ident : $putstr:literal),* ], attrs = $attrs:tt, filter = $filter:meta ) => {
111        mod $func {
112            #![allow(unused_imports)]
113            #![allow(unexpected_cfgs)]
114
115            use super::$func;
116            use super::test_puts;
117            use puffin_macros::expand_cfg;
118
119            $(
120                test_puts!(@expand-one $func, put = $put : $putstr, attrs = $attrs, filter = $filter);
121            )*
122        }
123    };
124
125    (@expand-one $func:ident, put = $put:ident : $putstr:literal, attrs = [ $( $attr:meta ),* ], filter = $filter:meta) => {
126        #[cfg(has_put = $putstr)]
127        #[expand_cfg($putstr, $filter)]
128        #[test_log::test]
129        $( #[$attr] )*
130        fn $put() {
131            $func($putstr);
132        }
133    };
134}
135
136#[macro_export]
137macro_rules! test_differential_puts {
138    ($func:ident,first = $first:literal,second = $second:literal) => {
139        mod $func {
140            #![allow(unexpected_cfgs)]
141            use super::*;
142
143            #[cfg(all(has_put = $first, has_put = $second))]
144            #[test_log::test]
145            fn run() {
146                super::$func();
147            }
148        }
149    };
150}
151
152#[macro_export]
153#[allow(clippy::crate_in_macro_def)]
154macro_rules! supports {
155    ($put:expr, $cap:expr) => {{
156        use crate::put_registry::tls_registry;
157
158        tls_registry()
159            .find_by_id($put)
160            .expect("PUT was not found")
161            .supports($cap)
162    }};
163}
164
165#[allow(unused_imports)]
166pub(crate) use {supports, test_differential_puts, test_puts};