tests/auto/qsharedpointer/tst_qsharedpointer.cpp
changeset 30 5dc02b23752f
parent 22 79de32ba3296
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #define QT_SHAREDPOINTER_TRACK_POINTERS
    42 #define QT_SHAREDPOINTER_TRACK_POINTERS
    43 #include "qsharedpointer.h"
    43 #include "qsharedpointer.h"
    44 #include <QtTest/QtTest>
    44 #include <QtTest/QtTest>
       
    45 #include <QtCore/QHash>
       
    46 #include <QtCore/QMap>
    45 #include <QtCore/QThread>
    47 #include <QtCore/QThread>
    46 #include <QtCore/QVector>
    48 #include <QtCore/QVector>
    47 
    49 
    48 #include "externaltests.h"
    50 #include "externaltests.h"
    49 #include "wrapper.h"
    51 #include "wrapper.h"
    66     Q_OBJECT
    68     Q_OBJECT
    67 
    69 
    68 private slots:
    70 private slots:
    69     void basics_data();
    71     void basics_data();
    70     void basics();
    72     void basics();
       
    73     void operators();
    71     void swap();
    74     void swap();
    72     void forwardDeclaration1();
    75     void forwardDeclaration1();
    73     void forwardDeclaration2();
    76     void forwardDeclaration2();
    74     void memoryManagement();
    77     void memoryManagement();
    75     void downCast();
    78     void downCast();
    92     void creating();
    95     void creating();
    93     void creatingQObject();
    96     void creatingQObject();
    94     void mixTrackingPointerCode();
    97     void mixTrackingPointerCode();
    95     void threadStressTest_data();
    98     void threadStressTest_data();
    96     void threadStressTest();
    99     void threadStressTest();
       
   100     void map();
       
   101     void hash();
    97     void validConstructs();
   102     void validConstructs();
    98     void invalidConstructs_data();
   103     void invalidConstructs_data();
    99     void invalidConstructs();
   104     void invalidConstructs();
   100 
   105 
   101 public slots:
   106 public slots:
   267     }
   272     }
   268     QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1);
   273     QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1);
   269     QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1);
   274     QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1);
   270 
   275 
   271     // aData is deleted here
   276     // aData is deleted here
       
   277 }
       
   278 
       
   279 void tst_QSharedPointer::operators()
       
   280 {
       
   281     QSharedPointer<char> p1;
       
   282     QSharedPointer<char> p2(new char);
       
   283     qptrdiff diff = p2.data() - p1.data();
       
   284     Q_ASSERT(p1.data() < p2.data());
       
   285     Q_ASSERT(diff > 0);
       
   286 
       
   287     // operator-
       
   288     QCOMPARE(p2 - p1.data(), diff);
       
   289     QCOMPARE(p2.data() - p1, diff);
       
   290     QCOMPARE(p2 - p1, diff);
       
   291     QCOMPARE(p1 - p2, -diff);
       
   292     QCOMPARE(p1 - p1, qptrdiff(0));
       
   293     QCOMPARE(p2 - p2, qptrdiff(0));
       
   294 
       
   295     // operator<
       
   296     QVERIFY(p1 < p2.data());
       
   297     QVERIFY(p1.data() < p2);
       
   298     QVERIFY(p1 < p2);
       
   299     QVERIFY(!(p2 < p1));
       
   300     QVERIFY(!(p2 < p2));
       
   301     QVERIFY(!(p1 < p1));
       
   302 
       
   303     // qHash
       
   304     QCOMPARE(qHash(p1), qHash(p1.data()));
       
   305     QCOMPARE(qHash(p2), qHash(p2.data()));
   272 }
   306 }
   273 
   307 
   274 void tst_QSharedPointer::swap()
   308 void tst_QSharedPointer::swap()
   275 {
   309 {
   276     QSharedPointer<int> p1, p2(new int(42)), control = p2;
   310     QSharedPointer<int> p1, p2(new int(42)), control = p2;
  1540         int minValue = strongThreadCount;
  1574         int minValue = strongThreadCount;
  1541         int maxValue = strongThreadCount + weakThreadCount;
  1575         int maxValue = strongThreadCount + weakThreadCount;
  1542         QVERIFY(counter >= minValue);
  1576         QVERIFY(counter >= minValue);
  1543         QVERIFY(counter <= maxValue);
  1577         QVERIFY(counter <= maxValue);
  1544     }
  1578     }
       
  1579 }
       
  1580 
       
  1581 template<typename Container, bool Ordered>
       
  1582 void hashAndMapTest()
       
  1583 {
       
  1584     typedef typename Container::key_type Key;
       
  1585     typedef typename Container::mapped_type Value;
       
  1586 
       
  1587     Container c;
       
  1588     QVERIFY(c.isEmpty());
       
  1589 
       
  1590     Key k0;
       
  1591     c.insert(k0, Value(0));
       
  1592     QVERIFY(!c.isEmpty());
       
  1593 
       
  1594     typename Container::iterator it;
       
  1595     it = c.find(k0);
       
  1596     QVERIFY(it != c.end());
       
  1597     it = c.find(Key());
       
  1598     QVERIFY(it != c.end());
       
  1599     it = c.find(Key(0));
       
  1600     QVERIFY(it != c.end());
       
  1601 
       
  1602     Key k1(new typename Key::value_type(42));
       
  1603     it = c.find(k1);
       
  1604     QVERIFY(it == c.end());
       
  1605 
       
  1606     c.insert(k1, Value(42));
       
  1607     it = c.find(k1);
       
  1608     QVERIFY(it != c.end());
       
  1609     QVERIFY(it != c.find(Key()));
       
  1610 
       
  1611     if (Ordered) {
       
  1612         Q_ASSERT(k0 < k1);
       
  1613 
       
  1614         it = c.begin();
       
  1615         QCOMPARE(it.key(), k0);
       
  1616         QCOMPARE(it.value(), Value(0));
       
  1617 
       
  1618         ++it;
       
  1619         QCOMPARE(it.key(), k1);
       
  1620         QCOMPARE(it.value(), Value(42));
       
  1621 
       
  1622         ++it;
       
  1623         QVERIFY(it == c.end());
       
  1624     }
       
  1625 
       
  1626     c.insertMulti(k1, Value(47));
       
  1627     it = c.find(k1);
       
  1628     QVERIFY(it != c.end());
       
  1629     QCOMPARE(it.key(), k1);
       
  1630     ++it;
       
  1631     QVERIFY(it != c.end());
       
  1632     QCOMPARE(it.key(), k1);
       
  1633     ++it;
       
  1634     QVERIFY(it == c.end());
       
  1635 }
       
  1636 
       
  1637 void tst_QSharedPointer::map()
       
  1638 {
       
  1639     hashAndMapTest<QMap<QSharedPointer<int>, int>, true>();
       
  1640 }
       
  1641 
       
  1642 void tst_QSharedPointer::hash()
       
  1643 {
       
  1644     hashAndMapTest<QHash<QSharedPointer<int>, int>, false>();
  1545 }
  1645 }
  1546 
  1646 
  1547 void tst_QSharedPointer::validConstructs()
  1647 void tst_QSharedPointer::validConstructs()
  1548 {
  1648 {
  1549     {
  1649     {