rlp tests, the beginning
This commit is contained in:
parent
84cc7715b4
commit
05f7e85d30
39
json-tests/json/rlp/README.md
Normal file
39
json-tests/json/rlp/README.md
Normal file
@ -0,0 +1,39 @@
|
||||
# Rlp tests guideline
|
||||
|
||||
Rlp can be tested in various ways. It can encode/decode a value or an array of values. Let's start with encoding.
|
||||
|
||||
Each operation must have field:
|
||||
|
||||
- `operation` - `append`, `append_list`, `append_empty` or `append_raw`
|
||||
|
||||
Additionally `append` and `append_raw` must additionally define a `value` field:
|
||||
|
||||
- `value` - data
|
||||
|
||||
Also `append_raw` and `append_list` requires `len` field
|
||||
|
||||
- `len` - integer
|
||||
|
||||
### Encoding Test Example
|
||||
|
||||
```json
|
||||
{
|
||||
"input":
|
||||
[
|
||||
{
|
||||
"operation": "append_list",
|
||||
"len": 2
|
||||
},
|
||||
{
|
||||
"operation": "append",
|
||||
"value": "cat"
|
||||
},
|
||||
{
|
||||
"operation": "append",
|
||||
"value": "dog"
|
||||
}
|
||||
]
|
||||
"output": "0xc88363617183646f67"
|
||||
}
|
||||
```
|
||||
|
18
json-tests/json/rlp/catdog.json
Normal file
18
json-tests/json/rlp/catdog.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"input":
|
||||
[
|
||||
{
|
||||
"operation": "append_list",
|
||||
"len": 2
|
||||
},
|
||||
{
|
||||
"operation": "append",
|
||||
"value": "cat"
|
||||
},
|
||||
{
|
||||
"operation": "append",
|
||||
"value": "dog"
|
||||
}
|
||||
]
|
||||
"output": "0xc88363617183646f67"
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
@ -742,7 +742,7 @@ impl Trie for TrieDB {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate json_tests;
|
||||
use self::json_tests::*;
|
||||
use self::json_tests::{trie, execute_tests_from_directory};
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use triehash::*;
|
||||
use hash::*;
|
||||
|
Loading…
Reference in New Issue
Block a user