From 2cbbc872ff14f7462f0f06ff01374587b1773a9c Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 21:49:57 +0100 Subject: [PATCH] json-tests init --- Cargo.toml | 3 + json-tests/Cargo.toml | 8 +++ json-tests/README.md | 15 +++++ json-tests/json/trie/README.md | 35 ++++++++++ json-tests/json/trie/basic.json | 11 ++++ json-tests/src/lib.rs | 68 +++++++++++++++++++ json-tests/src/trie.rs | 113 ++++++++++++++++++++++++++++++++ src/triehash.rs | 85 +++--------------------- 8 files changed, 263 insertions(+), 75 deletions(-) create mode 100644 json-tests/Cargo.toml create mode 100644 json-tests/README.md create mode 100644 json-tests/json/trie/README.md create mode 100644 json-tests/json/trie/basic.json create mode 100644 json-tests/src/lib.rs create mode 100644 json-tests/src/trie.rs diff --git a/Cargo.toml b/Cargo.toml index de4dcd41b..41ca44580 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,6 @@ num = "0.1" lazy_static = "0.1.*" secp256k1 = "0.5.1" rust-crypto = "0.2.34" + +[dev-dependencies] +json-tests = { path = "json-tests" } diff --git a/json-tests/Cargo.toml b/json-tests/Cargo.toml new file mode 100644 index 000000000..3185e2e59 --- /dev/null +++ b/json-tests/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "json-tests" +version = "0.1.0" +authors = ["debris "] + +[dependencies] +rustc-serialize = "0.3" +glob = "*" diff --git a/json-tests/README.md b/json-tests/README.md new file mode 100644 index 000000000..170cd588d --- /dev/null +++ b/json-tests/README.md @@ -0,0 +1,15 @@ +# How to write json test file? + +Cause it's very hard to write generic json test files, each subdirectory should follow its own +convention. BUT all json files `within` same directory should be consistent. + +### Test file should always contain a single file with input and output. + +```json +{ + input: ..., + output: ... +} +``` + +As a reference, please use trietests. diff --git a/json-tests/json/trie/README.md b/json-tests/json/trie/README.md new file mode 100644 index 000000000..6d7f479a3 --- /dev/null +++ b/json-tests/json/trie/README.md @@ -0,0 +1,35 @@ +# Trie tests guideline + +Trie test input is an array of operations. Each operation must have 2 fields: + +- `operation` - string, either `insert` or `remove` +- `key` - string, or hex value prefixed with `0x` + +And optional field: + +- `value`- which is used by `insert` operation + +### Example + +```json +{ + "input": + [ + { + "operation": "insert", + "key": "world", + "value": "hello" + }, + { + "operation": "insert", + "key": "0x1234", + "value": "ooooops" + }, + { + "operation": "remove", + "key": "0x1234" + } + ], + "output": "0x5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84" +} +``` diff --git a/json-tests/json/trie/basic.json b/json-tests/json/trie/basic.json new file mode 100644 index 000000000..f737ef337 --- /dev/null +++ b/json-tests/json/trie/basic.json @@ -0,0 +1,11 @@ +{ + "input": + [ + { + "operation": "insert", + "key": "A", + "value": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } + ], + "output": "0xd23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab" +} diff --git a/json-tests/src/lib.rs b/json-tests/src/lib.rs new file mode 100644 index 000000000..64466084a --- /dev/null +++ b/json-tests/src/lib.rs @@ -0,0 +1,68 @@ +extern crate rustc_serialize; +extern crate glob; + +use std::str::from_utf8; +use std::path::*; +use std::io::prelude::*; +use std::fs::File; +use glob::glob; +use rustc_serialize::*; + +pub mod trie; + +pub trait JsonTest: Sized { + type Input; + type Output; + + fn new(data: &[u8]) -> Self; + fn input(&self) -> Self::Input; + fn output(&self) -> Self::Output; +} + +pub struct JsonLoader { + json: json::Json +} + +impl JsonTest for JsonLoader { + type Input = json::Json; + type Output = json::Json; + + fn new(data: &[u8]) -> Self { + JsonLoader { + json: json::Json::from_str(from_utf8(data).unwrap()).unwrap() + } + } + fn input(&self) -> Self::Input { + self.json.as_object().unwrap()["input"].clone() + } + + fn output(&self) -> Self::Output { + self.json.as_object().unwrap()["output"].clone() + } +} + +pub fn execute_test(data: &[u8], f: &mut F) where T: JsonTest, F: FnMut(T::Input, T::Output) { + let test = T::new(data); + f(test.input(), test.output()) +} + +pub fn execute_test_from_file(path: &Path, f: &mut F) where T: JsonTest, F: FnMut(T::Input, T::Output) { + let mut file = File::open(path).unwrap(); + let mut buffer = vec![]; + let _ = file.read_to_end(&mut buffer); + let test = T::new(&buffer); + f(test.input(), test.output()) +} + +pub fn execute_tests_in_directory(pattern: &str, f: &mut F) where T: JsonTest, F: FnMut(String, T::Input, T::Output) { + for path in glob(pattern).unwrap().filter_map(Result::ok) { + execute_test_from_file::(&path, &mut | input, output | { + f(path.to_str().unwrap().to_string(), input, output); + }); + } +} + +#[test] +fn it_works() { +} + diff --git a/json-tests/src/trie.rs b/json-tests/src/trie.rs new file mode 100644 index 000000000..f065085fe --- /dev/null +++ b/json-tests/src/trie.rs @@ -0,0 +1,113 @@ +//! json trie tests +use std::collections::HashMap; +use rustc_serialize::*; +use rustc_serialize::hex::FromHex; +use super::{JsonTest, JsonLoader}; + +pub enum OperationType { + Insert, + Remove +} + +impl Decodable for OperationType { + fn decode(d: &mut D) -> Result where D: Decoder { + match try!(String::decode(d)).as_ref() { + "insert" => Ok(OperationType::Insert), + "remove" => Ok(OperationType::Remove), + other => panic!("invalid operation type: {}", other) + } + } +} + +#[derive(RustcDecodable)] +struct RawOperation { + operation: OperationType, + key: String, + value: Option +} + +pub struct Operation { + pub operation: OperationType, + pub key: Vec, + pub value: Option> +} + +fn hex_or_string(s: &str) -> Vec { + match s.starts_with("0x") { + true => s[2..].from_hex().unwrap(), + false => From::from(s) + } +} + +impl Into for RawOperation { + fn into(self) -> Operation { + Operation { + operation: self.operation, + key: hex_or_string(&self.key), + value: self.value.map(|v| { + hex_or_string(&v) + }) + } + } +} + +pub struct TrieTest { + loader: JsonLoader +} + +impl JsonTest for TrieTest { + type Input = Vec; + type Output = Vec; + + fn new(data: &[u8]) -> Self { + TrieTest { + loader: JsonLoader::new(data) + } + } + + fn input(&self) -> Self::Input { + let mut decoder = json::Decoder::new(self.loader.input()); + let raw: Vec = Decodable::decode(&mut decoder).unwrap(); + raw.into_iter() + .map(|i| i.into()) + .collect() + } + fn output(&self) -> Self::Output { + hex_or_string(self.loader.output().as_string().unwrap()) + } +} + +pub struct TriehashTest { + trietest: TrieTest +} + +impl JsonTest for TriehashTest { + type Input = Vec<(Vec, Vec)>; + type Output = Vec; + + fn new(data: &[u8]) -> Self { + TriehashTest { + trietest: TrieTest::new(data) + } + } + + fn input(&self) -> Self::Input { + self.trietest.input() + .into_iter() + .fold(HashMap::new(), | mut map, o | { + match o.operation { + OperationType::Insert => map.insert(o.key, o.value.unwrap()), + OperationType::Remove => map.remove(&o.key) + }; + map + }) + .into_iter() + .map(|p| { p }) + .collect() + } + + fn output(&self) -> Self::Output { + self.trietest.output() + } +} + diff --git a/src/triehash.rs b/src/triehash.rs index ab0c75683..0467ea284 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -379,80 +379,15 @@ mod tests { assert_eq!(trie_root(v), H256::from_str("9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100").unwrap()); } - //fn hex_or_string(s: &str) -> Vec { - //match s.starts_with("0x") { - //true => s[2..].from_hex().unwrap(), - //false => From::from(s) - //} - //} - - //pub fn yield_json_trietest(data: &[u8], name: &str, insert: &mut I, remove: &mut R) -> H256 where I: FnMut(Vec, Vec), R: FnMut(Vec) { - ////let data = include_bytes!("../tests/TrieTests/trietest.json"); - - //let s = String::from_bytes(data).unwrap(); - //let json = Json::from_str(&s).unwrap(); - //let obj = json.as_object().unwrap(); - //println!("here"); - - //let value = &obj[name]; - - //let i = &value["in"]; - //let o = &value["root"]; - - //let root_str = o.as_string().unwrap(); - - //println!("here2"); - //for i in i.as_array().unwrap().iter() { - //let key = hex_or_string(i[0].as_string().unwrap()); - - //match i[1].is_null() { - //true => remove(key), - //false => insert(key, hex_or_string(i[1].as_string().unwrap())) - //}; - //} - - //H256::from_str(&root_str[2..]).unwrap() - //} - - ////fn load_json_trietest(data: &[u8], name: &str) -> (Vec<(Vec, Vec)>, H256) { - ////use std::cell::RefCell; - //let map = RefCell::new(HashMap::new()); - //let root = yield_json_trietest(data, name, &mut | key, value | { - //map.borrow_mut().insert(key, value); - //}, &mut | key | { - //map.borrow_mut().remove(&key); - //}); - - //let res = map.into_inner() - //.into_iter() - //.map(|p| p) - //.collect(); - //(res, root) - //} - - //#[test] - //fn triehash_json_empty_values() { - //let (input, root) = load_json_trietest(include_bytes!("../tests/TrieTests/trietest.json"), "emptyValues"); - //assert_eq!(trie_root(input), root); - //} - - //#[test] - //fn triehash_json_branching_tests() { - //let (input, root) = load_json_trietest(include_bytes!("../tests/TrieTests/trietest.json"), "branchingTests"); - //assert_eq!(trie_root(input), root); - //} - - //#[test] - //fn triehash_json_jeff_tests() { - //let (input, root) = load_json_trietest(include_bytes!("../tests/TrieTests/trietest.json"), "jeff"); - //assert_eq!(trie_root(input), root); - //} - - ////#[test] - ////fn triehash_json_test1() { - //let (input, root) = load_json_trietest(include_bytes!("../tests/TrieTests/hex_encoded_securetrie_test.json"), "test1"); - //assert_eq!(trie_root(input), root); - //} - + extern crate json_tests; + use self::json_tests::*; + #[test] + fn run_trie_tests() { + execute_tests_in_directory::("json-tests/json/trie/*.json", &mut | file, input, output | { + println!("file: {}, output: {:?}", file, output); + assert_eq!(trie_root(input), H256::from_slice(&output)); + }); + } } +