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 (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Mar 13, 2022
1 parent 7454874 commit 63ca341
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 249 deletions.
11 changes: 11 additions & 0 deletions src/array/primitive/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ impl<T: NativeType> MutablePrimitiveArray<T> {
validity.shrink_to_fit()
}
}

/// Returns the capacity of this [`MutablePrimitiveArray`].
pub fn capacity(&self) -> usize {
self.values.capacity()
}
}

/// Accessors
Expand Down Expand Up @@ -477,6 +482,12 @@ impl<T: NativeType> MutablePrimitiveArray<T> {
}
}

/// Creates a (non-null) [`MutablePrimitiveArray`] from a vector of values.
/// This does not have memcopy and is the fastest way to create a [`PrimitiveArray`].
pub fn from_vec(values: Vec<T>) -> Self {
Self::from_data(T::PRIMITIVE.into(), values, None)
}

/// Creates a new [`MutablePrimitiveArray`] from an iterator over values
/// # Safety
/// The iterator must be [`TrustedLen`](https://doc.rust-lang.org/std/iter/trait.TrustedLen.html).
Expand Down
42 changes: 24 additions & 18 deletions src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,24 +341,30 @@ impl<O: Offset> Utf8Array<O> {
}
} else {
match (self.values.into_mut(), self.offsets.into_mut()) {
(Left(immutable_values), Left(immutable_offsets)) => Left(Utf8Array::from_data(
self.data_type,
immutable_offsets,
immutable_values,
None,
)),
(Left(immutable_values), Right(mutable_offsets)) => Left(Utf8Array::from_data(
self.data_type,
mutable_offsets.into(),
immutable_values,
None,
)),
(Right(mutable_values), Left(immutable_offsets)) => Left(Utf8Array::from_data(
self.data_type,
immutable_offsets,
mutable_values.into(),
None,
)),
(Left(immutable_values), Left(immutable_offsets)) => Left(unsafe {
Utf8Array::new_unchecked(
self.data_type,
immutable_offsets,
immutable_values,
None,
)
}),
(Left(immutable_values), Right(mutable_offsets)) => Left(unsafe {
Utf8Array::new_unchecked(
self.data_type,
mutable_offsets.into(),
immutable_values,
None,
)
}),
(Right(mutable_values), Left(immutable_offsets)) => Left(unsafe {
Utf8Array::from_data(
self.data_type,
immutable_offsets,
mutable_values.into(),
None,
)
}),
(Right(mutable_values), Right(mutable_offsets)) => {
Right(MutableUtf8Array::from_data(
self.data_type,
Expand Down
34 changes: 33 additions & 1 deletion tests/it/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn basics() {
#[test]
fn with_validity() {
let values = Buffer::from_slice([1, 2, 3, 4, 5, 6]);
let a = FixedSizeBinaryArray::from_data(DataType::FixedSizeBinary(2), values, None);
let a = FixedSizeBinaryArray::new(DataType::FixedSizeBinary(2), values, None);
let a = a.with_validity(Some(Bitmap::from([true, false, true])));
assert!(a.validity().is_some());
}
Expand Down Expand Up @@ -56,3 +56,35 @@ fn from_iter() {
let a = FixedSizeBinaryArray::from_iter(iter, 2);
assert_eq!(a.len(), 2);
}

#[test]
fn wrong_size() {
let values = Buffer::from_slice(b"abb");
assert!(FixedSizeBinaryArray::try_new(DataType::FixedSizeBinary(2), values, None).is_err());
}

#[test]
fn wrong_len() {
let values = Buffer::from_slice(b"abba");
let validity = Some([true, false, false].into()); // it should be 2
assert!(FixedSizeBinaryArray::try_new(DataType::FixedSizeBinary(2), values, validity).is_err());
}

#[test]
fn wrong_data_type() {
let values = Buffer::from_slice(b"abba");
assert!(FixedSizeBinaryArray::try_new(DataType::Binary, values, None).is_err());
}

#[test]
fn to() {
let values = Buffer::from_slice(b"abba");
let a = FixedSizeBinaryArray::new(DataType::FixedSizeBinary(2), values, None);

let extension = DataType::Extension(
"a".to_string(),
Box::new(DataType::FixedSizeBinary(2)),
None,
);
let _ = a.to(extension);
}
226 changes: 226 additions & 0 deletions tests/it/array/primitive/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
use arrow2::{
array::*,
datatypes::*,
types::{days_ms, months_days_ns},
};

#[test]
fn debug_int32() {
let array = Int32Array::from(&[Some(1), None, Some(2)]);
assert_eq!(format!("{:?}", array), "Int32[1, None, 2]");
}

#[test]
fn debug_date32() {
let array = Int32Array::from(&[Some(1), None, Some(2)]).to(DataType::Date32);
assert_eq!(
format!("{:?}", array),
"Date32[1970-01-02, None, 1970-01-03]"
);
}

#[test]
fn debug_time32s() {
let array = Int32Array::from(&[Some(1), None, Some(2)]).to(DataType::Time32(TimeUnit::Second));
assert_eq!(
format!("{:?}", array),
"Time32(Second)[00:00:01, None, 00:00:02]"
);
}

#[test]
fn debug_time32ms() {
let array =
Int32Array::from(&[Some(1), None, Some(2)]).to(DataType::Time32(TimeUnit::Millisecond));
assert_eq!(
format!("{:?}", array),
"Time32(Millisecond)[00:00:00.001, None, 00:00:00.002]"
);
}

#[test]
fn debug_interval_d() {
let array =
Int32Array::from(&[Some(1), None, Some(2)]).to(DataType::Interval(IntervalUnit::YearMonth));
assert_eq!(format!("{:?}", array), "Interval(YearMonth)[1m, None, 2m]");
}

#[test]
fn debug_int64() {
let array = Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Int64);
assert_eq!(format!("{:?}", array), "Int64[1, None, 2]");
}

#[test]
fn debug_date64() {
let array = Int64Array::from(&[Some(1), None, Some(86400000)]).to(DataType::Date64);
assert_eq!(
format!("{:?}", array),
"Date64[1970-01-01, None, 1970-01-02]"
);
}

#[test]
fn debug_time64us() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Time64(TimeUnit::Microsecond));
assert_eq!(
format!("{:?}", array),
"Time64(Microsecond)[00:00:00.000001, None, 00:00:00.000002]"
);
}

#[test]
fn debug_time64ns() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Time64(TimeUnit::Nanosecond));
assert_eq!(
format!("{:?}", array),
"Time64(Nanosecond)[00:00:00.000000001, None, 00:00:00.000000002]"
);
}

#[test]
fn debug_timestamp_s() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Timestamp(TimeUnit::Second, None));
assert_eq!(
format!("{:?}", array),
"Timestamp(Second, None)[1970-01-01 00:00:01, None, 1970-01-01 00:00:02]"
);
}

#[test]
fn debug_timestamp_ms() {
let array = Int64Array::from(&[Some(1), None, Some(2)])
.to(DataType::Timestamp(TimeUnit::Millisecond, None));
assert_eq!(
format!("{:?}", array),
"Timestamp(Millisecond, None)[1970-01-01 00:00:00.001, None, 1970-01-01 00:00:00.002]"
);
}

#[test]
fn debug_timestamp_us() {
let array = Int64Array::from(&[Some(1), None, Some(2)])
.to(DataType::Timestamp(TimeUnit::Microsecond, None));
assert_eq!(
format!("{:?}", array),
"Timestamp(Microsecond, None)[1970-01-01 00:00:00.000001, None, 1970-01-01 00:00:00.000002]"
);
}

#[test]
fn debug_timestamp_ns() {
let array = Int64Array::from(&[Some(1), None, Some(2)])
.to(DataType::Timestamp(TimeUnit::Nanosecond, None));
assert_eq!(
format!("{:?}", array),
"Timestamp(Nanosecond, None)[1970-01-01 00:00:00.000000001, None, 1970-01-01 00:00:00.000000002]"
);
}

#[test]
fn debug_timestamp_tz_ns() {
let array = Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Timestamp(
TimeUnit::Nanosecond,
Some("+02:00".to_string()),
));
assert_eq!(
format!("{:?}", array),
"Timestamp(Nanosecond, Some(\"+02:00\"))[1970-01-01 02:00:00.000000001 +02:00, None, 1970-01-01 02:00:00.000000002 +02:00]"
);
}

#[cfg(feature = "chrono-tz")]
#[test]
fn debug_timestamp_tz1_ns() {
let array = Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Timestamp(
TimeUnit::Nanosecond,
Some("Europe/Lisbon".to_string()),
));
assert_eq!(
format!("{:?}", array),
"Timestamp(Nanosecond, Some(\"Europe/Lisbon\"))[1970-01-01 01:00:00.000000001 CET, None, 1970-01-01 01:00:00.000000002 CET]"
);
}

#[test]
fn debug_duration_ms() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Duration(TimeUnit::Millisecond));
assert_eq!(
format!("{:?}", array),
"Duration(Millisecond)[1ms, None, 2ms]"
);
}

#[test]
fn debug_duration_s() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Duration(TimeUnit::Second));
assert_eq!(format!("{:?}", array), "Duration(Second)[1s, None, 2s]");
}

#[test]
fn debug_duration_us() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Duration(TimeUnit::Microsecond));
assert_eq!(
format!("{:?}", array),
"Duration(Microsecond)[1us, None, 2us]"
);
}

#[test]
fn debug_duration_ns() {
let array =
Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Duration(TimeUnit::Nanosecond));
assert_eq!(
format!("{:?}", array),
"Duration(Nanosecond)[1ns, None, 2ns]"
);
}

#[test]
fn debug_decimal() {
let array = Int128Array::from(&[Some(12345), None, Some(23456)]).to(DataType::Decimal(5, 2));
assert_eq!(
format!("{:?}", array),
"Decimal(5, 2)[123.45, None, 234.56]"
);
}

#[test]
fn debug_decimal1() {
let array = Int128Array::from(&[Some(12345), None, Some(23456)]).to(DataType::Decimal(5, 1));
assert_eq!(
format!("{:?}", array),
"Decimal(5, 1)[1234.5, None, 2345.6]"
);
}

#[test]
fn debug_interval_days_ms() {
let array = DaysMsArray::from(&[Some(days_ms::new(1, 1)), None, Some(days_ms::new(2, 2))]);
assert_eq!(
format!("{:?}", array),
"Interval(DayTime)[1d1ms, None, 2d2ms]"
);
}

#[test]
fn debug_months_days_ns() {
let data = &[
Some(months_days_ns::new(1, 1, 2)),
None,
Some(months_days_ns::new(2, 3, 3)),
];

let array = MonthsDaysNsArray::from(&data);

assert_eq!(
format!("{:?}", array),
"Interval(MonthDayNano)[1m1d2ns, None, 2m3d3ns]"
);
}
Loading

0 comments on commit 63ca341

Please sign in to comment.