diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2017-05-28 17:51:48 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2017-05-28 17:51:48 +0200 |
commit | f9fc98d0091ec72d639f1fdb4cce1ef25f7eea05 (patch) | |
tree | 4412ad33524793b768a585b67026729d7d939ab7 | |
parent | fe1c30d52f643843004230f804cfd55f356d0d6d (diff) |
Additional maths tests
-rw-r--r-- | src/math.rs | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/src/math.rs b/src/math.rs index f0e323c..a93d03e 100644 --- a/src/math.rs +++ b/src/math.rs @@ -40,6 +40,18 @@ impl Direction { } } +#[cfg(test)] +mod direction_tests { + use super::*; + + #[test] + fn random_direction_does_not_panic() { + for _ in 0..10000 { + Direction::random(); + } + } +} + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] pub struct Point { pub x: u16, @@ -139,10 +151,76 @@ impl Point { #[cfg(test)] -mod tests { +mod point_tests { use super::*; #[test] + fn random_point_is_in_correct_range() { + for _ in 0..10000 { + let point = Point::random(15); + assert!(point.x < 15); + assert!(point.y < 15); + } + } + + + #[test] + fn move_point_works_west() { + assert_eq!(Some(Point::new(3,5)), Point::new(5,5).move_point(West, 2, 10)); + assert_eq!(Some(Point::new(7,5)), Point::new(5,5).move_point(West, -2, 10)); + assert_eq!(None, Point::new(5,5).move_point(West, 6, 10)); + assert_eq!(None, Point::new(5,5).move_point(West, -5, 10)); + } + + #[test] + fn move_point_works_east() { + assert_eq!(Some(Point::new(7,5)), Point::new(5,5).move_point(East, 2, 10)); + assert_eq!(Some(Point::new(3,5)), Point::new(5,5).move_point(East, -2, 10)); + assert_eq!(None, Point::new(5,5).move_point(East, 5, 10)); + assert_eq!(None, Point::new(5,5).move_point(East, -6, 10)); + } + + #[test] + fn move_point_works_south() { + assert_eq!(Some(Point::new(5,3)), Point::new(5,5).move_point(South, 2, 10)); + assert_eq!(Some(Point::new(5,7)), Point::new(5,5).move_point(South, -2, 10)); + assert_eq!(None, Point::new(5,5).move_point(South, 6, 10)); + assert_eq!(None, Point::new(5,5).move_point(South, -5, 10)); + } + + #[test] + fn move_point_works_north() { + assert_eq!(Some(Point::new(5,7)), Point::new(5,5).move_point(North, 2, 10)); + assert_eq!(Some(Point::new(5,3)), Point::new(5,5).move_point(North, -2, 10)); + assert_eq!(None, Point::new(5,5).move_point(North, 5, 10)); + assert_eq!(None, Point::new(5,5).move_point(North, -6, 10)); + } + + #[test] + fn unrestricted_move_point_works_west() { + assert_eq!(Point::new(3,5), Point::new(5,5).move_point_no_bounds_check(West, 2)); + assert_eq!(Point::new(7,5), Point::new(5,5).move_point_no_bounds_check(West, -2)); + } + + #[test] + fn unrestricted_move_point_works_east() { + assert_eq!(Point::new(7,5), Point::new(5,5).move_point_no_bounds_check(East, 2)); + assert_eq!(Point::new(3,5), Point::new(5,5).move_point_no_bounds_check(East, -2)); + } + + #[test] + fn unrestricted_move_point_works_south() { + assert_eq!(Point::new(5,3), Point::new(5,5).move_point_no_bounds_check(South, 2)); + assert_eq!(Point::new(5,7), Point::new(5,5).move_point_no_bounds_check(South, -2)); + } + + #[test] + fn unrestricted_move_point_works_north() { + assert_eq!(Point::new(5,7), Point::new(5,5).move_point_no_bounds_check(North, 2)); + assert_eq!(Point::new(5,3), Point::new(5,5).move_point_no_bounds_check(North, -2)); + } + + #[test] fn ship_collision_check_works_west() { assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,5), West, 5)); assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(6,5), West, 5)); |