use std::io::{Read, Write};
use std::os::unix::io::FromRawFd;
use std::process::Command;
const SEP: &str = "#==#";
const CORPUS: &str = "tests/data/parity_corpus.R";
const EXPECTED: &str = "tests/data/parity_expected.txt";
const EXAMPLES_DIR: &str = "examples";
const EXAMPLES_OUT: &str = "tests/data/examples";
const STACK_SIZE: usize = 512 * 1024 * 1024;
fn main() {
std::thread::Builder::new()
.stack_size(STACK_SIZE)
.spawn(run)
.expect("spawn parity thread")
.join()
.expect("parity thread panicked");
}
fn run() {
if std::env::args().any(|a| a == "--freeze-examples") {
freeze_examples();
return;
}
let freeze = std::env::args().any(|a| a == "--freeze");
let corpus = std::fs::read_to_string(CORPUS).unwrap_or_else(|e| {
eprintln!("parity: cannot read {CORPUS}: {e}");
std::process::exit(1);
});
let snippets = split_snippets(&corpus);
if freeze {
let mut out = String::new();
for (i, s) in snippets.iter().enumerate() {
if i > 0 {
out.push_str(SEP);
out.push('\n');
}
out.push_str(&oracle(s));
}
std::fs::write(EXPECTED, out).expect("write expected");
println!("froze {} oracle outputs -> {EXPECTED}", snippets.len());
return;
}
let (mut parity, mut gaps, mut panics) = (0, Vec::new(), Vec::new());
for (i, s) in snippets.iter().enumerate() {
let want = oracle(s);
match rlang_run(s) {
Ok(got) if got == want => parity += 1,
Ok(got) => gaps.push((i, s.clone(), want, got)),
Err(e) => panics.push((i, s.clone(), e)),
}
}
println!("parity: {}/{} snippets", parity, snippets.len());
for (i, s, want, got) in &gaps {
println!(
"\n── GAP #{i} ──\n{}\n expected: {:?}\n got: {:?}",
first_line(s),
want,
got
);
}
for (i, s, e) in &panics {
println!("\n── PANIC #{i} ──\n{}\n error: {e}", first_line(s));
}
if !gaps.is_empty() || !panics.is_empty() {
println!("\n{} gap(s), {} panic(s)", gaps.len(), panics.len());
std::process::exit(1);
}
println!("all snippets at parity");
}
fn freeze_examples() {
std::fs::create_dir_all(EXAMPLES_OUT).expect("create examples out dir");
let mut names: Vec<String> = std::fs::read_dir(EXAMPLES_DIR)
.expect("read examples dir")
.filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().to_string()))
.filter(|n| n.ends_with(".R"))
.collect();
names.sort();
for name in &names {
let path = format!("{EXAMPLES_DIR}/{name}");
let out = Command::new("Rscript")
.arg(&path)
.output()
.expect("run Rscript");
if !out.status.success() {
eprintln!(
"parity: reference Rscript rejected {path}:\n{}",
String::from_utf8_lossy(&out.stderr)
);
std::process::exit(1);
}
let stem = name.strip_suffix(".R").unwrap();
std::fs::write(format!("{EXAMPLES_OUT}/{stem}.out"), &out.stdout).expect("write out");
}
println!("froze {} example outputs -> {EXAMPLES_OUT}/", names.len());
}
fn split_snippets(corpus: &str) -> Vec<String> {
corpus
.split(&format!("{SEP}\n"))
.map(|s| s.trim_end_matches('\n').to_string())
.filter(|s| !s.trim().is_empty())
.collect()
}
fn first_line(s: &str) -> &str {
s.lines().next().unwrap_or("")
}
fn oracle(snippet: &str) -> String {
match Command::new("Rscript").arg("-e").arg(snippet).output() {
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
Ok(_) => "<error>".to_string(),
Err(e) => format!("<oracle-unavailable: {e}>"),
}
}
fn rlang_run(snippet: &str) -> Result<String, String> {
let snippet = snippet.to_string();
let res = std::panic::catch_unwind(move || capture_stdout(|| rlang::eval_str(&snippet)));
match res {
Ok((Ok(_), out)) => Ok(out),
Ok((Err(_), _)) => Ok("<error>".to_string()),
Err(_) => Err("panicked".to_string()),
}
}
fn capture_stdout<R>(f: impl FnOnce() -> R) -> (R, String) {
unsafe {
let mut fds = [0i32; 2];
if libc::pipe(fds.as_mut_ptr()) != 0 {
return (f(), String::new());
}
let saved = libc::dup(1);
libc::dup2(fds[1], 1);
libc::close(fds[1]);
let r = f();
let _ = std::io::stdout().flush();
libc::dup2(saved, 1);
libc::close(saved);
let mut out = String::new();
let mut file = std::fs::File::from_raw_fd(fds[0]);
let _ = file.read_to_string(&mut out);
(r, out)
}
}