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 bitmap exact chunk. (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Sep 4, 2021
1 parent 43d8cf5 commit 4e53c2e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/bitmap/utils/chunk_iterator/chunks_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ impl<'a, T: BitChunk> BitChunksExact<'a, T> {
/// Creates a new [`BitChunksExact`].
#[inline]
pub fn new(slice: &'a [u8], len: usize) -> Self {
assert!(len <= slice.len() * 8);
let size_of = std::mem::size_of::<T>();

let chunks = &slice[..len / 8];
let split = (len / 8 / size_of) * size_of;
let chunks = &slice[..split];
let remainder = &slice[split..];
let iter = chunks.chunks_exact(size_of);

let start = if slice.len() > size_of {
slice.len() - size_of
} else {
0
};
let remainder = &slice[start..];

Self {
iter,
remainder,
Expand Down
12 changes: 12 additions & 0 deletions tests/it/bitmap/bitmap_ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use arrow2::bitmap::Bitmap;

#[test]
fn not_random() {
let iter = (0..100).map(|x| x % 7 == 0);
let iter_not = iter.clone().map(|x| !x);

let bitmap: Bitmap = iter.collect();
let expected: Bitmap = iter_not.collect();

assert_eq!(!&bitmap, expected);
}
1 change: 1 addition & 0 deletions tests/it/bitmap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bitmap_ops;
mod immutable;
mod mutable;
mod utils;
Expand Down
13 changes: 12 additions & 1 deletion tests/it/bitmap/utils/bit_chunks_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn basics() {

#[test]
fn basics_u16_small() {
let mut iter = BitChunksExact::<u16>::new(&[0b11111111u8], 9);
let mut iter = BitChunksExact::<u16>::new(&[0b11111111u8], 7);
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b0000_0000_1111_1111u16);
}
Expand All @@ -20,3 +20,14 @@ fn basics_u16() {
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b0000_0001_1111_1111u16);
}

#[test]
fn remainder_u16() {
let mut iter = BitChunksExact::<u16>::new(
&[0b11111111u8, 0b00000001u8, 0b00000001u8, 0b11011011u8],
23,
);
assert_eq!(iter.next(), Some(511));
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b1101_1011_0000_0001u16);
}
1 change: 1 addition & 0 deletions tests/it/bitmap/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use arrow2::bitmap::utils::*;

mod bit_chunks_exact;
mod chunk_iter;
mod iterator;
mod slice_iterator;
Expand Down

0 comments on commit 4e53c2e

Please sign in to comment.