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

Commit

Permalink
Moved tests to integration tests (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Aug 14, 2021
1 parent 959d549 commit b77de38
Show file tree
Hide file tree
Showing 66 changed files with 4,303 additions and 4,323 deletions.
6 changes: 4 additions & 2 deletions src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::types::NativeType;

mod alignment;

use alignment::ALIGNMENT;
pub use alignment::ALIGNMENT;

// If this number is not zero after all objects have been `drop`, there is a memory leak
pub static mut ALLOCATIONS: AtomicIsize = AtomicIsize::new(0);
Expand All @@ -39,8 +39,10 @@ pub fn total_allocated_bytes() -> isize {
unsafe { ALLOCATIONS.load(std::sync::atomic::Ordering::SeqCst) }
}

/// # Safety
/// This pointer may only be used to check if memory is allocated.
#[inline]
unsafe fn dangling<T: NativeType>() -> NonNull<T> {
pub unsafe fn dangling<T: NativeType>() -> NonNull<T> {
NonNull::new_unchecked(ALIGNMENT as *mut T)
}

Expand Down
3 changes: 1 addition & 2 deletions src/array/binary/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ mod tests {
let array = BinaryArray::<i32>::from(&[Some(b"hello".as_ref()), Some(b" ".as_ref()), None]);

let a = array.validity().as_ref().unwrap();
assert_eq!(a.len(), 3);
assert_eq!(a.as_slice()[0], 0b00000011);
assert_eq!(a, &Bitmap::from([true, true, false]));
}
}
3 changes: 1 addition & 2 deletions src/array/utf8/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ mod tests {
let array = Utf8Array::<i32>::from(&[Some("hello"), Some(" "), None]);

let a = array.validity().as_ref().unwrap();
assert_eq!(a.len(), 3);
assert_eq!(a.as_slice()[0], 0b00000011);
assert_eq!(a, &Bitmap::from([true, true, false]));
}

#[test]
Expand Down
61 changes: 7 additions & 54 deletions src/bitmap/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,17 @@ impl Bitmap {
}
}

// Methods used for IPC
impl Bitmap {
#[inline]
pub(crate) fn offset(&self) -> usize {
self.offset % 8
}

/// Returns the byte slice of this Bitmap.
#[inline]
pub(crate) fn as_slice(&self) -> &[u8] {
pub fn as_slice(&self) -> (&[u8], usize, usize) {
let start = self.offset / 8;
let len = (self.offset() + self.length).saturating_add(7) / 8;
&self.bytes[start..start + len]
let len = (self.offset % 8 + self.length).saturating_add(7) / 8;
(
&self.bytes[start..start + len],
self.offset % 8,
self.length,
)
}
}

Expand All @@ -242,48 +240,3 @@ impl<'a> Bitmap {
BitmapIter::<'a>::new(&self.bytes, self.offset, self.length)
}
}

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

#[test]
fn as_slice() {
let b = Bitmap::from([true, true, true, true, true, true, true, true, true]);

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

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

#[test]
fn as_slice_offset() {
let b = Bitmap::from([true, true, true, true, true, true, true, true, true]);
let b = b.slice(8, 1);

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

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

#[test]
fn test_debug() {
let b = Bitmap::from([true, true, false, true, true, true, true, true, true]);
let b = b.slice(2, 7);

assert_eq!(format!("{:?}", b), "[0b111110__, 0b_______1]");
}
}
190 changes: 10 additions & 180 deletions src/bitmap/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,16 @@ impl MutableBitmap {
/// Extends the [`MutableBitmap`] from a [`Bitmap`].
#[inline]
pub fn extend_from_bitmap(&mut self, bitmap: &Bitmap) {
self.extend_from_slice(bitmap.as_slice(), bitmap.offset(), bitmap.len());
let (slice, offset, length) = bitmap.as_slice();
self.extend_from_slice(slice, offset, length);
}

/// Returns the slice of bytes of this [`MutableBitmap`].
/// Note that the last byte may not be fully used.
#[inline]
pub fn as_slice(&self) -> &[u8] {
let len = (self.length).saturating_add(7) / 8;
&self.buffer[..len]
}
}

Expand All @@ -448,182 +457,3 @@ impl Default for MutableBitmap {
Self::new()
}
}

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

#[test]
fn test_trusted_len() {
let data = vec![true; 65];
let bitmap = MutableBitmap::from_trusted_len_iter(data.into_iter());
let bitmap: Bitmap = bitmap.into();
assert_eq!(bitmap.len(), 65);

assert_eq!(bitmap.as_slice()[8], 0b00000001);
}

#[test]
fn test_trusted_len_small() {
let data = vec![true; 7];
let bitmap = MutableBitmap::from_trusted_len_iter(data.into_iter());
let bitmap: Bitmap = bitmap.into();
assert_eq!(bitmap.len(), 7);

assert_eq!(bitmap.as_slice()[0], 0b01111111);
}

#[test]
fn test_push() {
let mut bitmap = MutableBitmap::new();
bitmap.push(true);
bitmap.push(false);
bitmap.push(false);
for _ in 0..7 {
bitmap.push(true)
}
let bitmap: Bitmap = bitmap.into();
assert_eq!(bitmap.len(), 10);

assert_eq!(bitmap.as_slice(), &[0b11111001, 0b00000011]);
}

#[test]
fn test_push_small() {
let mut bitmap = MutableBitmap::new();
bitmap.push(true);
bitmap.push(true);
bitmap.push(false);
let bitmap: Option<Bitmap> = bitmap.into();
let bitmap = bitmap.unwrap();
assert_eq!(bitmap.len(), 3);
assert_eq!(bitmap.as_slice()[0], 0b00000011);
}

#[test]
fn test_push_exact_zeros() {
let mut bitmap = MutableBitmap::new();
for _ in 0..8 {
bitmap.push(false)
}
let bitmap: Option<Bitmap> = bitmap.into();
let bitmap = bitmap.unwrap();
assert_eq!(bitmap.len(), 8);
assert_eq!(bitmap.as_slice().len(), 1);
}

#[test]
fn test_push_exact_ones() {
let mut bitmap = MutableBitmap::new();
for _ in 0..8 {
bitmap.push(true)
}
let bitmap: Option<Bitmap> = bitmap.into();
assert!(bitmap.is_none());
}

#[test]
fn test_capacity() {
let b = MutableBitmap::with_capacity(10);
assert_eq!(b.capacity(), 512);

let b = MutableBitmap::with_capacity(512);
assert_eq!(b.capacity(), 512);

let mut b = MutableBitmap::with_capacity(512);
b.reserve(8);
assert_eq!(b.capacity(), 512);
}

#[test]
fn test_capacity_push() {
let mut b = MutableBitmap::with_capacity(512);
(0..512).for_each(|_| b.push(true));
assert_eq!(b.capacity(), 512);
b.reserve(8);
assert_eq!(b.capacity(), 1024);
}

#[test]
fn test_extend() {
let mut b = MutableBitmap::new();

let iter = (0..512).map(|i| i % 6 == 0);
unsafe { b.extend_from_trusted_len_iter_unchecked(iter) };
let b: Bitmap = b.into();
for (i, v) in b.iter().enumerate() {
assert_eq!(i % 6 == 0, v);
}
}

#[test]
fn test_extend_offset() {
let mut b = MutableBitmap::new();
b.push(true);

let iter = (0..512).map(|i| i % 6 == 0);
unsafe { b.extend_from_trusted_len_iter_unchecked(iter) };
let b: Bitmap = b.into();
let mut iter = b.iter().enumerate();
assert!(iter.next().unwrap().1);
for (i, v) in iter {
assert_eq!((i - 1) % 6 == 0, v);
}
}

#[test]
fn test_set() {
let mut bitmap = MutableBitmap::from_len_zeroed(12);
bitmap.set(0, true);
assert!(bitmap.get(0));
bitmap.set(0, false);
assert!(!bitmap.get(0));

bitmap.set(11, true);
assert!(bitmap.get(11));
bitmap.set(11, false);
assert!(!bitmap.get(11));
bitmap.set(11, true);

let bitmap: Option<Bitmap> = bitmap.into();
let bitmap = bitmap.unwrap();
assert_eq!(bitmap.len(), 12);
assert_eq!(bitmap.as_slice()[0], 0b00000000);
}

#[test]
fn test_extend_from_bitmap() {
let other = Bitmap::from(&[true, false, true]);
let mut bitmap = MutableBitmap::new();

// call is optimized to perform a memcopy
bitmap.extend_from_bitmap(&other);

assert_eq!(bitmap.len(), 3);
assert_eq!(bitmap.buffer[0], 0b00000101);

// this call iterates over all bits
bitmap.extend_from_bitmap(&other);

assert_eq!(bitmap.len(), 6);
assert_eq!(bitmap.buffer[0], 0b00101101);
}

#[test]
fn test_debug() {
let mut b = MutableBitmap::new();
assert_eq!(format!("{:?}", b), "[]");
b.push(true);
b.push(false);
assert_eq!(format!("{:?}", b), "[0b______01]");
b.push(false);
b.push(false);
b.push(false);
b.push(false);
b.push(true);
b.push(true);
assert_eq!(format!("{:?}", b), "[0b11000001]");
b.push(true);
assert_eq!(format!("{:?}", b), "[0b11000001, 0b_______1]");
}
}
26 changes: 0 additions & 26 deletions src/bitmap/utils/chunk_iterator/chunks_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,3 @@ impl<T: BitChunk> BitChunkIterExact<T> for BitChunksExact<'_, T> {
self.remainder()
}
}

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

#[test]
fn basics() {
let mut iter = BitChunksExact::<u8>::new(&[0b11111111u8, 0b00000001u8], 9);
assert_eq!(iter.next().unwrap(), 0b11111111u8);
assert_eq!(iter.remainder(), 0b00000001u8);
}

#[test]
fn basics_u16_small() {
let mut iter = BitChunksExact::<u16>::new(&[0b11111111u8], 9);
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b0000_0000_1111_1111u16);
}

#[test]
fn basics_u16() {
let mut iter = BitChunksExact::<u16>::new(&[0b11111111u8, 0b00000001u8], 9);
assert_eq!(iter.next(), None);
assert_eq!(iter.remainder(), 0b0000_0001_1111_1111u16);
}
}
Loading

0 comments on commit b77de38

Please sign in to comment.