Hi everybody
I want to compare double numbers like in CppUnit: CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
Thanks
Ivan
Printable View
Hi everybody
I want to compare double numbers like in CppUnit: CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
Thanks
Ivan
Code:
QCOMPARE(expected+1, actual+1);
I want to compare with 'delta'. Because I have double numbers
Output:
Code:
Starting C:\Users\Ivan\qt_projects\TestSphere\debug\TestSphere.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\TestSphere.exe exited with code 2
This is my code:
TestSphere.pro
Code:
SOURCES += \ main.cpp \ testsphere.cpp \ sphere.cpp HEADERS += \ testsphere.h \ sphere.h CONFIG += \ qtestlib console
main.cpp
Code:
#include <QtTest> #include "testsphere.h" QTEST_MAIN(TestSphere)
testsphere.h
Code:
#ifndef TESTSPHERE_H #define TESTSPHERE_H #include <QObject> { Q_OBJECT private slots: void testCalcArea(); void testCalcVolume(); }; #endif // TESTSPHERE_H
testsphere.cpp
Code:
#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
Code:
#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
Code:
#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; }
Then compare with delta.
qFuzzyCompare has an internal delta it uses but if you want your own then simply do this:
Code:
QVERIFY(actual-delta <= expected && actual+delta >=expected)
This is qFuzzyCompare:
You can implement your own function that takes the delta as a parameter.Code:
static inline bool qFuzzyCompare(double p1, double p2) { return (qAbs(p1 - p2) <= 0.000000000001 * qMin(qAbs(p1), qAbs(p2))); }
Thank you very much!