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 Mar 13, 2022
1 parent 34f9eab commit 6c3bb8d
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 229 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
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 02: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 6c3bb8d

Please sign in to comment.