PDA

View Full Version : QtTest. How to compare double numbers



8Observer8
22nd January 2013, 07:10
Hi everybody

I want to compare double numbers like in CppUnit: CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)

Thanks

Ivan

wysota
22nd January 2013, 11:59
QCOMPARE(expected+1, actual+1);

8Observer8
22nd January 2013, 12:15
I want to compare with 'delta'. Because I have double numbers

Output:


Starting C:\Users\Ivan\qt_projects\TestSphere\debug\TestSph ere.exe...
********* Start testing of TestSphere *********
Config: Using QTest library 4.8.1, Qt 4.8.1
PASS : TestSphere::initTestCase()
FAIL! : TestSphere::testCalcArea() Compared doubles are not the same (fuzzy compare)
Actual (s.calcArea(6)): 452.389
Expected (452.389): 452.389
testsphere.cpp(9) : failure location
FAIL! : TestSphere::testCalcVolume() Compared doubles are not the same (fuzzy compare)
Actual (s.calcVolume(6)): 904.779
Expected (904.779): 904.779
testsphere.cpp(18) : failure location
PASS : TestSphere::cleanupTestCase()
Totals: 2 passed, 2 failed, 0 skipped
********* Finished testing of TestSphere *********
********* Start testing of TestSphere *********
Config: Using QTest library 4.8.1, Qt 4.8.1
PASS : TestSphere::initTestCase()
FAIL! : TestSphere::testCalcArea() Compared doubles are not the same (fuzzy compare)
Actual (s.calcArea(6)): 452.389
Expected (452.389): 452.389
testsphere.cpp(9) : failure location
FAIL! : TestSphere::testCalcVolume() Compared doubles are not the same (fuzzy compare)
Actual (s.calcVolume(6)): 904.779
Expected (904.779): 904.779
testsphere.cpp(18) : failure location
PASS : TestSphere::cleanupTestCase()
Totals: 2 passed, 2 failed, 0 skipped
********* Finished testing of TestSphere *********
C:\Users\Ivan\qt_projects\TestSphere\debug\TestSph ere.exe exited with code 2

This is my code:

TestSphere.pro


SOURCES += \
main.cpp \
testsphere.cpp \
sphere.cpp

HEADERS += \
testsphere.h \
sphere.h

CONFIG += \
qtestlib console


main.cpp


#include <QtTest>
#include "testsphere.h"

QTEST_MAIN(TestSphere)


testsphere.h


#ifndef TESTSPHERE_H
#define TESTSPHERE_H

#include <QObject>

class TestSphere : public QObject
{
Q_OBJECT

private slots:
void testCalcArea();
void testCalcVolume();
};

#endif // TESTSPHERE_H


testsphere.cpp


#include <QtTest>
#include "sphere.h"
#include "testsphere.h"

void TestSphere::testCalcArea()
{
Sphere s;

QCOMPARE(s.calcArea(6), 452.389);
QCOMPARE(s.calcArea(7), 615.752);
QCOMPARE(s.calcArea(8), 804.2477);
}

void TestSphere::testCalcVolume()
{
Sphere s;

QCOMPARE(s.calcVolume(6), 904.779);
QCOMPARE(s.calcVolume(7), 1436.755);
QCOMPARE(s.calcVolume(8), 2144.661);
}


sphere.h


#ifndef SPHERE_H
#define SPHERE_H

class Sphere {
public:
Sphere();
double calcArea(double r);
double calcVolume(double r);
private:
double area;
double volume;
};

#endif /* SPHERE_H */


sphere.cpp


#include "sphere.h"
#include <cmath>

Sphere::Sphere() {
area = 0;
volume = 0;
}

double Sphere::calcArea(double r) {
area = 4.0 * M_PI * pow(r, 2.0);
return area;
}

double Sphere::calcVolume(double r) {
volume = (4.0/3.0) * M_PI * pow(r, 3.0);
return volume;
}

wysota
22nd January 2013, 13:37
Then compare with delta.

qFuzzyCompare has an internal delta it uses but if you want your own then simply do this:


QVERIFY(actual-delta <= expected && actual+delta >=expected)

This is qFuzzyCompare:


static inline bool qFuzzyCompare(double p1, double p2)
{
return (qAbs(p1 - p2) <= 0.000000000001 * qMin(qAbs(p1), qAbs(p2)));
}
You can implement your own function that takes the delta as a parameter.

8Observer8
22nd January 2013, 13:39
Thank you very much!