1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use cranelift_codegen::data_value::{self, DataValue, DisplayDataValues};
use std::fmt::{self, Display, Formatter};
#[derive(PartialEq, Debug)]
pub enum RunCommand {
Print(Invocation),
Run(Invocation, Comparison, Vec<DataValue>),
}
impl RunCommand {
pub fn run<F>(&self, invoke_fn: F) -> Result<(), String>
where
F: FnOnce(&str, &[DataValue]) -> Result<Vec<DataValue>, String>,
{
match self {
RunCommand::Print(invoke) => {
let actual = invoke_fn(&invoke.func, &invoke.args)?;
println!("{} -> {}", invoke, DisplayDataValues(&actual))
}
RunCommand::Run(invoke, compare, expected) => {
let actual = invoke_fn(&invoke.func, &invoke.args)?;
let matched = match compare {
Comparison::Equals => *expected == actual,
Comparison::NotEquals => *expected != actual,
};
if !matched {
let actual = DisplayDataValues(&actual);
return Err(format!("Failed test: {}, actual: {}", self, actual));
}
}
}
Ok(())
}
}
impl Display for RunCommand {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
RunCommand::Print(invocation) => write!(f, "print: {}", invocation),
RunCommand::Run(invocation, comparison, expected) => {
let expected = DisplayDataValues(expected);
write!(f, "run: {} {} {}", invocation, comparison, expected)
}
}
}
}
#[derive(Debug, PartialEq)]
pub struct Invocation {
pub func: String,
pub args: Vec<DataValue>,
}
impl Invocation {
pub(crate) fn new(func: &str, args: Vec<DataValue>) -> Self {
let func = func.to_string();
Self { func, args }
}
}
impl Display for Invocation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "%{}(", self.func)?;
data_value::write_data_value_list(f, &self.args)?;
write!(f, ")")
}
}
#[allow(missing_docs)]
#[derive(Debug, PartialEq)]
pub enum Comparison {
Equals,
NotEquals,
}
impl Display for Comparison {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Comparison::Equals => write!(f, "=="),
Comparison::NotEquals => write!(f, "!="),
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::parse_run_command;
use cranelift_codegen::ir::{types, AbiParam, Signature};
use cranelift_codegen::isa::CallConv;
#[test]
fn run_a_command() {
let mut signature = Signature::new(CallConv::Fast);
signature.returns.push(AbiParam::new(types::I32));
let command = parse_run_command(";; run: %return42() == 42 ", &signature)
.unwrap()
.unwrap();
assert!(command.run(|_, _| Ok(vec![DataValue::I32(42)])).is_ok());
assert!(command.run(|_, _| Ok(vec![DataValue::I32(43)])).is_err());
}
}