From 84eb30a133653a9519be13b34e2e4e89234d984b Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 3 Dec 2015 06:06:42 +0100 Subject: [PATCH] empty lists tests --- json-tests/json/rlp/catdog.json | 2 +- json-tests/json/rlp/empty_lists.json | 38 ++++++++++++++++ src/rlp.rs | 24 ++++++++++ src/trie.rs | 65 ---------------------------- 4 files changed, 63 insertions(+), 66 deletions(-) create mode 100644 json-tests/json/rlp/empty_lists.json diff --git a/json-tests/json/rlp/catdog.json b/json-tests/json/rlp/catdog.json index ed4c02602..724329784 100644 --- a/json-tests/json/rlp/catdog.json +++ b/json-tests/json/rlp/catdog.json @@ -14,5 +14,5 @@ "value": "dog" } ], - "output": "0xc88363617183646f67" + "output": "0xc88363617483646f67" } diff --git a/json-tests/json/rlp/empty_lists.json b/json-tests/json/rlp/empty_lists.json new file mode 100644 index 000000000..5ac649c2c --- /dev/null +++ b/json-tests/json/rlp/empty_lists.json @@ -0,0 +1,38 @@ +{ + "input": + [ + { + "operation": "append_list", + "len": 3 + }, + { + "operation": "append_list", + "len": 0 + }, + { + "operation": "append_list", + "len": 1 + }, + { + "operation": "append_list", + "len": 0 + }, + { + "operation": "append_list", + "len": 2 + }, + { + "operation": "append_list", + "len": 0 + }, + { + "operation": "append_list", + "len": 1 + }, + { + "operation": "append_list", + "len": 0 + } + ], + "output": "0xc7c0c1c0c3c0c1c0" +} diff --git a/src/rlp.rs b/src/rlp.rs index b055632b1..8cb5f2b9b 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -1098,6 +1098,9 @@ impl Encoder for BasicEncoder { #[cfg(test)] mod tests { + extern crate json_tests; + use self::json_tests::execute_tests_from_directory; + use self::json_tests::rlp as rlptest; use std::{fmt, cmp}; use std::str::FromStr; use rlp; @@ -1496,4 +1499,25 @@ mod tests { let view = View::new(&data); let _data_slice = view.offset(1).data(); } + + #[test] + fn test_rlp_json() { + println!("Json rlp test: "); + execute_tests_from_directory::("json-tests/json/rlp/*.json", &mut | file, input, output | { + println!("file: {}", file); + + let mut stream = RlpStream::new(); + for operation in input.into_iter() { + match operation { + rlptest::Operation::Append(ref v) => stream.append(v), + rlptest::Operation::AppendList(len) => stream.append_list(len), + rlptest::Operation::AppendRaw(ref raw, len) => stream.append_raw(raw, len), + rlptest::Operation::AppendEmpty => stream.append_empty_data() + }; + } + + assert_eq!(stream.out(), output); + }); + } + } diff --git a/src/trie.rs b/src/trie.rs index 9dd12d8a5..674d22488 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -743,7 +743,6 @@ impl Trie for TrieDB { mod tests { extern crate json_tests; use self::json_tests::{trie, execute_tests_from_directory}; - use rustc_serialize::hex::FromHex; use triehash::*; use hash::*; use super::*; @@ -969,24 +968,6 @@ mod tests { //assert!(false); } - fn test_all(v: Vec<(Vec, Vec)>) { - let mut t = TrieDB::new_memory(); - - for i in 0..v.len() { - let key: &[u8]= &v[i].0; - let val: &[u8] = &v[i].1; - t.insert(&key, &val); - } - -// trace!("{:?}", t); -// println!("{:?}", t); - - // check lifetime -// let _q = t.at(&[b'd', b'o']).unwrap(); - - assert_eq!(*t.root(), trie_root(v)); - } - fn random_key() -> Vec { let chars = b"abcdefgrstuvwABCDEFGRSTUVW"; let mut ret: Vec = Vec::new(); @@ -1041,52 +1022,6 @@ mod tests { t } - #[test] - fn test_at_dog() { - env_logger::init().ok(); - 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")), - ]; - - test_all(v); - } - - #[test] - fn test_more_data() { - 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()) - - ]; - - test_all(v); - } - #[test] fn test_trie_json() { println!("Json trie test: ");