#[cfg(test)]
mod tests {
use std::sync::Arc;
use vortex_buffer::Buffer;
use vortex_buffer::ByteBuffer;
use vortex_buffer::buffer;
use vortex_error::VortexError;
use crate::IntoArray;
use crate::arrays::ChunkedArray;
use crate::arrays::DecimalArray;
use crate::arrays::FixedSizeListArray;
use crate::arrays::ListArray;
use crate::arrays::PrimitiveArray;
use crate::arrays::StructArray;
use crate::arrays::VarBinArray;
use crate::arrays::VarBinViewArray;
use crate::arrays::varbinview::build_views::BinaryView;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::validity::Validity;
#[test]
fn test_chunked_array_validation_success() {
let chunk1 = buffer![1i32, 2, 3].into_array();
let chunk2 = buffer![4i32, 5, 6].into_array();
let result = ChunkedArray::try_new(vec![chunk1, chunk2], PType::I32.into());
assert!(result.is_ok());
}
#[test]
fn test_chunked_array_validation_failure_mismatched_dtypes() {
let chunk1 = buffer![1i32, 2, 3].into_array();
let chunk2 = buffer![4i64, 5, 6].into_array();
let result = ChunkedArray::try_new(vec![chunk1, chunk2], PType::I32.into());
assert!(matches!(result, Err(VortexError::MismatchedTypes(_, _, _))));
assert!(result.is_err());
}
#[test]
fn test_decimal_array_validation_success() {
let buffer = Buffer::from_iter([100i128, 200, 300]);
let decimal_dtype = crate::dtype::DecimalDType::new(10, 2);
let result = DecimalArray::try_new(buffer, decimal_dtype, Validity::NonNullable);
assert!(result.is_ok());
}
#[test]
fn test_decimal_array_validation_failure_length_mismatch() {
let buffer = Buffer::from_iter([100i128, 200, 300]);
let validity = Validity::from_iter([true, false]); let decimal_dtype = crate::dtype::DecimalDType::new(10, 2);
let result = DecimalArray::try_new(buffer, decimal_dtype, validity);
assert!(matches!(result, Err(VortexError::InvalidArgument(_, _))));
assert!(result.is_err());
}
#[test]
fn test_primitive_array_validation_success() {
let buffer = Buffer::from_iter([1i32, 2, 3]);
let result = PrimitiveArray::try_new(buffer, Validity::NonNullable);
assert!(result.is_ok());
}
#[test]
fn test_primitive_array_validation_failure_length_mismatch() {
let buffer = Buffer::from_iter([1i32, 2, 3]);
let validity = Validity::from_iter([true, false]); let result = PrimitiveArray::try_new(buffer, validity);
assert!(matches!(result, Err(VortexError::InvalidArgument(_, _))));
assert!(result.is_err());
}
#[test]
fn test_varbin_array_validation_success() {
let offsets = buffer![0i32, 3, 6, 10].into_array();
let bytes = ByteBuffer::from(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
let result = VarBinArray::try_new(
offsets,
bytes,
DType::Binary(Nullability::NonNullable),
Validity::NonNullable,
);
assert!(result.is_ok());
}
#[test]
fn test_varbin_array_validation_non_monotonic_offsets_accepted() {
let offsets = buffer![0i32, 3, 2, 5].into_array(); let bytes = ByteBuffer::from(vec![0u8, 1, 2, 3, 4]);
let result = VarBinArray::try_new(
offsets,
bytes,
DType::Binary(Nullability::NonNullable),
Validity::NonNullable,
);
assert!(result.is_ok());
}
#[test]
fn test_list_array_validation_success() {
let elements = buffer![1i32, 2, 3, 4, 5].into_array();
let offsets = buffer![0i64, 2, 3, 5].into_array();
let result = ListArray::try_new(elements, offsets, Validity::NonNullable);
assert!(result.is_ok());
}
#[test]
fn test_list_array_validation_failure_offsets_out_of_bounds() {
let elements = buffer![1i32, 2, 3].into_array();
let offsets = buffer![0i64, 2, 5].into_array(); let result = ListArray::try_new(elements, offsets, Validity::NonNullable);
assert!(matches!(result, Err(VortexError::InvalidArgument(_, _))));
assert!(result.is_err());
}
#[test]
fn test_fixed_size_list_array_validation_success() {
let elements = buffer![1i32, 2, 3, 4, 5, 6].into_array();
let result = FixedSizeListArray::try_new(elements, 2, Validity::NonNullable, 3);
assert!(result.is_ok());
}
#[test]
fn test_fixed_size_list_array_validation_failure_length_mismatch() {
let elements = buffer![1i32, 2, 3, 4, 5].into_array(); let result = FixedSizeListArray::try_new(elements, 2, Validity::NonNullable, 3);
assert!(matches!(result, Err(VortexError::InvalidArgument(_, _))));
assert!(result.is_err());
}
#[test]
fn test_varbinview_array_validation_success() {
let view1 = BinaryView::new_inlined(b"foo");
let view2 = BinaryView::new_inlined(b"bar");
let views = Buffer::from_iter([view1, view2]);
let result = VarBinViewArray::try_new(
views,
Arc::new([]),
DType::Utf8(Nullability::NonNullable),
Validity::NonNullable,
);
assert!(result.is_ok());
}
#[test]
fn test_varbinview_array_validation_failure_buffer_index_out_of_bounds() {
let data = b"this is a long string that needs a buffer";
let view = BinaryView::make_view(data, 1, 0);
let views = Buffer::from_iter([view]);
let buffers = Arc::new([ByteBuffer::from(data.to_vec())]);
let result = VarBinViewArray::try_new(
views,
buffers,
DType::Binary(Nullability::NonNullable),
Validity::NonNullable,
);
assert!(matches!(result, Err(VortexError::InvalidArgument(_, _))));
assert!(result.is_err());
}
#[test]
fn test_struct_array_validation_success() {
let field1 = buffer![1i32, 2, 3].into_array();
let field2 = buffer![4.0f64, 5.0, 6.0].into_array();
let fields = vec![field1, field2];
let names = ["a", "b"];
let result = StructArray::try_new(names.into(), fields, 3, Validity::NonNullable);
assert!(result.is_ok());
}
#[test]
fn test_struct_array_validation_failure_field_length_mismatch() {
let field1 = buffer![1i32, 2, 3].into_array();
let field2 = buffer![4.0f64, 5.0].into_array(); let fields = vec![field1, field2];
let names = ["a", "b"];
let result = StructArray::try_new(names.into(), fields, 3, Validity::NonNullable);
assert!(matches!(result, Err(VortexError::InvalidArgument(_, _))));
assert!(result.is_err());
}
}