Skip to content

Commit 855299a

Browse files
committed
Merge rust-bitcoin#4532: units: Kill mutants found in weekly mutation testing
b538a10 Add deprecated functions to mutants exclude list (Jamil Lambert, PhD) fd0a756 Add tests to relative locktime (Jamil Lambert, PhD) 24cc059 Add tests to result (Jamil Lambert, PhD) c1d2f03 Add tests to block (Jamil Lambert, PhD) Pull request description: Weekly mutation testing found new mutants. Add tests to kill the valid mutants. Add deprecated functions to the exclude list so they are not mutated. Closes rust-bitcoin#4488, Closes rust-bitcoin#4528 ACKs for top commit: apoelstra: ACK b538a10; successfully ran local tests tcharding: ACK b538a10 Tree-SHA512: 8393ded6c073b2580fbb0fde9a8ce702a3d1e8c581c035870c2ba6a12d718cee577e345c9d92d0761552765248a6fb5ae9bbacbc88cac75e7153516de46de4ca
2 parents c21d445 + b538a10 commit 855299a

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

.cargo/mutants.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ exclude_re = [
2323
"FeeRate::fee_vb", # Deprecated
2424
"FeeRate::fee_wu", # Deprecated
2525
"SignedAmount::checked_abs", # Deprecated
26+
"NumberOfBlocks::value", # Deprecated
27+
"NumberOf512Seconds::to_consensus_u32", # Deprecated
2628

2729
# primitives
2830
"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

units/src/block.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl<'a> core::iter::Sum<&'a BlockMtpInterval> for BlockMtpInterval {
439439
#[cfg(test)]
440440
mod tests {
441441
use super::*;
442+
use crate::locktime::relative::NumberOf512Seconds;
442443

443444
#[test]
444445
fn sanity_check() {
@@ -479,6 +480,7 @@ mod tests {
479480
// interval - interval = interval
480481
assert!(BlockHeightInterval(10) - BlockHeightInterval(7) == BlockHeightInterval(3));
481482

483+
// Sum for BlockHeightInterval by reference and by value
482484
assert!(
483485
[BlockHeightInterval(1), BlockHeightInterval(2), BlockHeightInterval(3)]
484486
.iter()
@@ -492,6 +494,20 @@ mod tests {
492494
== BlockHeightInterval(15)
493495
);
494496

497+
// Sum for BlockMtpInterval by reference and by value
498+
assert!(
499+
[BlockMtpInterval(1), BlockMtpInterval(2), BlockMtpInterval(3)]
500+
.iter()
501+
.sum::<BlockMtpInterval>()
502+
== BlockMtpInterval(6)
503+
);
504+
assert!(
505+
[BlockMtpInterval(4), BlockMtpInterval(5), BlockMtpInterval(6)]
506+
.into_iter()
507+
.sum::<BlockMtpInterval>()
508+
== BlockMtpInterval(15)
509+
);
510+
495511
// interval += interval
496512
let mut int = BlockHeightInterval(1);
497513
int += BlockHeightInterval(2);
@@ -502,4 +518,54 @@ mod tests {
502518
int -= BlockHeightInterval(7);
503519
assert_eq!(int, BlockHeightInterval(3));
504520
}
521+
522+
#[test]
523+
fn block_height_checked() {
524+
let a = BlockHeight(10);
525+
let b = BlockHeight(5);
526+
assert_eq!(a.checked_sub(b), Some(BlockHeightInterval(5)));
527+
assert_eq!(a.checked_add(BlockHeightInterval(5)), Some(BlockHeight(15)));
528+
assert_eq!(a.checked_sub(BlockHeight(11)), None);
529+
assert_eq!(a.checked_add(BlockHeightInterval(u32::MAX - 5)), None);
530+
}
531+
532+
#[test]
533+
fn block_height_interval_checked() {
534+
let a = BlockHeightInterval(10);
535+
let b = BlockHeightInterval(5);
536+
assert_eq!(a.checked_sub(b), Some(BlockHeightInterval(5)));
537+
assert_eq!(a.checked_add(b), Some(BlockHeightInterval(15)));
538+
assert_eq!(a.checked_sub(BlockHeightInterval(11)), None);
539+
assert_eq!(a.checked_add(BlockHeightInterval(u32::MAX - 5)), None);
540+
}
541+
542+
#[test]
543+
fn block_mtp_interval_checked() {
544+
let a = BlockMtpInterval(10);
545+
let b = BlockMtpInterval(5);
546+
assert_eq!(a.checked_sub(b), Some(BlockMtpInterval(5)));
547+
assert_eq!(a.checked_add(b), Some(BlockMtpInterval(15)));
548+
assert_eq!(a.checked_sub(BlockMtpInterval(11)), None);
549+
assert_eq!(a.checked_add(BlockMtpInterval(u32::MAX - 5)), None);
550+
}
551+
552+
#[test]
553+
fn block_mtp_checked() {
554+
let a = BlockMtp(10);
555+
let b = BlockMtp(5);
556+
assert_eq!(a.checked_sub(b), Some(BlockMtpInterval(5)));
557+
assert_eq!(a.checked_add(BlockMtpInterval(5)), Some(BlockMtp(15)));
558+
assert_eq!(a.checked_sub(BlockMtp(11)), None);
559+
assert_eq!(a.checked_add(BlockMtpInterval(u32::MAX - 5)), None);
560+
}
561+
562+
#[test]
563+
fn block_mtp_interval_from_number_of_512seconds() {
564+
let n = NumberOf512Seconds::from_seconds_floor(0).unwrap();
565+
let interval = BlockMtpInterval::from(n);
566+
assert_eq!(interval, BlockMtpInterval(0));
567+
let n = NumberOf512Seconds::from_seconds_floor(1024).unwrap();
568+
let interval = BlockMtpInterval::from(n);
569+
assert_eq!(interval, BlockMtpInterval(1024));
570+
}
505571
}

units/src/locktime/relative.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ mod tests {
333333
NumberOf512Seconds::from_512_second_intervals(100).to_consensus_u32(),
334334
4_194_404u32
335335
); // 0x400064
336+
assert_eq!(NumberOf512Seconds::from_512_second_intervals(1).to_seconds(), 512);
336337
}
337338

338339
#[test]

units/src/result.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,34 @@ impl fmt::Display for MathOp {
310310
}
311311
}
312312
}
313+
314+
#[cfg(test)]
315+
mod tests {
316+
use crate::MathOp;
317+
318+
#[test]
319+
fn mathop_predicates() {
320+
assert!(MathOp::Add.is_overflow());
321+
assert!(MathOp::Sub.is_overflow());
322+
assert!(MathOp::Mul.is_overflow());
323+
assert!(MathOp::Neg.is_overflow());
324+
assert!(!MathOp::Div.is_overflow());
325+
assert!(!MathOp::Rem.is_overflow());
326+
327+
assert!(MathOp::Div.is_div_by_zero());
328+
assert!(MathOp::Rem.is_div_by_zero());
329+
assert!(!MathOp::Add.is_div_by_zero());
330+
331+
assert!(MathOp::Add.is_addition());
332+
assert!(!MathOp::Sub.is_addition());
333+
334+
assert!(MathOp::Sub.is_subtraction());
335+
assert!(!MathOp::Add.is_subtraction());
336+
337+
assert!(MathOp::Mul.is_multiplication());
338+
assert!(!MathOp::Div.is_multiplication());
339+
340+
assert!(MathOp::Neg.is_negation());
341+
assert!(!MathOp::Add.is_negation());
342+
}
343+
}

0 commit comments

Comments
 (0)