From 32041eeefa790c79c8c72a45bd9bb2ac5ad6ae96 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Fri, 12 Nov 2021 05:41:04 +0000 Subject: [PATCH 1/2] Added more tests. --- src/array/fixed_size_binary/mod.rs | 8 ++-- tests/it/array/boolean/mod.rs | 14 +++++++ tests/it/array/fixed_size_binary/mod.rs | 54 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/array/fixed_size_binary/mod.rs b/src/array/fixed_size_binary/mod.rs index c7bf5e30a03..9baad61a504 100644 --- a/src/array/fixed_size_binary/mod.rs +++ b/src/array/fixed_size_binary/mod.rs @@ -1,6 +1,6 @@ use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType, error::Result}; -use super::{display_fmt, display_helper, Array}; +use super::{display_fmt, Array}; mod ffi; mod iterator; @@ -122,7 +122,8 @@ impl FixedSizeBinaryArray { /// Panics iff `i >= self.len()`. #[inline] pub fn value(&self, i: usize) -> &[u8] { - &self.values()[i * self.size as usize..(i + 1) * self.size as usize] + assert!(i < self.len()); + unsafe { self.value_unchecked(i) } } /// Returns the element at index `i` as &str @@ -183,8 +184,7 @@ impl Array for FixedSizeBinaryArray { impl std::fmt::Display for FixedSizeBinaryArray { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let a = |x: &[u8]| display_helper(x.iter().map(|x| Some(format!("{:b}", x)))).join(" "); - let iter = self.iter().map(|x| x.map(a)); + let iter = self.iter().map(|x| x.map(|x| format!("{:?}", x))); display_fmt(iter, "FixedSizeBinaryArray", f, false) } } diff --git a/tests/it/array/boolean/mod.rs b/tests/it/array/boolean/mod.rs index 454414ac947..84decd124f8 100644 --- a/tests/it/array/boolean/mod.rs +++ b/tests/it/array/boolean/mod.rs @@ -36,6 +36,20 @@ fn basics() { assert!(!array.value(1)); } +#[test] +fn with_validity() { + let bitmap = Bitmap::from([true, false, true]); + let a = BooleanArray::from_data(DataType::Boolean, bitmap, None); + let a = a.with_validity(Some(Bitmap::from([true, false, true]))); + assert!(a.validity().is_some()); +} + +#[test] +fn display() { + let array = BooleanArray::from([Some(true), None, Some(false)]); + assert_eq!(format!("{}", array), "BooleanArray[true, , false]"); +} + #[test] fn empty() { let array = BooleanArray::new_empty(DataType::Boolean); diff --git a/tests/it/array/fixed_size_binary/mod.rs b/tests/it/array/fixed_size_binary/mod.rs index f7a9b61988d..79a1bf21311 100644 --- a/tests/it/array/fixed_size_binary/mod.rs +++ b/tests/it/array/fixed_size_binary/mod.rs @@ -1 +1,55 @@ +use arrow2::{array::FixedSizeBinaryArray, bitmap::Bitmap, buffer::Buffer, datatypes::DataType}; + mod mutable; + +#[test] +fn basics() { + let array = FixedSizeBinaryArray::from_data( + DataType::FixedSizeBinary(2), + Buffer::from([1, 2, 3, 4, 5, 6]), + Some(Bitmap::from([true, false, true])), + ); + assert_eq!(array.size(), 2); + assert_eq!(array.len(), 3); + assert_eq!(array.validity(), Some(&Bitmap::from([true, false, true]))); + + assert_eq!(array.value(0), [1, 2]); + assert_eq!(array.value(2), [5, 6]); + + let array = array.slice(1, 2); + + assert_eq!(array.value(1), [5, 6]); +} + +#[test] +fn with_validity() { + let values = Buffer::from([1, 2, 3, 4, 5, 6]); + let a = FixedSizeBinaryArray::from_data(DataType::FixedSizeBinary(2), values, None); + let a = a.with_validity(Some(Bitmap::from([true, false, true]))); + assert!(a.validity().is_some()); +} + +#[test] +fn display() { + let values = Buffer::from([1, 2, 3, 4, 5, 6]); + let a = FixedSizeBinaryArray::from_data( + DataType::FixedSizeBinary(2), + values, + Some(Bitmap::from([true, false, true])), + ); + assert_eq!(format!("{}", a), "FixedSizeBinaryArray[[1, 2], , [5, 6]]"); +} + +#[test] +fn empty() { + let array = FixedSizeBinaryArray::new_empty(DataType::FixedSizeBinary(2)); + assert_eq!(array.values().len(), 0); + assert_eq!(array.validity(), None); +} + +#[test] +fn from_iter() { + let iter = std::iter::repeat(vec![1u8, 2]).take(2).map(Some); + let a = FixedSizeBinaryArray::from_iter(iter, 2); + assert_eq!(a.len(), 2); +} From 7a2e50bb33fe2a17f35e6f76ad72686ce344a743 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Fri, 12 Nov 2021 05:52:50 +0000 Subject: [PATCH 2/2] More tests. --- src/array/list/mutable.rs | 6 +++++- tests/it/array/list/mutable.rs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/array/list/mutable.rs b/src/array/list/mutable.rs index 1c9b258aa4e..57a3590122e 100644 --- a/src/array/list/mutable.rs +++ b/src/array/list/mutable.rs @@ -34,7 +34,6 @@ impl MutableListArray { let mut offsets = MutableBuffer::::with_capacity(capacity + 1); offsets.push(O::default()); - assert_eq!(values.len(), 0); Self { data_type, offsets, @@ -155,6 +154,11 @@ impl MutableListArray { &mut self.values } + /// The offseta + pub fn offsets(&self) -> &MutableBuffer { + &self.offsets + } + /// The values pub fn values(&self) -> &M { &self.values diff --git a/tests/it/array/list/mutable.rs b/tests/it/array/list/mutable.rs index 2102735a935..649663bb8b2 100644 --- a/tests/it/array/list/mutable.rs +++ b/tests/it/array/list/mutable.rs @@ -30,10 +30,23 @@ fn basics() { assert_eq!(expected, array); } +#[test] +fn with_capacity() { + let array = MutableListArray::>::with_capacity(10); + assert!(array.offsets().capacity() >= 10); + assert_eq!(array.offsets().len(), 1); + assert_eq!(array.values().values().capacity(), 0); + assert_eq!(array.validity(), None); +} + #[test] fn push() { let mut array = MutableListArray::>::new(); array .try_push(Some(vec![Some(1i32), Some(2), Some(3)])) .unwrap(); + assert_eq!(array.len(), 1); + assert_eq!(array.values().values().as_ref(), [1, 2, 3]); + assert_eq!(array.offsets().as_ref(), [0, 3]); + assert_eq!(array.validity(), None); }