tests/auto/qthreadonce/tst_qthreadonce.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qcoreapplication.h>
       
    46 #include <qmutex.h>
       
    47 #include <qthread.h>
       
    48 #include <qwaitcondition.h>
       
    49 #include "qthreadonce.h"
       
    50 
       
    51 //TESTED_CLASS=
       
    52 //TESTED_FILES=corelib/thread/qthreadonce.h corelib/thread/qthreadonce.cpp
       
    53 
       
    54 class tst_QThreadOnce : public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57 
       
    58 private slots:
       
    59     void sameThread();
       
    60     void sameThread_data();
       
    61     void multipleThreads();
       
    62 
       
    63     void nesting();
       
    64     void reentering();
       
    65     void exception();
       
    66 };
       
    67 
       
    68 class SingletonObject: public QObject
       
    69 {
       
    70     Q_OBJECT
       
    71 public:
       
    72     static int runCount;
       
    73     SingletonObject() { val = 42; ++runCount; }
       
    74     ~SingletonObject() { }
       
    75 
       
    76     QBasicAtomicInt val;
       
    77 };
       
    78 
       
    79 class IncrementThread: public QThread
       
    80 {
       
    81 public:
       
    82     static QBasicAtomicInt runCount;
       
    83     static QSingleton<SingletonObject> singleton;
       
    84     QSemaphore &sem1, &sem2;
       
    85     int &var;
       
    86 
       
    87     IncrementThread(QSemaphore *psem1, QSemaphore *psem2, int *pvar, QObject *parent)
       
    88         : QThread(parent), sem1(*psem1), sem2(*psem2), var(*pvar)
       
    89         { start(); }
       
    90 
       
    91     ~IncrementThread() { wait(); }
       
    92 
       
    93 protected:
       
    94     void run()
       
    95         {
       
    96             sem2.release();
       
    97             sem1.acquire();             // synchronize
       
    98 
       
    99             Q_ONCE {
       
   100                 ++var;
       
   101             }
       
   102             runCount.ref();
       
   103             singleton->val.ref();
       
   104         }
       
   105 };
       
   106 int SingletonObject::runCount = 0;
       
   107 QBasicAtomicInt IncrementThread::runCount = Q_BASIC_ATOMIC_INITIALIZER(0);
       
   108 QSingleton<SingletonObject> IncrementThread::singleton;
       
   109 
       
   110 void tst_QThreadOnce::sameThread_data()
       
   111 {
       
   112     SingletonObject::runCount = 0;
       
   113     QTest::addColumn<int>("expectedValue");
       
   114 
       
   115     QTest::newRow("first") << 42;
       
   116     QTest::newRow("second") << 43;
       
   117 }
       
   118 
       
   119 void tst_QThreadOnce::sameThread()
       
   120 {
       
   121     static int controlVariable = 0;
       
   122     Q_ONCE {
       
   123         QCOMPARE(controlVariable, 0);
       
   124         ++controlVariable;
       
   125     }
       
   126     QCOMPARE(controlVariable, 1);
       
   127 
       
   128     static QSingleton<SingletonObject> s;
       
   129     QTEST((int)s->val, "expectedValue");
       
   130     s->val.ref();
       
   131 
       
   132     QCOMPARE(SingletonObject::runCount, 1);
       
   133 }
       
   134 
       
   135 void tst_QThreadOnce::multipleThreads()
       
   136 {
       
   137 #if defined(Q_OS_WINCE) || defined(Q_OS_VXWORKS) || defined(Q_OS_SYMBIAN)
       
   138     const int NumberOfThreads = 20;
       
   139 #else
       
   140     const int NumberOfThreads = 100;
       
   141 #endif
       
   142     int controlVariable = 0;
       
   143     QSemaphore sem1, sem2(NumberOfThreads);
       
   144 
       
   145     QObject *parent = new QObject;
       
   146     for (int i = 0; i < NumberOfThreads; ++i)
       
   147         new IncrementThread(&sem1, &sem2, &controlVariable, parent);
       
   148 
       
   149     QCOMPARE(controlVariable, 0); // nothing must have set them yet
       
   150     SingletonObject::runCount = 0;
       
   151     IncrementThread::runCount = 0;
       
   152 
       
   153     // wait for all of them to be ready
       
   154     sem2.acquire(NumberOfThreads);
       
   155     // unleash the threads
       
   156     sem1.release(NumberOfThreads);
       
   157 
       
   158     // wait for all of them to terminate:
       
   159     delete parent;
       
   160 
       
   161     QCOMPARE(controlVariable, 1);
       
   162     QCOMPARE((int)IncrementThread::runCount, NumberOfThreads);
       
   163     QCOMPARE(SingletonObject::runCount, 1);
       
   164 }
       
   165 
       
   166 void tst_QThreadOnce::nesting()
       
   167 {
       
   168     int variable = 0;
       
   169     Q_ONCE {
       
   170         Q_ONCE {
       
   171             ++variable;
       
   172         }
       
   173     }
       
   174 
       
   175     QVERIFY(variable == 1);
       
   176 }
       
   177 
       
   178 static void reentrant(int control, int &counter)
       
   179 {
       
   180     Q_ONCE {
       
   181         if (counter)
       
   182             reentrant(--control, counter);
       
   183         ++counter;
       
   184     }
       
   185     static QSingleton<SingletonObject> s;
       
   186     s->val.ref();
       
   187 }
       
   188 
       
   189 void tst_QThreadOnce::reentering()
       
   190 {
       
   191     const int WantedRecursions = 5;
       
   192     int count = 0;
       
   193     SingletonObject::runCount = 0;
       
   194     reentrant(WantedRecursions, count);
       
   195 
       
   196     // reentrancy is undefined behavior:
       
   197     QVERIFY(count == 1 || count == WantedRecursions);
       
   198     QCOMPARE(SingletonObject::runCount, 1);
       
   199 }
       
   200 
       
   201 #if !defined(QT_NO_EXCEPTIONS)
       
   202 static void exception_helper(int &val)
       
   203 {
       
   204     Q_ONCE {
       
   205         if (val++ == 0) throw 0;
       
   206     }
       
   207 }
       
   208 #endif
       
   209 
       
   210 void tst_QThreadOnce::exception()
       
   211 {
       
   212 #if defined(QT_NO_EXCEPTIONS)
       
   213     QSKIP("Compiled without exceptions, skipping test", SkipAll);
       
   214 #else
       
   215     int count = 0;
       
   216 
       
   217     try {
       
   218         exception_helper(count);
       
   219     } catch (...) {
       
   220         // nothing
       
   221     }
       
   222     QCOMPARE(count, 1);
       
   223 
       
   224     try {
       
   225         exception_helper(count);
       
   226     } catch (...) {
       
   227         QVERIFY2(false, "Exception shouldn't have been thrown...");
       
   228     }
       
   229     QCOMPARE(count, 2);
       
   230 #endif
       
   231 }
       
   232 
       
   233 QTEST_MAIN(tst_QThreadOnce)
       
   234 #include "tst_qthreadonce.moc"