PDA

View Full Version : The best way for testing "division by zero".



8Observer8
5th October 2013, 17:20
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


HEADERS += \
complex.h

SOURCES += \
test.cpp

CONFIG += qtestlib


complex.h


#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


Added after 4 minutes:

test.cpp


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

// ================================================== ====================
class Test_Complex : public QObject {
Q_OBJECT
private 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"


Thank you!

anda_skoa
5th October 2013, 18:33
What do you want to test? That division by zero throws an expection?

Cheers,
_

8Observer8
5th October 2013, 18:40
Yes. I want to test this function:



Complex operator/(Complex c) {
Complex quotient;
quotient.m_im = m_im / c.m_im;
quotient.m_re = m_re / c.m_re;
return quotient;
}


Show me the best way, please.

andre_teprom
5th October 2013, 18:52
As said by anda_skoa, you can cath the exception.


+++

8Observer8
5th October 2013, 19:19
Ok. I will use the exception:



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;
}