Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed error in slicing bitmap. (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Aug 4, 2021
1 parent fa3c2ce commit 1908eb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/bitmap/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Bitmap {
#[inline]
pub(crate) fn as_slice(&self) -> &[u8] {
let start = self.offset / 8;
let len = self.length.saturating_add(7) / 8;
let len = (self.offset() + self.length).saturating_add(7) / 8;
&self.bytes[start..start + len]
}
}
Expand Down Expand Up @@ -261,4 +261,15 @@ mod tests {

assert_eq!(0, b.offset());
}

#[test]
fn as_slice_offset_middle() {
let b = Bitmap::from_u8_slice(&[0, 0, 0, 0b00010101], 27);
let b = b.slice(22, 5);

let slice = b.as_slice();
assert_eq!(slice, &[0, 0b00010101]);

assert_eq!(6, b.offset());
}
}
9 changes: 9 additions & 0 deletions src/bitmap/utils/slice_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,13 @@ mod tests {
// the first "11" in the second byte
assert_eq!(chunks, vec![(0, 2)]);
}

#[test]
fn remainder_1() {
let values = Bitmap::from_u8_slice(&[0, 0, 0b00000000, 0b00010101], 27);
let values = values.slice(22, 5);
let iter = SlicesIterator::new(&values);
let chunks = iter.collect::<Vec<_>>();
assert_eq!(chunks, vec![(2, 1), (4, 1)]);
}
}

0 comments on commit 1908eb7

Please sign in to comment.