Thread: The best way for testing "division by zero".

1. Re: The best way for testing "division by zero".

I found my way of the development software by Test-driven development. I love Qt and qtestlib module!

How I need to test "division by zero" for "operator/"? Can I use exceptions in Qt? Please talk me about the best way for testing "division by zero" for "operator/".

This is my example:

TestLib_Complex_table.pro
Qt Code:
`HEADERS += \    complex.h SOURCES += \    test.cpp CONFIG += qtestlib`
To copy to clipboard, switch view to plain text mode

complex.h
Qt Code:
`#ifndef COMPLEX_H#define COMPLEX_H class Complex {public:    Complex(double i = 0, double r = 0)        :m_im(i), m_re(r){}     void setIm(double i) {        m_im = i;    }    void setRe(double r) {        m_re = r;    }    double getIm() {        return m_im;    }    double getRe() {        return m_re;    }     Complex operator+(Complex c) {        Complex sum;            sum.m_im = m_im + c.m_im;            sum.m_re = m_re + c.m_re;        return sum;    }     Complex operator-(Complex c) {        Complex difference;        difference.m_im = m_im - c.m_im;        difference.m_re = m_re - c.m_re;        return difference;    }     Complex operator*(Complex c) {        Complex product;        product.m_im = m_im * c.m_im;        product.m_re = m_re * c.m_re;        return product;    }     Complex operator/(Complex c) {        Complex quotient;        quotient.m_im = m_im / c.m_im;        quotient.m_re = m_re / c.m_re;        return quotient;    } private:    double m_im;    double m_re;}; #endif // COMPLEX_H`
To copy to clipboard, switch view to plain text mode

Added after 4 minutes:

test.cpp
Qt Code:
`#include <QtTest>#include "complex.h" // ======================================================================class Test_Complex : public QObject {    Q_OBJECTprivate slots:    void setIm_data();    void setRe_data();    void operator_plus_data();    void operator_minus_data();    void operator_multiply_data();    void operator_divide_data();     void setIm();    void setRe();    void operator_plus();    void operator_minus();    void operator_multiply();    void operator_divide();     static inline bool qFuzzyCompare(double p1, double p2, double delta)    {        return (qAbs(p1 - p2) <= delta * qMin(qAbs(p1), qAbs(p2)));    }}; // ----------------------------------------------------------------------void Test_Complex::setIm_data(){    QTest::addColumn<double>("arg1");    QTest::addColumn<double>("result");     QTest::newRow("setIm_test1") << 0.0 << 0.0;    QTest::newRow("setIm_test2") << -12.5 << -12.5;    QTest::newRow("setIm_test3") << 2007.0 << 2007.0;    QTest::newRow("setIm_test4") << 5.1 << 5.1;} // ----------------------------------------------------------------------void Test_Complex::setRe_data(){    QTest::addColumn<double>("arg1");    QTest::addColumn<double>("result");     QTest::newRow("setRe_test1") << 0.0 << 0.0;    QTest::newRow("setRe_test2") << -12.5 << -12.5;    QTest::newRow("setRe_test3") << 2007.0 << 2007.0;    QTest::newRow("setRe_test4") << 5.1 << 5.1;} // ----------------------------------------------------------------------void Test_Complex::operator_plus_data(){    QTest::addColumn<double>("input_im1");    QTest::addColumn<double>("input_re1");    QTest::addColumn<double>("input_im2");    QTest::addColumn<double>("input_re2");    QTest::addColumn<double>("result_im");    QTest::addColumn<double>("result_re");     QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 6.8 << 5.5;    QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -1.2 << -1.1;    QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << 4.2 << 1.1;    QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << 1.3 << -2.2;} // ----------------------------------------------------------------------void Test_Complex::operator_minus_data(){    QTest::addColumn<double>("input_im1");    QTest::addColumn<double>("input_re1");    QTest::addColumn<double>("input_im2");    QTest::addColumn<double>("input_re2");    QTest::addColumn<double>("result_im");    QTest::addColumn<double>("result_re");     QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 4.2 << 1.1;    QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -3.8 << -5.5;    QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << 6.8 << 5.5;    QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << -1.3 << 2.2;} // ----------------------------------------------------------------------void Test_Complex::operator_multiply_data(){    QTest::addColumn<double>("input_im1");    QTest::addColumn<double>("input_re1");    QTest::addColumn<double>("input_im2");    QTest::addColumn<double>("input_re2");    QTest::addColumn<double>("result_im");    QTest::addColumn<double>("result_re");     QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 7.15 << 7.26;    QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -3.25 << -7.26;    QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << -7.15 << -7.26;    QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << 0.0 << 0.0;} // ----------------------------------------------------------------------void Test_Complex::operator_divide_data(){    QTest::addColumn<double>("input_im1");    QTest::addColumn<double>("input_re1");    QTest::addColumn<double>("input_im2");    QTest::addColumn<double>("input_re2");    QTest::addColumn<double>("result_im");    QTest::addColumn<double>("result_re");     QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 4.230769230769231 << 1.5;    QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -1.923076923076923 << -1.5;    QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << -4.230769230769231 << -1.5;    QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << 0.0 << 0.0;} // ----------------------------------------------------------------------void Test_Complex::setIm(){    Complex complex;    QFETCH(double, arg1);    QFETCH(double, result);     complex.setIm(arg1);     QCOMPARE(complex.getIm(), result);} // ----------------------------------------------------------------------void Test_Complex::setRe(){    Complex complex;    QFETCH(double, arg1);    QFETCH(double, result);     complex.setRe(arg1);     QCOMPARE(complex.getRe(), result);} // ----------------------------------------------------------------------void Test_Complex::operator_plus(){    QFETCH(double, input_im1);    QFETCH(double, input_re1);    QFETCH(double, input_im2);    QFETCH(double, input_re2);    QFETCH(double, result_im);    QFETCH(double, result_re);     Complex complex1;    complex1.setIm(input_im1);    complex1.setRe(input_re1);     Complex complex2;    complex2.setIm(input_im2);    complex2.setRe(input_re2);     Complex complex3;    complex3 = complex1 + complex2;    double im = complex3.getIm();    double re = complex3.getRe();     double delta = 0.001;     QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);    QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());    message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);    QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());} // ----------------------------------------------------------------------void Test_Complex::operator_minus(){    QFETCH(double, input_im1);    QFETCH(double, input_re1);    QFETCH(double, input_im2);    QFETCH(double, input_re2);    QFETCH(double, result_im);    QFETCH(double, result_re);     Complex complex1;    complex1.setIm(input_im1);    complex1.setRe(input_re1);     Complex complex2;    complex2.setIm(input_im2);    complex2.setRe(input_re2);     Complex complex3;    complex3 = complex1 - complex2;    double im = complex3.getIm();    double re = complex3.getRe();     double delta = 0.001;     QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);    QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());    message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);    QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());} // ----------------------------------------------------------------------void Test_Complex::operator_multiply(){    QFETCH(double, input_im1);    QFETCH(double, input_re1);    QFETCH(double, input_im2);    QFETCH(double, input_re2);    QFETCH(double, result_im);    QFETCH(double, result_re);     Complex complex1;    complex1.setIm(input_im1);    complex1.setRe(input_re1);     Complex complex2;    complex2.setIm(input_im2);    complex2.setRe(input_re2);     Complex complex3;    complex3 = complex1 * complex2;    double im = complex3.getIm();    double re = complex3.getRe();     double delta = 0.001;     QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);    QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());    message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);    QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());} // ----------------------------------------------------------------------void Test_Complex::operator_divide(){    QFETCH(double, input_im1);    QFETCH(double, input_re1);    QFETCH(double, input_im2);    QFETCH(double, input_re2);    QFETCH(double, result_im);    QFETCH(double, result_re);     Complex complex1;    complex1.setIm(input_im1);    complex1.setRe(input_re1);     Complex complex2;    complex2.setIm(input_im2);    complex2.setRe(input_re2);     Complex complex3;    complex3 = complex1 / complex2;    double im = complex3.getIm();    double re = complex3.getRe();     double delta = 0.001;     QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);    QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());    message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);    QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());} QTEST_MAIN(Test_Complex)#include "test.moc"`
To copy to clipboard, switch view to plain text mode

Thank you!
Last edited by 8Observer8; 5th October 2013 at 18:20.

2. Administrator
Join Date
Jan 2006
Location
Graz, Austria
Posts
8,419
Thanks
37
Thanked 1,546 Times in 1,496 Posts
Qt products
Platforms

Re: The best way for testing "division by zero".

What do you want to test? That division by zero throws an expection?

Cheers,
_

3. The following user says thank you to anda_skoa for this useful post:

8Observer8 (5th October 2013)

4. Re: The best way for testing "division by zero".

Yes. I want to test this function:

Qt Code:
`Complex operator/(Complex c) {        Complex quotient;        quotient.m_im = m_im / c.m_im;        quotient.m_re = m_re / c.m_re;        return quotient;    }`
To copy to clipboard, switch view to plain text mode

Show me the best way, please.

5. Re: The best way for testing "division by zero".

As said by anda_skoa, you can cath the exception.

+++

6. The following user says thank you to andre_teprom for this useful post:

8Observer8 (5th October 2013)

7. Re: The best way for testing "division by zero".

Ok. I will use the exception:

Qt Code:
`Complex operator/(Complex c) throw(invalid_argument) {         if ( (c.m_im == 0) || (c.m_re == 0) ) {            throw invalid_argument("Error: division by zero.");        }         Complex quotient;        quotient.m_im = m_im / c.m_im;        quotient.m_re = m_re / c.m_re;        return quotient;    }`
To copy to clipboard, switch view to plain text mode
Last edited by 8Observer8; 5th October 2013 at 20:23.

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.