Skip to content

Commit c8c87db

Browse files
MarkusZoppeltsdd
authored andcommitted
test: add regression test for remainder slice access bug
tests nearest_n_within with size-33 dataset to verify items in remainder region are found correctly. Before the fix, this would access self.content_items[0] instead of remainder_items[0], returning wrong items. Signed-off-by: Markus Zoppelt <markus.zoppelt@helsing.ai>
1 parent 6dd2f32 commit c8c87db

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/float_leaf_slice/leaf_slice.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ where
466466

467467
#[cfg(test)]
468468
mod test {
469-
use crate::float_leaf_slice::leaf_slice::{LeafFixedSlice, LeafSliceFloat};
469+
use crate::float_leaf_slice::leaf_slice::{LeafFixedSlice, LeafSlice, LeafSliceFloat};
470470
use crate::{BestNeighbour, NearestNeighbour, SquaredEuclidean};
471471
use std::collections::BinaryHeap;
472472

@@ -624,4 +624,32 @@ mod test {
624624
]
625625
);
626626
}
627+
628+
// Test for remainder path processing with non-chunk-aligned sizes (CHUNK_SIZE=32)
629+
// Verifies the fix for using remainder_items[idx] instead of self.content_items[idx]
630+
#[test]
631+
fn test_remainder_processing_finds_correct_item() {
632+
// Size 33 = 1 chunk (32) + 1 remainder
633+
// Item 32 is in the remainder region - if the bug existed, it would return
634+
// self.content_items[0] instead of remainder_items[0]
635+
let mut dim0 = Vec::with_capacity(33);
636+
let mut dim1 = Vec::with_capacity(33);
637+
let mut items = Vec::with_capacity(33);
638+
for i in 0..33 {
639+
dim0.push(i as f64);
640+
dim1.push(0.0f64);
641+
items.push(i as u32);
642+
}
643+
644+
let slice = LeafSlice {
645+
content_points: [&dim0[..], &dim1[..]],
646+
content_items: &items[..],
647+
};
648+
649+
let mut results: BinaryHeap<NearestNeighbour<f64, u32>> = BinaryHeap::with_capacity(10);
650+
slice.nearest_n_within::<SquaredEuclidean, _>(&[32.0f64, 0.0f64], 4.0f64, &mut results);
651+
652+
let items_found: Vec<_> = results.iter().map(|n| n.item).collect();
653+
assert!(items_found.contains(&32u32), "Should find item 32 in remainder region");
654+
}
627655
}

0 commit comments

Comments
 (0)