Fix bug in boolean bitfield

The length generated when converting from a string was incorrect.
This commit is contained in:
Paul Hauner 2018-09-30 14:24:03 +09:30
parent 9642c4b7e1
commit a87fe88d97
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -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();