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

Commit

Permalink
Added tests to scalar API (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Aug 19, 2021
1 parent 8ff8f7d commit bbe607c
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 139 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Arrow2: Transmute-free Arrow

[![test](/~https://github.com/jorgecarleitao/arrow2/actions/workflows/test.yml/badge.svg)](/~https://github.com/jorgecarleitao/arrow2/actions/workflows/Build.yml)
[![codecov](https://codecov.io/gh/jorgecarleitao/arrow2/branch/main/graph/badge.svg?token=AgyTF60R3D)](https://codecov.io/gh/jorgecarleitao/arrow2)
[![](https://img.shields.io/crates/d/arrow2.svg)](https://crates.io/crates/arrow2)
[![](https://img.shields.io/crates/dv/arrow2.svg)](https://crates.io/crates/arrow2)
[![](https://docs.rs/arrow2/badge.svg)](https://docs.rs/arrow2/)
Expand Down
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());
}
}
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)
}
24 changes: 17 additions & 7 deletions tests/it/array/growable/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,32 @@ fn many() {

let array = StructArray::from_data(fields.clone(), values.clone(), None);

let mut mutable = GrowableStruct::new(vec![&array, &array], false, 0);
let mut mutable = GrowableStruct::new(vec![&array, &array], true, 0);

mutable.extend(0, 1, 2);
mutable.extend(1, 0, 2);
let result: StructArray = mutable.into();
mutable.extend_validity(1);
let result = mutable.as_box();

let expected_string: Arc<dyn Array> = Arc::new(Utf8Array::<i32>::from(&[
Some("aa"),
None,
Some("a"),
Some("aa"),
None,
]));
let expected_int: Arc<dyn Array> = Arc::new(PrimitiveArray::<i32>::from(vec![
Some(2),
Some(3),
Some(1),
Some(2),
None,
]));
let expected_int: Arc<dyn Array> = Arc::new(
PrimitiveArray::<i32>::from(vec![Some(2), Some(3), Some(1), Some(2)]).to(DataType::Int32),
);

let expected = StructArray::from_data(fields, vec![expected_string, expected_int], None);
assert_eq!(result, expected)
let expected = StructArray::from_data(
fields,
vec![expected_string, expected_int],
Some(Bitmap::from([true, true, true, true, false])),
);
assert_eq!(expected, result.as_ref())
}
2 changes: 2 additions & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ mod array;
mod bitmap;
mod buffer;
mod ffi;
mod scalar;

mod io;
mod test_util;

#[cfg(feature = "compute")]
mod compute;
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.values().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 bbe607c

Please sign in to comment.