Add Rectangle tests

This commit is contained in:
expectocode 2019-12-31 00:52:31 +00:00
parent 0fff7e4c0b
commit 5e028c20ae
1 changed files with 140 additions and 0 deletions

View File

@ -220,4 +220,144 @@ mod tests {
Point::new(0, 10) + Point::new(10, 0)
);
}
#[test]
fn test_rectangle_get_width() {
assert_eq!(
Rectangle::from_corners(Point::new(1, 1), Point::new(11, 11)).width(),
10
);
assert_eq!(
Rectangle::from_corner_width_height(Point::new(1, 1), 20, 30).width(),
20
);
}
#[test]
fn test_rectangle_get_height() {
assert_eq!(
Rectangle::from_corners(Point::new(1, 0), Point::new(11, 11)).height(),
11
);
assert_eq!(
Rectangle::from_corner_width_height(Point::new(7, 6), 20, 30).height(),
30
);
}
#[test]
fn test_rectangle_set_width() {
assert_eq!(
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 4).with_width(5),
Rectangle::from_corner_width_height(Point::new(1, 2), 5, 4)
);
assert_eq!(
Rectangle::from_corners(Point::new(1, 2), Point::new(6, 7)).with_width(10),
Rectangle::from_corners(Point::new(1, 2), Point::new(11, 7))
);
}
#[test]
fn test_rectangle_set_height() {
assert_eq!(
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 4).with_height(5),
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 5)
);
assert_eq!(
Rectangle::from_corners(Point::new(1, 2), Point::new(6, 7)).with_height(10),
Rectangle::from_corners(Point::new(1, 2), Point::new(6, 12))
);
}
#[test]
fn test_rectangle_get_top_left() {
assert_eq!(
Point::new(1, 2),
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 4).top_left()
);
}
#[test]
fn test_rectangle_get_top_right() {
assert_eq!(
Point::new(4, 2),
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 4).top_right()
);
}
#[test]
fn test_rectangle_get_bottom_left() {
assert_eq!(
Point::new(1, 6),
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 4).bottom_left()
);
}
#[test]
fn test_rectangle_get_bottom_right() {
assert_eq!(
Point::new(4, 6),
Rectangle::from_corner_width_height(Point::new(1, 2), 3, 4).bottom_right()
);
}
#[test]
fn test_rectangle_get_area() {
assert_eq!(100, Rectangle::at_origin_with_size(10, 10).area());
assert_eq!(
30,
Rectangle::from_corners(Point::origin(), Point::new(6, 5)).area()
);
}
#[test]
fn test_rectangle_intersect_1() {
let r1 = Rectangle::from_corner_width_height(Point::new(0, 0), 10, 10);
let r2 = Rectangle::from_corner_width_height(Point::new(8, 0), 10, 10);
let r3 = Rectangle::from_corner_width_height(Point::new(0, 8), 10, 10);
let r4 = Rectangle::from_corner_width_height(Point::new(8, 8), 10, 10);
let r5 = Rectangle::from_corner_width_height(Point::new(0, 100), 5, 5);
let r6 = Rectangle::from_corner_width_height(Point::new(100, 100), 5, 5);
assert!(r1.intersects(&r2));
assert!(r1.intersects(&r3));
assert!(r1.intersects(&r4));
assert!(r2.intersects(&r3));
assert!(r2.intersects(&r4));
assert!(r3.intersects(&r4));
assert!(r1.intersects(&r1));
assert!(r2.intersects(&r2));
assert!(r3.intersects(&r3));
assert!(r4.intersects(&r4));
assert!(!r1.intersects(&r5));
assert!(!r2.intersects(&r5));
assert!(!r3.intersects(&r5));
assert!(!r4.intersects(&r5));
assert!(!r1.intersects(&r6));
assert!(!r2.intersects(&r6));
assert!(!r3.intersects(&r6));
assert!(!r4.intersects(&r6));
assert!(!r5.intersects(&r6));
}
#[test]
fn test_rectangle_intersect_2() {
assert!(Rectangle::at_origin_with_size(3, 3).intersects(
&Rectangle::from_corner_width_height(Point::new(3, 3), 10, 10)
));
assert!(Rectangle::at_origin_with_size(3, 3).intersects(
&Rectangle::from_corner_width_height(Point::new(3, 0), 10, 10)
));
}
#[test]
fn test_rectangle_intersect_3() {
assert!(!Rectangle::at_origin_with_size(3, 3).intersects(
&Rectangle::from_corner_width_height(Point::new(4, 4), 10, 10)
));
}
}