Results 1 to 12 of 12

Thread: QtTest and asserts

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest and asserts

    How is it better than:
    Qt Code:
    1. static float myFunction(float value)
    2. {
    3. if (value <= 0.0) // check precondition
    4. {
    5. return eNAN;
    6. }
    7. return 2 / value;
    8. }
    To copy to clipboard, switch view to plain text mode 
    ?

    I realize that after your function is completely polished you can remove your assert and doing "if" will not take time. But, I think that your function should be error-resistable, since it gets value inputted by someone. Your function should take care of exceptional/errorneous situation (in your case, it is division by zero).
    Lets pretend that your app reads numbers from some file and does divisions for them. This file is created by somebody who put 0 in that file.
    Way 1: Your application is crashed by your assert only because someone was not careful enough and put 0 in that file.
    Way 2: On processing that division by 0, your application returns NAN value and reports that wrong arguments have been specified.

    What would you choose? I think that the 2nd way is more correct.
    I'm a rebel in the S.D.G.

  2. #2
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtTest and asserts

    We seem to be moving a little away from my actual question but I'm very interested in this more generic topic as well. My answer to your question would (as always) be: IT DEPENDS!
    It depends on what the contract between invoking function and the invoked one specifies and this can be to either not to care at all (most common), return a status and leave it to the caller to decide (your suggestion) or impose a strict rule of parameter passing using a precondition (this is what I wanted to do).

    A more realistic (longer) version of myClass::myFunction would typically look like this:
    Qt Code:
    1. float myClass::myFunction(float value)
    2. {
    3. /* test consistency of class */
    4. my_class_invariant(...);
    5.  
    6. /*test consistency of parameter */
    7. my_precondition(value > 0, "you are using an invalid value=("+toString(value)+")");
    8.  
    9. /* usually the algorithms are "little" more complex */
    10. float temp = 10.0 / value;
    11.  
    12. /* test consistency of intermediate result */
    13. my_assert(temp > 0.0 && ...);
    14.  
    15. /* test consistency of result */
    16. my_postcondition(fuzzy_compare(temp * value, 10.0);
    17.  
    18. return temp;
    19. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTTest and my own gui
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 19th March 2010, 10:26
  2. QtTest example not compiling
    By GrahamLabdon in forum Newbie
    Replies: 3
    Last Post: 19th March 2010, 09:41
  3. redirecting ASSERTs
    By drhex in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2009, 21:59
  4. Back tracing asserts with gdb
    By ucomesdag in forum Qt Programming
    Replies: 8
    Last Post: 30th July 2007, 21:59
  5. QtTest bug
    By graeme in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:16

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.