Skip to content

Commit

Permalink
shardtree: Add proptest comparing insert_tree to batch_insert
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 24, 2023
1 parent faabef3 commit 067f688
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion shardtree/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,15 @@ fn build_minimal_tree<H: Hashable + Clone + PartialEq>(
#[cfg(test)]
mod tests {
use incrementalmerkletree::{Address, Level, Position, Retention};
use proptest::prelude::*;

use super::{LocatedPrunableTree, RetentionFlags};
use crate::tree::tests::{leaf, nil, parent};
use crate::{
memory::MemoryShardStore,
testing::{arb_char_str, arb_leaves},
tree::tests::{leaf, nil, parent},
BatchInsertionResult, ShardTree,
};

#[test]
fn located_from_iter_non_sibling_adjacent() {
Expand Down Expand Up @@ -465,4 +471,41 @@ mod tests {
.unwrap();
assert_eq!(complete.subtree.right_filled_root(), Ok("abcd".to_string()));
}

proptest! {
#[test]
fn batch_insert_matches_from_iter_and_insert_tree(
leaves in arb_leaves(arb_char_str())
) {
const DEPTH: u8 = 6;
const SHARD_HEIGHT: u8 = 3;

let start = Position::from(0);
let end = start + leaves.len() as u64;

let BatchInsertionResult {
subtree,
mut remainder,
..
} = LocatedPrunableTree::from_iter(start..end, 0.into(), leaves.clone().into_iter())
.unwrap();
assert_eq!(remainder.next(), None);

let subtree_addr = subtree.root_addr();
let root_from_iter = subtree.root().root_hash(subtree_addr, 0.into()).unwrap();

// Construct a tree using `ShardTree::insert_tree`.
let mut left =
ShardTree::<_, DEPTH, SHARD_HEIGHT>::new(MemoryShardStore::<_, usize>::empty(), 10);
left.insert_tree(subtree).unwrap();
assert_eq!(root_from_iter, left.root(subtree_addr, 0.into()).unwrap());

// Construct a tree using `ShardTree::batch_insert`.
let mut right = ShardTree::<_, DEPTH, SHARD_HEIGHT>::new(MemoryShardStore::empty(), 10);
right.batch_insert(start, leaves.into_iter()).unwrap();

// Check that the resulting trees are equal.
assert_eq!(left.root_at_checkpoint(0), right.root_at_checkpoint(0));
}
}
}

0 comments on commit 067f688

Please sign in to comment.