Skip to content

Commit

Permalink
Fix gridbool iter when gridbool is big (W*H > 65535)
Browse files Browse the repository at this point in the history
We multiply W and H to iterate, but their product may not fill in
their own u16 type - so we have to convert them to usize beforehand.
  • Loading branch information
lpenz committed Dec 15, 2021
1 parent e837506 commit 6511bd3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/sqrid/gridbool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<const W: u16, const H: u16, const WORDS: usize> Gridbool<W, H, WORDS> {
/// Iterate over all `true`/`false` values in the `Gridbool`.
#[inline]
pub fn iter(&self) -> impl iter::Iterator<Item = bool> + '_ {
(0..(W * H)).map(move |i| {
(0..(W as usize * H as usize)).map(move |i| {
let (byte, bit) = Self::byte_bit(i);
self.0[byte] & bit != 0
})
Expand Down
22 changes: 20 additions & 2 deletions tests/gridbool_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ type Gbool2 = sqrid::Gridbool<11, 3, 2>;
type Qa5 = sqrid::Qa<5, 5>;
type Gbool5 = sqrid::Gridbool<5, 5, 1>;

type _QaScale = sqrid::Qa<0xffff, 0xffff>;
type _GridboolScale = sqrid::gridbool_create!(_QaScale);
type QaScale = sqrid::Qa<2000, 2000>;
type GridboolScale = sqrid::gridbool_create!(QaScale);

type _QaMax = sqrid::Qa<0xffff, 0xffff>;
type _GridboolMax = sqrid::gridbool_create!(_QaMax);

#[test]
fn test_getset() -> Result<()> {
Expand Down Expand Up @@ -148,3 +151,18 @@ fn test_rotate() -> Result<()> {
assert_eq!(gb2, gb);
Ok(())
}

#[test]
fn test_scale() -> Result<()> {
let mut gb = Box::new(GridboolScale::default());
for qa in QaScale::iter() {
gb.set_t(qa);
}
for value in gb.iter() {
// Dummy operation, we are really just testing gb.iter
if !value {
break;
}
}
Ok(())
}

0 comments on commit 6511bd3

Please sign in to comment.