use chain_core::{
packer::Codec,
property::{DeserializeFromSlice, Serialize},
};
use quickcheck::{Arbitrary, TestResult};
pub fn serialization_bijection<T>(t: T) -> TestResult
where
T: Arbitrary + Serialize + DeserializeFromSlice + Eq,
{
let vec = match t.serialize_as_vec() {
Err(error) => return TestResult::error(format!("serialization: {}", error)),
Ok(v) => v,
};
let decoded_t = match T::deserialize_from_slice(&mut Codec::new(vec.as_slice())) {
Err(error) => return TestResult::error(format!("deserialization: {:?}", error)),
Ok(v) => v,
};
assert_eq!(vec.len(), t.serialized_size());
TestResult::from_bool(decoded_t == t)
}