add tests for bitmatrix out of bounds panic

addresses #27 and #28
This commit is contained in:
Henry Schimke
2023-03-21 09:57:58 -05:00
parent 94d07a1b65
commit 2b3ed37924
4 changed files with 134 additions and 4 deletions

View File

@@ -202,14 +202,17 @@ impl BitMatrix {
}
/**
* <p>Gets the requested bit, where true means black.</p>
* Gets the requested bit, where true means black.
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
* @return value of given bit in matrix
* x The horizontal component (i.e. which column)
* y The vertical component (i.e. which row)
* returns the value of given bit in matrix, or false if the requested point is out of bounds of the image
*/
pub fn get(&self, x: u32, y: u32) -> bool {
let offset = self.get_offset(y, x);
if offset >= self.bits.len() {
return false;
}
((self.bits[offset] >> (x & 0x1f)) & 1) != 0
}
@@ -232,6 +235,10 @@ impl BitMatrix {
Some(((self.bits[offset] >> (x & 0x1f)) & 1) != 0)
}
pub fn try_get_point(&self, point: Point) -> Option<bool> {
self.try_get(point.x as u32, point.y as u32)
}
pub fn try_get_area(&self, x: u32, y: u32, box_size: u32) -> Option<bool> {
let mut matrix = Vec::with_capacity((box_size * box_size) as usize);
let start_x = (x as i32 - box_size as i32 / 2).max(0) as u32;