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

Last change on this file since 69 was 2, checked in by Dmitry A. Kuminov, 17 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);