From c1d2f0386d4f0b177e9dfb524b842e5a9257b757 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 20 May 2025 16:04:23 +0100 Subject: [PATCH 1/4] Add tests to block New mutants found in weekly mutation testing. Add tests to kill them. --- units/src/block.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/units/src/block.rs b/units/src/block.rs index 4efe0547b..57cae8ed7 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -439,6 +439,7 @@ impl<'a> core::iter::Sum<&'a BlockMtpInterval> for BlockMtpInterval { #[cfg(test)] mod tests { use super::*; + use crate::locktime::relative::NumberOf512Seconds; #[test] fn sanity_check() { @@ -479,6 +480,7 @@ mod tests { // interval - interval = interval assert!(BlockHeightInterval(10) - BlockHeightInterval(7) == BlockHeightInterval(3)); + // Sum for BlockHeightInterval by reference and by value assert!( [BlockHeightInterval(1), BlockHeightInterval(2), BlockHeightInterval(3)] .iter() @@ -492,6 +494,20 @@ mod tests { == BlockHeightInterval(15) ); + // Sum for BlockMtpInterval by reference and by value + assert!( + [BlockMtpInterval(1), BlockMtpInterval(2), BlockMtpInterval(3)] + .iter() + .sum::() + == BlockMtpInterval(6) + ); + assert!( + [BlockMtpInterval(4), BlockMtpInterval(5), BlockMtpInterval(6)] + .into_iter() + .sum::() + == BlockMtpInterval(15) + ); + // interval += interval let mut int = BlockHeightInterval(1); int += BlockHeightInterval(2); @@ -502,4 +518,54 @@ mod tests { int -= BlockHeightInterval(7); assert_eq!(int, BlockHeightInterval(3)); } + + #[test] + fn block_height_checked() { + let a = BlockHeight(10); + let b = BlockHeight(5); + assert_eq!(a.checked_sub(b), Some(BlockHeightInterval(5))); + assert_eq!(a.checked_add(BlockHeightInterval(5)), Some(BlockHeight(15))); + assert_eq!(a.checked_sub(BlockHeight(11)), None); + assert_eq!(a.checked_add(BlockHeightInterval(u32::MAX - 5)), None); + } + + #[test] + fn block_height_interval_checked() { + let a = BlockHeightInterval(10); + let b = BlockHeightInterval(5); + assert_eq!(a.checked_sub(b), Some(BlockHeightInterval(5))); + assert_eq!(a.checked_add(b), Some(BlockHeightInterval(15))); + assert_eq!(a.checked_sub(BlockHeightInterval(11)), None); + assert_eq!(a.checked_add(BlockHeightInterval(u32::MAX - 5)), None); + } + + #[test] + fn block_mtp_interval_checked() { + let a = BlockMtpInterval(10); + let b = BlockMtpInterval(5); + assert_eq!(a.checked_sub(b), Some(BlockMtpInterval(5))); + assert_eq!(a.checked_add(b), Some(BlockMtpInterval(15))); + assert_eq!(a.checked_sub(BlockMtpInterval(11)), None); + assert_eq!(a.checked_add(BlockMtpInterval(u32::MAX - 5)), None); + } + + #[test] + fn block_mtp_checked() { + let a = BlockMtp(10); + let b = BlockMtp(5); + assert_eq!(a.checked_sub(b), Some(BlockMtpInterval(5))); + assert_eq!(a.checked_add(BlockMtpInterval(5)), Some(BlockMtp(15))); + assert_eq!(a.checked_sub(BlockMtp(11)), None); + assert_eq!(a.checked_add(BlockMtpInterval(u32::MAX - 5)), None); + } + + #[test] + fn block_mtp_interval_from_number_of_512seconds() { + let n = NumberOf512Seconds::from_seconds_floor(0).unwrap(); + let interval = BlockMtpInterval::from(n); + assert_eq!(interval, BlockMtpInterval(0)); + let n = NumberOf512Seconds::from_seconds_floor(1024).unwrap(); + let interval = BlockMtpInterval::from(n); + assert_eq!(interval, BlockMtpInterval(1024)); + } } From 24cc059a78648fd6412dc84c8734c16ddf130cb4 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 20 May 2025 16:04:59 +0100 Subject: [PATCH 2/4] Add tests to result New mutants found in weekly mutation testing. Add tests to kill them. --- units/src/result.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/units/src/result.rs b/units/src/result.rs index f0587b2b2..1685778ae 100644 --- a/units/src/result.rs +++ b/units/src/result.rs @@ -311,3 +311,34 @@ impl fmt::Display for MathOp { } } } + +#[cfg(test)] +mod tests { + use crate::MathOp; + + #[test] + fn mathop_predicates() { + assert!(MathOp::Add.is_overflow()); + assert!(MathOp::Sub.is_overflow()); + assert!(MathOp::Mul.is_overflow()); + assert!(MathOp::Neg.is_overflow()); + assert!(!MathOp::Div.is_overflow()); + assert!(!MathOp::Rem.is_overflow()); + + assert!(MathOp::Div.is_div_by_zero()); + assert!(MathOp::Rem.is_div_by_zero()); + assert!(!MathOp::Add.is_div_by_zero()); + + assert!(MathOp::Add.is_addition()); + assert!(!MathOp::Sub.is_addition()); + + assert!(MathOp::Sub.is_subtraction()); + assert!(!MathOp::Add.is_subtraction()); + + assert!(MathOp::Mul.is_multiplication()); + assert!(!MathOp::Div.is_multiplication()); + + assert!(MathOp::Neg.is_negation()); + assert!(!MathOp::Add.is_negation()); + } +} From fd0a756344f1f9790efb0e8b08c9e4f3ebf01bef Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 20 May 2025 16:05:47 +0100 Subject: [PATCH 3/4] Add tests to relative locktime New mutants found in weekly mutation testing. Add tests to kill them. --- units/src/locktime/relative.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/units/src/locktime/relative.rs b/units/src/locktime/relative.rs index 4aa330710..23be03ca3 100644 --- a/units/src/locktime/relative.rs +++ b/units/src/locktime/relative.rs @@ -333,6 +333,7 @@ mod tests { NumberOf512Seconds::from_512_second_intervals(100).to_consensus_u32(), 4_194_404u32 ); // 0x400064 + assert_eq!(NumberOf512Seconds::from_512_second_intervals(1).to_seconds(), 512); } #[test] From b538a1095652f535aeb15f6e3bbc44969db1ea88 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 20 May 2025 16:07:03 +0100 Subject: [PATCH 4/4] Add deprecated functions to mutants exclude list New mutants found in deprecated functions in the weekly mutation testing. Add the deprecated functions to the exclude list so they are not mutated. --- .cargo/mutants.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.cargo/mutants.toml b/.cargo/mutants.toml index 87cffbcf2..70a1b8fd3 100644 --- a/.cargo/mutants.toml +++ b/.cargo/mutants.toml @@ -23,6 +23,8 @@ exclude_re = [ "FeeRate::fee_vb", # Deprecated "FeeRate::fee_wu", # Deprecated "SignedAmount::checked_abs", # Deprecated + "NumberOfBlocks::value", # Deprecated + "NumberOf512Seconds::to_consensus_u32", # Deprecated # primitives "Sequence::from_512_second_intervals", # Mutant from replacing | with ^, this returns the same value since the XOR is taken against the u16 with an all-zero bitmask