Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio 2008 unit test keeps failing

I've create a method that calculates the harmonic mean based on a list of doubles. But when I'm running the test it keeps failing even thou the output result are the same.

My harmonic mean method:

public static double GetHarmonicMean(List<double> parameters)
{
    var cumReciprocal = 0.0d;
    var countN = parameters.Count;

    foreach( var param in parameters)
    {
        cumReciprocal += 1.0d/param;
    }

    return 1.0d/(cumReciprocal/countN);
}

My test method:

[TestMethod()]
public void GetHarmonicMeanTest()
{
    var parameters = new List<double> { 1.5d, 2.3d, 2.9d, 1.9d, 5.6d };
    const double expected = 2.32432293165495; 
    var actual = OwnFunctions.GetHarmonicMean(parameters);
    Assert.AreEqual(expected, actual);
}

After running the test the following message is showing:

Assert.AreEqual failed. Expected:<2.32432293165495>. Actual:<2.32432293165495>.

For me that are both the same values.

Can somebody explain this? Or am I doing something wrong?

like image 256
Gerbrand Avatar asked Jan 23 '23 05:01

Gerbrand


1 Answers

The double overload for Assert.AreEqual takes a "delta" parameter to allow for the imprecision of doubles. You should specify a small value for this parameter.

like image 86
David M Avatar answered Jan 26 '23 01:01

David M