diff --git a/json-tests/src/trie.rs b/json-tests/src/trie.rs index f6d4eaae1..827bf8c9a 100644 --- a/json-tests/src/trie.rs +++ b/json-tests/src/trie.rs @@ -4,7 +4,7 @@ use rustc_serialize::*; use rustc_serialize::hex::FromHex; use super::{JsonTest, JsonLoader}; -pub enum OperationType { +enum OperationType { Insert, Remove } @@ -26,10 +26,9 @@ struct RawOperation { value: Option } -pub struct Operation { - pub operation: OperationType, - pub key: Vec, - pub value: Option> +pub enum Operation { + Insert(Vec, Vec), + Remove(Vec) } fn hex_or_string(s: &str) -> Vec { @@ -41,12 +40,9 @@ fn hex_or_string(s: &str) -> Vec { 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) - }) + match self.operation { + OperationType::Insert => Operation::Insert(hex_or_string(&self.key), hex_or_string(&self.value.unwrap())), + OperationType::Remove => Operation::Remove(hex_or_string(&self.key)) } } } @@ -96,9 +92,9 @@ impl JsonTest for TriehashTest { 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) + match o { + Operation::Insert(k, v) => map.insert(k, v), + Operation::Remove(k) => map.remove(&k) }; map }) diff --git a/src/trie.rs b/src/trie.rs index 85312a58c..11e87c302 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -721,8 +721,11 @@ impl Trie for TrieDB { #[cfg(test)] mod tests { + extern crate json_tests; + use self::json_tests::*; use rustc_serialize::hex::FromHex; use triehash::*; + use hash::*; use super::*; use nibbleslice::*; use rlp; @@ -1061,4 +1064,22 @@ mod tests { test_all(v); } + + #[test] + fn test_trie_json() { + println!("Json trie test: "); + execute_tests_from_directory::("json-tests/json/trie/*.json", &mut | file, input, output | { + println!("file: {}", file); + + let mut t = TrieDB::new_memory(); + for operation in input.into_iter() { + match operation { + trie::Operation::Insert(key, value) => t.insert(&key, &value), + trie::Operation::Remove(key) => t.remove(&key) + } + } + + assert_eq!(*t.root(), H256::from_slice(&output)); + }); + } } diff --git a/src/triehash.rs b/src/triehash.rs index 6cfa2103d..93d21aacc 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -283,7 +283,7 @@ mod tests { use triehash::*; #[test] - fn test_trie_out_of_order() { + fn test_triehash_out_of_order() { assert!(trie_root(vec![ (vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x81u8, 0x23], vec![0x81u8, 0x23]), @@ -297,7 +297,7 @@ mod tests { } #[test] - fn test_trie_json() { + fn test_triehash_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));