Skip to content

Commit

Permalink
test: patch bools (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwwmanning authored Jan 6, 2025
1 parent 53ed1d5 commit 6fe8143
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions vortex-array/src/array/bool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,15 @@ impl VisitorVTable<BoolArray> for BoolEncoding {

#[cfg(test)]
mod tests {
use crate::array::BoolArray;
use crate::compute::scalar_at;
use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder};
use vortex_buffer::buffer;
use vortex_dtype::Nullability;

use crate::array::{BoolArray, PrimitiveArray};
use crate::compute::{scalar_at, slice};
use crate::patches::Patches;
use crate::validity::Validity;
use crate::IntoArrayData;
use crate::{ArrayLen, IntoArrayData, IntoArrayVariant};

#[test]
fn bool_array() {
Expand Down Expand Up @@ -252,4 +257,55 @@ mod tests {
let scalar = scalar_at(&arr, 4).unwrap();
assert!(scalar.is_null());
}

#[test]
fn patch_sliced_bools() {
let arr = {
let mut builder = BooleanBufferBuilder::new(12);
builder.append(false);
builder.append_n(11, true);
BoolArray::from(builder.finish())
};
let sliced = slice(arr.clone(), 4, 12).unwrap();
let (values, offset) = sliced.clone().into_bool().unwrap().into_boolean_builder();
assert_eq!(offset, 4);
assert_eq!(values.as_slice(), &[254, 15]);

// patch the underlying array
let patches = Patches::new(
arr.len(),
PrimitiveArray::new(buffer![4u32], Validity::AllValid).into_array(),
BoolArray::from(BooleanBuffer::new_unset(1)).into_array(),
);
let arr = arr.patch(patches).unwrap();
let (values, offset) = arr.into_bool().unwrap().into_boolean_builder();
assert_eq!(offset, 0);
assert_eq!(values.as_slice(), &[238, 15]);

// the slice should be unchanged
let (values, offset) = sliced.into_bool().unwrap().into_boolean_builder();
assert_eq!(offset, 4);
assert_eq!(values.as_slice(), &[254, 15]); // unchanged
}

#[test]
#[should_panic]
fn patch_bools_owned() {
let buffer = buffer![255u8; 2];
let buf = BooleanBuffer::new(buffer.into_arrow_buffer(), 0, 15);
let arr = BoolArray::new(buf, Nullability::NonNullable);
let buf_ptr = arr.boolean_buffer().sliced().as_ptr();

let patches = Patches::new(
arr.len(),
PrimitiveArray::new(buffer![0u32], Validity::AllValid).into_array(),
BoolArray::from(BooleanBuffer::new_unset(1)).into_array(),
);
let arr = arr.patch(patches).unwrap();
assert_eq!(arr.boolean_buffer().sliced().as_ptr(), buf_ptr);

let (values, offset) = arr.into_bool().unwrap().into_boolean_builder();
assert_eq!(offset, 0);
assert_eq!(values.as_slice(), &[254, 127]);
}
}

0 comments on commit 6fe8143

Please sign in to comment.