Implement Mul for Vector2D * scalar

This commit is contained in:
Matthew Gordon 2019-10-24 20:42:58 -04:00
parent 7e5c9d5231
commit 0cb1157df0
1 changed files with 72 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use std::ops::Add;
use std::ops::Mul;
use std::ops::Sub;
#[derive(Clone, Copy, Eq, PartialEq)]
@ -20,6 +21,14 @@ impl<T: Sub> Sub for Vector2D<T> {
}
}
impl<T: Mul + Copy> Mul<T> for Vector2D<T> {
type Output = Vector2D<T::Output>;
fn mul(self, other: T) -> Vector2D<T::Output> {
Vector2D(self.0 * other, self.1 * other)
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
struct Vector3D<T>(T, T, T);
@ -238,6 +247,69 @@ mod tests {
}
}
#[test]
fn test_vector2_multiply_zeroes_yields_zeroes() {
let a = Vector2D(0.0, 0.0);
let b = 0.0;
let c = a * b;
assert!(c.0 == 0.0);
assert!(c.1 == 0.0);
}
#[test]
fn test_vector2_multiplication_by_zero_yields_zeroes() {
let a = Vector2D(1.0, 2.0);
let b = 0.0;
let c = a * b;
assert!(c.0 == 0.0);
assert!(c.1 == 0.0);
}
#[test]
fn test_vector2_multiplication_identity() {
let a = Vector2D(1.0, 2.0);
let b = 1.0;
let c = a * b;
assert!(c.0 == a.0);
assert!(c.1 == a.1);
}
#[test]
fn test_vector2_multiplication_by_positive_float() {
let a = Vector2D(1.0, 2.0);
let b = 2.0;
let c = a * b;
assert!(c.0 == a.0 * 2.0);
assert!(c.1 == a.1 * 2.0);
}
#[test]
fn test_vector2_multiplication_by_negative_float() {
let a = Vector2D(1.0, 2.0);
let b = -2.0;
let c = a * b;
assert!(c.0 == a.0 * -2.0);
assert!(c.1 == a.1 * -2.0);
}
#[test]
fn test_vector2_multiplication_by_positive_int() {
let a = Vector2D(1, 2);
let b = 2;
let c = a * b;
assert!(c.0 == a.0 * 2);
assert!(c.1 == a.1 * 2);
}
#[test]
fn test_vector2_multiplication_by_negative_int() {
let a = Vector2D(1, 2);
let b = -2;
let c = a * b;
assert!(c.0 == a.0 * -2);
assert!(c.1 == a.1 * -2);
}
#[test]
fn test_vector3d_add_zeroes_yields_zeroes() {
let a = Vector3D(0.0, 0.0, 0.0);