Moving relevant tests to corelib/global
[profile/ivi/qtbase.git] / tests / auto / corelib / global / qglobal / tst_qglobal.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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     QTest::ignoreMessage(QtWarningMsg, "Object::connect: No such signal QObject::mysignal(x)");
134     ok = QObject::connect(&a, signal.toLatin1(), &b, slot.toLatin1(), Qt::AutoConnection);
135     QVERIFY(!ok);
136     QCOMPARE(connect_info.sender, (QObject *) 0);
137
138     QTest::ignoreMessage(QtWarningMsg, "Object::disconnect: No such signal QObject::mysignal(x)");
139     ok = QObject::disconnect(&a, signal.toLatin1(), &b, slot.toLatin1());
140     QVERIFY(!ok);
141     QCOMPARE(connect_info.sender, (QObject *) 0);
142 }
143
144 void tst_QGlobal::for_each()
145 {
146     QList<int> list;
147     list << 0 << 1 << 2 << 3 << 4 << 5;
148
149     int counter = 0;
150     foreach(int i, list) {
151         QCOMPARE(i, counter++);
152     }
153     QCOMPARE(counter, list.count());
154
155     // do it again, to make sure we don't have any for-scoping
156     // problems with older compilers
157     counter = 0;
158     foreach(int i, list) {
159         QCOMPARE(i, counter++);
160     }
161     QCOMPARE(counter, list.count());
162 }
163
164 void tst_QGlobal::qassert()
165 {
166     bool passed = false;
167     if (false) {
168         Q_ASSERT(false);
169     } else {
170         passed = true;
171     }
172     QVERIFY(passed);
173
174     passed = false;
175     if (false) {
176         Q_ASSERT_X(false, "tst_QGlobal", "qassert");
177     } else {
178         passed = true;
179     }
180     QVERIFY(passed);
181
182     passed = false;
183     if (false)
184         Q_ASSERT(false);
185     else
186         passed = true;
187     QVERIFY(passed);
188
189     passed = false;
190     if (false)
191         Q_ASSERT_X(false, "tst_QGlobal", "qassert");
192     else
193         passed = true;
194     QVERIFY(passed);
195 }
196
197 void tst_QGlobal::qtry()
198 {
199     int i = 0;
200     QT_TRY {
201         i = 1;
202         QT_THROW(42);
203         i = 2;
204     } QT_CATCH(int) {
205         QCOMPARE(i, 1);
206         i = 7;
207     }
208 #ifdef QT_NO_EXCEPTIONS
209     QCOMPARE(i, 2);
210 #else
211     QCOMPARE(i, 7);
212 #endif
213
214     // check propper if/else scoping
215     i = 0;
216     if (true)
217         QT_TRY {
218             i = 2;
219             QT_THROW(42);
220             i = 4;
221         } QT_CATCH(int) {
222             QCOMPARE(i, 2);
223             i = 4;
224         }
225     else
226         QCOMPARE(i, 0);
227     QCOMPARE(i, 4);
228
229     i = 0;
230     if (false)
231         QT_TRY {
232             i = 2;
233             QT_THROW(42);
234             i = 4;
235         } QT_CATCH(int) {
236             QCOMPARE(i, 2);
237             i = 2;
238         }
239     else
240         i = 8;
241     QCOMPARE(i, 8);
242
243     i = 0;
244     if (false)
245         i = 42;
246     else
247         QT_TRY {
248             i = 2;
249             QT_THROW(42);
250             i = 4;
251         } QT_CATCH(int) {
252             QCOMPARE(i, 2);
253             i = 4;
254         }
255     QCOMPARE(i, 4);
256 }
257
258 void tst_QGlobal::checkptr()
259 {
260     int i;
261     QCOMPARE(q_check_ptr(&i), &i);
262
263     const char *c = "hello";
264     QCOMPARE(q_check_ptr(c), c);
265 }
266
267 QTEST_MAIN(tst_QGlobal)
268 #include "tst_qglobal.moc"