Skip to content

Commit

Permalink
Make encode test generic to cover all schemas (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinifinity authored Dec 15, 2023
1 parent e107c6e commit dd65b11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
38 changes: 16 additions & 22 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,28 +1491,22 @@ mod tests {
assert_eq!(encoded, new_node_encoded);
}

#[test_case(leaf(Vec::new(), Vec::new()) ; "empty leaf encoding")]
#[test_case(leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding")]
#[test_case(branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd")]
#[test_case(branch(Some(b"value".to_vec()), None); "branch without chd")]
#[test_case(branch(None, None); "branch without value and chd")]
fn node_encode_decode(node: Node) {
let merkle = create_test_merkle();
let node_ref = merkle.put_node(node.clone()).unwrap();

let encoded = merkle.encode(&node_ref).unwrap();
let new_node = Node::from(merkle.decode(encoded.as_ref()).unwrap());

assert_eq!(node, new_node);
}

#[test_case(leaf(Vec::new(), Vec::new()) ; "empty leaf encoding")]
#[test_case(leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding")]
#[test_case(branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd")]
#[test_case(branch(Some(b"value".to_vec()), Some(Vec::new())); "branch with empty chd")]
#[test_case(branch(Some(Vec::new()), vec![1, 2, 3].into()); "branch with empty value")]
fn node_encode_decode_plain(node: Node) {
let merkle = create_generic_test_merkle::<PlainCodec>();
#[test_case(Bincode::new(), leaf(Vec::new(), Vec::new()) ; "empty leaf encoding with Bincode")]
#[test_case(Bincode::new(), leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding with Bincode")]
#[test_case(Bincode::new(), branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd with Bincode")]
#[test_case(Bincode::new(), branch(Some(b"value".to_vec()), None); "branch without chd with Bincode")]
#[test_case(Bincode::new(), branch(None, None); "branch without value and chd with Bincode")]
#[test_case(PlainCodec::new(), leaf(Vec::new(), Vec::new()) ; "empty leaf encoding with PlainCodec")]
#[test_case(PlainCodec::new(), leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding with PlainCodec")]
#[test_case(PlainCodec::new(), branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd with PlainCodec")]
#[test_case(PlainCodec::new(), branch(Some(b"value".to_vec()), Some(Vec::new())); "branch with empty chd with PlainCodec")]
#[test_case(PlainCodec::new(), branch(Some(Vec::new()), vec![1, 2, 3].into()); "branch with empty value with PlainCodec")]
fn node_encode_decode<T>(_codec: T, node: Node)
where
T: BinarySerde,
for<'de> EncodedNode<T>: serde::Serialize + serde::Deserialize<'de>,
{
let merkle = create_generic_test_merkle::<T>();
let node_ref = merkle.put_node(node.clone()).unwrap();

let encoded = merkle.encode(&node_ref).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions firewood/src/merkle/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ pub trait BinarySerde {
) -> Result<T, Self::DeserializeError>;
}

#[derive(Default)]
pub struct Bincode(pub bincode::DefaultOptions);

impl Debug for Bincode {
Expand Down Expand Up @@ -750,6 +751,7 @@ impl BinarySerde for Bincode {
}
}

#[derive(Default)]
pub struct PlainCodec(pub bincode::DefaultOptions);

impl Debug for PlainCodec {
Expand Down

0 comments on commit dd65b11

Please sign in to comment.