Skip to content

Commit

Permalink
tests: add prop test for encoding/decoding of Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Nov 29, 2024
1 parent 87c9ed6 commit 6f3f02a
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion nomt/src/store/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) const VERSION: u32 = 1;
pub(crate) const META_SIZE: usize = 64;

/// This data structure describes the state of the btree.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Meta {
/// The magic number of the metadata file.
pub magic: [u8; 4],
Expand Down Expand Up @@ -133,3 +133,47 @@ impl Meta {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::{Meta, META_SIZE};
use quickcheck::quickcheck;

impl quickcheck::Arbitrary for Meta {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Meta {
magic: u32::arbitrary(g).to_le_bytes(),
version: u32::arbitrary(g),
ln_freelist_pn: u32::arbitrary(g),
ln_bump: u32::arbitrary(g),
bbn_freelist_pn: u32::arbitrary(g),
bbn_bump: u32::arbitrary(g),
sync_seqn: u32::arbitrary(g),
bitbox_num_pages: u32::arbitrary(g),
bitbox_seed: u128::arbitrary(g).to_le_bytes(),
rollback_start_live: u64::arbitrary(g),
rollback_end_live: u64::arbitrary(g),
}
}
}

quickcheck! {
fn encode_decode_roundtrip(meta: Meta) -> bool {
let mut buf = vec![0u8; META_SIZE];
meta.encode_to(&mut buf);
let decoded = Meta::decode(&buf);

meta.magic == decoded.magic &&
meta.version == decoded.version &&
meta.ln_freelist_pn == decoded.ln_freelist_pn &&
meta.ln_bump == decoded.ln_bump &&
meta.bbn_freelist_pn == decoded.bbn_freelist_pn &&
meta.bbn_bump == decoded.bbn_bump &&
meta.sync_seqn == decoded.sync_seqn &&
meta.bitbox_num_pages == decoded.bitbox_num_pages &&
meta.bitbox_seed == decoded.bitbox_seed &&
meta.rollback_start_live == decoded.rollback_start_live &&
meta.rollback_end_live == decoded.rollback_end_live
}
}
}

0 comments on commit 6f3f02a

Please sign in to comment.