Increase test coverage in block.rs

Add tests to `primitive::block` module to increase test coverage.
This commit is contained in:
Jamil Lambert, PhD 2025-03-07 16:33:19 +00:00
parent 3e750d7ba1
commit c1ea20b958
No known key found for this signature in database
GPG Key ID: 54DC29234AB5D2C0
1 changed files with 110 additions and 0 deletions

View File

@ -406,6 +406,12 @@ mod tests {
assert_eq!(version.to_consensus(), 1_234_567_890);
}
#[test]
fn version_default() {
let version = Version::default();
assert_eq!(version.to_consensus(), Version::NO_SOFT_FORK_SIGNALLING.to_consensus());
}
// Check that the size of the header consensus serialization matches the const SIZE value
#[test]
fn header_size() {
@ -422,4 +428,108 @@ mod tests {
assert_eq!(header_size, Header::SIZE);
}
#[test]
#[cfg(feature = "alloc")]
fn block_new_unchecked() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions.clone());
assert_eq!(block.header, header);
assert_eq!(block.transactions, transactions);
}
#[test]
#[cfg(feature = "alloc")]
fn block_assume_checked() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions.clone());
let witness_root = Some(WitnessMerkleNode::from_byte_array([0x88; 32]));
let checked_block = block.assume_checked(witness_root);
assert_eq!(checked_block.header(), &header);
assert_eq!(checked_block.transactions(), &transactions);
assert_eq!(checked_block.cached_witness_root(), witness_root);
}
#[test]
#[cfg(feature = "alloc")]
fn block_into_parts() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions.clone());
let (block_header, block_transactions) = block.into_parts();
assert_eq!(block_header, header);
assert_eq!(block_transactions, transactions);
}
#[test]
#[cfg(feature = "alloc")]
fn block_cached_witness_root() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions);
let witness_root = Some(WitnessMerkleNode::from_byte_array([0x88; 32]));
let checked_block = block.assume_checked(witness_root);
assert_eq!(checked_block.cached_witness_root(), witness_root);
}
#[test]
#[cfg(feature = "alloc")]
fn block_block_hash() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions);
assert_eq!(block.block_hash(), header.block_hash());
}
#[test]
fn block_hash_from_header() {
let header = dummy_header();
let block_hash = header.block_hash();
assert_eq!(block_hash, BlockHash::from(header));
}
#[test]
fn block_hash_from_header_ref() {
let header = dummy_header();
let block_hash: BlockHash = BlockHash::from(&header);
assert_eq!(block_hash, header.block_hash());
}
#[test]
#[cfg(feature = "alloc")]
fn block_hash_from_block() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions);
let block_hash: BlockHash = BlockHash::from(block);
assert_eq!(block_hash, header.block_hash());
}
#[test]
#[cfg(feature = "alloc")]
fn block_hash_from_block_ref() {
let header = dummy_header();
let transactions = vec![];
let block = Block::new_unchecked(header, transactions);
let block_hash: BlockHash = BlockHash::from(&block);
assert_eq!(block_hash, header.block_hash());
}
#[test]
fn header_debug() {
let header = dummy_header();
let expected = format!(
"Header {{ block_hash: {:?}, version: {:?}, prev_blockhash: {:?}, merkle_root: {:?}, time: {:?}, bits: {:?}, nonce: {:?} }}",
header.block_hash(),
header.version,
header.prev_blockhash,
header.merkle_root,
header.time,
header.bits,
header.nonce
);
assert_eq!(format!("{:?}", header), expected);
}
}