Results 1 to 9 of 9

Thread: if statement doesn't behave correctly

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

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