Don't try to mmap past EOF
[qt-netbsd.git] / src / testlib / qtestresult.cpp
bloba62975ec049b109fb622f94e2c3cd9705892ab93
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 QtTest module 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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "QtTest/private/qtestresult_p.h"
43 #include <QtCore/qglobal.h>
45 #include "QtTest/private/qtestlog_p.h"
46 #include "QtTest/qtestdata.h"
47 #include "QtTest/qtestassert.h"
49 #include <stdio.h>
50 #include <string.h>
52 QT_BEGIN_NAMESPACE
54 namespace QTest
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 dataFailed = false;
62 static bool skipCurrentTest = false;
63 static QTestResult::TestLocation location = QTestResult::NoWhere;
65 static int fails = 0;
66 static int passes = 0;
67 static int skips = 0;
69 static const char *expectFailComment = 0;
70 static int expectFailMode = 0;
73 void QTestResult::reset()
75 QTest::currentTestData = 0;
76 QTest::currentGlobalTestData = 0;
77 QTest::currentTestFunc = 0;
78 QTest::currentTestObjectName = 0;
79 QTest::failed = false;
80 QTest::dataFailed = false;
81 QTest::location = QTestResult::NoWhere;
83 QTest::fails = 0;
84 QTest::passes = 0;
85 QTest::skips = 0;
87 QTest::expectFailComment = 0;
88 QTest::expectFailMode = 0;
91 bool QTestResult::allDataPassed()
93 return !QTest::failed;
96 bool QTestResult::currentTestFailed()
98 return QTest::dataFailed;
101 QTestData *QTestResult::currentGlobalTestData()
103 return QTest::currentGlobalTestData;
106 QTestData *QTestResult::currentTestData()
108 return QTest::currentTestData;
111 void QTestResult::setCurrentGlobalTestData(QTestData *data)
113 QTest::currentGlobalTestData = data;
116 void QTestResult::setCurrentTestData(QTestData *data)
118 QTest::currentTestData = data;
119 QTest::dataFailed = false;
122 void QTestResult::setCurrentTestFunction(const char *func)
124 QTest::currentTestFunc = func;
125 QTest::failed = false;
126 if (!func)
127 QTest::location = NoWhere;
128 if (func)
129 QTestLog::enterTestFunction(func);
132 static void clearExpectFail()
134 QTest::expectFailMode = 0;
135 delete [] const_cast<char *>(QTest::expectFailComment);
136 QTest::expectFailComment = 0;
139 void QTestResult::finishedCurrentTestFunction()
141 if (!QTest::failed && QTestLog::unhandledIgnoreMessages()) {
142 QTestLog::printUnhandledIgnoreMessages();
143 addFailure("Not all expected messages were received", 0, 0);
146 if (!QTest::failed && !QTest::skipCurrentTest) {
147 QTestLog::addPass("");
148 ++QTest::passes;
150 QTest::currentTestFunc = 0;
151 QTest::failed = false;
152 QTest::dataFailed = false;
153 QTest::location = NoWhere;
155 QTestLog::leaveTestFunction();
157 clearExpectFail();
160 const char *QTestResult::currentTestFunction()
162 return QTest::currentTestFunc;
165 const char *QTestResult::currentDataTag()
167 return QTest::currentTestData ? QTest::currentTestData->dataTag()
168 : static_cast<const char *>(0);
171 const char *QTestResult::currentGlobalDataTag()
173 return QTest::currentGlobalTestData ? QTest::currentGlobalTestData->dataTag()
174 : static_cast<const char *>(0);
177 static bool isExpectFailData(const char *dataIndex)
179 if (!dataIndex || dataIndex[0] == '\0')
180 return true;
181 if (!QTest::currentTestData)
182 return false;
183 if (strcmp(dataIndex, QTest::currentTestData->dataTag()) == 0)
184 return true;
185 return false;
188 bool QTestResult::expectFail(const char *dataIndex, const char *comment,
189 QTest::TestFailMode mode, const char *file, int line)
191 QTEST_ASSERT(comment);
192 QTEST_ASSERT(mode > 0);
194 if (!isExpectFailData(dataIndex))
195 return true; // we don't care
197 if (QTest::expectFailMode) {
198 clearExpectFail();
199 addFailure("Already expecting a fail", file, line);
200 return false;
203 QTest::expectFailMode = mode;
204 QTest::expectFailComment = comment;
205 return true;
208 static bool checkStatement(bool statement, const char *msg, const char *file, int line)
210 if (statement) {
211 if (QTest::expectFailMode) {
212 QTestLog::addXPass(msg, file, line);
213 bool doContinue = (QTest::expectFailMode == QTest::Continue);
214 clearExpectFail();
215 QTest::failed = true;
216 ++QTest::fails;
217 return doContinue;
219 return true;
222 if (QTest::expectFailMode) {
223 QTestLog::addXFail(QTest::expectFailComment, file, line);
224 bool doContinue = (QTest::expectFailMode == QTest::Continue);
225 clearExpectFail();
226 return doContinue;
229 QTestResult::addFailure(msg, file, line);
230 return false;
233 bool QTestResult::verify(bool statement, const char *statementStr,
234 const char *description, const char *file, int line)
236 char msg[1024];
238 if (QTestLog::verboseLevel() >= 2) {
239 QTest::qt_snprintf(msg, 1024, "QVERIFY(%s)", statementStr);
240 QTestLog::info(msg, file, line);
243 QTest::qt_snprintf(msg, 1024, "'%s' returned FALSE. (%s)", statementStr, description);
245 return checkStatement(statement, msg, file, line);
248 bool QTestResult::compare(bool success, const char *msg, const char *file, int line)
250 if (QTestLog::verboseLevel() >= 2) {
251 QTestLog::info(msg, file, line);
254 return checkStatement(success, msg, file, line);
257 bool QTestResult::compare(bool success, const char *msg, char *val1, char *val2,
258 const char *actual, const char *expected, const char *file, int line)
260 QTEST_ASSERT(expected);
261 QTEST_ASSERT(actual);
263 if (!val1 && !val2)
264 return compare(success, msg, file, line);
266 char buf[1024];
267 QTest::qt_snprintf(buf, 1024, "%s\n Actual (%s): %s\n Expected (%s): %s", msg,
268 actual, val1 ? val1 : "<null>",
269 expected, val2 ? val2 : "<null>");
270 delete [] val1;
271 delete [] val2;
272 return compare(success, buf, file, line);
275 void QTestResult::addFailure(const char *message, const char *file, int line)
277 clearExpectFail();
279 QTestLog::addFail(message, file, line);
280 QTest::failed = true;
281 QTest::dataFailed = true;
282 ++QTest::fails;
285 void QTestResult::addSkip(const char *message, QTest::SkipMode mode,
286 const char *file, int line)
288 clearExpectFail();
290 QTestLog::addSkip(message, mode, file, line);
291 ++QTest::skips;
294 QTestResult::TestLocation QTestResult::currentTestLocation()
296 return QTest::location;
299 void QTestResult::setCurrentTestLocation(TestLocation loc)
301 QTest::location = loc;
304 void QTestResult::setCurrentTestObject(const char *name)
306 QTest::currentTestObjectName = name;
309 const char *QTestResult::currentTestObjectName()
311 return QTest::currentTestObjectName ? QTest::currentTestObjectName : "";
314 int QTestResult::passCount()
316 return QTest::passes;
319 int QTestResult::failCount()
321 return QTest::fails;
324 int QTestResult::skipCount()
326 return QTest::skips;
329 void QTestResult::ignoreMessage(QtMsgType type, const char *msg)
331 QTestLog::addIgnoreMessage(type, msg);
334 bool QTestResult::testFailed()
336 return QTest::failed;
339 void QTestResult::setSkipCurrentTest(bool value)
341 QTest::skipCurrentTest = value;
344 bool QTestResult::skipCurrentTest()
346 return QTest::skipCurrentTest;
349 QT_END_NAMESPACE