Results 1 to 5 of 5

Thread: QtTest. How to compare double numbers

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default QtTest. How to compare double numbers

    Hi everybody

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

    Thanks

    Ivan

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtTest. How to compare double numbers

    Qt Code:
    1. QCOMPARE(expected+1, actual+1);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: QtTest. How to compare double numbers

    I want to compare with 'delta'. Because I have double numbers

    Output:
    Qt Code:
    1. Starting C:\Users\Ivan\qt_projects\TestSphere\debug\TestSphere.exe...
    2. ********* Start testing of TestSphere *********
    3. Config: Using QTest library 4.8.1, Qt 4.8.1
    4. PASS : TestSphere::initTestCase()
    5. FAIL! : TestSphere::testCalcArea() Compared doubles are not the same (fuzzy compare)
    6. Actual (s.calcArea(6)): 452.389
    7. Expected (452.389): 452.389
    8. testsphere.cpp(9) : failure location
    9. FAIL! : TestSphere::testCalcVolume() Compared doubles are not the same (fuzzy compare)
    10. Actual (s.calcVolume(6)): 904.779
    11. Expected (904.779): 904.779
    12. testsphere.cpp(18) : failure location
    13. PASS : TestSphere::cleanupTestCase()
    14. Totals: 2 passed, 2 failed, 0 skipped
    15. ********* Finished testing of TestSphere *********
    16. ********* Start testing of TestSphere *********
    17. Config: Using QTest library 4.8.1, Qt 4.8.1
    18. PASS : TestSphere::initTestCase()
    19. FAIL! : TestSphere::testCalcArea() Compared doubles are not the same (fuzzy compare)
    20. Actual (s.calcArea(6)): 452.389
    21. Expected (452.389): 452.389
    22. testsphere.cpp(9) : failure location
    23. FAIL! : TestSphere::testCalcVolume() Compared doubles are not the same (fuzzy compare)
    24. Actual (s.calcVolume(6)): 904.779
    25. Expected (904.779): 904.779
    26. testsphere.cpp(18) : failure location
    27. PASS : TestSphere::cleanupTestCase()
    28. Totals: 2 passed, 2 failed, 0 skipped
    29. ********* Finished testing of TestSphere *********
    30. C:\Users\Ivan\qt_projects\TestSphere\debug\TestSphere.exe exited with code 2
    To copy to clipboard, switch view to plain text mode 

    This is my code:

    TestSphere.pro
    Qt Code:
    1. SOURCES += \
    2. main.cpp \
    3. testsphere.cpp \
    4. sphere.cpp
    5.  
    6. HEADERS += \
    7. testsphere.h \
    8. sphere.h
    9.  
    10. CONFIG += \
    11. qtestlib console
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtTest>
    2. #include "testsphere.h"
    3.  
    4. QTEST_MAIN(TestSphere)
    To copy to clipboard, switch view to plain text mode 

    testsphere.h
    Qt Code:
    1. #ifndef TESTSPHERE_H
    2. #define TESTSPHERE_H
    3.  
    4. #include <QObject>
    5.  
    6. class TestSphere : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. private slots:
    11. void testCalcArea();
    12. void testCalcVolume();
    13. };
    14.  
    15. #endif // TESTSPHERE_H
    To copy to clipboard, switch view to plain text mode 

    testsphere.cpp
    Qt Code:
    1. #include <QtTest>
    2. #include "sphere.h"
    3. #include "testsphere.h"
    4.  
    5. void TestSphere::testCalcArea()
    6. {
    7. Sphere s;
    8.  
    9. QCOMPARE(s.calcArea(6), 452.389);
    10. QCOMPARE(s.calcArea(7), 615.752);
    11. QCOMPARE(s.calcArea(8), 804.2477);
    12. }
    13.  
    14. void TestSphere::testCalcVolume()
    15. {
    16. Sphere s;
    17.  
    18. QCOMPARE(s.calcVolume(6), 904.779);
    19. QCOMPARE(s.calcVolume(7), 1436.755);
    20. QCOMPARE(s.calcVolume(8), 2144.661);
    21. }
    To copy to clipboard, switch view to plain text mode 

    sphere.h
    Qt Code:
    1. #ifndef SPHERE_H
    2. #define SPHERE_H
    3.  
    4. class Sphere {
    5. public:
    6. Sphere();
    7. double calcArea(double r);
    8. double calcVolume(double r);
    9. private:
    10. double area;
    11. double volume;
    12. };
    13.  
    14. #endif /* SPHERE_H */
    To copy to clipboard, switch view to plain text mode 

    sphere.cpp
    Qt Code:
    1. #include "sphere.h"
    2. #include <cmath>
    3.  
    4. Sphere::Sphere() {
    5. area = 0;
    6. volume = 0;
    7. }
    8.  
    9. double Sphere::calcArea(double r) {
    10. area = 4.0 * M_PI * pow(r, 2.0);
    11. return area;
    12. }
    13.  
    14. double Sphere::calcVolume(double r) {
    15. volume = (4.0/3.0) * M_PI * pow(r, 3.0);
    16. return volume;
    17. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtTest. How to compare double numbers

    Then compare with delta.

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

    Qt Code:
    1. QVERIFY(actual-delta <= expected && actual+delta >=expected)
    To copy to clipboard, switch view to plain text mode 

    This is qFuzzyCompare:

    Qt Code:
    1. static inline bool qFuzzyCompare(double p1, double p2)
    2. {
    3. return (qAbs(p1 - p2) <= 0.000000000001 * qMin(qAbs(p1), qAbs(p2)));
    4. }
    To copy to clipboard, switch view to plain text mode 
    You can implement your own function that takes the delta as a parameter.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    8Observer8 (22nd January 2013)

  6. #5
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: QtTest. How to compare double numbers

    Thank you very much!

Similar Threads

  1. qttest output
    By BalaQT in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2011, 17:17
  2. Program not finding an existing double in a QList<double>
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 20:59
  3. Replies: 3
    Last Post: 4th March 2011, 23:49
  4. QTTest and my own gui
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 19th March 2010, 10:26
  5. QtTest bug
    By graeme in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.