Skip to content

Commit

Permalink
feat: proptest for pending
Browse files Browse the repository at this point in the history
  • Loading branch information
joske committed Jul 6, 2023
1 parent b831a5e commit 2640287
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions node/narwhal/src/helpers/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod tests {
ledger::{coinbase::PuzzleCommitment, narwhal::TransmissionID},
prelude::{Rng, TestRng},
};
use test_strategy::{proptest, Arbitrary};

type CurrentNetwork = snarkvm::prelude::Testnet3;

Expand Down Expand Up @@ -168,4 +169,44 @@ mod tests {
// Check empty again.
assert!(pending.is_empty());
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Item {
pub id: usize,
}

#[derive(Arbitrary, Clone, Debug)]
pub struct PendingInput {
#[strategy(1..5_000usize)]
pub count: usize,
}

impl PendingInput {
pub fn to_pending(&self) -> Pending<Item> {
let pending = Pending::<Item>::new();
for i in 0..self.count {
pending.insert(Item { id: i }, SocketAddr::from(([127, 0, 0, 1], i as u16)), None);
}
pending
}
}

#[proptest]
fn test_pending_proptest(input: PendingInput) {
println!("input: {:?}", input);
let pending = input.to_pending();
assert_eq!(pending.len(), input.count);
assert!(!pending.is_empty());
assert!(!pending.contains(Item { id: input.count + 1 }));
assert_eq!(pending.get(Item { id: input.count + 1 }), None);
assert!(!pending.remove(Item { id: input.count + 1 }));
for i in 0..input.count {
assert!(pending.contains(Item { id: i }));
let peer_ip = SocketAddr::from(([127, 0, 0, 1], i as u16));
assert!(pending.contains_peer(Item { id: i }, peer_ip));
assert_eq!(pending.get(Item { id: i }), Some(HashSet::from([peer_ip])));
assert!(pending.remove(Item { id: i }));
}
assert!(pending.is_empty());
}
}

0 comments on commit 2640287

Please sign in to comment.