Amit Rege
Amit Rege

Reputation: 37

Why doesn't QCOMPARE return True in this case?

I'm writing tests for functions which are yet to be implemented. In the code below,even though the output and result vectors have the same value QCOMPARE returns False. Could someone explain why?

void EigenValuesTest::EigenValuesTestx2_data()
{
QTest::addColumn<Eigen::MatrixXd>("data");

Eigen::MatrixXd a(2,2);
a<<12,3,4,5;

QTest::newRow("0") << a ;
}

void EigenValuesTest::EigenValuesTestx2()
{
QFETCH(Eigen::MatrixXd, data);

Eigen::EigenSolver<Eigen::MatrixXd> es(data,false);

Eigen::Vector2cd result;
result << std::complex<double>(13.4244,0),std::complex<double>(3.57557,0);

Eigen::Vector2cd output;
output = es.eigenvalues();

QCOMPARE(result,output);

}

Upvotes: 0

Views: 925

Answers (2)

UmNyobe
UmNyobe

Reputation: 22890

You need to use

QCOMPARE(result.isApprox(output), true);

From the Eigen documentation,

bool operator== (const MatrixBase< OtherDerived > & other) const

Returns true if each coefficients of *this and other are all exactly equal. Warning When using floating point scalar values you probably should rather use a fuzzy comparison such as isApprox() See Also isApprox(), operator!=

edit:
Your eigen vectors are incorrect. The zeros should be ones, ie (13.4244, 1.0), (3.57557, 1.0). Furthermore, they might appear in different order than the ones specified in the reference vector result. You need to take that in account for a more solid test.

Upvotes: 2

maxik
maxik

Reputation: 1123

I would like to quote from the documentation

In the case of comparing floats and doubles, qFuzzyCompare() is used for comparing. This means that comparing to 0 will likely fail. One solution to this is to compare to 1, and add 1 to the produced output.

So far, so good. Done automatically, but now

Note that comparing values where either p1 or p2 is 0.0 will not work. The solution to this is to compare against values greater than or equal to 1.0.

might result in a problem because you have zeros in your compared values.

Moreover I cannot say for sure, if Q_COMPARE works for the provided types. Any output may be helpful.

QCOMPARE tries to output the contents of the values if the comparison fails, so it is visible from the test log why the comparison failed.

Upvotes: 0

Related Questions