Add unit test for calc_tree_width

Add a unit test for a `PartialMerkleTree` with node counts of 1-7
This commit is contained in:
Tobin C. Harding 2022-11-14 13:18:51 +11:00
parent 672656515e
commit 46f5588646
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 50 additions and 0 deletions

View File

@ -767,4 +767,54 @@ mod tests {
let block_hex = include_str!("../../tests/data/block_13b8a.hex");
deserialize(&Vec::from_hex(block_hex).unwrap()).unwrap()
}
macro_rules! check_calc_tree_width {
($($test_name:ident, $num_transactions:literal, $height:literal, $expected_width:literal);* $(;)?) => {
$(
#[test]
fn $test_name() {
let pmt = PartialMerkleTree {
num_transactions: $num_transactions,
bits: vec![],
hashes: vec![],
};
let got = pmt.calc_tree_width($height);
assert_eq!(got, $expected_width)
}
)*
}
}
// tree_width_<id> <num txs> <height> <expected_width>
//
// height 0 is the bottom of the tree, where the leaves are.
check_calc_tree_width! {
tree_width_01, 1, 0, 1;
//
tree_width_02, 2, 0, 2;
tree_width_03, 2, 1, 1;
//
tree_width_04, 3, 0, 3;
tree_width_05, 3, 1, 2;
tree_width_06, 3, 2, 1;
//
tree_width_07, 4, 0, 4;
tree_width_08, 4, 1, 2;
tree_width_09, 4, 2, 1;
//
tree_width_10, 5, 0, 5;
tree_width_11, 5, 1, 3;
tree_width_12, 5, 2, 2;
tree_width_13, 5, 3, 1;
//
tree_width_14, 6, 0, 6;
tree_width_15, 6, 1, 3;
tree_width_16, 6, 2, 2;
tree_width_17, 6, 3, 1;
//
tree_width_18, 7, 0, 7;
tree_width_19, 7, 1, 4;
tree_width_20, 7, 2, 2;
tree_width_21, 7, 3, 1;
}
}