Add tests and clean up add/sub/mul for positions
This commit is contained in:
parent
123d9bd1fb
commit
4b4f4fbf80
|
@ -61,9 +61,22 @@ pub fn draw_ascii<T: Display + Default, S: BuildHasher>(coordinates: &HashMap<Po
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add() {
|
fn test_add() {
|
||||||
assert_eq!(PositionND::from([0, 2]) + PositionND::from([-1, 0]), [-1, 2].into());
|
assert_eq!(PositionND::from([0, 2]) + PositionND::from([-1, 0]), [-1, 2].into());
|
||||||
assert_eq!(PositionND::from([0, -1]) + PositionND::from(Direction::Up), [0, 0].into());
|
assert_eq!(PositionND::from([0, -1]) + PositionND::from(Direction::Up), [0, 0].into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sub() {
|
||||||
|
assert_eq!(PositionND::from([0, 2]) - PositionND::from([-1, 0]), [1, 2].into());
|
||||||
|
assert_eq!(PositionND::from([0, -1]) - PositionND::from([0, -1]), [0, 0].into());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mul() {
|
||||||
|
assert_eq!(PositionND::from([0, 2]) * 5, [0, 10].into());
|
||||||
|
assert_eq!(PositionND::from([0, -1]) * -2, [0, 2].into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,36 +117,33 @@ impl<const DIMS: usize> PositionND<DIMS> {
|
||||||
impl<const D: usize> Mul<i64> for PositionND<D> {
|
impl<const D: usize> Mul<i64> for PositionND<D> {
|
||||||
type Output = PositionND<D>;
|
type Output = PositionND<D>;
|
||||||
|
|
||||||
fn mul(self, rhs: i64) -> Self::Output {
|
fn mul(mut self, rhs: i64) -> Self::Output {
|
||||||
let mut points = [0; D];
|
for p in self.points.iter_mut() {
|
||||||
for i in 0..D {
|
*p *= rhs;
|
||||||
points[i] = self.points[i] * rhs;
|
|
||||||
}
|
}
|
||||||
PositionND { points }
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const D: usize> Add<PositionND<D>> for PositionND<D> {
|
impl<const D: usize> Add<PositionND<D>> for PositionND<D> {
|
||||||
type Output = PositionND<D>;
|
type Output = PositionND<D>;
|
||||||
|
|
||||||
fn add(self, rhs: PositionND<D>) -> Self::Output {
|
fn add(mut self, rhs: PositionND<D>) -> Self::Output {
|
||||||
let mut points = [0; D];
|
for (x, y) in self.points.iter_mut().zip(rhs.points) {
|
||||||
for i in 0..D {
|
*x += y;
|
||||||
points[i] = self.points[i] + rhs.points[i];
|
|
||||||
}
|
}
|
||||||
PositionND { points }
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const D: usize> Sub<PositionND<D>> for PositionND<D> {
|
impl<const D: usize> Sub<PositionND<D>> for PositionND<D> {
|
||||||
type Output = PositionND<D>;
|
type Output = PositionND<D>;
|
||||||
|
|
||||||
fn sub(self, rhs: PositionND<D>) -> Self::Output {
|
fn sub(mut self, rhs: PositionND<D>) -> Self::Output {
|
||||||
let mut points = [0; D];
|
for (x, y) in self.points.iter_mut().zip(rhs.points) {
|
||||||
for i in 0..D {
|
*x -= y;
|
||||||
points[i] = self.points[i] - rhs.points[i];
|
|
||||||
}
|
}
|
||||||
PositionND { points }
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user