rlp tests, the beginning
This commit is contained in:
@@ -8,7 +8,9 @@ use std::fs::File;
|
||||
use glob::glob;
|
||||
use rustc_serialize::*;
|
||||
|
||||
mod util;
|
||||
pub mod trie;
|
||||
pub mod rlp;
|
||||
|
||||
pub trait JsonTest: Sized {
|
||||
type Input;
|
||||
|
||||
52
json-tests/src/rlp.rs
Normal file
52
json-tests/src/rlp.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
//! json rlp tests
|
||||
use rustc_serialize::*;
|
||||
use super::{JsonTest, JsonLoader};
|
||||
use util::*;
|
||||
|
||||
pub enum Operation {
|
||||
Append(Vec<u8>),
|
||||
AppendList(usize),
|
||||
AppendRaw(Vec<u8>, usize),
|
||||
AppendEmpty
|
||||
}
|
||||
|
||||
impl Into<Operation> for json::Json {
|
||||
fn into(self) -> Operation {
|
||||
let obj = self.as_object().unwrap();
|
||||
match obj["operation"].as_string().unwrap().as_ref() {
|
||||
"append" => Operation::Append(hex_or_string(obj["value"].as_string().unwrap())),
|
||||
"append_list" => Operation::AppendList(obj["len"].as_u64().unwrap() as usize),
|
||||
"append_raw" => Operation::AppendRaw(hex_or_string(obj["value"].as_string().unwrap()), obj["len"].as_u64().unwrap() as usize),
|
||||
"append_empty" => Operation::AppendEmpty,
|
||||
other => { panic!("Unsupported opertation: {}", other); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RlpStreamTest {
|
||||
loader: JsonLoader
|
||||
}
|
||||
|
||||
impl JsonTest for RlpStreamTest {
|
||||
type Input = Vec<Operation>;
|
||||
type Output = Vec<u8>;
|
||||
|
||||
fn new(data: &[u8]) -> Self {
|
||||
RlpStreamTest {
|
||||
loader: JsonLoader::new(data)
|
||||
}
|
||||
}
|
||||
|
||||
fn input(&self) -> Self::Input {
|
||||
self.loader.input().as_array().unwrap()
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|i| i.into())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn output(&self) -> Self::Output {
|
||||
hex_or_string(self.loader.output().as_string().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,12 @@
|
||||
//! json trie tests
|
||||
use std::collections::HashMap;
|
||||
use rustc_serialize::*;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use super::{JsonTest, JsonLoader};
|
||||
|
||||
enum OperationType {
|
||||
Insert,
|
||||
Remove
|
||||
}
|
||||
|
||||
impl Decodable for OperationType {
|
||||
fn decode<D>(d: &mut D) -> Result<OperationType, D::Error> where D: Decoder {
|
||||
match try!(String::decode(d)).as_ref() {
|
||||
"insert" => Ok(OperationType::Insert),
|
||||
"remove" => Ok(OperationType::Remove),
|
||||
other => panic!("invalid operation type: {}", other)
|
||||
}
|
||||
}
|
||||
}
|
||||
use util::*;
|
||||
|
||||
#[derive(RustcDecodable)]
|
||||
struct RawOperation {
|
||||
operation: OperationType,
|
||||
operation: String,
|
||||
key: String,
|
||||
value: Option<String>
|
||||
}
|
||||
@@ -31,18 +16,12 @@ pub enum Operation {
|
||||
Remove(Vec<u8>)
|
||||
}
|
||||
|
||||
fn hex_or_string(s: &str) -> Vec<u8> {
|
||||
match s.starts_with("0x") {
|
||||
true => s[2..].from_hex().unwrap(),
|
||||
false => From::from(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Operation> for RawOperation {
|
||||
fn into(self) -> Operation {
|
||||
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))
|
||||
match self.operation.as_ref() {
|
||||
"insert" => Operation::Insert(hex_or_string(&self.key), hex_or_string(&self.value.unwrap())),
|
||||
"remove" => Operation::Remove(hex_or_string(&self.key)),
|
||||
other => panic!("invalid operation type: {}", other)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
json-tests/src/util.rs
Normal file
8
json-tests/src/util.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use rustc_serialize::hex::FromHex;
|
||||
|
||||
pub fn hex_or_string(s: &str) -> Vec<u8> {
|
||||
match s.starts_with("0x") {
|
||||
true => s[2..].from_hex().unwrap(),
|
||||
false => From::from(s)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user