Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
chore: add tests for downcast functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aldanor committed Sep 8, 2023
1 parent df48173 commit 4daf21c
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tests/it/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ mod struct_;
mod union;
mod utf8;

use arrow2::array::{clone, new_empty_array, new_null_array, Array, PrimitiveArray};
use arrow2::array::{
clone, downcast_mut, downcast_ref, new_empty_array, new_null_array, Array, MutableArray,
MutablePrimitiveArray, PrimitiveArray,
};
use arrow2::bitmap::Bitmap;
use arrow2::datatypes::{DataType, Field, UnionMode};

Expand Down Expand Up @@ -140,3 +143,27 @@ fn test_with_validity() {
struct A {
array: Box<dyn Array>,
}

#[test]
fn test_downcast() {
let arr = PrimitiveArray::from_slice([1i32, 2, 3]);
let arr_box: Box<dyn Array> = Box::new(arr.clone());
assert_eq!(downcast_ref::<PrimitiveArray<i32>>(&arr_box).unwrap(), &arr);
assert_eq!(
downcast_ref::<PrimitiveArray<i32>>(arr_box.as_ref()).unwrap(),
&arr
);
assert!(downcast_ref::<PrimitiveArray<u8>>(&arr_box).is_err());

let mut_arr = MutablePrimitiveArray::from_slice([1i32, 2, 3]);
let mut mut_arr_box: Box<dyn MutableArray> = Box::new(mut_arr.clone());
assert_eq!(
downcast_mut::<MutablePrimitiveArray<i32>>(&mut mut_arr_box).unwrap(),
&mut_arr
);
assert_eq!(
downcast_mut::<MutablePrimitiveArray<i32>>(mut_arr_box.as_mut()).unwrap(),
&mut_arr
);
assert!(downcast_mut::<MutablePrimitiveArray<u8>>(&mut mut_arr_box).is_err());
}

0 comments on commit 4daf21c

Please sign in to comment.