Skip to content

Commit

Permalink
fix(Rect): fix underflow in the Rect::intersection method (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 authored Dec 9, 2023
1 parent 2a87251 commit f69d57c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/layout/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ impl Rect {
Rect {
x: x1,
y: y1,
width: x2 - x1,
height: y2 - y1,
width: x2.saturating_sub(x1),
height: y2.saturating_sub(y1),
}
}

Expand Down Expand Up @@ -280,6 +280,14 @@ mod tests {
);
}

#[test]
fn intersection_underflow() {
assert_eq!(
Rect::new(1, 1, 2, 2).intersection(Rect::new(4, 4, 2, 2)),
Rect::new(4, 4, 0, 0)
);
}

#[test]
fn intersects() {
assert!(Rect::new(1, 2, 3, 4).intersects(Rect::new(2, 3, 4, 5)));
Expand Down

0 comments on commit f69d57c

Please sign in to comment.