source: trunk/src/testlib/qplaintestlogger.cpp@ 318

Last change on this file since 318 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
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
48#include <stdarg.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#ifdef Q_OS_WIN
54#include "windows.h"
55#endif
56
57#ifdef Q_OS_WINCE
58#include <QtCore/QString>
59#endif
60
61#include <QtCore/QByteArray>
62#include <QtCore/qmath.h>
63
64QT_BEGIN_NAMESPACE
65
66namespace QTest {
67
68#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
69
70 static CRITICAL_SECTION outputCriticalSection;
71 static HANDLE hConsole = INVALID_HANDLE_VALUE;
72 static WORD consoleAttributes = 0;
73
74 static const char *qWinColoredMsg(int prefix, int color, const char *msg)
75 {
76 if (!hConsole)
77 return msg;
78
79 WORD attr = consoleAttributes & ~(FOREGROUND_GREEN | FOREGROUND_BLUE
80 | FOREGROUND_RED | FOREGROUND_INTENSITY);
81 if (prefix)
82 attr |= FOREGROUND_INTENSITY;
83 if (color == 32)
84 attr |= FOREGROUND_GREEN;
85 if (color == 36)
86 attr |= FOREGROUND_BLUE | FOREGROUND_GREEN;
87 if (color == 31)
88 attr |= FOREGROUND_RED | FOREGROUND_INTENSITY;
89 if (color == 37)
90 attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
91 if (color == 33)
92 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
93 SetConsoleTextAttribute(hConsole, attr);
94 printf(msg);
95 SetConsoleTextAttribute(hConsole, consoleAttributes);
96 return "";
97 }
98
99# define COLORED_MSG(prefix, color, msg) colored ? qWinColoredMsg(prefix, color, msg) : msg
100#else
101# define COLORED_MSG(prefix, color, msg) colored && QAbstractTestLogger::isTtyOutput() ? "\033["#prefix";"#color"m" msg "\033[0m" : msg
102#endif
103
104 static const char *incidentType2String(QAbstractTestLogger::IncidentTypes type)
105 {
106 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
107 switch (type) {
108 case QAbstractTestLogger::Pass:
109 return COLORED_MSG(0, 32, "PASS "); //green
110 case QAbstractTestLogger::XFail:
111 return COLORED_MSG(1, 32, "XFAIL "); //light green
112 case QAbstractTestLogger::Fail:
113 return COLORED_MSG(0, 31, "FAIL! "); //red
114 case QAbstractTestLogger::XPass:
115 return COLORED_MSG(0, 31, "XPASS "); //red, too
116 }
117 return "??????";
118 }
119
120 static const char *benchmarkResult2String()
121 {
122 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
123 return COLORED_MSG(0, 36, "RESULT "); // cyan
124 }
125
126 static const char *messageType2String(QAbstractTestLogger::MessageTypes type)
127 {
128 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
129 switch (type) {
130 case QAbstractTestLogger::Skip:
131 return COLORED_MSG(0, 37, "SKIP "); //white
132 case QAbstractTestLogger::Warn:
133 return COLORED_MSG(0, 33, "WARNING"); // yellow
134 case QAbstractTestLogger::QWarning:
135 return COLORED_MSG(1, 33, "QWARN ");
136 case QAbstractTestLogger::QDebug:
137 return COLORED_MSG(1, 33, "QDEBUG ");
138 case QAbstractTestLogger::QSystem:
139 return COLORED_MSG(1, 33, "QSYSTEM");
140 case QAbstractTestLogger::QFatal:
141 return COLORED_MSG(0, 31, "QFATAL "); // red
142 case QAbstractTestLogger::Info:
143 return "INFO "; // no coloring
144 }
145 return "??????";
146 }
147
148 static void outputMessage(const char *str)
149 {
150#if defined(Q_OS_WINCE)
151 int length = strlen(str);
152 for (int pos = 0; pos < length; pos +=255) {
153 QString uniText = QString::fromLatin1(str + pos, 255);
154 OutputDebugStringW((const LPCWSTR) uniText.utf16());
155 }
156 if (QTestLog::outputFileName())
157#elif defined(Q_OS_WIN)
158 EnterCriticalSection(&outputCriticalSection);
159 // OutputDebugString is not threadsafe
160 OutputDebugStringA(str);
161 LeaveCriticalSection(&outputCriticalSection);
162#endif
163 QAbstractTestLogger::outputString(str);
164 }
165
166 static void printMessage(const char *type, const char *msg, const char *file = 0, int line = 0)
167 {
168 QTEST_ASSERT(type);
169 QTEST_ASSERT(msg);
170
171 char buf[1024];
172
173 const char *fn = QTestResult::currentTestFunction() ? QTestResult::currentTestFunction()
174 : "UnknownTestFunc";
175 const char *tag = QTestResult::currentDataTag() ? QTestResult::currentDataTag() : "";
176 const char *gtag = QTestResult::currentGlobalDataTag()
177 ? QTestResult::currentGlobalDataTag()
178 : "";
179 const char *filler = (tag[0] && gtag[0]) ? ":" : "";
180 if (file) {
181 QTest::qt_snprintf(buf, sizeof(buf), "%s: %s::%s(%s%s%s)%s%s\n"
182#ifdef Q_OS_WIN
183 "%s(%d) : failure location\n"
184#else
185 " Loc: [%s(%d)]\n"
186#endif
187 , type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag,
188 msg[0] ? " " : "", msg, file, line);
189 } else {
190 QTest::qt_snprintf(buf, sizeof(buf), "%s: %s::%s(%s%s%s)%s%s\n",
191 type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag,
192 msg[0] ? " " : "", msg);
193 }
194 memcpy(buf, type, strlen(type));
195 outputMessage(buf);
196 }
197
198 template <typename T>
199 static int countSignificantDigits(T num)
200 {
201 if (num <= 0)
202 return 0;
203
204 int digits = 0;
205 qreal divisor = 1;
206
207 while (num / divisor >= 1) {
208 divisor *= 10;
209 ++digits;
210 }
211
212 return digits;
213 }
214
215 // Pretty-prints a benchmark result using the given number of digits.
216 template <typename T> QString formatResult(T number, int significantDigits)
217 {
218 if (number < T(0))
219 return QString(QLatin1String("NAN"));
220 if (number == T(0))
221 return QString(QLatin1String("0"));
222
223 QString beforeDecimalPoint = QString::number(qint64(number), 'f', 0);
224 QString afterDecimalPoint = QString::number(number, 'f', 20);
225 afterDecimalPoint.remove(0, beforeDecimalPoint.count() + 1);
226
227 int beforeUse = qMin(beforeDecimalPoint.count(), significantDigits);
228 int beforeRemove = beforeDecimalPoint.count() - beforeUse;
229
230 // Replace insignificant digits before the decimal point with zeros.
231 beforeDecimalPoint.chop(beforeRemove);
232 for (int i = 0; i < beforeRemove; ++i) {
233 beforeDecimalPoint.append(QLatin1Char('0'));
234 }
235
236 int afterUse = significantDigits - beforeUse;
237
238 // leading zeroes after the decimal point does not count towards the digit use.
239 if (beforeDecimalPoint == QLatin1String("0") && afterDecimalPoint.isEmpty() == false) {
240 ++afterUse;
241
242 int i = 0;
243 while (i < afterDecimalPoint.count() && afterDecimalPoint.at(i) == QLatin1Char('0')) {
244 ++i;
245 }
246
247 afterUse += i;
248 }
249
250 int afterRemove = afterDecimalPoint.count() - afterUse;
251 afterDecimalPoint.chop(afterRemove);
252
253 QChar separator = QLatin1Char(',');
254 QChar decimalPoint = QLatin1Char('.');
255
256 // insert thousands separators
257 int length = beforeDecimalPoint.length();
258 for (int i = beforeDecimalPoint.length() -1; i >= 1; --i) {
259 if ((length - i) % 3 == 0)
260 beforeDecimalPoint.insert(i, separator);
261 }
262
263 QString print;
264 print = beforeDecimalPoint;
265 if (afterUse > 0)
266 print.append(decimalPoint);
267
268 print += afterDecimalPoint;
269
270
271 return print;
272 }
273
274 template <typename T>
275 int formatResult(char * buffer, int bufferSize, T number, int significantDigits)
276 {
277 QString result = formatResult(number, significantDigits);
278 qstrncpy(buffer, result.toAscii().constData(), bufferSize);
279 int size = result.count();
280 return size;
281 }
282
283// static void printBenchmarkResult(const char *bmtag, int value, int iterations)
284 static void printBenchmarkResult(const QBenchmarkResult &result)
285 {
286 const char *bmtag = QTest::benchmarkResult2String();
287
288 char buf1[1024];
289 QTest::qt_snprintf(
290 buf1, sizeof(buf1), "%s: %s::%s",
291 bmtag,
292 QTestResult::currentTestObjectName(),
293 result.context.slotName.toAscii().data());
294
295
296 char bufTag[1024];
297 bufTag[0] = 0;
298 QByteArray tag = result.context.tag.toAscii();
299 if (tag.isEmpty() == false) {
300 QTest::qt_snprintf(bufTag, sizeof(bufTag), ":\"%s\"", tag.data());
301 }
302
303
304 char fillFormat[8];
305 int fillLength = 5;
306 QTest::qt_snprintf(
307 fillFormat, sizeof(fillFormat), ":\n%%%ds", fillLength);
308 char fill[1024];
309 QTest::qt_snprintf(fill, sizeof(fill), fillFormat, "");
310
311
312 QByteArray unitText = QBenchmarkGlobalData::current->measurer->unitText().toAscii();
313
314 qreal valuePerIteration = qreal(result.value) / qreal(result.iterations);
315 char resultBuffer[100] = "";
316 formatResult(resultBuffer, 100, valuePerIteration, countSignificantDigits(result.value));
317
318 QByteArray iterationText = "per iteration";
319
320 char buf2[1024];
321 Q_ASSERT(result.iterations > 0);
322 QTest::qt_snprintf(
323 buf2, sizeof(buf2), "%s %s %s",
324 resultBuffer,
325 unitText.data(),
326 iterationText.data());
327
328 char buf3[1024];
329 Q_ASSERT(result.iterations > 0);
330 QTest::qt_snprintf(
331 buf3, sizeof(buf3), " (total: %s, iterations: %d)\n",
332 QByteArray::number(result.value).constData(), // no 64-bit qt_snprintf support
333 result.iterations);
334
335 char buf[1024];
336 QTest::qt_snprintf(buf, sizeof(buf), "%s%s%s%s%s", buf1, bufTag, fill, buf2, buf3);
337 memcpy(buf, bmtag, strlen(bmtag));
338 outputMessage(buf);
339 }
340}
341
342QPlainTestLogger::QPlainTestLogger()
343{
344#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
345 InitializeCriticalSection(&QTest::outputCriticalSection);
346 QTest::hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
347 if (QTest::hConsole != INVALID_HANDLE_VALUE) {
348 CONSOLE_SCREEN_BUFFER_INFO info;
349 if (GetConsoleScreenBufferInfo(QTest::hConsole, &info)) {
350 QTest::consoleAttributes = info.wAttributes;
351 } else {
352 QTest::hConsole = INVALID_HANDLE_VALUE;
353 }
354 }
355#endif
356}
357
358QPlainTestLogger::~QPlainTestLogger()
359{
360#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
361 DeleteCriticalSection(&QTest::outputCriticalSection);
362#endif
363}
364
365void QPlainTestLogger::startLogging()
366{
367 QAbstractTestLogger::startLogging();
368
369 char buf[1024];
370 if (QTestLog::verboseLevel() < 0) {
371 QTest::qt_snprintf(buf, sizeof(buf), "Testing %s\n",
372 QTestResult::currentTestObjectName());
373 } else {
374 QTest::qt_snprintf(buf, sizeof(buf),
375 "********* Start testing of %s *********\n"
376 "Config: Using QTest library " QTEST_VERSION_STR
377 ", Qt %s\n", QTestResult::currentTestObjectName(), qVersion());
378 }
379 QTest::outputMessage(buf);
380}
381
382void QPlainTestLogger::stopLogging()
383{
384 char buf[1024];
385 if (QTestLog::verboseLevel() < 0) {
386 QTest::qt_snprintf(buf, sizeof(buf), "Totals: %d passed, %d failed, %d skipped\n",
387 QTestResult::passCount(), QTestResult::failCount(),
388 QTestResult::skipCount());
389 } else {
390 QTest::qt_snprintf(buf, sizeof(buf),
391 "Totals: %d passed, %d failed, %d skipped\n"
392 "********* Finished testing of %s *********\n",
393 QTestResult::passCount(), QTestResult::failCount(),
394 QTestResult::skipCount(), QTestResult::currentTestObjectName());
395 }
396 QTest::outputMessage(buf);
397
398 QAbstractTestLogger::stopLogging();
399}
400
401
402void QPlainTestLogger::enterTestFunction(const char * /*function*/)
403{
404 if (QTestLog::verboseLevel() >= 1)
405 QTest::printMessage(QTest::messageType2String(Info), "entering");
406}
407
408void QPlainTestLogger::leaveTestFunction()
409{
410}
411
412void QPlainTestLogger::addIncident(IncidentTypes type, const char *description,
413 const char *file, int line)
414{
415 // suppress PASS in silent mode
416 if (type == QAbstractTestLogger::Pass && QTestLog::verboseLevel() < 0)
417 return;
418
419 QTest::printMessage(QTest::incidentType2String(type), description, file, line);
420}
421
422void QPlainTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
423{
424// QTest::printBenchmarkResult(QTest::benchmarkResult2String(), value, iterations);
425 QTest::printBenchmarkResult(result);
426}
427
428void QPlainTestLogger::addMessage(MessageTypes type, const char *message,
429 const char *file, int line)
430{
431 // suppress PASS in silent mode
432 if ((type == QAbstractTestLogger::Skip || type == QAbstractTestLogger::Info)
433 && QTestLog::verboseLevel() < 0)
434 return;
435
436 QTest::printMessage(QTest::messageType2String(type), message, file, line);
437}
438
439QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.