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

Added more tests #601

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType, error::Result};

use super::{display_fmt, display_helper, Array};
use super::{display_fmt, Array};

mod ffi;
mod iterator;
Expand Down Expand Up @@ -122,7 +122,8 @@ impl FixedSizeBinaryArray {
/// Panics iff `i >= self.len()`.
#[inline]
pub fn value(&self, i: usize) -> &[u8] {
&self.values()[i * self.size as usize..(i + 1) * self.size as usize]
assert!(i < self.len());
unsafe { self.value_unchecked(i) }
}

/// Returns the element at index `i` as &str
Expand Down Expand Up @@ -183,8 +184,7 @@ impl Array for FixedSizeBinaryArray {

impl std::fmt::Display for FixedSizeBinaryArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let a = |x: &[u8]| display_helper(x.iter().map(|x| Some(format!("{:b}", x)))).join(" ");
let iter = self.iter().map(|x| x.map(a));
let iter = self.iter().map(|x| x.map(|x| format!("{:?}", x)));
display_fmt(iter, "FixedSizeBinaryArray", f, false)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/array/list/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl<O: Offset, M: MutableArray + Default> MutableListArray<O, M> {

let mut offsets = MutableBuffer::<O>::with_capacity(capacity + 1);
offsets.push(O::default());
assert_eq!(values.len(), 0);
Self {
data_type,
offsets,
Expand Down Expand Up @@ -155,6 +154,11 @@ impl<O: Offset, M: MutableArray> MutableListArray<O, M> {
&mut self.values
}

/// The offseta
pub fn offsets(&self) -> &MutableBuffer<O> {
&self.offsets
}

/// The values
pub fn values(&self) -> &M {
&self.values
Expand Down
14 changes: 14 additions & 0 deletions tests/it/array/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ fn basics() {
assert!(!array.value(1));
}

#[test]
fn with_validity() {
let bitmap = Bitmap::from([true, false, true]);
let a = BooleanArray::from_data(DataType::Boolean, bitmap, None);
let a = a.with_validity(Some(Bitmap::from([true, false, true])));
assert!(a.validity().is_some());
}

#[test]
fn display() {
let array = BooleanArray::from([Some(true), None, Some(false)]);
assert_eq!(format!("{}", array), "BooleanArray[true, , false]");
}

#[test]
fn empty() {
let array = BooleanArray::new_empty(DataType::Boolean);
Expand Down
54 changes: 54 additions & 0 deletions tests/it/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
use arrow2::{array::FixedSizeBinaryArray, bitmap::Bitmap, buffer::Buffer, datatypes::DataType};

mod mutable;

#[test]
fn basics() {
let array = FixedSizeBinaryArray::from_data(
DataType::FixedSizeBinary(2),
Buffer::from([1, 2, 3, 4, 5, 6]),
Some(Bitmap::from([true, false, true])),
);
assert_eq!(array.size(), 2);
assert_eq!(array.len(), 3);
assert_eq!(array.validity(), Some(&Bitmap::from([true, false, true])));

assert_eq!(array.value(0), [1, 2]);
assert_eq!(array.value(2), [5, 6]);

let array = array.slice(1, 2);

assert_eq!(array.value(1), [5, 6]);
}

#[test]
fn with_validity() {
let values = Buffer::from([1, 2, 3, 4, 5, 6]);
let a = FixedSizeBinaryArray::from_data(DataType::FixedSizeBinary(2), values, None);
let a = a.with_validity(Some(Bitmap::from([true, false, true])));
assert!(a.validity().is_some());
}

#[test]
fn display() {
let values = Buffer::from([1, 2, 3, 4, 5, 6]);
let a = FixedSizeBinaryArray::from_data(
DataType::FixedSizeBinary(2),
values,
Some(Bitmap::from([true, false, true])),
);
assert_eq!(format!("{}", a), "FixedSizeBinaryArray[[1, 2], , [5, 6]]");
}

#[test]
fn empty() {
let array = FixedSizeBinaryArray::new_empty(DataType::FixedSizeBinary(2));
assert_eq!(array.values().len(), 0);
assert_eq!(array.validity(), None);
}

#[test]
fn from_iter() {
let iter = std::iter::repeat(vec![1u8, 2]).take(2).map(Some);
let a = FixedSizeBinaryArray::from_iter(iter, 2);
assert_eq!(a.len(), 2);
}
13 changes: 13 additions & 0 deletions tests/it/array/list/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,23 @@ fn basics() {
assert_eq!(expected, array);
}

#[test]
fn with_capacity() {
let array = MutableListArray::<i32, MutablePrimitiveArray<i32>>::with_capacity(10);
assert!(array.offsets().capacity() >= 10);
assert_eq!(array.offsets().len(), 1);
assert_eq!(array.values().values().capacity(), 0);
assert_eq!(array.validity(), None);
}

#[test]
fn push() {
let mut array = MutableListArray::<i32, MutablePrimitiveArray<i32>>::new();
array
.try_push(Some(vec![Some(1i32), Some(2), Some(3)]))
.unwrap();
assert_eq!(array.len(), 1);
assert_eq!(array.values().values().as_ref(), [1, 2, 3]);
assert_eq!(array.offsets().as_ref(), [0, 3]);
assert_eq!(array.validity(), None);
}