Running parametrized JSON tests

This commit is contained in:
Tomusdrw 2016-01-14 18:29:18 +01:00
parent d3a71d3058
commit 4c1b8ddd8f
2 changed files with 39 additions and 7 deletions

View File

@ -1,11 +1,31 @@
//! Evm factory.
use std::fmt;
use evm::Evm;
#[derive(Clone)]
pub enum VMType {
Jit,
Interpreter
}
impl fmt::Display for VMType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
VMType::Jit => "JIT",
VMType::Interpreter => "INT"
})
}
}
impl VMType {
#[cfg(feature="jit")]
pub fn all() -> Vec<VMType> {
vec![VMType::Jit, VMType::Interpreter]
}
#[cfg(not(feature="jit"))]
pub fn all() -> Vec<VMType> {
vec![VMType::Interpreter]
}
}
/// Evm factory. Creates appropriate Evm.
pub struct Factory {

View File

@ -156,6 +156,15 @@ impl<'a> Ext for TestExt<'a> {
}
fn do_json_test(json_data: &[u8]) -> Vec<String> {
let vms = VMType::all();
vms
.iter()
.flat_map(|vm| do_json_test_for(vm, json_data))
.collect()
}
fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new();
for (name, test) in json.as_object().unwrap() {
@ -165,7 +174,10 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
// ::std::io::stdout().flush();
let mut fail = false;
//let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true };
let mut fail_unless = |cond: bool, s: &str | if !cond && !fail { failed.push(name.to_string() + ": "+ s); fail = true };
let mut fail_unless = |cond: bool, s: &str | if !cond && !fail {
failed.push(format!("[{}] {}: {}", vm, name.to_string(), s));
fail = true
};
// test env
let mut state = State::new_temp();
@ -197,7 +209,7 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
info.timestamp = u256_from_json(&env["currentTimestamp"]).low_u64();
});
let engine = TestEngine::new(0, VMType::Jit);
let engine = TestEngine::new(0, vm.clone());
// params
let mut params = ActionParams::new();
@ -222,7 +234,7 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
let (res, callcreates) = {
let ex = Externalities::new(&mut state, &info, &engine, 0, &params, &mut substate, OutputPolicy::Return(BytesRef::Flexible(&mut output)));
let mut test_ext = TestExt::new(ex);
let evm = Factory::default().create();
let evm = engine.vm_factory().create();
let res = evm.exec(&params, &mut test_ext);
(res, test_ext.callcreates)
};