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

Commit

Permalink
Moved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed May 21, 2022
1 parent aafba7b commit 6b91721
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 98 deletions.
29 changes: 0 additions & 29 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,3 @@ mod bitmap_ops;
pub use bitmap_ops::*;

pub mod utils;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn subslicing_gives_correct_null_count() {
let mut base = MutableBitmap::new();
base.push(false);
base.push(true);
base.push(true);
base.push(false);
base.push(false);
base.push(true);
base.push(true);
base.push(true);

let base = Bitmap::from(base);
assert_eq!(base.null_count(), 3);

let view1 = base.clone().slice(0, 1);
let view2 = base.slice(1, 7);
assert_eq!(view1.null_count(), 1);
assert_eq!(view2.null_count(), 2);

let view3 = view2.slice(0, 1);
assert_eq!(view3.null_count(), 0);
}
}
43 changes: 0 additions & 43 deletions src/bitmap/utils/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,3 @@ pub fn fmt(
}
f.write_char(']')
}

#[cfg(test)]
mod tests {
use super::*;

struct A<'a>(&'a [u8], usize, usize);
impl<'a> std::fmt::Debug for A<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt(self.0, self.1, self.2, f)
}
}

#[test]
fn test_debug() -> std::fmt::Result {
assert_eq!(format!("{:?}", A(&[1], 0, 0)), "[]");
assert_eq!(format!("{:?}", A(&[0b11000001], 0, 8)), "[0b11000001]");
assert_eq!(
format!("{:?}", A(&[0b11000001, 1], 0, 9)),
"[0b11000001, 0b_______1]"
);
assert_eq!(format!("{:?}", A(&[1], 0, 2)), "[0b______01]");
assert_eq!(format!("{:?}", A(&[1], 1, 2)), "[0b_____00_]");
assert_eq!(format!("{:?}", A(&[1], 2, 2)), "[0b____00__]");
assert_eq!(format!("{:?}", A(&[1], 3, 2)), "[0b___00___]");
assert_eq!(format!("{:?}", A(&[1], 4, 2)), "[0b__00____]");
assert_eq!(format!("{:?}", A(&[1], 5, 2)), "[0b_00_____]");
assert_eq!(format!("{:?}", A(&[1], 6, 2)), "[0b00______]");
assert_eq!(
format!("{:?}", A(&[0b11000001, 1], 1, 9)),
"[0b1100000_, 0b______01]"
);
// extra bytes are ignored
assert_eq!(
format!("{:?}", A(&[0b11000001, 1, 1, 1], 1, 9)),
"[0b1100000_, 0b______01]"
);
assert_eq!(
format!("{:?}", A(&[0b11000001, 1, 1], 2, 16)),
"[0b110000__, 0b00000001, 0b______01]"
);
Ok(())
}
}
27 changes: 1 addition & 26 deletions src/types/bit_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl<T: BitChunk> Iterator for BitChunkIter<T> {
// a mathematical invariant of this iterator
unsafe impl<T: BitChunk> crate::trusted_len::TrustedLen for BitChunkIter<T> {}

/// An [`Iterator<Item=usize>`] over a [`BitChunk`].
/// This iterator returns the postion of bit set.
/// An [`Iterator<Item=usize>`] over a [`BitChunk`] returning the index of each bit set in the chunk
/// Refer: https://lemire.me/blog/2018/03/08/iterating-over-set-bits-quickly-simd-edition/
/// # Example
/// ```
Expand Down Expand Up @@ -154,27 +153,3 @@ impl<T: BitChunk> Iterator for BitChunkOnes<T> {
// # Safety
// a mathematical invariant of this iterator
unsafe impl<T: BitChunk> crate::trusted_len::TrustedLen for BitChunkOnes<T> {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_basic1() {
let a = [0b00000001, 0b00010000]; // 0th and 13th entry
let a = u16::from_ne_bytes(a);
let iter = BitChunkIter::new(a, 16);
let r = iter.collect::<Vec<_>>();
assert_eq!(r, (0..16).map(|x| x == 0 || x == 12).collect::<Vec<_>>(),);
}

#[test]
fn test_ones() {
let a = [0b00000001, 0b00010000]; // 0th and 13th entry
let a = u16::from_ne_bytes(a);
let mut iter = BitChunkOnes::new(a);
assert_eq!(iter.size_hint(), (2, Some(2)));
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(12));
}
}
14 changes: 14 additions & 0 deletions tests/it/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,17 @@ fn not() {
let expected = create_bitmap([0b00010101], 6);
assert_eq!(!&lhs, expected);
}

#[test]
fn subslicing_gives_correct_null_count() {
let base = Bitmap::from([false, true, true, false, false, true, true, true]);
assert_eq!(base.null_count(), 3);

let view1 = base.clone().slice(0, 1);
let view2 = base.slice(1, 7);
assert_eq!(view1.null_count(), 1);
assert_eq!(view2.null_count(), 2);

let view3 = view2.slice(0, 1);
assert_eq!(view3.null_count(), 0);
}
40 changes: 40 additions & 0 deletions tests/it/bitmap/utils/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use arrow2::bitmap::utils::fmt;

struct A<'a>(&'a [u8], usize, usize);

impl<'a> std::fmt::Debug for A<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt(self.0, self.1, self.2, f)
}
}

#[test]
fn test_debug() -> std::fmt::Result {
assert_eq!(format!("{:?}", A(&[1], 0, 0)), "[]");
assert_eq!(format!("{:?}", A(&[0b11000001], 0, 8)), "[0b11000001]");
assert_eq!(
format!("{:?}", A(&[0b11000001, 1], 0, 9)),
"[0b11000001, 0b_______1]"
);
assert_eq!(format!("{:?}", A(&[1], 0, 2)), "[0b______01]");
assert_eq!(format!("{:?}", A(&[1], 1, 2)), "[0b_____00_]");
assert_eq!(format!("{:?}", A(&[1], 2, 2)), "[0b____00__]");
assert_eq!(format!("{:?}", A(&[1], 3, 2)), "[0b___00___]");
assert_eq!(format!("{:?}", A(&[1], 4, 2)), "[0b__00____]");
assert_eq!(format!("{:?}", A(&[1], 5, 2)), "[0b_00_____]");
assert_eq!(format!("{:?}", A(&[1], 6, 2)), "[0b00______]");
assert_eq!(
format!("{:?}", A(&[0b11000001, 1], 1, 9)),
"[0b1100000_, 0b______01]"
);
// extra bytes are ignored
assert_eq!(
format!("{:?}", A(&[0b11000001, 1, 1, 1], 1, 9)),
"[0b1100000_, 0b______01]"
);
assert_eq!(
format!("{:?}", A(&[0b11000001, 1, 1], 2, 16)),
"[0b110000__, 0b00000001, 0b______01]"
);
Ok(())
}
1 change: 1 addition & 0 deletions tests/it/bitmap/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::bitmap::bitmap_strategy;

mod bit_chunks_exact;
mod chunk_iter;
mod fmt;
mod iterator;
mod slice_iterator;
mod zip_validity;
Expand Down
1 change: 1 addition & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod buffer;
mod ffi;
mod scalar;
mod temporal_conversions;
mod types;

mod io;
mod test_util;
Expand Down
20 changes: 20 additions & 0 deletions tests/it/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use arrow2::types::{BitChunkIter, BitChunkOnes};

#[test]
fn test_basic1() {
let a = [0b00000001, 0b00010000]; // 0th and 13th entry
let a = u16::from_ne_bytes(a);
let iter = BitChunkIter::new(a, 16);
let r = iter.collect::<Vec<_>>();
assert_eq!(r, (0..16).map(|x| x == 0 || x == 12).collect::<Vec<_>>(),);
}

#[test]
fn test_ones() {
let a = [0b00000001, 0b00010000]; // 0th and 13th entry
let a = u16::from_ne_bytes(a);
let mut iter = BitChunkOnes::new(a);
assert_eq!(iter.size_hint(), (2, Some(2)));
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(12));
}

0 comments on commit 6b91721

Please sign in to comment.