Results 1 to 9 of 9

Thread: if statement doesn't behave correctly

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default if statement doesn't behave correctly

    Hello all,

    I came across quite unpleasant problem.
    Little intro: I created templated Multiset class, suitable for use in Coloured Petri Nets, nothing fancy. As a private data holder, I'm using the QHash class.

    Let's have a look on the header:
    Qt Code:
    1. template<class T>
    2. class Multiset
    3. {
    4. typedef class QHash<T, uint>::iterator Iterator;
    5. typedef class QHash<T, uint>::const_iterator ConstIterator;
    6. public:
    7. Multiset();
    8. virtual ~Multiset();
    9. ...
    10. Multiset<T> operator-(const Multiset<T>& other) const;
    11. Multiset<T> operator*(uint scalar) const;
    12.  
    13. bool operator<=(const Multiset<T>& other) const;
    14. private:
    15. QHash<T, uint> m_values;
    16. };
    To copy to clipboard, switch view to plain text mode 

    I took the liberty to post only the relevant part.

    Now the method definitions (methods, which doesn't behave correctly):
    Qt Code:
    1. template<class T>
    2. Multiset<T> Multiset<T>::operator-(const Multiset<T>& other) const
    3. {
    4. Multiset<T> ret = *this;
    5.  
    6. ConstIterator itOther = other.m_values.constBegin();
    7.  
    8. if (!(ret <= other))
    9. {
    10. while (itOther != other.m_values.constEnd())
    11. {
    12. ret.m_values[itOther.key()] -= itOther.value();
    13. ++itOther;
    14. }
    15. }
    16.  
    17. return ret;
    18. }
    19. ...
    20. template<class T>
    21. bool Multiset<T>::operator<=(const Multiset<T>& other) const
    22. {
    23. bool ret = false;
    24.  
    25. ConstIterator itThis;
    26. ConstIterator itOther = other.m_values.constBegin();
    27.  
    28. while (itOther != other.m_values.constEnd())
    29. {
    30. itThis = m_values.find(itOther.key());
    31.  
    32. if (itThis == m_values.constEnd()
    33. || itThis.value() <= itOther.value())
    34. {
    35. ret = true;
    36. break;
    37. }
    38. ++itOther;
    39. }
    40.  
    41. return ret;
    42. }
    To copy to clipboard, switch view to plain text mode 

    My problem is, that no matter what the lessOrEqual operator returns, it always jumps inside the:
    Qt Code:
    1. if (!(ret <= other))
    2. {
    3. while (itOther != other.m_values.constEnd())
    4. {
    5. ret.m_values[itOther.key()] -= itOther.value();
    6. ++itOther;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    And it is causing the first series of QCOMPARE to fail in this testcase:
    Qt Code:
    1. void Multiset_mt::operatorMinus()
    2. {
    3. QFETCH(uint, value);
    4. QFETCH(uint, result);
    5.  
    6. QCOMPARE(value, result);
    7. }
    8.  
    9. void Multiset_mt::operatorMinus_data()
    10. {
    11. const uint first_color_size = 2;
    12. const uint second_color_size = 1;
    13. const uint third_color_size = 0;
    14. const uint not_valid_sub_size = 0;
    15.  
    16. Multiset<Color> ms1, ms2, msRes;
    17. Color c1("black"), c2("red"), c3("blue");
    18.  
    19. ms1.insert(c1);
    20. ms1.insert(c2);
    21. ms1.insert(c2);
    22. ms2.insert(c1);
    23. ms2.insert(c2);
    24. ms2.insert(c3);
    25.  
    26. msRes = ms1 - ms2;
    27.  
    28. QTest::addColumn<uint>("value");
    29. QTest::addColumn<uint>("result");
    30.  
    31. QTest::newRow("NOT VALID Size == 0") << msRes.size(c1) << not_valid_sub_size;
    32. QTest::newRow("NOT VALID Size == 0") << msRes.size(c2) << not_valid_sub_size;
    33. QTest::newRow("NOT VALID Size == 0") << msRes.size(c3) << not_valid_sub_size;
    34.  
    35. ms1.insert(c1);
    36. ms1.insert(c1);
    37. ms2.remove(c3);
    38.  
    39. msRes = ms1 - ms2;
    40.  
    41. QTest::newRow("Size == 2") << msRes.size(c1) << first_color_size;
    42. QTest::newRow("Size == 1") << msRes.size(c2) << second_color_size;
    43. QTest::newRow("Size == 0") << msRes.size(c3) << third_color_size;
    44. }
    To copy to clipboard, switch view to plain text mode 

    If anyone could help me with this, it would be really appreciated. Maybe I missed something. Thank you.

    I tried to also attach the whole files.
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    Your code doesnt work and you think the if statement is what's wrong? ok...

    Have you debugged your operator <= ?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    Hello, thank you for your answer.

    Actually my code does work, I've written separate module test also for operator <= and it works correctly. Of course I've debugged my code. As I'm saying, the <= returns true, bool is evaluated as false so it should not jump inside the if statement.

    I also tried to create a simple stub method like:
    Qt Code:
    1. bool Multiset<T>::getTrue() const
    2. {
    3. return true;
    4. }
    To copy to clipboard, switch view to plain text mode 

    and replace the bool expression with that method so it looked like:
    Qt Code:
    1. if (!getTrue())
    2. {
    3. while (itOther != other.m_values.constEnd())
    4. {
    5. ret.m_values[itOther.key()] -= itOther.value();
    6. ++itOther;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    but it jumps inside the if anyway.

  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: if statement doesn't behave correctly

    Please prepare a minimal compilable example reproducing the problem.
    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. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    I can tell you for a fact that your code does not work.

    Line 33 of your second code block:

    if you have a multiset1 with "a", "b", "b" and multiset2 with "a", "b", "c", "d" then
    multiset1 <= multiset2 is true
    multiset2 <= multiset1 is true

    just because "a" is in both sets.
    Last edited by amleto; 29th July 2012 at 13:28.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    Qt Code:
    1. if (!getTrue())
    2. {
    3. while (itOther != other.m_values.constEnd())
    4. {
    5. ret.m_values[itOther.key()] -= itOther.value();
    6. ++itOther;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    And what happens when you eliminate the method call altogether and write if ( false ) as the conditional? If it still fails to execute correctly, then I would strongly suspect that your stack is corrupted by something you did earlier on and that the failing conditional is simply an artifact.

    That's why Wysota says, "Provide a minimal compilable example." You might find that in a minimal case things magically start working properly, which should be taken as a strong hint that the problem is not where you are looking.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    amleto: And what is wrong with that? Operator is less OR equal. I think it returns correct result.

    d_stranz: hello, yes I have tried to change the code in that way and it seems that when I use value that is known in compile time, it works correctly.

    wysota: unfortunately I am away from my work computer and I am leaving for a vacation for a two weeks
    Last edited by esotery; 29th July 2012 at 20:06.

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    so you're saying that 'less than or equal' is true if both sets have a common element? That's... strange.

    to clarify:

    {a,b,b,b,b} is less than or equal to {a}?
    Last edited by amleto; 29th July 2012 at 22:19.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: if statement doesn't behave correctly

    unfortunately I am away from my work computer and I am leaving for a vacation for a two weeks
    Maybe you will be lucky and the code elves will fix your program while you are away.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 31st August 2011, 17:15
  2. Replies: 1
    Last Post: 1st June 2011, 19:37
  3. Replies: 0
    Last Post: 21st May 2011, 19:57
  4. QWebView doesn't display correctly unicode
    By binaural in forum Qt for Embedded and Mobile
    Replies: 16
    Last Post: 3rd September 2010, 09:24
  5. QGraphicsLinearLayout does not behave like QVBoxLayout
    By jobrandt in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2009, 10:59

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.