Results 1 to 5 of 5

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

  1. #1
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    228
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:
    1. HEADERS += \
    2. complex.h
    3.  
    4. SOURCES += \
    5. test.cpp
    6.  
    7. CONFIG += qtestlib
    To copy to clipboard, switch view to plain text mode 

    complex.h
    Qt Code:
    1. #ifndef COMPLEX_H
    2. #define COMPLEX_H
    3.  
    4. class Complex {
    5. public:
    6. Complex(double i = 0, double r = 0)
    7. :m_im(i), m_re(r){}
    8.  
    9. void setIm(double i) {
    10. m_im = i;
    11. }
    12. void setRe(double r) {
    13. m_re = r;
    14. }
    15. double getIm() {
    16. return m_im;
    17. }
    18. double getRe() {
    19. return m_re;
    20. }
    21.  
    22. Complex operator+(Complex c) {
    23. Complex sum;
    24. sum.m_im = m_im + c.m_im;
    25. sum.m_re = m_re + c.m_re;
    26. return sum;
    27. }
    28.  
    29. Complex operator-(Complex c) {
    30. Complex difference;
    31. difference.m_im = m_im - c.m_im;
    32. difference.m_re = m_re - c.m_re;
    33. return difference;
    34. }
    35.  
    36. Complex operator*(Complex c) {
    37. Complex product;
    38. product.m_im = m_im * c.m_im;
    39. product.m_re = m_re * c.m_re;
    40. return product;
    41. }
    42.  
    43. Complex operator/(Complex c) {
    44. Complex quotient;
    45. quotient.m_im = m_im / c.m_im;
    46. quotient.m_re = m_re / c.m_re;
    47. return quotient;
    48. }
    49.  
    50. private:
    51. double m_im;
    52. double m_re;
    53. };
    54.  
    55. #endif // COMPLEX_H
    To copy to clipboard, switch view to plain text mode 


    Added after 4 minutes:


    test.cpp
    Qt Code:
    1. #include <QtTest>
    2. #include "complex.h"
    3.  
    4. // ======================================================================
    5. class Test_Complex : public QObject {
    6. Q_OBJECT
    7. private slots:
    8. void setIm_data();
    9. void setRe_data();
    10. void operator_plus_data();
    11. void operator_minus_data();
    12. void operator_multiply_data();
    13. void operator_divide_data();
    14.  
    15. void setIm();
    16. void setRe();
    17. void operator_plus();
    18. void operator_minus();
    19. void operator_multiply();
    20. void operator_divide();
    21.  
    22. static inline bool qFuzzyCompare(double p1, double p2, double delta)
    23. {
    24. return (qAbs(p1 - p2) <= delta * qMin(qAbs(p1), qAbs(p2)));
    25. }
    26. };
    27.  
    28. // ----------------------------------------------------------------------
    29. void Test_Complex::setIm_data()
    30. {
    31. QTest::addColumn<double>("arg1");
    32. QTest::addColumn<double>("result");
    33.  
    34. QTest::newRow("setIm_test1") << 0.0 << 0.0;
    35. QTest::newRow("setIm_test2") << -12.5 << -12.5;
    36. QTest::newRow("setIm_test3") << 2007.0 << 2007.0;
    37. QTest::newRow("setIm_test4") << 5.1 << 5.1;
    38. }
    39.  
    40. // ----------------------------------------------------------------------
    41. void Test_Complex::setRe_data()
    42. {
    43. QTest::addColumn<double>("arg1");
    44. QTest::addColumn<double>("result");
    45.  
    46. QTest::newRow("setRe_test1") << 0.0 << 0.0;
    47. QTest::newRow("setRe_test2") << -12.5 << -12.5;
    48. QTest::newRow("setRe_test3") << 2007.0 << 2007.0;
    49. QTest::newRow("setRe_test4") << 5.1 << 5.1;
    50. }
    51.  
    52. // ----------------------------------------------------------------------
    53. void Test_Complex::operator_plus_data()
    54. {
    55. QTest::addColumn<double>("input_im1");
    56. QTest::addColumn<double>("input_re1");
    57. QTest::addColumn<double>("input_im2");
    58. QTest::addColumn<double>("input_re2");
    59. QTest::addColumn<double>("result_im");
    60. QTest::addColumn<double>("result_re");
    61.  
    62. QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 6.8 << 5.5;
    63. QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -1.2 << -1.1;
    64. QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << 4.2 << 1.1;
    65. QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << 1.3 << -2.2;
    66. }
    67.  
    68. // ----------------------------------------------------------------------
    69. void Test_Complex::operator_minus_data()
    70. {
    71. QTest::addColumn<double>("input_im1");
    72. QTest::addColumn<double>("input_re1");
    73. QTest::addColumn<double>("input_im2");
    74. QTest::addColumn<double>("input_re2");
    75. QTest::addColumn<double>("result_im");
    76. QTest::addColumn<double>("result_re");
    77.  
    78. QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 4.2 << 1.1;
    79. QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -3.8 << -5.5;
    80. QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << 6.8 << 5.5;
    81. QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << -1.3 << 2.2;
    82. }
    83.  
    84. // ----------------------------------------------------------------------
    85. void Test_Complex::operator_multiply_data()
    86. {
    87. QTest::addColumn<double>("input_im1");
    88. QTest::addColumn<double>("input_re1");
    89. QTest::addColumn<double>("input_im2");
    90. QTest::addColumn<double>("input_re2");
    91. QTest::addColumn<double>("result_im");
    92. QTest::addColumn<double>("result_re");
    93.  
    94. QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 7.15 << 7.26;
    95. QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -3.25 << -7.26;
    96. QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << -7.15 << -7.26;
    97. QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << 0.0 << 0.0;
    98. }
    99.  
    100. // ----------------------------------------------------------------------
    101. void Test_Complex::operator_divide_data()
    102. {
    103. QTest::addColumn<double>("input_im1");
    104. QTest::addColumn<double>("input_re1");
    105. QTest::addColumn<double>("input_im2");
    106. QTest::addColumn<double>("input_re2");
    107. QTest::addColumn<double>("result_im");
    108. QTest::addColumn<double>("result_re");
    109.  
    110. QTest::newRow("operator_plus_test1") << 5.5 << 3.3 << 1.3 << 2.2 << 4.230769230769231 << 1.5;
    111. QTest::newRow("operator_plus_test2") << -2.5 << -3.3 << 1.3 << 2.2 << -1.923076923076923 << -1.5;
    112. QTest::newRow("operator_plus_test3") << 5.5 << 3.3 << -1.3 << -2.2 << -4.230769230769231 << -1.5;
    113. QTest::newRow("operator_plus_test4") << 0.0 << 0.0 << 1.3 << -2.2 << 0.0 << 0.0;
    114. }
    115.  
    116. // ----------------------------------------------------------------------
    117. void Test_Complex::setIm()
    118. {
    119. Complex complex;
    120. QFETCH(double, arg1);
    121. QFETCH(double, result);
    122.  
    123. complex.setIm(arg1);
    124.  
    125. QCOMPARE(complex.getIm(), result);
    126. }
    127.  
    128. // ----------------------------------------------------------------------
    129. void Test_Complex::setRe()
    130. {
    131. Complex complex;
    132. QFETCH(double, arg1);
    133. QFETCH(double, result);
    134.  
    135. complex.setRe(arg1);
    136.  
    137. QCOMPARE(complex.getRe(), result);
    138. }
    139.  
    140. // ----------------------------------------------------------------------
    141. void Test_Complex::operator_plus()
    142. {
    143. QFETCH(double, input_im1);
    144. QFETCH(double, input_re1);
    145. QFETCH(double, input_im2);
    146. QFETCH(double, input_re2);
    147. QFETCH(double, result_im);
    148. QFETCH(double, result_re);
    149.  
    150. Complex complex1;
    151. complex1.setIm(input_im1);
    152. complex1.setRe(input_re1);
    153.  
    154. Complex complex2;
    155. complex2.setIm(input_im2);
    156. complex2.setRe(input_re2);
    157.  
    158. Complex complex3;
    159. complex3 = complex1 + complex2;
    160. double im = complex3.getIm();
    161. double re = complex3.getRe();
    162.  
    163. double delta = 0.001;
    164.  
    165. QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);
    166. QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());
    167. message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);
    168. QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());
    169. }
    170.  
    171. // ----------------------------------------------------------------------
    172. void Test_Complex::operator_minus()
    173. {
    174. QFETCH(double, input_im1);
    175. QFETCH(double, input_re1);
    176. QFETCH(double, input_im2);
    177. QFETCH(double, input_re2);
    178. QFETCH(double, result_im);
    179. QFETCH(double, result_re);
    180.  
    181. Complex complex1;
    182. complex1.setIm(input_im1);
    183. complex1.setRe(input_re1);
    184.  
    185. Complex complex2;
    186. complex2.setIm(input_im2);
    187. complex2.setRe(input_re2);
    188.  
    189. Complex complex3;
    190. complex3 = complex1 - complex2;
    191. double im = complex3.getIm();
    192. double re = complex3.getRe();
    193.  
    194. double delta = 0.001;
    195.  
    196. QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);
    197. QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());
    198. message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);
    199. QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());
    200. }
    201.  
    202. // ----------------------------------------------------------------------
    203. void Test_Complex::operator_multiply()
    204. {
    205. QFETCH(double, input_im1);
    206. QFETCH(double, input_re1);
    207. QFETCH(double, input_im2);
    208. QFETCH(double, input_re2);
    209. QFETCH(double, result_im);
    210. QFETCH(double, result_re);
    211.  
    212. Complex complex1;
    213. complex1.setIm(input_im1);
    214. complex1.setRe(input_re1);
    215.  
    216. Complex complex2;
    217. complex2.setIm(input_im2);
    218. complex2.setRe(input_re2);
    219.  
    220. Complex complex3;
    221. complex3 = complex1 * complex2;
    222. double im = complex3.getIm();
    223. double re = complex3.getRe();
    224.  
    225. double delta = 0.001;
    226.  
    227. QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);
    228. QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());
    229. message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);
    230. QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());
    231. }
    232.  
    233. // ----------------------------------------------------------------------
    234. void Test_Complex::operator_divide()
    235. {
    236. QFETCH(double, input_im1);
    237. QFETCH(double, input_re1);
    238. QFETCH(double, input_im2);
    239. QFETCH(double, input_re2);
    240. QFETCH(double, result_im);
    241. QFETCH(double, result_re);
    242.  
    243. Complex complex1;
    244. complex1.setIm(input_im1);
    245. complex1.setRe(input_re1);
    246.  
    247. Complex complex2;
    248. complex2.setIm(input_im2);
    249. complex2.setRe(input_re2);
    250.  
    251. Complex complex3;
    252. complex3 = complex1 / complex2;
    253. double im = complex3.getIm();
    254. double re = complex3.getRe();
    255.  
    256. double delta = 0.001;
    257.  
    258. QString message = QString("im = %1, result_im = %2, delta = %3").arg(im).arg(result_im).arg(delta);
    259. QVERIFY2(qFuzzyCompare(im, result_im, delta), message.toAscii());
    260. message = QString("re = %1, result_re = %2, delta = %3").arg(re).arg(result_re).arg(delta);
    261. QVERIFY2(qFuzzyCompare(re, result_re, delta), message.toAscii());
    262. }
    263.  
    264. QTEST_MAIN(Test_Complex)
    265. #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. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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. #3
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    228
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    Yes. I want to test this function:

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

    Show me the best way, please.

  5. #4
    Join Date
    Jul 2011
    Location
    BRAZIL-RIO DE JANEIRO
    Posts
    47
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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. #5
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    228
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    Ok. I will use the exception:

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

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 22:46
  2. Replies: 3
    Last Post: 15th February 2010, 18:27
  3. Replies: 3
    Last Post: 8th July 2008, 20:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

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.