Skip to content

Commit e6a64e4

Browse files
committed
fix bitvector testn random impl in situations where N isnt divisible by 8
1 parent 3e10e68 commit e6a64e4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

consensus/types/src/test_utils/test_random/bitfield.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ impl<N: Unsigned + Clone> TestRandom for BitVector<N> {
2626
fn random_for_test(rng: &mut impl RngCore) -> Self {
2727
let mut raw_bytes = smallvec![0; std::cmp::max(1, (N::to_usize() + 7) / 8)];
2828
rng.fill_bytes(&mut raw_bytes);
29+
// If N isn't divisible by 8
30+
// zero out bits greater than N
31+
if let Some(last_byte) = raw_bytes.last_mut() {
32+
let mut mask = 0;
33+
for i in 0..N::to_usize() % 8 {
34+
mask |= 1 << i;
35+
}
36+
*last_byte &= mask;
37+
}
2938
Self::from_bytes(raw_bytes).expect("we generate a valid BitVector")
3039
}
3140
}

0 commit comments

Comments
 (0)