| @@ -1,46 +1,22 @@ |
| 1 | | −//! Differential testing (M5): every `tests/run` program is compiled to C, |
| 1 | +//! Differential testing: every `tests/run` program is compiled to C, |
| 2 | 2 | //! built against `tilrt`, and run. It must print exactly what the |
| 3 | 3 | //! interpreter prints (its `//>` lines; `tests/run.rs` in `tilc_interp` |
| 4 | 4 | //! checks the interpreter against the same lines), stop with the expected |
| 5 | 5 | //! panic if any, and leave 0 live heap blocks (the Perceus leak oracle). |
| 6 | 6 | //! |
| 7 | | −//! Programs needing what M5 part 1 does not compile yet are listed in |
| 8 | | −//! `NOT_YET`, with the reason; any other program that is rejected fails. |
| 9 | | −//! |
| 10 | 7 | //! Set `TILC_CC` and `TILC_CFLAGS` to choose the compiler and add flags |
| 11 | 8 | //! (CI adds `-fsanitize=address,undefined` with clang). |
| 12 | 9 | |
| 13 | | −mod common; |
| 14 | | − |
| 15 | 10 | use std::path::{Path, PathBuf}; |
| 16 | 11 | use std::process::Command; |
| 17 | 12 | |
| 18 | | −use common::build_runtime; |
| 19 | | −use tilc_db::Db; |
| 20 | | − |
| 21 | | −/// Programs M5 part 1 rejects, and the reason given. |
| 22 | | −const NOT_YET: &[(&str, &str)] = &[]; |
| 23 | | − |
| 24 | | −enum Outcome { |
| 25 | | − Passed, |
| 26 | | − Rejected(String), |
| 27 | | −} |
| 13 | +use tilc_testkit::{Expectations, build_runtime, compile_file, run_programs}; |
| 28 | 14 | |
| 29 | | −fn run(path: &Path, out_dir: &Path) -> Result<Outcome, String> { |
| 30 | | − let text = std::fs::read_to_string(path).unwrap(); |
| 31 | | − let module = path.file_stem().unwrap().to_string_lossy().into_owned(); |
| 32 | | − let mut db = Db::new(); |
| 33 | | − db.add_module(&module, &path.display().to_string(), text.clone()); |
| 34 | | − let analysis = db.analyze(); |
| 35 | | − if let Some(d) = analysis.diagnostics.iter().find(|d| d.is_error()) { |
| 36 | | − return Err(format!("does not check: [{}] {}", d.code, d.message)); |
| 37 | | − } |
| 38 | | − let program = tilc_core::lower::lower(&analysis.hir, &analysis.typeck); |
| 39 | | − let entry = tilc_core::entry(&analysis.hir, &analysis.typeck, &program, &module)?; |
| 40 | | − let c = match tilc_codegen_c::generate(&program, entry, Some(db.sources())) { |
| 41 | | − Ok(c) => c, |
| 42 | | − Err(e) => return Ok(Outcome::Rejected(e.0)), |
| 43 | | − }; |
| 15 | +fn run(path: &Path, out_dir: &Path) -> Result<(), String> { |
| 16 | + let (compiled, module) = compile_file(path)?; |
| 17 | + let entry = compiled.entry(&module)?; |
| 18 | + let c = tilc_codegen_c::generate(&compiled.program, entry, Some(compiled.db.sources())) |
| 19 | + .map_err(|e| format!("rejected: {}", e.0))?; |
| 44 | 20 | let exe = out_dir.join(&module); |
| 45 | 21 | tilc_build::build_executable(&c, &exe, &tilc_build::CcOptions::default()) |
| 46 | 22 | .map_err(|e| e.to_string())?; |
| @@ -70,21 +46,14 @@ fn run(path: &Path, out_dir: &Path) -> Result<Outcome, String> { |
| 70 | 46 | )); |
| 71 | 47 | } |
| 72 | 48 | |
| 73 | | − let expected: String = text |
| 74 | | − .lines() |
| 75 | | − .filter_map(|l| l.trim_start().strip_prefix("//>")) |
| 76 | | − .map(|l| format!("{}\n", l.strip_prefix(' ').unwrap_or(l))) |
| 77 | | − .collect(); |
| 78 | | − if stdout != expected { |
| 49 | + let expected = Expectations::parse(&std::fs::read_to_string(path).unwrap()); |
| 50 | + if stdout != expected.output { |
| 79 | 51 | return Err(format!( |
| 80 | | − "output differs\n--- expected\n{expected}--- got\n{stdout}--- stderr\n{stderr}" |
| 52 | + "output differs\n--- expected\n{}--- got\n{stdout}--- stderr\n{stderr}", |
| 53 | + expected.output |
| 81 | 54 | )); |
| 82 | 55 | } |
| 83 | | − let expected_panic = text |
| 84 | | − .lines() |
| 85 | | − .find_map(|l| l.trim_start().strip_prefix("//! panic:")) |
| 86 | | − .map(str::trim); |
| 87 | | − match expected_panic { |
| 56 | + match expected.panic { |
| 88 | 57 | Some(msg) => { |
| 89 | 58 | if result.status.code() != Some(101) || !stderr.contains(&format!("panic: {msg}")) { |
| 90 | 59 | return Err(format!( |
| @@ -103,36 +72,20 @@ fn run(path: &Path, out_dir: &Path) -> Result<Outcome, String> { |
| 103 | 72 | if !stderr.contains("tilrt: live blocks: 0\n") { |
| 104 | 73 | return Err(format!("leaks (or no leak report):\n{stderr}")); |
| 105 | 74 | } |
| 106 | | − Ok(Outcome::Passed) |
| 75 | + Ok(()) |
| 107 | 76 | } |
| 108 | 77 | |
| 109 | 78 | #[test] |
| 110 | 79 | fn run_programs_natively() { |
| 111 | 80 | build_runtime(); |
| 112 | | − let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/run"); |
| 113 | 81 | let out_dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("native"); |
| 114 | | − let mut files: Vec<PathBuf> = std::fs::read_dir(&dir) |
| 115 | | − .unwrap() |
| 116 | | − .map(|e| e.unwrap().path()) |
| 117 | | − .filter(|p| p.extension().is_some_and(|e| e == "til")) |
| 82 | + let failures: Vec<String> = run_programs() |
| 83 | + .iter() |
| 84 | + .filter_map(|f| { |
| 85 | + run(f, &out_dir) |
| 86 | + .err() |
| 87 | + .map(|e| format!("{}: {e}", f.file_stem().unwrap().to_string_lossy())) |
| 88 | + }) |
| 118 | 89 | .collect(); |
| 119 | | − files.sort(); |
| 120 | | − let mut failures = Vec::new(); |
| 121 | | − for f in &files { |
| 122 | | − let name = f.file_stem().unwrap().to_string_lossy().into_owned(); |
| 123 | | − let not_yet = NOT_YET |
| 124 | | − .iter() |
| 125 | | − .find(|(n, _)| *n == name) |
| 126 | | − .map(|(_, why)| *why); |
| 127 | | − match (run(f, &out_dir), not_yet) { |
| 128 | | − (Ok(Outcome::Passed), None) => {} |
| 129 | | − (Ok(Outcome::Passed), Some(_)) => { |
| 130 | | − failures.push(format!("{name}: now compiles; remove it from NOT_YET")); |
| 131 | | − } |
| 132 | | − (Ok(Outcome::Rejected(why)), Some(reason)) if why.contains(reason) => {} |
| 133 | | − (Ok(Outcome::Rejected(why)), _) => failures.push(format!("{name}: rejected: {why}")), |
| 134 | | − (Err(e), _) => failures.push(format!("{name}: {e}")), |
| 135 | | − } |
| 136 | | − } |
| 137 | 90 | assert!(failures.is_empty(), "{}", failures.join("\n\n")); |
| 138 | 91 | } |