Move ethcore files back into root.
This commit is contained in:
66
util/json-tests/src/lib.rs
Normal file
66
util/json-tests/src/lib.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
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::*;
|
||||
|
||||
mod util;
|
||||
pub mod trie;
|
||||
pub mod rlp;
|
||||
|
||||
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<T, F>(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<T, F>(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_from_directory<T, F>(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::<T, _>(&path, &mut | input, output | {
|
||||
f(path.to_str().unwrap().to_string(), input, output);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
52
util/json-tests/src/rlp.rs
Normal file
52
util/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())
|
||||
}
|
||||
}
|
||||
|
||||
89
util/json-tests/src/trie.rs
Normal file
89
util/json-tests/src/trie.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
//! json trie tests
|
||||
use std::collections::HashMap;
|
||||
use rustc_serialize::*;
|
||||
use super::{JsonTest, JsonLoader};
|
||||
use util::*;
|
||||
|
||||
#[derive(RustcDecodable)]
|
||||
struct RawOperation {
|
||||
operation: String,
|
||||
key: String,
|
||||
value: Option<String>
|
||||
}
|
||||
|
||||
pub enum Operation {
|
||||
Insert(Vec<u8>, Vec<u8>),
|
||||
Remove(Vec<u8>)
|
||||
}
|
||||
|
||||
impl Into<Operation> for RawOperation {
|
||||
fn into(self) -> Operation {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TrieTest {
|
||||
loader: JsonLoader
|
||||
}
|
||||
|
||||
impl JsonTest for TrieTest {
|
||||
type Input = Vec<Operation>;
|
||||
type Output = Vec<u8>;
|
||||
|
||||
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<RawOperation> = 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<u8>, Vec<u8>)>;
|
||||
type Output = Vec<u8>;
|
||||
|
||||
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::Insert(k, v) => map.insert(k, v),
|
||||
Operation::Remove(k) => map.remove(&k)
|
||||
};
|
||||
map
|
||||
})
|
||||
.into_iter()
|
||||
.map(|p| { p })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn output(&self) -> Self::Output {
|
||||
self.trietest.output()
|
||||
}
|
||||
}
|
||||
|
||||
8
util/json-tests/src/util.rs
Normal file
8
util/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