From 4372a1cd55cc68d502789361d9ad3ae9bce57a1a Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 17:00:49 +0100 Subject: [PATCH 1/8] removed submodule --- .gitmodules | 3 --- tests | 1 - 2 files changed, 4 deletions(-) delete mode 100644 .gitmodules delete mode 160000 tests diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 2571e36c6..000000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "tests"] - path = tests - url = https://github.com/ethereum/tests diff --git a/tests b/tests deleted file mode 160000 index 2e4987ad2..000000000 --- a/tests +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2e4987ad2a973e2cf85ef742a8b9bd094363cd18 From 9c85e4d305a569d1f7a23b5878dabb5b3ba2fad2 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 17:52:06 +0100 Subject: [PATCH 2/8] commented out tests --- src/triehash.rs | 86 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/src/triehash.rs b/src/triehash.rs index 778dc7f17..ab0c75683 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -278,7 +278,7 @@ fn test_hex_prefix_encode() { #[cfg(test)] mod tests { use std::str::FromStr; - use std::collections::BTreeMap; + use std::collections::HashMap; use rustc_serialize::hex::FromHex; use rustc_serialize::json::Json; use bytes::*; @@ -379,18 +379,80 @@ mod tests { assert_eq!(trie_root(v), H256::from_str("9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100").unwrap()); } - #[test] - fn test_triehash_json_trietest_json() { - let data = include_bytes!("../tests/TrieTests/trietest.json"); + //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(); + //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); + //} - for (key, value) in obj.iter() { - println!("running test: {}", key); - } - assert!(false); - } } From 2cbbc872ff14f7462f0f06ff01374587b1773a9c Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 21:49:57 +0100 Subject: [PATCH 3/8] 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)); + }); + } } + From 51bd813734038726cfa04cb888ee91b070400b25 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 21:51:20 +0100 Subject: [PATCH 4/8] updated gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index eabd0a44e..959045cf9 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ Cargo.lock # Generated by Cargo /target/ +/json-tests/target/ # Vim *.swp From b9181b42bc7dfb8673751e3eb8e84a2fa3ce2156 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 22:49:47 +0100 Subject: [PATCH 5/8] *.json test files --- json-tests/json/trie/branching.json | 229 +++++++++++++++++++++++++ json-tests/json/trie/dogs.json | 21 +++ json-tests/json/trie/empty.json | 4 + json-tests/json/trie/empty_values.json | 44 +++++ json-tests/json/trie/foo.json | 16 ++ json-tests/json/trie/jeff.json | 58 +++++++ json-tests/src/trie.rs | 1 + src/triehash.rs | 34 ---- 8 files changed, 373 insertions(+), 34 deletions(-) create mode 100644 json-tests/json/trie/branching.json create mode 100644 json-tests/json/trie/dogs.json create mode 100644 json-tests/json/trie/empty.json create mode 100644 json-tests/json/trie/empty_values.json create mode 100644 json-tests/json/trie/foo.json create mode 100644 json-tests/json/trie/jeff.json diff --git a/json-tests/json/trie/branching.json b/json-tests/json/trie/branching.json new file mode 100644 index 000000000..7bbb13f03 --- /dev/null +++ b/json-tests/json/trie/branching.json @@ -0,0 +1,229 @@ +{ + "input": [ + { + "operation": "insert", + "key": "0x04110d816c380812a427968ece99b1c963dfbce6", + "value": "something" + }, + { + "operation": "insert", + "key": "0x095e7baea6a6c7c4c2dfeb977efac326af552d87", + "value": "something" + }, + { + "operation": "insert", + "key": "0x0a517d755cebbf66312b30fff713666a9cb917e0", + "value": "something" + }, + { + "operation": "insert", + "key": "0x24dd378f51adc67a50e339e8031fe9bd4aafab36", + "value": "something" + }, + { + "operation": "insert", + "key": "0x293f982d000532a7861ab122bdc4bbfd26bf9030", + "value": "something" + }, + { + "operation": "insert", + "key": "0x2cf5732f017b0cf1b1f13a1478e10239716bf6b5", + "value": "something" + }, + { + "operation": "insert", + "key": "0x31c640b92c21a1f1465c91070b4b3b4d6854195f", + "value": "something" + }, + { + "operation": "insert", + "key": "0x37f998764813b136ddf5a754f34063fd03065e36", + "value": "something" + }, + { + "operation": "insert", + "key": "0x37fa399a749c121f8a15ce77e3d9f9bec8020d7a", + "value": "something" + }, + { + "operation": "insert", + "key": "0x4f36659fa632310b6ec438dea4085b522a2dd077", + "value": "something" + }, + { + "operation": "insert", + "key": "0x62c01474f089b07dae603491675dc5b5748f7049", + "value": "something" + }, + { + "operation": "insert", + "key": "0x729af7294be595a0efd7d891c9e51f89c07950c7", + "value": "something" + }, + { + "operation": "insert", + "key": "0x83e3e5a16d3b696a0314b30b2534804dd5e11197", + "value": "something" + }, + { + "operation": "insert", + "key": "0x8703df2417e0d7c59d063caa9583cb10a4d20532", + "value": "something" + }, + { + "operation": "insert", + "key": "0x8dffcd74e5b5923512916c6a64b502689cfa65e1", + "value": "something" + }, + { + "operation": "insert", + "key": "0x95a4d7cccb5204733874fa87285a176fe1e9e240", + "value": "something" + }, + { + "operation": "insert", + "key": "0x99b2fcba8120bedd048fe79f5262a6690ed38c39", + "value": "something" + }, + { + "operation": "insert", + "key": "0xa4202b8b8afd5354e3e40a219bdc17f6001bf2cf", + "value": "something" + }, + { + "operation": "insert", + "key": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", + "value": "something" + }, + { + "operation": "insert", + "key": "0xa9647f4a0a14042d91dc33c0328030a7157c93ae", + "value": "something" + }, + { + "operation": "insert", + "key": "0xaa6cffe5185732689c18f37a7f86170cb7304c2a", + "value": "something" + }, + { + "operation": "insert", + "key": "0xaae4a2e3c51c04606dcb3723456e58f3ed214f45", + "value": "something" + }, + { + "operation": "insert", + "key": "0xc37a43e940dfb5baf581a0b82b351d48305fc885", + "value": "something" + }, + { + "operation": "insert", + "key": "0xd2571607e241ecf590ed94b12d87c94babe36db6", + "value": "something" + }, + { + "operation": "insert", + "key": "0xf735071cbee190d76b704ce68384fc21e389fbe7", + "value": "something" + }, + { + "operation": "remove", + "key": "0x04110d816c380812a427968ece99b1c963dfbce6" + }, + { + "operation": "remove", + "key": "0x095e7baea6a6c7c4c2dfeb977efac326af552d87" + }, + { + "operation": "remove", + "key": "0x0a517d755cebbf66312b30fff713666a9cb917e0" + }, + { + "operation": "remove", + "key": "0x24dd378f51adc67a50e339e8031fe9bd4aafab36" + }, + { + "operation": "remove", + "key": "0x293f982d000532a7861ab122bdc4bbfd26bf9030" + }, + { + "operation": "remove", + "key": "0x2cf5732f017b0cf1b1f13a1478e10239716bf6b5" + }, + { + "operation": "remove", + "key": "0x31c640b92c21a1f1465c91070b4b3b4d6854195f" + }, + { + "operation": "remove", + "key": "0x37f998764813b136ddf5a754f34063fd03065e36" + }, + { + "operation": "remove", + "key": "0x37fa399a749c121f8a15ce77e3d9f9bec8020d7a" + }, + { + "operation": "remove", + "key": "0x4f36659fa632310b6ec438dea4085b522a2dd077" + }, + { + "operation": "remove", + "key": "0x62c01474f089b07dae603491675dc5b5748f7049" + }, + { + "operation": "remove", + "key": "0x729af7294be595a0efd7d891c9e51f89c07950c7" + }, + { + "operation": "remove", + "key": "0x83e3e5a16d3b696a0314b30b2534804dd5e11197" + }, + { + "operation": "remove", + "key": "0x8703df2417e0d7c59d063caa9583cb10a4d20532" + }, + { + "operation": "remove", + "key": "0x8dffcd74e5b5923512916c6a64b502689cfa65e1" + }, + { + "operation": "remove", + "key": "0x95a4d7cccb5204733874fa87285a176fe1e9e240" + }, + { + "operation": "remove", + "key": "0x99b2fcba8120bedd048fe79f5262a6690ed38c39" + }, + { + "operation": "remove", + "key": "0xa4202b8b8afd5354e3e40a219bdc17f6001bf2cf" + }, + { + "operation": "remove", + "key": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" + }, + { + "operation": "remove", + "key": "0xa9647f4a0a14042d91dc33c0328030a7157c93ae" + }, + { + "operation": "remove", + "key": "0xaa6cffe5185732689c18f37a7f86170cb7304c2a" + }, + { + "operation": "remove", + "key": "0xaae4a2e3c51c04606dcb3723456e58f3ed214f45" + }, + { + "operation": "remove", + "key": "0xc37a43e940dfb5baf581a0b82b351d48305fc885" + }, + { + "operation": "remove", + "key": "0xd2571607e241ecf590ed94b12d87c94babe36db6" + }, + { + "operation": "remove", + "key": "0xf735071cbee190d76b704ce68384fc21e389fbe7" + }], + "output": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" +} \ No newline at end of file diff --git a/json-tests/json/trie/dogs.json b/json-tests/json/trie/dogs.json new file mode 100644 index 000000000..a6ff7c891 --- /dev/null +++ b/json-tests/json/trie/dogs.json @@ -0,0 +1,21 @@ +{ + "input": + [ + { + "operation": "insert", + "key": "doe", + "value": "reindeer" + }, + { + "operation": "insert", + "key": "dogglesworth", + "value": "cat" + }, + { + "operation": "insert", + "key": "dog", + "value": "puppy" + } + ], + "output": "0x8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3" +} diff --git a/json-tests/json/trie/empty.json b/json-tests/json/trie/empty.json new file mode 100644 index 000000000..ca146df54 --- /dev/null +++ b/json-tests/json/trie/empty.json @@ -0,0 +1,4 @@ +{ + "input": [], + "output": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" +} diff --git a/json-tests/json/trie/empty_values.json b/json-tests/json/trie/empty_values.json new file mode 100644 index 000000000..ac367f913 --- /dev/null +++ b/json-tests/json/trie/empty_values.json @@ -0,0 +1,44 @@ +{ + "input": + [ + { + "operation": "insert", + "key": "do", + "value": "verb" + }, + { + "operation": "insert", + "key": "ether", + "value": "wookiedoo" + }, + { + "operation": "insert", + "key": "horse", + "value": "stallion" + }, + { + "operation": "insert", + "key": "shaman", + "value": "horse" + }, + { + "operation": "insert", + "key": "doge", + "value": "coin" + }, + { + "operation": "remove", + "key": "ether" + }, + { + "operation": "insert", + "key": "dog", + "value": "puppy" + }, + { + "operation": "remove", + "key": "shaman" + } + ], + "output": "0x5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84" +} diff --git a/json-tests/json/trie/foo.json b/json-tests/json/trie/foo.json new file mode 100644 index 000000000..4b8c0a87f --- /dev/null +++ b/json-tests/json/trie/foo.json @@ -0,0 +1,16 @@ +{ + "input": + [ + { + "operation": "insert", + "key": "foo", + "value": "bar" + }, + { + "operation": "insert", + "key": "food", + "value": "bass" + } + ], + "output": "0x17beaa1648bafa633cda809c90c04af50fc8aed3cb40d16efbddee6fdf63c4c3" +} diff --git a/json-tests/json/trie/jeff.json b/json-tests/json/trie/jeff.json new file mode 100644 index 000000000..1f3093fad --- /dev/null +++ b/json-tests/json/trie/jeff.json @@ -0,0 +1,58 @@ +{ + "input": [ + { + "operation": "insert", + "key": "0x0000000000000000000000000000000000000000000000000000000000000045", + "value": "0x22b224a1420a802ab51d326e29fa98e34c4f24ea" + }, + { + "operation": "insert", + "key": "0x0000000000000000000000000000000000000000000000000000000000000046", + "value": "0x67706c2076330000000000000000000000000000000000000000000000000000" + }, + { + "operation": "insert", + "key": "0x0000000000000000000000000000000000000000000000000000001234567890", + "value": "0x697c7b8c961b56f675d570498424ac8de1a918f6" + }, + { + "operation": "insert", + "key": "0x000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6", + "value": "0x1234567890" + }, + { + "operation": "insert", + "key": "0x0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2", + "value": "0x4655474156000000000000000000000000000000000000000000000000000000" + }, + { + "operation": "insert", + "key": "0x000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1", + "value": "0x4e616d6552656700000000000000000000000000000000000000000000000000" + }, + { + "operation": "insert", + "key": "0x4655474156000000000000000000000000000000000000000000000000000000", + "value": "0x7ef9e639e2733cb34e4dfc576d4b23f72db776b2" + }, + { + "operation": "insert", + "key": "0x4e616d6552656700000000000000000000000000000000000000000000000000", + "value": "0xec4f34c97e43fbb2816cfd95e388353c7181dab1" + }, + { + "operation": "remove", + "key": "0x0000000000000000000000000000000000000000000000000000001234567890" + }, + { + "operation": "insert", + "key": "0x000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6", + "value": "0x6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000" + }, + { + "operation": "insert", + "key": "0x6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000", + "value": "0x697c7b8c961b56f675d570498424ac8de1a918f6" + }], + "output": "0x9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100" +} \ No newline at end of file diff --git a/json-tests/src/trie.rs b/json-tests/src/trie.rs index f065085fe..f6d4eaae1 100644 --- a/json-tests/src/trie.rs +++ b/json-tests/src/trie.rs @@ -72,6 +72,7 @@ impl JsonTest for TrieTest { .map(|i| i.into()) .collect() } + fn output(&self) -> Self::Output { hex_or_string(self.loader.output().as_string().unwrap()) } diff --git a/src/triehash.rs b/src/triehash.rs index 0467ea284..a8b1670d3 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -285,40 +285,6 @@ mod tests { use hash::*; use triehash::*; - #[test] - fn empty_trie_root() { - assert_eq!(trie_root(vec![]), H256::from_str("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap()); - } - - #[test] - fn single_trie_item() { - let v = vec![(From::from("A"), From::from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))]; - assert_eq!(trie_root(v), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); - } - - #[test] - fn foo_trie_item() { - - let v = vec![ - (From::from("foo"), From::from("bar")), - (From::from("food"), From::from("bass")) - ]; - - assert_eq!(trie_root(v), H256::from_str("17beaa1648bafa633cda809c90c04af50fc8aed3cb40d16efbddee6fdf63c4c3").unwrap()); - } - - #[test] - fn dogs_trie_item() { - - let v = vec![ - (From::from("doe"), From::from("reindeer")), - (From::from("dog"), From::from("puppy")), - (From::from("dogglesworth"), From::from("cat")), - ]; - - assert_eq!(trie_root(v), H256::from_str("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3").unwrap()); - } - #[test] fn puppy_trie_items() { From 166b84d489ffe2ddfe2d282be9c096db4c8553e8 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 22:56:38 +0100 Subject: [PATCH 6/8] removed duplicate tests --- json-tests/src/lib.rs | 6 +---- src/triehash.rs | 62 ++++--------------------------------------- 2 files changed, 6 insertions(+), 62 deletions(-) diff --git a/json-tests/src/lib.rs b/json-tests/src/lib.rs index 64466084a..8a800e8f9 100644 --- a/json-tests/src/lib.rs +++ b/json-tests/src/lib.rs @@ -54,7 +54,7 @@ pub fn execute_test_from_file(path: &Path, f: &mut F) where T: JsonTest, F 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) { +pub fn execute_tests_from_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); @@ -62,7 +62,3 @@ pub fn execute_tests_in_directory(pattern: &str, f: &mut F) where T: JsonT } } -#[test] -fn it_works() { -} - diff --git a/src/triehash.rs b/src/triehash.rs index a8b1670d3..6cfa2103d 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -277,29 +277,13 @@ fn test_hex_prefix_encode() { #[cfg(test)] mod tests { - use std::str::FromStr; - use std::collections::HashMap; - use rustc_serialize::hex::FromHex; - use rustc_serialize::json::Json; - use bytes::*; + extern crate json_tests; + use self::json_tests::*; use hash::*; use triehash::*; #[test] - fn puppy_trie_items() { - - let v = vec![ - (From::from("do"), From::from("verb")), - (From::from("dog"), From::from("puppy")), - (From::from("doge"), From::from("coin")), - (From::from("horse"), From::from("stallion")), - ]; - - assert_eq!(trie_root(v), H256::from_str("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84").unwrap()); - } - - #[test] - fn out_of_order() { + fn test_trie_out_of_order() { assert!(trie_root(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x81u8, 0x23], vec![0x81u8, 0x23]), @@ -313,44 +297,8 @@ mod tests { } #[test] - fn test_trie_root() { - let v = vec![ - - ("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(), - "22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()), - - ("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(), - "67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()), - - ("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(), - "6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()), - - ("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(), - "4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()), - - ("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(), - "4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()), - - ("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(), - "7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()), - - ("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(), - "ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()), - - ("6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap(), - "697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap()) - - ]; - - assert_eq!(trie_root(v), H256::from_str("9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100").unwrap()); - } - - 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 | { + fn test_trie_json() { + execute_tests_from_directory::("json-tests/json/trie/*.json", &mut | file, input, output | { println!("file: {}, output: {:?}", file, output); assert_eq!(trie_root(input), H256::from_slice(&output)); }); From 3f31d617505ffc75c66a46f9d2319a4e0ba70d86 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 2 Dec 2015 23:29:22 +0100 Subject: [PATCH 7/8] Update README.md --- json-tests/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-tests/README.md b/json-tests/README.md index 170cd588d..9e6915ca3 100644 --- a/json-tests/README.md +++ b/json-tests/README.md @@ -3,7 +3,7 @@ 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. +### Test files should always contain a single test with input and output. ```json { From 46d841f81f7eda103eb9fc913065b62720efb000 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 23:35:34 +0100 Subject: [PATCH 8/8] removed redundant lifetime --- src/trie.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trie.rs b/src/trie.rs index 1dde1993c..3c8527424 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -303,7 +303,7 @@ impl TrieDB { } } - fn get_raw_or_lookup<'a, 'b>(&'a self, node: &'b [u8]) -> &'b [u8] where 'a: 'b { + fn get_raw_or_lookup<'a>(&'a self, node: &'a [u8]) -> &'a [u8] { // check if its sha3 + len let r = Rlp::new(node); match r.is_data() && r.size() == 32 {