From 873832e8c4f087a9dd0b0e88d4af7895b33602f0 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Mon, 27 Jun 2022 18:10:55 +0000 Subject: [PATCH 1/2] Added more tests --- src/array/utf8/mod.rs | 6 ++---- tests/it/array/utf8/mod.rs | 18 ++++++++++++++++++ tests/it/array/utf8/mutable.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/array/utf8/mod.rs b/src/array/utf8/mod.rs index b6b56e14ccb..69c5af6e8f0 100644 --- a/src/array/utf8/mod.rs +++ b/src/array/utf8/mod.rs @@ -486,8 +486,7 @@ impl Utf8Array { P: AsRef, I: TrustedLen>, { - // soundness: I is `TrustedLen` - unsafe { Self::from_trusted_len_iter_unchecked(iterator) } + MutableUtf8Array::::from_trusted_len_iter(iterator).into() } /// Creates a [`Utf8Array`] from an falible iterator of trusted length. @@ -512,8 +511,7 @@ impl Utf8Array { P: AsRef, I: TrustedLen, E>>, { - // soundness: I: TrustedLen - unsafe { Self::try_from_trusted_len_iter_unchecked(iter) } + MutableUtf8Array::::try_from_trusted_len_iter(iter).map(|x| x.into()) } /// Alias for `new` diff --git a/tests/it/array/utf8/mod.rs b/tests/it/array/utf8/mod.rs index fc55ebccdec..c6a0acec03f 100644 --- a/tests/it/array/utf8/mod.rs +++ b/tests/it/array/utf8/mod.rs @@ -215,3 +215,21 @@ fn into_mut_4() { let array = Utf8Array::::new(DataType::Utf8, offsets, values, validity); assert!(array.into_mut().is_right()); } + +#[test] +fn rev_iter() { + let array = Utf8Array::::from(&[Some("hello"), Some(" "), None]); + + assert_eq!( + array.into_iter().rev().collect::>(), + vec![None, Some(" "), Some("hello")] + ); +} + +#[test] +fn iter_nth() { + let array = Utf8Array::::from(&[Some("hello"), Some(" "), None]); + + assert_eq!(array.iter().nth(1), Some(Some(" "))); + assert_eq!(array.iter().nth(10), None); +} diff --git a/tests/it/array/utf8/mutable.rs b/tests/it/array/utf8/mutable.rs index 8ee76476cbb..80cc24ca3c8 100644 --- a/tests/it/array/utf8/mutable.rs +++ b/tests/it/array/utf8/mutable.rs @@ -141,3 +141,29 @@ fn test_extend_values() { assert_eq!(array.offsets().as_slice(), &[0, 2, 7, 12, 17]); assert_eq!(array.validity(), None,); } + +#[test] +fn test_extend() { + let mut array = MutableUtf8Array::::new(); + + array.extend([Some("hi"), None, Some("there"), None].into_iter()); + + let array: Utf8Array = array.into(); + + assert_eq!( + array, + Utf8Array::::from([Some("hi"), None, Some("there"), None]) + ); +} + +#[test] +fn as_arc() { + let mut array = MutableUtf8Array::::new(); + + array.extend([Some("hi"), None, Some("there"), None].into_iter()); + + assert_eq!( + Utf8Array::::from([Some("hi"), None, Some("there"), None]), + array.as_arc().as_ref() + ); +} From c2d01f5c392b2052662c66afee06bab1c1d90fa3 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Mon, 27 Jun 2022 20:51:27 +0000 Subject: [PATCH 2/2] Removed to_ne_bytes --- src/types/native.rs | 69 +++++---------------------------- tests/it/array/primitive/mod.rs | 2 +- tests/it/types.rs | 22 ++++++++++- 3 files changed, 32 insertions(+), 61 deletions(-) diff --git a/src/types/native.rs b/src/types/native.rs index fbc8ac408b3..4d4f34eb602 100644 --- a/src/types/native.rs +++ b/src/types/native.rs @@ -33,9 +33,6 @@ pub trait NativeType: /// To bytes in little endian fn to_le_bytes(&self) -> Self::Bytes; - /// To bytes in native endian - fn to_ne_bytes(&self) -> Self::Bytes; - /// To bytes in big endian fn to_be_bytes(&self) -> Self::Bytes; @@ -59,11 +56,6 @@ macro_rules! native_type { Self::to_be_bytes(*self) } - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - Self::to_ne_bytes(*self) - } - #[inline] fn from_be_bytes(bytes: Self::Bytes) -> Self { Self::from_be_bytes(bytes) @@ -88,25 +80,25 @@ native_type!(i128, PrimitiveType::Int128); #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, Zeroable, Pod)] #[allow(non_camel_case_types)] #[repr(C)] -pub struct days_ms([i32; 2]); +pub struct days_ms(pub i32, pub i32); impl days_ms { /// A new [`days_ms`]. #[inline] pub fn new(days: i32, milliseconds: i32) -> Self { - Self([days, milliseconds]) + Self(days, milliseconds) } /// The number of days #[inline] pub fn days(&self) -> i32 { - self.0[0] + self.0 } /// The number of milliseconds #[inline] pub fn milliseconds(&self) -> i32 { - self.0[1] + self.1 } } @@ -115,24 +107,8 @@ impl NativeType for days_ms { type Bytes = [u8; 8]; #[inline] fn to_le_bytes(&self) -> Self::Bytes { - let days = self.0[0].to_le_bytes(); - let ms = self.0[1].to_le_bytes(); - let mut result = [0; 8]; - result[0] = days[0]; - result[1] = days[1]; - result[2] = days[2]; - result[3] = days[3]; - result[4] = ms[0]; - result[5] = ms[1]; - result[6] = ms[2]; - result[7] = ms[3]; - result - } - - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - let days = self.0[0].to_ne_bytes(); - let ms = self.0[1].to_ne_bytes(); + let days = self.0.to_le_bytes(); + let ms = self.1.to_le_bytes(); let mut result = [0; 8]; result[0] = days[0]; result[1] = days[1]; @@ -147,8 +123,8 @@ impl NativeType for days_ms { #[inline] fn to_be_bytes(&self) -> Self::Bytes { - let days = self.0[0].to_be_bytes(); - let ms = self.0[1].to_be_bytes(); + let days = self.0.to_be_bytes(); + let ms = self.1.to_be_bytes(); let mut result = [0; 8]; result[0] = days[0]; result[1] = days[1]; @@ -173,7 +149,7 @@ impl NativeType for days_ms { ms[1] = bytes[5]; ms[2] = bytes[6]; ms[3] = bytes[7]; - Self([i32::from_be_bytes(days), i32::from_be_bytes(ms)]) + Self(i32::from_be_bytes(days), i32::from_be_bytes(ms)) } } @@ -181,7 +157,7 @@ impl NativeType for days_ms { #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, Zeroable, Pod)] #[allow(non_camel_case_types)] #[repr(C)] -pub struct months_days_ns(i32, i32, i64); +pub struct months_days_ns(pub i32, pub i32, pub i64); impl months_days_ns { /// A new [`months_days_ns`]. @@ -232,26 +208,6 @@ impl NativeType for months_days_ns { result } - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - let months = self.months().to_ne_bytes(); - let days = self.days().to_ne_bytes(); - let ns = self.ns().to_ne_bytes(); - let mut result = [0; 16]; - result[0] = months[0]; - result[1] = months[1]; - result[2] = months[2]; - result[3] = months[3]; - result[4] = days[0]; - result[5] = days[1]; - result[6] = days[2]; - result[7] = days[3]; - (0..8).for_each(|i| { - result[8 + i] = ns[i]; - }); - result - } - #[inline] fn to_be_bytes(&self) -> Self::Bytes { let months = self.months().to_be_bytes(); @@ -420,11 +376,6 @@ impl NativeType for f16 { self.0.to_le_bytes() } - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - self.0.to_ne_bytes() - } - #[inline] fn to_be_bytes(&self) -> Self::Bytes { self.0.to_be_bytes() diff --git a/tests/it/array/primitive/mod.rs b/tests/it/array/primitive/mod.rs index 058d503f479..5a618cba256 100644 --- a/tests/it/array/primitive/mod.rs +++ b/tests/it/array/primitive/mod.rs @@ -72,7 +72,7 @@ fn from() { } #[test] -fn months_days_ns() { +fn months_days_ns_from_slice() { let data = &[ months_days_ns::new(1, 1, 2), months_days_ns::new(1, 1, 3), diff --git a/tests/it/types.rs b/tests/it/types.rs index 7dfa07cc167..ee337e901fb 100644 --- a/tests/it/types.rs +++ b/tests/it/types.rs @@ -1,4 +1,4 @@ -use arrow2::types::{BitChunkIter, BitChunkOnes}; +use arrow2::types::{days_ms, months_days_ns, BitChunkIter, BitChunkOnes, NativeType}; #[test] fn test_basic1() { @@ -18,3 +18,23 @@ fn test_ones() { assert_eq!(iter.next(), Some(0)); assert_eq!(iter.next(), Some(12)); } + +#[test] +fn months_days_ns_roundtrip() { + let a = months_days_ns(1, 2, 3); + let bytes = a.to_le_bytes(); + assert_eq!(bytes, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0]); + + let a = months_days_ns(1, 1, 1); + assert_eq!(a, months_days_ns::from_be_bytes(a.to_be_bytes())); +} + +#[test] +fn days_ms_roundtrip() { + let a = days_ms(1, 2); + let bytes = a.to_le_bytes(); + assert_eq!(bytes, [1, 0, 0, 0, 2, 0, 0, 0]); + + let a = days_ms(1, 2); + assert_eq!(a, days_ms::from_be_bytes(a.to_be_bytes())); +}