tests/auto/qglobal/tst_qglobal.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 class tst_QGlobal: public QObject
       
    46 {
       
    47     Q_OBJECT
       
    48 private slots:
       
    49     void qIsNull();
       
    50     void qInternalCallbacks();
       
    51     void for_each();
       
    52     void qassert();
       
    53     void qtry();
       
    54     void checkptr();
       
    55 };
       
    56 
       
    57 void tst_QGlobal::qIsNull()
       
    58 {
       
    59     double d = 0.0;
       
    60     float f = 0.0f;
       
    61 
       
    62     QVERIFY(::qIsNull(d));
       
    63     QVERIFY(::qIsNull(f));
       
    64 
       
    65     d += 0.000000001;
       
    66     f += 0.0000001f;
       
    67 
       
    68     QVERIFY(!::qIsNull(d));
       
    69     QVERIFY(!::qIsNull(f));
       
    70 }
       
    71 
       
    72 struct ConnectInfo {
       
    73     QObject *sender;
       
    74     QObject *receiver;
       
    75     QString signal, slot;
       
    76     int type;
       
    77     void reset() {
       
    78         sender = receiver = 0;
       
    79         signal = slot = QString();
       
    80         type = -1;
       
    81     }
       
    82 } connect_info;
       
    83 
       
    84 bool disconnect_callback(void **data)
       
    85 {
       
    86     connect_info.sender = (QObject *)(data[0]);
       
    87     connect_info.receiver = (QObject *)(data[2]);
       
    88     connect_info.signal = QString::fromLatin1((const char *) data[1]);
       
    89     connect_info.slot = QString::fromLatin1((const char *) data[3]);
       
    90     return true;
       
    91 }
       
    92 
       
    93 bool connect_callback(void **data)
       
    94 {
       
    95     disconnect_callback(data);
       
    96     connect_info.type = *(int *) data[4];
       
    97     return true;
       
    98 }
       
    99 
       
   100 void tst_QGlobal::qInternalCallbacks()
       
   101 {
       
   102     QInternal::registerCallback(QInternal::ConnectCallback, connect_callback);
       
   103     QInternal::registerCallback(QInternal::DisconnectCallback, disconnect_callback);
       
   104 
       
   105     QObject a, b;
       
   106     QString signal = QLatin1String("2mysignal(x)");
       
   107     QString slot = QLatin1String("1myslot(x)");
       
   108 
       
   109     // Test that connect works as expected...
       
   110     connect_info.reset();
       
   111     bool ok = QObject::connect(&a, signal.toLatin1(), &b, slot.toLatin1(), Qt::AutoConnection);
       
   112     QVERIFY(ok);
       
   113     QCOMPARE(&a, connect_info.sender);
       
   114     QCOMPARE(&b, connect_info.receiver);
       
   115     QCOMPARE(signal, connect_info.signal);
       
   116     QCOMPARE(slot, connect_info.slot);
       
   117     QCOMPARE((int) Qt::AutoConnection, connect_info.type);
       
   118 
       
   119     // Test that disconnect works as expected
       
   120     connect_info.reset();
       
   121     ok = QObject::disconnect(&a, signal.toLatin1(), &b, slot.toLatin1());
       
   122     QVERIFY(ok);
       
   123     QCOMPARE(&a, connect_info.sender);
       
   124     QCOMPARE(&b, connect_info.receiver);
       
   125     QCOMPARE(signal, connect_info.signal);
       
   126     QCOMPARE(slot, connect_info.slot);
       
   127 
       
   128     // Unregister callbacks and verify that they are not triggered...
       
   129     QInternal::unregisterCallback(QInternal::ConnectCallback, connect_callback);
       
   130     QInternal::unregisterCallback(QInternal::DisconnectCallback, disconnect_callback);
       
   131 
       
   132     connect_info.reset();
       
   133     ok = QObject::connect(&a, signal.toLatin1(), &b, slot.toLatin1(), Qt::AutoConnection);
       
   134     QVERIFY(!ok);
       
   135     QCOMPARE(connect_info.sender, (QObject *) 0);
       
   136 
       
   137     ok = QObject::disconnect(&a, signal.toLatin1(), &b, slot.toLatin1());
       
   138     QVERIFY(!ok);
       
   139     QCOMPARE(connect_info.sender, (QObject *) 0);
       
   140 }
       
   141 
       
   142 void tst_QGlobal::for_each()
       
   143 {
       
   144     QList<int> list;
       
   145     list << 0 << 1 << 2 << 3 << 4 << 5;
       
   146 
       
   147     int counter = 0;
       
   148     foreach(int i, list) {
       
   149         QCOMPARE(i, counter++);
       
   150     }
       
   151     QCOMPARE(counter, list.count());
       
   152 
       
   153     // do it again, to make sure we don't have any for-scoping
       
   154     // problems with older compilers
       
   155     counter = 0;
       
   156     foreach(int i, list) {
       
   157         QCOMPARE(i, counter++);
       
   158     }
       
   159     QCOMPARE(counter, list.count());
       
   160 }
       
   161 
       
   162 void tst_QGlobal::qassert()
       
   163 {
       
   164     bool passed = false;
       
   165     if (false) {
       
   166         Q_ASSERT(false);
       
   167     } else {
       
   168         passed = true;
       
   169     }
       
   170     QVERIFY(passed);
       
   171 
       
   172     passed = false;
       
   173     if (false) {
       
   174         Q_ASSERT_X(false, "tst_QGlobal", "qassert");
       
   175     } else {
       
   176         passed = true;
       
   177     }
       
   178     QVERIFY(passed);
       
   179 
       
   180     passed = false;
       
   181     if (false)
       
   182         Q_ASSERT(false);
       
   183     else
       
   184         passed = true;
       
   185     QVERIFY(passed);
       
   186 
       
   187     passed = false;
       
   188     if (false)
       
   189         Q_ASSERT_X(false, "tst_QGlobal", "qassert");
       
   190     else
       
   191         passed = true;
       
   192     QVERIFY(passed);
       
   193 }
       
   194 
       
   195 void tst_QGlobal::qtry()
       
   196 {
       
   197     int i = 0;
       
   198     QT_TRY {
       
   199         i = 1;
       
   200         QT_THROW(42);
       
   201         i = 2;
       
   202     } QT_CATCH(int) {
       
   203         QCOMPARE(i, 1);
       
   204         i = 7;
       
   205     }
       
   206 #ifdef QT_NO_EXCEPTIONS
       
   207     QCOMPARE(i, 2);
       
   208 #else
       
   209     QCOMPARE(i, 7);
       
   210 #endif
       
   211 
       
   212     // check propper if/else scoping
       
   213     i = 0;
       
   214     if (true)
       
   215         QT_TRY {
       
   216             i = 2;
       
   217             QT_THROW(42);
       
   218             i = 4;
       
   219         } QT_CATCH(int) {
       
   220             QCOMPARE(i, 2);
       
   221             i = 4;
       
   222         }
       
   223     else
       
   224         QCOMPARE(i, 0);
       
   225     QCOMPARE(i, 4);
       
   226 
       
   227     i = 0;
       
   228     if (false)
       
   229         QT_TRY {
       
   230             i = 2;
       
   231             QT_THROW(42);
       
   232             i = 4;
       
   233         } QT_CATCH(int) {
       
   234             QCOMPARE(i, 2);
       
   235             i = 2;
       
   236         }
       
   237     else
       
   238         i = 8;
       
   239     QCOMPARE(i, 8);
       
   240 
       
   241     i = 0;
       
   242     if (false)
       
   243         i = 42;
       
   244     else
       
   245         QT_TRY {
       
   246             i = 2;
       
   247             QT_THROW(42);
       
   248             i = 4;
       
   249         } QT_CATCH(int) {
       
   250             QCOMPARE(i, 2);
       
   251             i = 4;
       
   252         }
       
   253     QCOMPARE(i, 4);
       
   254 }
       
   255 
       
   256 void tst_QGlobal::checkptr()
       
   257 {
       
   258     int i;
       
   259     QCOMPARE(q_check_ptr(&i), &i);
       
   260 
       
   261     const char *c = "hello";
       
   262     QCOMPARE(q_check_ptr(c), c);
       
   263 }
       
   264 
       
   265 QTEST_MAIN(tst_QGlobal)
       
   266 #include "tst_qglobal.moc"