1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtTest/private/qtestresult_p.h>
43 #include <QtTest/qtestassert.h>
44 #include <QtTest/private/qtestlog_p.h>
45 #include <QtTest/private/qplaintestlogger_p.h>
46 #include <QtTest/private/qbenchmark_p.h>
47 #include <QtTest/private/qbenchmarkmetric_p.h>
59 #include <QtCore/QString>
62 #include <QtCore/QByteArray>
63 #include <QtCore/qmath.h>
69 static const char *incidentType2String(QAbstractTestLogger::IncidentTypes type)
72 case QAbstractTestLogger::Pass:
74 case QAbstractTestLogger::XFail:
76 case QAbstractTestLogger::Fail:
78 case QAbstractTestLogger::XPass:
84 static const char *benchmarkResult2String()
89 static const char *messageType2String(QAbstractTestLogger::MessageTypes type)
92 case QAbstractTestLogger::Skip:
94 case QAbstractTestLogger::Warn:
96 case QAbstractTestLogger::QWarning:
98 case QAbstractTestLogger::QDebug:
100 case QAbstractTestLogger::QSystem:
102 case QAbstractTestLogger::QFatal:
104 case QAbstractTestLogger::Info:
110 template <typename T>
111 static int countSignificantDigits(T num)
119 while (num / divisor >= 1) {
127 // Pretty-prints a benchmark result using the given number of digits.
128 template <typename T> QString formatResult(T number, int significantDigits)
131 return QLatin1String("NAN");
133 return QLatin1String("0");
135 QString beforeDecimalPoint = QString::number(qint64(number), 'f', 0);
136 QString afterDecimalPoint = QString::number(number, 'f', 20);
137 afterDecimalPoint.remove(0, beforeDecimalPoint.count() + 1);
139 int beforeUse = qMin(beforeDecimalPoint.count(), significantDigits);
140 int beforeRemove = beforeDecimalPoint.count() - beforeUse;
142 // Replace insignificant digits before the decimal point with zeros.
143 beforeDecimalPoint.chop(beforeRemove);
144 for (int i = 0; i < beforeRemove; ++i) {
145 beforeDecimalPoint.append(QLatin1Char('0'));
148 int afterUse = significantDigits - beforeUse;
150 // leading zeroes after the decimal point does not count towards the digit use.
151 if (beforeDecimalPoint == QLatin1String("0") && afterDecimalPoint.isEmpty() == false) {
155 while (i < afterDecimalPoint.count() && afterDecimalPoint.at(i) == QLatin1Char('0')) {
162 int afterRemove = afterDecimalPoint.count() - afterUse;
163 afterDecimalPoint.chop(afterRemove);
165 QChar separator = QLatin1Char(',');
166 QChar decimalPoint = QLatin1Char('.');
168 // insert thousands separators
169 int length = beforeDecimalPoint.length();
170 for (int i = beforeDecimalPoint.length() -1; i >= 1; --i) {
171 if ((length - i) % 3 == 0)
172 beforeDecimalPoint.insert(i, separator);
176 print = beforeDecimalPoint;
178 print.append(decimalPoint);
180 print += afterDecimalPoint;
186 template <typename T>
187 int formatResult(char * buffer, int bufferSize, T number, int significantDigits)
189 QString result = formatResult(number, significantDigits);
190 qstrncpy(buffer, result.toLatin1().constData(), bufferSize);
191 int size = result.count();
196 void QPlainTestLogger::outputMessage(const char *str)
198 #if defined(Q_OS_WINCE)
199 QString strUtf16 = QString::fromLatin1(str);
200 const int maxOutputLength = 255;
202 QString tmp = strUtf16.left(maxOutputLength);
203 OutputDebugString((wchar_t*)tmp.utf16());
204 strUtf16.remove(0, maxOutputLength);
205 } while (!strUtf16.isEmpty());
206 if (stream != stdout)
207 #elif defined(Q_OS_WIN)
208 OutputDebugStringA(str);
213 void QPlainTestLogger::printMessage(const char *type, const char *msg, const char *file, int line)
220 const char *fn = QTestResult::currentTestFunction() ? QTestResult::currentTestFunction()
222 const char *tag = QTestResult::currentDataTag() ? QTestResult::currentDataTag() : "";
223 const char *gtag = QTestResult::currentGlobalDataTag()
224 ? QTestResult::currentGlobalDataTag()
226 const char *filler = (tag[0] && gtag[0]) ? ":" : "";
228 QTest::qt_asprintf(&buf, "%s: %s::%s(%s%s%s)%s%s\n"
230 "%s(%d) : failure location\n"
234 , type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag,
235 msg[0] ? " " : "", msg, file, line);
237 QTest::qt_asprintf(&buf, "%s: %s::%s(%s%s%s)%s%s\n",
238 type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag,
239 msg[0] ? " " : "", msg);
241 // In colored mode, printf above stripped our nonprintable control characters.
243 memcpy(buf.data(), type, strlen(type));
244 outputMessage(buf.data());
247 void QPlainTestLogger::printBenchmarkResult(const QBenchmarkResult &result)
249 const char *bmtag = QTest::benchmarkResult2String();
253 buf1, sizeof(buf1), "%s: %s::%s",
255 QTestResult::currentTestObjectName(),
256 result.context.slotName.toLatin1().data());
260 QByteArray tag = result.context.tag.toLocal8Bit();
261 if (tag.isEmpty() == false) {
262 qsnprintf(bufTag, sizeof(bufTag), ":\"%s\"", tag.data());
268 qsnprintf(fillFormat, sizeof(fillFormat), ":\n%%%ds", fillLength);
270 qsnprintf(fill, sizeof(fill), fillFormat, "");
272 const char * unitText = QTest::benchmarkMetricUnit(result.metric);
274 qreal valuePerIteration = qreal(result.value) / qreal(result.iterations);
275 char resultBuffer[100] = "";
276 QTest::formatResult(resultBuffer, 100, valuePerIteration, QTest::countSignificantDigits(result.value));
279 qsnprintf(buf2, sizeof(buf2), "%s %s", resultBuffer, unitText);
282 QByteArray iterationText = " per iteration";
283 Q_ASSERT(result.iterations > 0);
284 qsnprintf(buf2_, sizeof(buf2_), "%s", iterationText.data());
287 Q_ASSERT(result.iterations > 0);
288 QTest::formatResult(resultBuffer, 100, result.value, QTest::countSignificantDigits(result.value));
289 qsnprintf(buf3, sizeof(buf3), " (total: %s, iterations: %d)", resultBuffer, result.iterations);
293 if (result.setByMacro) {
294 qsnprintf(buf, sizeof(buf), "%s%s%s%s%s%s\n", buf1, bufTag, fill, buf2, buf2_, buf3);
296 qsnprintf(buf, sizeof(buf), "%s%s%s%s\n", buf1, bufTag, fill, buf2);
299 memcpy(buf, bmtag, strlen(bmtag));
303 QPlainTestLogger::QPlainTestLogger(const char *filename)
304 : QAbstractTestLogger(filename)
308 QPlainTestLogger::~QPlainTestLogger()
312 void QPlainTestLogger::startLogging()
314 QAbstractTestLogger::startLogging();
317 if (QTestLog::verboseLevel() < 0) {
318 qsnprintf(buf, sizeof(buf), "Testing %s\n", QTestResult::currentTestObjectName());
320 qsnprintf(buf, sizeof(buf),
321 "********* Start testing of %s *********\n"
322 "Config: Using QTest library " QTEST_VERSION_STR
323 ", Qt %s\n", QTestResult::currentTestObjectName(), qVersion());
328 void QPlainTestLogger::stopLogging()
331 if (QTestLog::verboseLevel() < 0) {
332 qsnprintf(buf, sizeof(buf), "Totals: %d passed, %d failed, %d skipped\n",
333 QTestLog::passCount(), QTestLog::failCount(),
334 QTestLog::skipCount());
336 qsnprintf(buf, sizeof(buf),
337 "Totals: %d passed, %d failed, %d skipped\n"
338 "********* Finished testing of %s *********\n",
339 QTestLog::passCount(), QTestLog::failCount(),
340 QTestLog::skipCount(), QTestResult::currentTestObjectName());
344 QAbstractTestLogger::stopLogging();
348 void QPlainTestLogger::enterTestFunction(const char * /*function*/)
350 if (QTestLog::verboseLevel() >= 1)
351 printMessage(QTest::messageType2String(Info), "entering");
354 void QPlainTestLogger::leaveTestFunction()
358 void QPlainTestLogger::addIncident(IncidentTypes type, const char *description,
359 const char *file, int line)
361 // suppress PASS and XFAIL in silent mode
362 if ((type == QAbstractTestLogger::Pass || type == QAbstractTestLogger::XFail)
363 && QTestLog::verboseLevel() < 0)
366 printMessage(QTest::incidentType2String(type), description, file, line);
369 void QPlainTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
371 // suppress benchmark results in silent mode
372 if (QTestLog::verboseLevel() < 0)
375 printBenchmarkResult(result);
378 void QPlainTestLogger::addMessage(MessageTypes type, const char *message,
379 const char *file, int line)
381 // suppress non-fatal messages in silent mode
382 if (type != QAbstractTestLogger::QFatal && QTestLog::verboseLevel() < 0)
385 printMessage(QTest::messageType2String(type), message, file, line);