diff --git a/src/lib.rs b/src/lib.rs index 93de9a1..3cfdc94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,6 +56,14 @@ impl Sub for Vector3D { } } +impl Mul for Vector3D { + type Output = Vector3D; + + fn mul(self, other: T) -> Vector3D { + Vector3D(self.0 * other, self.1 * other, self.2 * other) + } +} + #[cfg(test)] mod tests { use super::*; @@ -588,4 +596,72 @@ mod tests { assert!(r.2 == d.2 - c.2); } } + + #[test] + fn test_vector3_multiply_zeroes_yields_zeroes() { + let a = Vector3D(0.0, 0.0, 0.0); + let b = 0.0; + let c = a * b; + assert!(c.0 == 0.0); + assert!(c.1 == 0.0); + assert!(c.2 == 0.0); + } + + #[test] + fn test_vector3_multiplication_by_zero_yields_zeroes() { + let a = Vector3D(1.0, 2.0, 3.0); + let b = 0.0; + let c = a * b; + assert!(c.0 == 0.0); + assert!(c.1 == 0.0); + assert!(c.2 == 0.0); + } + + #[test] + fn test_vector3_multiplication_identity() { + let a = Vector3D(1.0, 2.0, 3.0); + let b = 1.0; + let c = a * b; + assert!(c == a); + } + + #[test] + fn test_vector3_multiplication_by_positive_float() { + let a = Vector3D(1.0, 2.0, 3.0); + let b = 2.0; + let c = a * b; + assert!(c.0 == a.0 * 2.0); + assert!(c.1 == a.1 * 2.0); + assert!(c.2 == a.2 * 2.0); + } + + #[test] + fn test_vector3_multiplication_by_negative_float() { + let a = Vector3D(1.0, 2.0, 3.0); + let b = -2.0; + let c = a * b; + assert!(c.0 == a.0 * -2.0); + assert!(c.1 == a.1 * -2.0); + assert!(c.2 == a.2 * -2.0); + } + + #[test] + fn test_vector3_multiplication_by_positive_int() { + let a = Vector3D(1, 2, 3); + let b = 2; + let c = a * b; + assert!(c.0 == a.0 * 2); + assert!(c.1 == a.1 * 2); + assert!(c.2 == a.2 * 2); + } + + #[test] + fn test_vector3_multiplication_by_negative_int() { + let a = Vector3D(1, 2, 3); + let b = -2; + let c = a * b; + assert!(c.0 == a.0 * -2); + assert!(c.1 == a.1 * -2); + assert!(c.2 == a.2 * -2); + } }