trie test failing

This commit is contained in:
debris 2015-12-03 00:29:31 +01:00
parent 3143e614a8
commit 8b8a2e39e2
3 changed files with 33 additions and 16 deletions

View File

@ -4,7 +4,7 @@ use rustc_serialize::*;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use super::{JsonTest, JsonLoader}; use super::{JsonTest, JsonLoader};
pub enum OperationType { enum OperationType {
Insert, Insert,
Remove Remove
} }
@ -26,10 +26,9 @@ struct RawOperation {
value: Option<String> value: Option<String>
} }
pub struct Operation { pub enum Operation {
pub operation: OperationType, Insert(Vec<u8>, Vec<u8>),
pub key: Vec<u8>, Remove(Vec<u8>)
pub value: Option<Vec<u8>>
} }
fn hex_or_string(s: &str) -> Vec<u8> { fn hex_or_string(s: &str) -> Vec<u8> {
@ -41,12 +40,9 @@ fn hex_or_string(s: &str) -> Vec<u8> {
impl Into<Operation> for RawOperation { impl Into<Operation> for RawOperation {
fn into(self) -> Operation { fn into(self) -> Operation {
Operation { match self.operation {
operation: self.operation, OperationType::Insert => Operation::Insert(hex_or_string(&self.key), hex_or_string(&self.value.unwrap())),
key: hex_or_string(&self.key), OperationType::Remove => Operation::Remove(hex_or_string(&self.key))
value: self.value.map(|v| {
hex_or_string(&v)
})
} }
} }
} }
@ -96,9 +92,9 @@ impl JsonTest for TriehashTest {
self.trietest.input() self.trietest.input()
.into_iter() .into_iter()
.fold(HashMap::new(), | mut map, o | { .fold(HashMap::new(), | mut map, o | {
match o.operation { match o {
OperationType::Insert => map.insert(o.key, o.value.unwrap()), Operation::Insert(k, v) => map.insert(k, v),
OperationType::Remove => map.remove(&o.key) Operation::Remove(k) => map.remove(&k)
}; };
map map
}) })

View File

@ -721,8 +721,11 @@ impl Trie for TrieDB {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate json_tests;
use self::json_tests::*;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use triehash::*; use triehash::*;
use hash::*;
use super::*; use super::*;
use nibbleslice::*; use nibbleslice::*;
use rlp; use rlp;
@ -1061,4 +1064,22 @@ mod tests {
test_all(v); test_all(v);
} }
#[test]
fn test_trie_json() {
println!("Json trie test: ");
execute_tests_from_directory::<trie::TrieTest, _>("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));
});
}
} }

View File

@ -283,7 +283,7 @@ mod tests {
use triehash::*; use triehash::*;
#[test] #[test]
fn test_trie_out_of_order() { fn test_triehash_out_of_order() {
assert!(trie_root(vec![ assert!(trie_root(vec![
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]), (vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
(vec![0x81u8, 0x23], vec![0x81u8, 0x23]), (vec![0x81u8, 0x23], vec![0x81u8, 0x23]),
@ -297,7 +297,7 @@ mod tests {
} }
#[test] #[test]
fn test_trie_json() { fn test_triehash_json() {
execute_tests_from_directory::<trie::TriehashTest, _>("json-tests/json/trie/*.json", &mut | file, input, output | { execute_tests_from_directory::<trie::TriehashTest, _>("json-tests/json/trie/*.json", &mut | file, input, output | {
println!("file: {}, output: {:?}", file, output); println!("file: {}, output: {:?}", file, output);
assert_eq!(trie_root(input), H256::from_slice(&output)); assert_eq!(trie_root(input), H256::from_slice(&output));