From a87fe88d975d353bc1f050c750299669b91c4c8f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 30 Sep 2018 14:24:03 +0930 Subject: [PATCH] Fix bug in boolean bitfield The length generated when converting from a string was incorrect. --- boolean-bitfield/src/lib.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/boolean-bitfield/src/lib.rs b/boolean-bitfield/src/lib.rs index 6ac8e082b..60d877e65 100644 --- a/boolean-bitfield/src/lib.rs +++ b/boolean-bitfield/src/lib.rs @@ -109,8 +109,8 @@ impl BooleanBitfield { pub fn compute_length(bytes: &[u8]) -> usize { for byte in (0..bytes.len()).rev() { for bit in (0..8).rev() { - if byte & (1 << (bit as u8)) != 0 { - return (byte * 8) + bit + if bytes[byte] & (1 << (bit as u8)) != 0 { + return (byte * 8) + bit + 1 } } } @@ -188,6 +188,25 @@ mod tests { use super::*; use ssz::Decodable; + #[test] + fn test_new_from_slice() { + let s = [0]; + let b = BooleanBitfield::from(&s[..]); + assert_eq!(b.len, 0); + + let s = [255]; + let b = BooleanBitfield::from(&s[..]); + assert_eq!(b.len, 8); + + let s = [0, 1]; + let b = BooleanBitfield::from(&s[..]); + assert_eq!(b.len, 9); + + let s = [31]; + let b = BooleanBitfield::from(&s[..]); + assert_eq!(b.len, 5); + } + #[test] fn test_ssz_encoding() { let mut b = BooleanBitfield::new();