Ensure an empty bitfield serializes to 1 byte

This commit is contained in:
Paul Hauner 2018-09-19 16:45:32 +10:00
parent 61fddb2660
commit 928bafb3da
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -19,15 +19,17 @@ impl BooleanBitfield {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
len: 0, len: 0,
vec: vec![] vec: vec![0]
} }
} }
/// Create a new bitfield of a certain capacity /// Create a new bitfield of a certain capacity
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
let mut vec = Vec::with_capacity(capacity / 8 + 1);
vec.push(0);
Self { Self {
len: 0, len: 0,
vec: Vec::with_capacity(capacity / 8 + 1) vec
} }
} }
@ -112,6 +114,17 @@ impl PartialEq for BooleanBitfield {
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_new_bitfield_len() {
let b = BooleanBitfield::new();
assert_eq!(b.len(), 0);
assert_eq!(b.to_be_vec(), vec![0]);
let b = BooleanBitfield::with_capacity(100);
assert_eq!(b.len(), 0);
assert_eq!(b.to_be_vec(), vec![0]);
}
#[test] #[test]
fn test_bitfield_set() { fn test_bitfield_set() {
let mut b = BooleanBitfield::new(); let mut b = BooleanBitfield::new();