mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-16 07:28:52 +00:00
Update skeleton
This commit is contained in:
136
src/tests.rs
136
src/tests.rs
@@ -220,3 +220,139 @@ pub fn test_opt<P1: AsRef<Path>, P2: AsRef<Path>, O: Optimize<ir::TranslationUni
|
||||
panic!("[test_opt]");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_asmgen(unit: &TranslationUnit, path: &Path) {
|
||||
// Check if the file has .c extension
|
||||
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("c")));
|
||||
|
||||
// Test parse
|
||||
c::Parse::default()
|
||||
.translate(&path)
|
||||
.expect("failed to parse the given program");
|
||||
|
||||
let file_path = path.display().to_string();
|
||||
let bin_path = path.with_extension("irgen").as_path().display().to_string();
|
||||
|
||||
// Compile c file: If fails, test is vacuously success
|
||||
if !Command::new("gcc")
|
||||
.args(&[
|
||||
"-fsanitize=undefined",
|
||||
"-fno-sanitize-recover=all",
|
||||
"-O1",
|
||||
&file_path,
|
||||
"-o",
|
||||
&bin_path,
|
||||
])
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.unwrap()
|
||||
.success()
|
||||
{
|
||||
::std::process::exit(SKIP_TEST);
|
||||
}
|
||||
|
||||
// Execute compiled executable
|
||||
let mut child = Command::new(fs::canonicalize(bin_path.clone()).unwrap())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute the compiled executable");
|
||||
|
||||
Command::new("rm")
|
||||
.arg(bin_path)
|
||||
.status()
|
||||
.expect("failed to remove compiled executable");
|
||||
|
||||
let status = some_or!(
|
||||
child
|
||||
.wait_timeout_ms(500)
|
||||
.expect("failed to obtain exit status from child process"),
|
||||
{
|
||||
println!("timeout occurs");
|
||||
child.kill().unwrap();
|
||||
child.wait().unwrap();
|
||||
::std::process::exit(SKIP_TEST);
|
||||
}
|
||||
);
|
||||
|
||||
if child
|
||||
.stderr
|
||||
.expect("`stderr` of `child` must be `Some`")
|
||||
.bytes()
|
||||
.next()
|
||||
.is_some()
|
||||
{
|
||||
println!("error occurs");
|
||||
::std::process::exit(SKIP_TEST);
|
||||
}
|
||||
|
||||
let gcc_status = some_or_exit!(status.code(), SKIP_TEST);
|
||||
|
||||
let ir = match Irgen::default().translate(unit) {
|
||||
Ok(ir) => ir,
|
||||
Err(irgen_error) => panic!("{}", irgen_error),
|
||||
};
|
||||
|
||||
let asm = Asmgen::default()
|
||||
.translate(&ir)
|
||||
.expect("fail to create riscv assembly code");
|
||||
let asm_path = path.with_extension("S").as_path().display().to_string();
|
||||
let mut buffer = File::create(Path::new(&asm_path)).expect("need to success creating file");
|
||||
write(&asm, &mut buffer).unwrap();
|
||||
|
||||
// Link to an RISC-V executable
|
||||
let bin_path = path.with_extension("asm").as_path().display().to_string();
|
||||
if !Command::new("riscv64-linux-gnu-gcc-10")
|
||||
.args(&["-static", &asm_path, "-o", &bin_path])
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.unwrap()
|
||||
.success()
|
||||
{
|
||||
::std::process::exit(SKIP_TEST);
|
||||
}
|
||||
|
||||
Command::new("rm")
|
||||
.arg(asm_path)
|
||||
.status()
|
||||
.expect("failed to remove assembly code file");
|
||||
|
||||
// Emulate the executable
|
||||
let mut child = Command::new("qemu-riscv64-static")
|
||||
.args(&[&bin_path])
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute the compiled executable");
|
||||
|
||||
Command::new("rm")
|
||||
.arg(bin_path)
|
||||
.status()
|
||||
.expect("failed to remove compiled executable");
|
||||
|
||||
let status = some_or!(
|
||||
child
|
||||
.wait_timeout_ms(500)
|
||||
.expect("failed to obtain exit status from child process"),
|
||||
{
|
||||
println!("timeout occurs");
|
||||
child.kill().unwrap();
|
||||
child.wait().unwrap();
|
||||
::std::process::exit(SKIP_TEST);
|
||||
}
|
||||
);
|
||||
|
||||
if child
|
||||
.stderr
|
||||
.expect("`stderr` of `child` must be `Some`")
|
||||
.bytes()
|
||||
.next()
|
||||
.is_some()
|
||||
{
|
||||
println!("error occurs");
|
||||
::std::process::exit(SKIP_TEST);
|
||||
}
|
||||
|
||||
let kecc_status = some_or_exit!(status.code(), SKIP_TEST);
|
||||
|
||||
println!("gcc: {}, kecc: {}", gcc_status, kecc_status);
|
||||
assert_eq!(gcc_status, kecc_status);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user