tests/auto/q3deepcopy/tst_q3deepcopy.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 
       
    46 #include <qapplication.h>
       
    47 #include <qthread.h>
       
    48 
       
    49 #include <qmutex.h>
       
    50 
       
    51 
       
    52 
       
    53 #include <q3deepcopy.h>
       
    54 
       
    55 //TESTED_CLASS=
       
    56 //TESTED_FILES=
       
    57 
       
    58 class tst_Q3DeepCopy : public QObject
       
    59 {
       
    60 Q_OBJECT
       
    61 
       
    62 public:
       
    63     tst_Q3DeepCopy();
       
    64     virtual ~tst_Q3DeepCopy();
       
    65 
       
    66 
       
    67 public slots:
       
    68     void initTestCase();
       
    69     void cleanupTestCase();
       
    70     void init();
       
    71     void cleanup();
       
    72 private slots:
       
    73     void deepcopy_data();
       
    74     void deepcopy();
       
    75 };
       
    76 
       
    77 // this is the thread object that stress tests Q3DeepCopy
       
    78 class Thread : public QThread
       
    79 {
       
    80 public:
       
    81     QMutex *mutex;
       
    82     Thread *other;
       
    83     Q3DeepCopy<QString> string;
       
    84     int iterations;
       
    85 
       
    86     Thread() : mutex( 0 ), other( 0 ) { }
       
    87 
       
    88     void run()
       
    89     {
       
    90         QString local;
       
    91         QStringList list;
       
    92 
       
    93         mutex->lock();
       
    94         local = string;
       
    95         mutex->unlock();
       
    96 
       
    97 	while ( iterations-- ) {
       
    98             mutex->lock();
       
    99             local = string;
       
   100 	    mutex->unlock();
       
   101 
       
   102             list.clear();
       
   103             list << local << local << local << local << local;
       
   104             list << local << local << local << local << local;
       
   105             list << local << local << local << local << local;
       
   106             list << local << local << local << local << local;
       
   107 
       
   108             local = QString::null;
       
   109             local = QString::null;
       
   110             local = QString::null;
       
   111             local = QString::null;
       
   112 
       
   113             mutex->lock();
       
   114             local = other->string;
       
   115             mutex->unlock();
       
   116 
       
   117             local = local + local;
       
   118             local = local + local;
       
   119             local = local + local;
       
   120             local = local + local;
       
   121 
       
   122             local = QString::null;
       
   123             local = QString::null;
       
   124             local = QString::null;
       
   125             local = QString::null;
       
   126         }
       
   127     }
       
   128 };
       
   129 
       
   130 
       
   131 tst_Q3DeepCopy::tst_Q3DeepCopy()
       
   132 {
       
   133 }
       
   134 
       
   135 tst_Q3DeepCopy::~tst_Q3DeepCopy()
       
   136 {
       
   137 
       
   138 }
       
   139 
       
   140 // initTestCase will be executed once before the first testfunction is executed.
       
   141 void tst_Q3DeepCopy::initTestCase()
       
   142 {
       
   143 }
       
   144 
       
   145 // cleanupTestCase will be executed once after the last testfunction is executed.
       
   146 void tst_Q3DeepCopy::cleanupTestCase()
       
   147 {
       
   148 }
       
   149 
       
   150 // init() will be executed immediately before each testfunction is run.
       
   151 void tst_Q3DeepCopy::init()
       
   152 {
       
   153 }
       
   154 
       
   155 // cleanup() will be executed immediately after each testfunction is run.
       
   156 void tst_Q3DeepCopy::cleanup()
       
   157 {
       
   158 }
       
   159 
       
   160 void tst_Q3DeepCopy::deepcopy_data()
       
   161 {
       
   162     // define the test elements we're going to use
       
   163     QTest::addColumn<QString>("string0");
       
   164     QTest::addColumn<int>("iterations0");
       
   165     QTest::addColumn<QString>("string1");
       
   166     QTest::addColumn<int>("iterations1");
       
   167     QTest::addColumn<QString>("string2");
       
   168     QTest::addColumn<int>("iterations2");
       
   169     QTest::addColumn<QString>("string3");
       
   170     QTest::addColumn<int>("iterations3");
       
   171 
       
   172     // create a first testdata instance and fill it with data
       
   173     QTest::newRow( "data0" )
       
   174 	<< QString( "one.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) << 1234
       
   175 	<< QString( "two.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) << 2345
       
   176 	<< QString( "three.ccccccccccccccccccccccccccccccccccccccccc" ) << 3456
       
   177 	<< QString( "four.dddddddddddddddddddddddddddddddddddddddddd" ) << 4567;
       
   178     QTest::newRow( "data1" )
       
   179 	<< QString( "one.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) << 12345
       
   180 	<< QString( "two.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) << 23456
       
   181 	<< QString( "three.ccccccccccccccccccccccccccccccccccccccccc" ) << 34567
       
   182 	<< QString( "four.dddddddddddddddddddddddddddddddddddddddddd" ) << 45678;
       
   183     QTest::newRow( "data2" )
       
   184 	<< QString( "one.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) << 45678
       
   185 	<< QString( "two.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) << 34567
       
   186 	<< QString( "three.ccccccccccccccccccccccccccccccccccccccccc" ) << 23456
       
   187 	<< QString( "four.dddddddddddddddddddddddddddddddddddddddddd" ) << 12345;
       
   188     QTest::newRow( "data3" )
       
   189 	<< QString( "one.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) << 4567
       
   190 	<< QString( "two.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) << 3456
       
   191 	<< QString( "three.ccccccccccccccccccccccccccccccccccccccccc" ) << 2345
       
   192 	<< QString( "four.dddddddddddddddddddddddddddddddddddddddddd" ) << 1234;
       
   193 }
       
   194 
       
   195 void tst_Q3DeepCopy::deepcopy()
       
   196 {
       
   197     QFETCH( QString, string0 );
       
   198     QFETCH( int, iterations0 );
       
   199     QFETCH( QString, string1 );
       
   200     QFETCH( int, iterations1);
       
   201     QFETCH( QString, string2 );
       
   202     QFETCH( int, iterations2);
       
   203     QFETCH( QString, string3 );
       
   204     QFETCH( int, iterations3);
       
   205 
       
   206     QMutex mutex1, mutex2;
       
   207     Thread one, two, three, four;
       
   208 
       
   209     one.string = string0;
       
   210     one.mutex = &mutex1;
       
   211     one.other = &two;
       
   212     one.iterations = iterations0;
       
   213 
       
   214     two.string = string1;
       
   215     two.mutex = &mutex1;
       
   216     two.other = &one;
       
   217     two.iterations = iterations1;
       
   218 
       
   219     three.string = string2;
       
   220     three.mutex = &mutex2;
       
   221     three.other = &four;
       
   222     three.iterations = iterations2;
       
   223 
       
   224     four.string = string3;
       
   225     four.mutex = &mutex2;
       
   226     four.other = &three;
       
   227     four.iterations = iterations3;
       
   228 
       
   229     one.start();
       
   230     two.start();
       
   231     three.start();
       
   232     four.start();
       
   233 
       
   234     // wait for the threads for a maximum of 5 minutes.  anything long, we assume
       
   235     // that the test has deadlocked.
       
   236     QVERIFY( one.wait( 300000 ) );
       
   237     QVERIFY( two.wait( 300000 ) );
       
   238     QVERIFY( three.wait( 300000 ) );
       
   239     QVERIFY( four.wait( 300000 ) );
       
   240 }
       
   241 
       
   242 QTEST_MAIN(tst_Q3DeepCopy)
       
   243 #include "tst_q3deepcopy.moc"