I have a vector class that has it's Equals(object obj)
method overridden so that I can compare them.
public class Vector3f
{
public float x,y,z;
public Vector3f(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3f operator +(Vector3f a, Vector3f b) {
return new Vector3f(a.x + b.x, a.y + b.y, a.z + b.z);
}
public static Vector3f operator -(Vector3f a, Vector3f b) {
return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
}
public override bool Equals(object obj)
{
Vector3f other = (Vector3f)obj;
return x == other.x && y == other.y && z == other.z;
}
public override string ToString()
{
return String.Format("<{0},{1},{2}>",x,y,z);
}
}
The plus operator works as expected in my unit tests. However, when I subtract two vectors it says they are not equal
Test 'RTTests.Testies.vector_subtraction_works' failed:
Expected: <<1.1,0.1,0.1>>
But was: <<1.1,0.1,0.1>>
Testies.cs(60,0): at RTTests.Testies.vector_sub_works()
I'm not sure why the comparison is working for addition and not subtraction especially since the output values are identical in both cases?
EDIT: My tests for this
[Test]
public void vector_addition_works()
{
Vector3f v1 = new Vector3f(1.0f, 1.0f, 1.0f);
Vector3f v2 = new Vector3f(1.6f, 3.2f, 4.7f);
Vector3f expected = new Vector3f(2.6f, 4.2f, 5.7f);
Vector3f actual = v1 + v2;
Assert.AreEqual(actual, expected);
}
[Test]
public void vector_sub_works()
{
Vector3f v1 = new Vector3f(1.1f, 1.1f, 1.1f);
Vector3f v2 = new Vector3f(0.0f, 1.0f, 1.0f);
Vector3f expected = new Vector3f(1.1f, 0.1f, 0.1f);
Vector3f actual = v1 - v2;
Assert.AreEqual(actual, expected);
}