Update test

This commit is contained in:
Jeehoon Kang
2020-03-17 22:30:16 +09:00
parent fddd07a769
commit 1b7d25314d
32 changed files with 171 additions and 130 deletions

View File

@@ -1,21 +0,0 @@
use std::path::Path;
use kecc::*;
#[test]
fn ast_printer_test() {
let mut parse = Parse::default();
let dir_path = Path::new("examples/");
let dir = dir_path.read_dir().expect("read_dir call failed");
for entry in dir {
let test_file = ok_or!(entry, continue);
let test_unit = parse.translate(&test_file.path().as_path()).expect(
&format!(
"parse failed {:?}",
test_file.path().into_os_string().to_str().unwrap()
)
.to_owned(),
);
write_c_test(&test_unit);
}
}

View File

@@ -22,6 +22,7 @@ REPLACE_DICT = {
"int16_t": "int",
"int32_t": "int",
"uint": "unsigned int",
"static ": "",
}
CSMITH_DIR = "csmith-2.3.0"
@@ -44,7 +45,12 @@ def generate(tests_dir, bin_file, runtime, file_name):
A developer may customize the options to meet one's needs for testing.
"""
global CSMITH_DIR
options = ["--no-builtins", "--no-safe-math", "--no-unions"]
options = [
"--no-argc", "--no-arrays", "--no-checksum",
"--no-jumps", "--no-longlong", "--no-int8",
"--no-uint8", "--no-safe-math", "--no-pointers",
"--no-structs", "--no-unions", "--no-builtins"
]
args = [bin_file] + options
dst_path = os.path.join(runtime, file_name)

View File

@@ -1,68 +0,0 @@
use kecc::run_ir::*;
use kecc::*;
use std::path::Path;
// TODO: cover all examples in the future
#[test]
fn ir_interpreter_test() {
// Test toy example
assert_eq!(run_example("examples/foo.c"), Ok(Value::Int(-1)));
// Test toy example with negate unary operator
assert_eq!(run_example("examples/negate.c"), Ok(Value::Int(1)));
// Test fibonacci function with for-loop
assert_eq!(run_example("examples/fib3.c"), Ok(Value::Int(34)));
// Test fibonacci function with while-loop
assert_eq!(run_example("examples/fib4.c"), Ok(Value::Int(34)));
// Test fibonacci function with do-while-loop
assert_eq!(run_example("examples/fib5.c"), Ok(Value::Int(34)));
// Test fibonacci function with recursive function call
assert_eq!(run_example("examples/fibonacci.c"), Ok(Value::Int(34)));
// Test example with global variable
assert_eq!(run_example("examples/foo3.c"), Ok(Value::Int(30)));
// Test example with comma expressions
assert_eq!(run_example("examples/comma.c"), Ok(Value::Int(7)));
// Test example with complex function call
assert_eq!(run_example("examples/foo4.c"), Ok(Value::Int(6)));
// Test example with pointer
assert_eq!(run_example("examples/pointer.c"), Ok(Value::Int(3)));
// Test example with sizeof
assert_eq!(run_example("examples/sizeof.c"), Ok(Value::Int(4)));
// Test example with alignof
assert_eq!(run_example("examples/alignof.c"), Ok(Value::Int(4)));
// Test example with simple for statement
assert_eq!(run_example("examples/simple_for.c"), Ok(Value::Int(55)));
// Test example with conditional expression
assert_eq!(run_example("examples/cond.c"), Ok(Value::Int(5)));
// Test example with switch statement
assert_eq!(run_example("examples/switch.c"), Ok(Value::Int(2)));
}
fn run_example(example_path: &str) -> Result<Value, InterpreterError> {
let example_path = Path::new(example_path);
let unit = Parse::default()
.translate(&example_path)
.expect("parse failed");
let ir = Irgen::default()
.translate(&unit)
.expect("failed to generate ir");
// TODO: consider command line arguments in the future
// TODO: randomly generate argument values
let args = Vec::new();
run_ir(&ir, args)
}

47
tests/test_examples.rs Normal file
View File

@@ -0,0 +1,47 @@
use std::path::Path;
use lang_c::ast::*;
use kecc::run_ir::*;
use kecc::*;
fn test_dir<F>(path: &Path, f: F)
where
F: Fn(&TranslationUnit),
{
let mut parse = Parse::default();
let dir = path.read_dir().expect("read_dir call failed");
for entry in dir {
let entry = ok_or!(entry, continue);
let path = entry.path();
if path.is_dir() {
continue;
}
println!("[testing {:?}]", path);
let test_unit = parse.translate(&path.as_path()).expect(
&format!("parse failed {:?}", path.into_os_string().to_str().unwrap()).to_owned(),
);
f(&test_unit);
}
}
#[test]
fn test_examples_write_c() {
test_dir(Path::new("examples/"), write_c_test);
test_dir(Path::new("examples/hw1"), write_c_test);
}
#[test]
fn test_examples_irgen() {
test_dir(Path::new("examples/"), |test_unit| {
let ir = Irgen::default()
.translate(test_unit)
.expect("failed to generate ir");
// TODO: insert randomly generated command line arguments
let args = Vec::new();
assert_eq!(run_ir(&ir, args), Ok(Value::Int(1)));
});
}