Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 committed Jun 25, 2024
1 parent d4a64f0 commit 27c864f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
53 changes: 51 additions & 2 deletions encodings/fastlanes/src/bitpacking/compute/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,82 @@ impl SliceFn for BitPackedArray {
mod test {
use vortex::array::primitive::PrimitiveArray;
use vortex::compute::slice::slice;
use vortex::compute::unary::scalar_at::scalar_at;
use vortex::{ArrayTrait, IntoArray};

use crate::BitPackedArray;

#[test]
pub fn slice_block() {
let arr = BitPackedArray::encode(
&PrimitiveArray::from((0u32..2048).map(|v| v % 64).collect::<Vec<_>>()).into_array(),
PrimitiveArray::from((0u32..2048).map(|v| v % 64).collect::<Vec<_>>()).array(),
6,
)
.unwrap()
.into_array();
let sliced = BitPackedArray::try_from(slice(&arr, 1024, 2048).unwrap()).unwrap();
assert_eq!(scalar_at(sliced.array(), 0).unwrap(), (1024u32 % 64).into());
assert_eq!(
scalar_at(sliced.array(), 1023).unwrap(),
(2047u32 % 64).into()
);
assert_eq!(sliced.offset(), 0);
assert_eq!(sliced.len(), 1024);
}

#[test]
pub fn slice_within_block() {
let arr = BitPackedArray::encode(
&PrimitiveArray::from((0u32..2048).map(|v| v % 64).collect::<Vec<_>>()).into_array(),
PrimitiveArray::from((0u32..2048).map(|v| v % 64).collect::<Vec<_>>()).array(),
6,
)
.unwrap()
.into_array();
let sliced = BitPackedArray::try_from(slice(&arr, 512, 1434).unwrap()).unwrap();
assert_eq!(scalar_at(sliced.array(), 0).unwrap(), (512u32 % 64).into());
assert_eq!(
scalar_at(sliced.array(), 921).unwrap(),
(1433u32 % 64).into()
);
assert_eq!(sliced.offset(), 512);
assert_eq!(sliced.len(), 922);
}

#[test]
fn slice_within_block_u8s() {
let packed = BitPackedArray::encode(
PrimitiveArray::from((0..10_000).map(|i| (i % 63) as u8).collect::<Vec<_>>()).array(),
7,
)
.unwrap();

let compressed = slice(packed.array(), 768, 9999).unwrap();
assert_eq!(
scalar_at(&compressed, 0).unwrap(),
((768 % 63) as u8).into()
);
assert_eq!(
scalar_at(&compressed, compressed.len() - 1).unwrap(),
((9998 % 63) as u8).into()
);
}

#[test]
fn slice_block_boundary_u8s() {
let packed = BitPackedArray::encode(
PrimitiveArray::from((0..10_000).map(|i| (i % 63) as u8).collect::<Vec<_>>()).array(),
7,
)
.unwrap();

let compressed = slice(packed.array(), 7168, 9216).unwrap();
assert_eq!(
scalar_at(&compressed, 0).unwrap(),
((7168 % 63) as u8).into()
);
assert_eq!(
scalar_at(&compressed, compressed.len() - 1).unwrap(),
((9215 % 63) as u8).into()
);
}
}
49 changes: 2 additions & 47 deletions encodings/fastlanes/src/bitpacking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ impl BitPackedArray {
}

let ptype: PType = (&dtype).try_into()?;
let expected_packed_size = (((length + 1023) / 1024)
+ if offset > 0 && offset + length > 1024 {
1
} else {
0
})
* (128 * bit_width / ptype.byte_width());
let expected_packed_size =
((length + offset + 1023) / 1024) * (128 * bit_width / ptype.byte_width());
if packed.len() != expected_packed_size {
return Err(vortex_err!(
"Expected {} packed bytes, got {}",
Expand Down Expand Up @@ -191,50 +186,10 @@ impl ArrayTrait for BitPackedArray {
#[cfg(test)]
mod test {
use vortex::array::primitive::PrimitiveArray;
use vortex::compute::slice::slice;
use vortex::compute::unary::scalar_at::scalar_at;
use vortex::{IntoArray, IntoCanonical};

use crate::BitPackedArray;

#[test]
fn slice_within_block() {
let packed = BitPackedArray::encode(
PrimitiveArray::from((0..10_000).map(|i| (i % 63) as u8).collect::<Vec<_>>()).array(),
7,
)
.unwrap();

let compressed = slice(packed.array(), 768, 9999).unwrap();
assert_eq!(
scalar_at(&compressed, 0).unwrap(),
((768 % 63) as u8).into()
);
assert_eq!(
scalar_at(&compressed, compressed.len() - 1).unwrap(),
((9998 % 63) as u8).into()
);
}

#[test]
fn slice_block_boundary() {
let packed = BitPackedArray::encode(
PrimitiveArray::from((0..10_000).map(|i| (i % 63) as u8).collect::<Vec<_>>()).array(),
7,
)
.unwrap();

let compressed = slice(packed.array(), 7168, 9216).unwrap();
assert_eq!(
scalar_at(&compressed, 0).unwrap(),
((7168 % 63) as u8).into()
);
assert_eq!(
scalar_at(&compressed, compressed.len() - 1).unwrap(),
((9215 % 63) as u8).into()
);
}

#[test]
fn test_encode() {
let values = vec![Some(1), None, Some(1), None, Some(1), None, Some(u64::MAX)];
Expand Down

0 comments on commit 27c864f

Please sign in to comment.