Kernel: Fix gcc warning in qsharedmemory_win.cpp
[profile/ivi/qtbase.git] / src / testlib / qtestresult.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/private/qtestresult_p.h>
43 #include <QtCore/qglobal.h>
44
45 #include <QtTest/private/qtestlog_p.h>
46 #include <QtTest/qtestdata.h>
47 #include <QtTest/qtestassert.h>
48
49 #include <stdio.h>
50 #include <string.h>
51
52 QT_BEGIN_NAMESPACE
53
54 namespace QTest
55 {
56     static QTestData *currentTestData = 0;
57     static QTestData *currentGlobalTestData = 0;
58     static const char *currentTestFunc = 0;
59     static const char *currentTestObjectName = 0;
60     static bool failed = false;
61     static bool skipCurrentTest = false;
62
63     static const char *expectFailComment = 0;
64     static int expectFailMode = 0;
65
66     static const char *currentAppname = 0;
67 }
68
69 void QTestResult::reset()
70 {
71     QTest::currentTestData = 0;
72     QTest::currentGlobalTestData = 0;
73     QTest::currentTestFunc = 0;
74     QTest::currentTestObjectName = 0;
75     QTest::failed = false;
76
77     QTest::expectFailComment = 0;
78     QTest::expectFailMode = 0;
79
80     QTestLog::resetCounters();
81 }
82
83 bool QTestResult::currentTestFailed()
84 {
85     return QTest::failed;
86 }
87
88 QTestData *QTestResult::currentGlobalTestData()
89 {
90     return QTest::currentGlobalTestData;
91 }
92
93 QTestData *QTestResult::currentTestData()
94 {
95     return QTest::currentTestData;
96 }
97
98 void QTestResult::setCurrentGlobalTestData(QTestData *data)
99 {
100     QTest::currentGlobalTestData = data;
101 }
102
103 void QTestResult::setCurrentTestData(QTestData *data)
104 {
105     QTest::currentTestData = data;
106     QTest::failed = false;
107 }
108
109 void QTestResult::setCurrentTestFunction(const char *func)
110 {
111     QTest::currentTestFunc = func;
112     QTest::failed = false;
113     if (func)
114         QTestLog::enterTestFunction(func);
115 }
116
117 static void clearExpectFail()
118 {
119     QTest::expectFailMode = 0;
120     delete [] const_cast<char *>(QTest::expectFailComment);
121     QTest::expectFailComment = 0;
122 }
123
124 void QTestResult::finishedCurrentTestData()
125 {
126     if (QTest::expectFailMode)
127         addFailure("QEXPECT_FAIL was called without any subsequent verification statements", 0, 0);
128     clearExpectFail();
129
130     if (!QTest::failed && QTestLog::unhandledIgnoreMessages()) {
131         QTestLog::printUnhandledIgnoreMessages();
132         addFailure("Not all expected messages were received", 0, 0);
133     }
134     QTestLog::clearIgnoreMessages();
135 }
136
137 void QTestResult::finishedCurrentTestDataCleanup()
138 {
139     // If the current test hasn't failed or been skipped, then it passes.
140     if (!QTest::failed && !QTest::skipCurrentTest) {
141         QTestLog::addPass("");
142     }
143
144     QTest::failed = false;
145 }
146
147 void QTestResult::finishedCurrentTestFunction()
148 {
149     QTest::currentTestFunc = 0;
150     QTest::failed = false;
151
152     QTestLog::leaveTestFunction();
153 }
154
155 const char *QTestResult::currentTestFunction()
156 {
157     return QTest::currentTestFunc;
158 }
159
160 const char *QTestResult::currentDataTag()
161 {
162     return QTest::currentTestData ? QTest::currentTestData->dataTag()
163                                    : static_cast<const char *>(0);
164 }
165
166 const char *QTestResult::currentGlobalDataTag()
167 {
168     return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag()
169                                          : static_cast<const char *>(0);
170 }
171
172 static bool isExpectFailData(const char *dataIndex)
173 {
174     if (!dataIndex || dataIndex[0] == '\0')
175         return true;
176     if (!QTest::currentTestData)
177         return false;
178     if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
179         return true;
180     return false;
181 }
182
183 bool QTestResult::expectFail(const char *dataIndex, const char *comment,
184                              QTest::TestFailMode mode, const char *file, int line)
185 {
186     QTEST_ASSERT(comment);
187     QTEST_ASSERT(mode > 0);
188
189     if (!isExpectFailData(dataIndex)) {
190         delete[] comment;
191         return true; // we don't care
192     }
193
194     if (QTest::expectFailMode) {
195         delete[] comment;
196         clearExpectFail();
197         addFailure("Already expecting a fail", file, line);
198         return false;
199     }
200
201     QTest::expectFailMode = mode;
202     QTest::expectFailComment = comment;
203     return true;
204 }
205
206 static bool checkStatement(bool statement, const char *msg, const char *file, int line)
207 {
208     if (statement) {
209         if (QTest::expectFailMode) {
210             QTestLog::addXPass(msg, file, line);
211             bool doContinue = (QTest::expectFailMode == QTest::Continue);
212             clearExpectFail();
213             QTest::failed = true;
214             return doContinue;
215         }
216         return true;
217     }
218
219     if (QTest::expectFailMode) {
220         QTestLog::addXFail(QTest::expectFailComment, file, line);
221         bool doContinue = (QTest::expectFailMode == QTest::Continue);
222         clearExpectFail();
223         return doContinue;
224     }
225
226     QTestResult::addFailure(msg, file, line);
227     return false;
228 }
229
230 bool QTestResult::verify(bool statement, const char *statementStr,
231                          const char *description, const char *file, int line)
232 {
233     QTEST_ASSERT(statementStr);
234
235     char msg[1024];
236
237     if (QTestLog::verboseLevel() >= 2) {
238         qsnprintf(msg, 1024, "QVERIFY(%s)", statementStr);
239         QTestLog::info(msg, file, line);
240     }
241
242     const char * format = QTest::expectFailMode
243         ? "'%s' returned TRUE unexpectedly. (%s)"
244         : "'%s' returned FALSE. (%s)";
245     qsnprintf(msg, 1024, format, statementStr, description ? description : "");
246
247     return checkStatement(statement, msg, file, line);
248 }
249
250 bool QTestResult::compare(bool success, const char *failureMsg,
251                           char *val1, char *val2,
252                           const char *actual, const char *expected,
253                           const char *file, int line)
254 {
255     QTEST_ASSERT(expected);
256     QTEST_ASSERT(actual);
257
258     char msg[1024];
259
260     if (QTestLog::verboseLevel() >= 2) {
261         qsnprintf(msg, 1024, "QCOMPARE(%s, %s)", actual, expected);
262         QTestLog::info(msg, file, line);
263     }
264
265     if (!failureMsg)
266         failureMsg = "Compared values are not the same";
267
268     if (success && QTest::expectFailMode) {
269         qsnprintf(msg, 1024, "QCOMPARE(%s, %s) returned TRUE unexpectedly.", actual, expected);
270     } else if (val1 || val2) {
271         qsnprintf(msg, 1024, "%s\n   Actual   (%s): %s\n   Expected (%s): %s",
272                   failureMsg,
273                   actual, val1 ? val1 : "<null>",
274                   expected, val2 ? val2 : "<null>");
275     } else
276         qsnprintf(msg, 1024, "%s", failureMsg);
277
278     delete [] val1;
279     delete [] val2;
280
281     return checkStatement(success, msg, file, line);
282 }
283
284 void QTestResult::addFailure(const char *message, const char *file, int line)
285 {
286     clearExpectFail();
287
288     QTestLog::addFail(message, file, line);
289     QTest::failed = true;
290 }
291
292 void QTestResult::addSkip(const char *message, const char *file, int line)
293 {
294     clearExpectFail();
295
296     QTestLog::addSkip(message, file, line);
297 }
298
299 void QTestResult::setCurrentTestObject(const char *name)
300 {
301     QTest::currentTestObjectName = name;
302 }
303
304 const char *QTestResult::currentTestObjectName()
305 {
306     return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
307 }
308
309 void QTestResult::setSkipCurrentTest(bool value)
310 {
311     QTest::skipCurrentTest = value;
312 }
313
314 bool QTestResult::skipCurrentTest()
315 {
316     return QTest::skipCurrentTest;
317 }
318
319 void QTestResult::setCurrentAppname(const char *appname)
320 {
321     QTest::currentAppname = appname;
322 }
323
324 const char *QTestResult::currentAppname()
325 {
326     return QTest::currentAppname;
327 }
328
329 QT_END_NAMESPACE