Added example unit tests for circles intersecting
authorJustin Worthe <justin@worthe-it.co.za>
Tue, 1 Jan 2019 19:49:37 +0000 (21:49 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Tue, 1 Jan 2019 19:49:37 +0000 (21:49 +0200)
src/hitbox.rs

index 75cc89b..191e1a1 100644 (file)
@@ -61,3 +61,50 @@ fn capsule_circle_intersect(x: &CapsuleHitbox, y: &CircleHitbox) -> bool {
 fn capsule_point_intersect(x: &CapsuleHitbox, y: &PointHitbox) -> bool {
     x.line.distance(y.pos) <= x.radius
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    
+    #[test]
+    fn circle_intersecting_circle_origin_example() {
+        assert_eq!(Hitbox::intersect(
+            &Hitbox::Circle(CircleHitbox {
+                pos: Vec2d::new(0.,0.),
+                radius: 0.
+            }),
+            &Hitbox::Circle(CircleHitbox {
+                pos: Vec2d::new(0.,0.),
+                radius: 0.
+            })
+        ), true);
+    }
+
+    #[test]
+    fn circle_intersecting_circle_origin_elsewhere() {
+        assert_eq!(Hitbox::intersect(
+            &Hitbox::Circle(CircleHitbox {
+                pos: Vec2d::new(0.,0.),
+                radius: 0.5
+            }),
+            &Hitbox::Circle(CircleHitbox {
+                pos: Vec2d::new(1.,0.),
+                radius: 0.5
+            })
+        ), true);
+    }
+
+    #[test]
+    fn circle_intersecting_circle_origin_outside() {
+        assert_eq!(Hitbox::intersect(
+            &Hitbox::Circle(CircleHitbox {
+                pos: Vec2d::new(0.,0.),
+                radius: 0.5
+            }),
+            &Hitbox::Circle(CircleHitbox {
+                pos: Vec2d::new(1.,0.),
+                radius: 0.4
+            })
+        ), false);
+    }
+}