mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Fix BitVectors TestRandom implementation (#5854)
* fix bitvector test random impl
This commit is contained in:
@@ -26,6 +26,15 @@ impl<N: Unsigned + Clone> TestRandom for BitVector<N> {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut raw_bytes = smallvec![0; std::cmp::max(1, (N::to_usize() + 7) / 8)];
|
||||
rng.fill_bytes(&mut raw_bytes);
|
||||
// If N isn't divisible by 8
|
||||
// zero out bits greater than N
|
||||
if let Some(last_byte) = raw_bytes.last_mut() {
|
||||
let mut mask = 0;
|
||||
for i in 0..N::to_usize() % 8 {
|
||||
mask |= 1 << i;
|
||||
}
|
||||
*last_byte &= mask;
|
||||
}
|
||||
Self::from_bytes(raw_bytes).expect("we generate a valid BitVector")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user