旋转点测试失败。为什么?

时间:2017-03-01 10:23:05

标签: c# visual-studio unit-testing

这是我失败的Point.cs文件。

// Rotates the point counter-clockwise by deg degrees.  For example, applying
// a 90 degree rotation to (1, 0) should give (0, 1).
public void rotate(double deg)
{
    double rotCos = Math.Cos(deg * Math.PI / 180);
    double rotSin = Math.Sin(deg * Math.PI / 180);
    double oldX = x;
    double oldY = y;
    x = oldX * rotCos - oldY * rotSin;
    y = oldX * rotSin + oldY * rotCos;
    Point point = new Point(x, y);
}

我运行此测试并获得结果:

  

"消息:Assert.AreEqual失败。预期:其中2。实际:&LT 2 - ;"

[TestMethod]
public void Rotate_Test()
{
    double rotate = -90;
    double i = 4;
    double o = 2;
    Point point = new Point(i, o);
    double expectedX = 2;
    double expectedY = 4;

    point.rotate(rotate);

    Assert.AreEqual(expectedX, point.getX());
    Assert.AreEqual(expectedY, point.getY());
}

1 个答案:

答案 0 :(得分:2)

双打不是确切的数字。如果没有在它们之间指定可接受的差异,则不应对它们进行比较。

https://msdn.microsoft.com/en-us/library/ms243458.aspx

https://msdn.microsoft.com/en-us/library/ya2zha7s(v=vs.110).aspx

尝试

Assert.AreEqual(expectedX, point.getX(), 0.001);