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

Commit

Permalink
Added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Aug 19, 2021
1 parent 2d3acea commit a938782
Show file tree
Hide file tree
Showing 15 changed files with 272 additions and 139 deletions.
32 changes: 0 additions & 32 deletions src/scalar/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,3 @@ impl<O: Offset> Scalar for BinaryScalar<O> {
}
}
}

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

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = BinaryScalar::<i32>::from(Some("a"));
let b = BinaryScalar::<i32>::from(None::<&str>);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = BinaryScalar::<i32>::from(Some("b"));
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let a = BinaryScalar::<i32>::from(Some("a"));

assert_eq!(a.value(), b"a");
assert_eq!(a.data_type(), &DataType::Binary);
assert!(a.is_valid());

let a = BinaryScalar::<i64>::from(None::<&str>);

assert_eq!(a.data_type(), &DataType::LargeBinary);
assert!(!a.is_valid());
}
}
27 changes: 0 additions & 27 deletions src/scalar/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,3 @@ impl From<Option<bool>> for BooleanScalar {
Self::new(v)
}
}

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

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = BooleanScalar::from(Some(true));
let b = BooleanScalar::from(None);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = BooleanScalar::from(Some(false));
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let a = BooleanScalar::new(Some(true));

assert!(a.value());
assert_eq!(a.data_type(), &DataType::Boolean);
assert!(a.is_valid());
}
}
14 changes: 7 additions & 7 deletions src/scalar/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::Scalar;
/// [`Array`]. The only difference is that this has only one element.
#[derive(Debug, Clone)]
pub struct ListScalar<O: Offset> {
values: Arc<dyn Array>,
value: Arc<dyn Array>,
is_valid: bool,
phantom: std::marker::PhantomData<O>,
data_type: DataType,
Expand All @@ -19,7 +19,7 @@ impl<O: Offset> PartialEq for ListScalar<O> {
fn eq(&self, other: &Self) -> bool {
(self.data_type == other.data_type)
&& (self.is_valid == other.is_valid)
&& ((!self.is_valid) | (self.values.as_ref() == other.values.as_ref()))
&& ((!self.is_valid) | (self.value.as_ref() == other.value.as_ref()))
}
}

Expand All @@ -29,25 +29,25 @@ impl<O: Offset> ListScalar<O> {
/// * the `data_type` is not `List` or `LargeList` (depending on this scalar's offset `O`)
/// * the child of the `data_type` is not equal to the `values`
#[inline]
pub fn new(data_type: DataType, values: Option<Arc<dyn Array>>) -> Self {
pub fn new(data_type: DataType, value: Option<Arc<dyn Array>>) -> Self {
let inner_data_type = ListArray::<O>::get_child_type(&data_type);
let (is_valid, values) = match values {
let (is_valid, value) = match value {
Some(values) => {
assert_eq!(inner_data_type, values.data_type());
(true, values)
}
None => (false, new_empty_array(inner_data_type.clone()).into()),
};
Self {
values,
value,
is_valid,
phantom: std::marker::PhantomData,
data_type,
}
}

pub fn values(&self) -> &Arc<dyn Array> {
&self.values
pub fn value(&self) -> &Arc<dyn Array> {
&self.value
}
}

Expand Down
38 changes: 0 additions & 38 deletions src/scalar/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,3 @@ impl<T: NativeType> Scalar for PrimitiveScalar<T> {
&self.data_type
}
}

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

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = PrimitiveScalar::from(Some(2i32));
let b = PrimitiveScalar::<i32>::from(None);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = PrimitiveScalar::<i32>::from(Some(1i32));
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let a = PrimitiveScalar::from(Some(2i32));

assert_eq!(a.value(), 2i32);
assert_eq!(a.data_type(), &DataType::Int32);
assert!(a.is_valid());

let a = a.to(DataType::Date32);
assert_eq!(a.data_type(), &DataType::Date32);

let a = PrimitiveScalar::<i32>::from(None);

assert_eq!(a.data_type(), &DataType::Int32);
assert!(!a.is_valid());

let a = a.to(DataType::Date32);
assert_eq!(a.data_type(), &DataType::Date32);
}
}
32 changes: 0 additions & 32 deletions src/scalar/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,3 @@ impl<O: Offset> Scalar for Utf8Scalar<O> {
}
}
}

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

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = Utf8Scalar::<i32>::from(Some("a"));
let b = Utf8Scalar::<i32>::from(None::<&str>);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = Utf8Scalar::<i32>::from(Some("b"));
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let a = Utf8Scalar::<i32>::from(Some("a"));

assert_eq!(a.value(), "a");
assert_eq!(a.data_type(), &DataType::Utf8);
assert!(a.is_valid());

let a = Utf8Scalar::<i64>::from(None::<&str>);

assert_eq!(a.data_type(), &DataType::LargeUtf8);
assert!(!a.is_valid());
}
}
19 changes: 16 additions & 3 deletions tests/it/array/boolean/mutable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use arrow2::array::{MutableArray, MutableBooleanArray};
use arrow2::bitmap::MutableBitmap;
use arrow2::error::Result;

#[test]
Expand All @@ -10,7 +11,8 @@ fn set() {
assert_eq!(
a,
MutableBooleanArray::from([Some(true), None, Some(false)])
)
);
assert_eq!(a.values(), &MutableBitmap::from([true, false, false]));
}

#[test]
Expand Down Expand Up @@ -42,7 +44,18 @@ fn from_iter() {

#[test]
fn try_from_trusted_len_iter() {
let iter = std::iter::repeat(Some(true)).take(2).map(Result::Ok);
let iter = vec![Some(true), Some(true), None]
.into_iter()
.map(Result::Ok);
let a = MutableBooleanArray::try_from_trusted_len_iter(iter).unwrap();
assert_eq!(a, MutableBooleanArray::from([Some(true), Some(true)]));
assert_eq!(a, MutableBooleanArray::from([Some(true), Some(true), None]));
}

#[test]
fn reserve() {
let mut a = MutableBooleanArray::from_data(MutableBitmap::new(), Some(MutableBitmap::new()));

a.reserve(10);
assert!(a.validity().as_ref().unwrap().capacity() > 0);
assert!(a.values().capacity() > 0)
}
1 change: 1 addition & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod array;
mod bitmap;
mod buffer;
mod ffi;
mod scalar;

mod io;
mod test_util;
Expand Down
33 changes: 33 additions & 0 deletions tests/it/scalar/binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use arrow2::{
datatypes::DataType,
scalar::{BinaryScalar, Scalar},
};

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = BinaryScalar::<i32>::from(Some("a"));
let b = BinaryScalar::<i32>::from(None::<&str>);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = BinaryScalar::<i32>::from(Some("b"));
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let a = BinaryScalar::<i32>::from(Some("a"));

assert_eq!(a.value(), b"a");
assert_eq!(a.data_type(), &DataType::Binary);
assert!(a.is_valid());

let a = BinaryScalar::<i64>::from(None::<&str>);

assert_eq!(a.data_type(), &DataType::LargeBinary);
assert!(!a.is_valid());

let _: &dyn std::any::Any = a.as_any();
}
28 changes: 28 additions & 0 deletions tests/it/scalar/boolean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use arrow2::{
datatypes::DataType,
scalar::{BooleanScalar, Scalar},
};

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = BooleanScalar::from(Some(true));
let b = BooleanScalar::from(None);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = BooleanScalar::from(Some(false));
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let a = BooleanScalar::new(Some(true));

assert!(a.value());
assert_eq!(a.data_type(), &DataType::Boolean);
assert!(a.is_valid());

let _: &dyn std::any::Any = a.as_any();
}
42 changes: 42 additions & 0 deletions tests/it/scalar/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::sync::Arc;

use arrow2::{
array::{Array, BooleanArray},
datatypes::{DataType, Field},
scalar::{ListScalar, Scalar},
};

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let dt = DataType::List(Box::new(Field::new("a", DataType::Boolean, true)));
let a = ListScalar::<i32>::new(
dt.clone(),
Some(Arc::new(BooleanArray::from_slice([true, false])) as Arc<dyn Array>),
);
let b = ListScalar::<i32>::new(dt.clone(), None);
assert_eq!(a, a);
assert_eq!(b, b);
assert!(a != b);
let b = ListScalar::<i32>::new(
dt,
Some(Arc::new(BooleanArray::from_slice([true, true])) as Arc<dyn Array>),
);
assert!(a != b);
assert_eq!(b, b);
}

#[test]
fn basics() {
let dt = DataType::List(Box::new(Field::new("a", DataType::Boolean, true)));
let a = ListScalar::<i32>::new(
dt.clone(),
Some(Arc::new(BooleanArray::from_slice([true, false])) as Arc<dyn Array>),
);

assert_eq!(BooleanArray::from_slice([true, false]), a.value().as_ref());
assert_eq!(a.data_type(), &dt);
assert!(a.is_valid());

let _: &dyn std::any::Any = a.as_any();
}
7 changes: 7 additions & 0 deletions tests/it/scalar/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod binary;
mod boolean;
mod list;
mod null;
mod primitive;
mod struct_;
mod utf8;
21 changes: 21 additions & 0 deletions tests/it/scalar/null.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use arrow2::{
datatypes::DataType,
scalar::{NullScalar, Scalar},
};

#[allow(clippy::eq_op)]
#[test]
fn equal() {
let a = NullScalar::new();
assert_eq!(a, a);
}

#[test]
fn basics() {
let a = NullScalar::default();

assert_eq!(a.data_type(), &DataType::Null);
assert!(!a.is_valid());

let _: &dyn std::any::Any = a.as_any();
}
Loading

0 comments on commit a938782

Please sign in to comment.