Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions vortex-buffer/src/bit/buf.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what made you add more tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you altenate only on a per bit basis [true,false,true,false] its not possible to spot if iter_bits starts on the correct byte.

I wanted a test that fails if let mut buffer_ptr = unsafe { self.buffer.as_ptr().add(self.offset / 8) }; would be set to let mut buffer_ptr = unsafe { self.buffer.as_ptr().add(0) };.

Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ mod tests {
#[case(3, 8)]
#[case(5, 10)]
#[case(2, 16)]
#[case(8, 16)]
#[case(9, 16)]
#[case(17, 16)]
fn test_iter_bits_with_offset(#[case] offset: usize, #[case] len: usize) {
let total_bits = offset + len;
let buf = BitBuffer::collect_bool(total_bits, |i| i % 2 == 0);
Expand All @@ -604,4 +607,36 @@ mod tests {
assert_eq!(is_set, (offset + idx) % 2 == 0);
}
}

#[rstest]
#[case(8, 10)]
#[case(9, 7)]
#[case(16, 8)]
#[case(17, 10)]
fn test_iter_bits_catches_wrong_byte_offset(#[case] offset: usize, #[case] len: usize) {
let total_bits = offset + len;
// Alternating pattern to catch byte offset errors: Bits are set for even indexed bytes.
let buf = BitBuffer::collect_bool(total_bits, |i| (i / 8) % 2 == 0);

let buf_with_offset = BitBuffer::new_with_offset(buf.inner().clone(), len, offset);

let mut collected = Vec::new();
buf_with_offset.iter_bits(|idx, is_set| {
collected.push((idx, is_set));
});

assert_eq!(collected.len(), len);

for (idx, is_set) in collected {
let bit_position = offset + idx;
let byte_index = bit_position / 8;
let expected_is_set = byte_index % 2 == 0;

assert_eq!(
is_set, expected_is_set,
"Bit mismatch at index {}: expected {} got {}",
bit_position, expected_is_set, is_set
);
}
}
}
Loading