diff options
Diffstat (limited to 'tests/auto/qmutex/tst_qmutex.cpp')
-rw-r--r-- | tests/auto/qmutex/tst_qmutex.cpp | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/tests/auto/qmutex/tst_qmutex.cpp b/tests/auto/qmutex/tst_qmutex.cpp index a8c4b37715..0f2d982843 100644 --- a/tests/auto/qmutex/tst_qmutex.cpp +++ b/tests/auto/qmutex/tst_qmutex.cpp @@ -39,7 +39,6 @@ ** ****************************************************************************/ - #include <QtTest/QtTest> #include <qatomic.h> @@ -49,8 +48,6 @@ #include <qthread.h> #include <qwaitcondition.h> - - //TESTED_CLASS= //TESTED_FILES= @@ -372,14 +369,6 @@ public: } }; -#ifdef QT3_SUPPORT -#define VERIFY_LOCKED(x) QVERIFY((x).locked()) -#define VERIFY_NLOCKED(x) QVERIFY(!(x).locked()) -#else -#define VERIFY_LOCKED(x) -#define VERIFY_NLOCKED(x) -#endif // QT3_SUPPORT - void tst_QMutex::lock_unlock_locked_tryLock() { // normal mutex @@ -391,7 +380,6 @@ void tst_QMutex::lock_unlock_locked_tryLock() for (int i = 0; i < iterations; ++i) { // normal mutex - VERIFY_NLOCKED(mutex); QVERIFY(mutex.tryLock()); mutex.unlock(); @@ -400,7 +388,6 @@ void tst_QMutex::lock_unlock_locked_tryLock() for (int j = 0; j < iterations; ++j) { QVERIFY(thread.cond.wait(&thread.mutex, 10000)); - VERIFY_LOCKED(mutex); QVERIFY(!mutex.tryLock()); thread.cond.wakeOne(); @@ -409,13 +396,11 @@ void tst_QMutex::lock_unlock_locked_tryLock() thread.mutex.unlock(); QVERIFY(thread.wait(10000)); - VERIFY_NLOCKED(mutex); QVERIFY(mutex.tryLock()); mutex.unlock(); // recursive mutex - VERIFY_NLOCKED(rmutex); QVERIFY(rmutex.tryLock()); QVERIFY(rmutex.tryLock()); QVERIFY(rmutex.tryLock()); @@ -431,7 +416,6 @@ void tst_QMutex::lock_unlock_locked_tryLock() for (int k = 0; k < iterations; ++k) { QVERIFY(rthread.cond.wait(&rthread.mutex, 10000)); - VERIFY_LOCKED(rmutex); QVERIFY(!rmutex.tryLock()); rthread.cond.wakeOne(); @@ -440,7 +424,6 @@ void tst_QMutex::lock_unlock_locked_tryLock() rthread.mutex.unlock(); QVERIFY(rthread.wait(10000)); - VERIFY_NLOCKED(rmutex); QVERIFY(rmutex.tryLock()); QVERIFY(rmutex.tryLock()); QVERIFY(rmutex.tryLock()); @@ -462,6 +445,7 @@ public: static QBasicAtomicInt lockCount; static QBasicAtomicInt sentinel; static QMutex mutex; + static int errorCount; void start() { t.start(); @@ -471,13 +455,13 @@ public: { while (t.elapsed() < one_minute) { mutex.lock(); - Q_ASSERT(!sentinel.ref()); - Q_ASSERT(sentinel.deref()); + if (sentinel.ref()) ++errorCount; + if (!sentinel.deref()) ++errorCount; lockCount.ref(); mutex.unlock(); if (mutex.tryLock()) { - Q_ASSERT(!sentinel.ref()); - Q_ASSERT(sentinel.deref()); + if (sentinel.ref()) ++errorCount; + if (!sentinel.deref()) ++errorCount; lockCount.ref(); mutex.unlock(); } @@ -487,6 +471,7 @@ public: QMutex StressTestThread::mutex; QBasicAtomicInt StressTestThread::lockCount = Q_BASIC_ATOMIC_INITIALIZER(0); QBasicAtomicInt StressTestThread::sentinel = Q_BASIC_ATOMIC_INITIALIZER(-1); +int StressTestThread::errorCount = 0; void tst_QMutex::stressTest() { @@ -496,6 +481,7 @@ void tst_QMutex::stressTest() QVERIFY(threads[0].wait(one_minute + 10000)); for (int i = 1; i < threadCount; ++i) QVERIFY(threads[i].wait(10000)); + QCOMPARE(StressTestThread::errorCount, 0); qDebug("locked %d times", int(StressTestThread::lockCount)); } @@ -534,7 +520,12 @@ void tst_QMutex::tryLockRace() TryLockRaceThread::mutex.unlock(); } +// Variable that will be protected by the mutex. Volatile so that the +// the optimiser doesn't mess with it based on the increment-then-decrement +// usage pattern. static volatile int qtbug16115_trylock_counter; +// Counter for how many times the protected variable has an incorrect value. +static int qtbug16115_failure_count = 0; void tst_QMutex::qtbug16115_trylock() { @@ -545,8 +536,10 @@ void tst_QMutex::qtbug16115_trylock() void run() { for (int i = 0; i < 1000000; ++i) { if (mut.tryLock(0)) { - Q_ASSERT((++qtbug16115_trylock_counter) == 1); - Q_ASSERT((--qtbug16115_trylock_counter) == 0); + if ((++qtbug16115_trylock_counter) != 1) + ++qtbug16115_failure_count; + if ((--qtbug16115_trylock_counter) != 0) + ++qtbug16115_failure_count; mut.unlock(); } } @@ -562,13 +555,16 @@ void tst_QMutex::qtbug16115_trylock() for (int i = 0; i < 1000000; ++i) { mut.lock(); - Q_ASSERT((++qtbug16115_trylock_counter) == 1); - Q_ASSERT((--qtbug16115_trylock_counter) == 0); + if ((++qtbug16115_trylock_counter) != 1) + ++qtbug16115_failure_count; + if ((--qtbug16115_trylock_counter) != 0) + ++qtbug16115_failure_count; mut.unlock(); } t1.wait(); t2.wait(); t3.wait(); + QCOMPARE(qtbug16115_failure_count, 0); } QTEST_MAIN(tst_QMutex) |