Fix binary incompatibility between openssl versions
[profile/ivi/qtbase.git] / src / testlib / qplaintestlogger.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
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 #include <QtTest/private/qbenchmarkmetric_p.h>
48
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #ifdef Q_OS_WIN
55 #include <windows.h>
56 #endif
57
58 #ifdef Q_OS_WINCE
59 #include <QtCore/QString>
60 #endif
61
62 #include <QtCore/QByteArray>
63 #include <QtCore/qmath.h>
64
65 QT_BEGIN_NAMESPACE
66
67 namespace QTest {
68
69     static const char *incidentType2String(QAbstractTestLogger::IncidentTypes type)
70     {
71         switch (type) {
72         case QAbstractTestLogger::Pass:
73             return "PASS   ";
74         case QAbstractTestLogger::XFail:
75             return "XFAIL  ";
76         case QAbstractTestLogger::Fail:
77             return "FAIL!  ";
78         case QAbstractTestLogger::XPass:
79             return "XPASS  ";
80         }
81         return "??????";
82     }
83
84     static const char *benchmarkResult2String()
85     {
86         return "RESULT ";
87     }
88
89     static const char *messageType2String(QAbstractTestLogger::MessageTypes type)
90     {
91         switch (type) {
92         case QAbstractTestLogger::Skip:
93             return "SKIP   ";
94         case QAbstractTestLogger::Warn:
95             return "WARNING";
96         case QAbstractTestLogger::QWarning:
97             return "QWARN  ";
98         case QAbstractTestLogger::QDebug:
99             return "QDEBUG ";
100         case QAbstractTestLogger::QSystem:
101             return "QSYSTEM";
102         case QAbstractTestLogger::QFatal:
103             return "QFATAL ";
104         case QAbstractTestLogger::Info:
105             return "INFO   ";
106         }
107         return "??????";
108     }
109
110     template <typename T>
111     static int countSignificantDigits(T num)
112     {
113         if (num <= 0)
114             return 0;
115
116         int digits = 0;
117         qreal divisor = 1;
118
119         while (num / divisor >= 1) {
120             divisor *= 10;
121             ++digits;
122         }
123
124         return digits;
125     }
126
127     // Pretty-prints a benchmark result using the given number of digits.
128     template <typename T> QString formatResult(T number, int significantDigits)
129     {
130         if (number < T(0))
131             return QLatin1String("NAN");
132         if (number == T(0))
133             return QLatin1String("0");
134
135         QString beforeDecimalPoint = QString::number(qint64(number), 'f', 0);
136         QString afterDecimalPoint = QString::number(number, 'f', 20);
137         afterDecimalPoint.remove(0, beforeDecimalPoint.count() + 1);
138
139         int beforeUse = qMin(beforeDecimalPoint.count(), significantDigits);
140         int beforeRemove = beforeDecimalPoint.count() - beforeUse;
141
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'));
146         }
147
148         int afterUse = significantDigits - beforeUse;
149
150         // leading zeroes after the decimal point does not count towards the digit use.
151         if (beforeDecimalPoint == QLatin1String("0") && afterDecimalPoint.isEmpty() == false) {
152             ++afterUse;
153
154             int i = 0;
155             while (i < afterDecimalPoint.count() && afterDecimalPoint.at(i) == QLatin1Char('0')) {
156                 ++i;
157             }
158
159             afterUse += i;
160         }
161
162         int afterRemove = afterDecimalPoint.count() - afterUse;
163         afterDecimalPoint.chop(afterRemove);
164
165         QChar separator = QLatin1Char(',');
166         QChar decimalPoint = QLatin1Char('.');
167
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);
173         }
174
175         QString print;
176         print = beforeDecimalPoint;
177         if (afterUse > 0)
178             print.append(decimalPoint);
179
180         print += afterDecimalPoint;
181
182
183         return print;
184     }
185
186     template <typename T>
187     int formatResult(char * buffer, int bufferSize, T number, int significantDigits)
188     {
189         QString result = formatResult(number, significantDigits);
190         qstrncpy(buffer, result.toLatin1().constData(), bufferSize);
191         int size = result.count();
192         return size;
193     }
194 }
195
196 void QPlainTestLogger::outputMessage(const char *str)
197 {
198 #if defined(Q_OS_WINCE)
199     QString strUtf16 = QString::fromLatin1(str);
200     const int maxOutputLength = 255;
201     do {
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);
209 #endif
210     outputString(str);
211 }
212
213 void QPlainTestLogger::printMessage(const char *type, const char *msg, const char *file, int line)
214 {
215     QTEST_ASSERT(type);
216     QTEST_ASSERT(msg);
217
218     QTestCharBuffer buf;
219
220     const char *fn = QTestResult::currentTestFunction() ? QTestResult::currentTestFunction()
221         : "UnknownTestFunc";
222     const char *tag = QTestResult::currentDataTag() ? QTestResult::currentDataTag() : "";
223     const char *gtag = QTestResult::currentGlobalDataTag()
224                      ? QTestResult::currentGlobalDataTag()
225                      : "";
226     const char *filler = (tag[0] && gtag[0]) ? ":" : "";
227     if (file) {
228         QTest::qt_asprintf(&buf, "%s: %s::%s(%s%s%s)%s%s\n"
229 #ifdef Q_OS_WIN
230                       "%s(%d) : failure location\n"
231 #else
232                       "   Loc: [%s(%d)]\n"
233 #endif
234                       , type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag,
235                       msg[0] ? " " : "", msg, file, line);
236     } else {
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);
240     }
241     // In colored mode, printf above stripped our nonprintable control characters.
242     // Put them back.
243     memcpy(buf.data(), type, strlen(type));
244     outputMessage(buf.data());
245 }
246
247 void QPlainTestLogger::printBenchmarkResult(const QBenchmarkResult &result)
248 {
249     const char *bmtag = QTest::benchmarkResult2String();
250
251     char buf1[1024];
252     qsnprintf(
253         buf1, sizeof(buf1), "%s: %s::%s",
254         bmtag,
255         QTestResult::currentTestObjectName(),
256         result.context.slotName.toLatin1().data());
257
258     char bufTag[1024];
259     bufTag[0] = 0;
260     QByteArray tag = result.context.tag.toLocal8Bit();
261     if (tag.isEmpty() == false) {
262         qsnprintf(bufTag, sizeof(bufTag), ":\"%s\"", tag.data());
263     }
264
265
266     char fillFormat[8];
267     int fillLength = 5;
268     qsnprintf(fillFormat, sizeof(fillFormat), ":\n%%%ds", fillLength);
269     char fill[1024];
270     qsnprintf(fill, sizeof(fill), fillFormat, "");
271
272     const char * unitText = QTest::benchmarkMetricUnit(result.metric);
273
274     qreal valuePerIteration = qreal(result.value) / qreal(result.iterations);
275     char resultBuffer[100] = "";
276     QTest::formatResult(resultBuffer, 100, valuePerIteration, QTest::countSignificantDigits(result.value));
277
278     char buf2[1024];
279     qsnprintf(buf2, sizeof(buf2), "%s %s", resultBuffer, unitText);
280
281     char buf2_[1024];
282     QByteArray iterationText = " per iteration";
283     Q_ASSERT(result.iterations > 0);
284     qsnprintf(buf2_, sizeof(buf2_), "%s", iterationText.data());
285
286     char buf3[1024];
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);
290
291     char buf[1024];
292
293     if (result.setByMacro) {
294         qsnprintf(buf, sizeof(buf), "%s%s%s%s%s%s\n", buf1, bufTag, fill, buf2, buf2_, buf3);
295     } else {
296         qsnprintf(buf, sizeof(buf), "%s%s%s%s\n", buf1, bufTag, fill, buf2);
297     }
298
299     memcpy(buf, bmtag, strlen(bmtag));
300     outputMessage(buf);
301 }
302
303 QPlainTestLogger::QPlainTestLogger(const char *filename)
304     : QAbstractTestLogger(filename)
305 {
306 }
307
308 QPlainTestLogger::~QPlainTestLogger()
309 {
310 }
311
312 void QPlainTestLogger::startLogging()
313 {
314     QAbstractTestLogger::startLogging();
315
316     char buf[1024];
317     if (QTestLog::verboseLevel() < 0) {
318         qsnprintf(buf, sizeof(buf), "Testing %s\n", QTestResult::currentTestObjectName());
319     } else {
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());
324     }
325     outputMessage(buf);
326 }
327
328 void QPlainTestLogger::stopLogging()
329 {
330     char buf[1024];
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());
335     } else {
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());
341     }
342     outputMessage(buf);
343
344     QAbstractTestLogger::stopLogging();
345 }
346
347
348 void QPlainTestLogger::enterTestFunction(const char * /*function*/)
349 {
350     if (QTestLog::verboseLevel() >= 1)
351         printMessage(QTest::messageType2String(Info), "entering");
352 }
353
354 void QPlainTestLogger::leaveTestFunction()
355 {
356 }
357
358 void QPlainTestLogger::addIncident(IncidentTypes type, const char *description,
359                                    const char *file, int line)
360 {
361     // suppress PASS and XFAIL in silent mode
362     if ((type == QAbstractTestLogger::Pass || type == QAbstractTestLogger::XFail)
363         && QTestLog::verboseLevel() < 0)
364         return;
365
366     printMessage(QTest::incidentType2String(type), description, file, line);
367 }
368
369 void QPlainTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
370 {
371     // suppress benchmark results in silent mode
372     if (QTestLog::verboseLevel() < 0)
373         return;
374
375     printBenchmarkResult(result);
376 }
377
378 void QPlainTestLogger::addMessage(MessageTypes type, const char *message,
379                                   const char *file, int line)
380 {
381     // suppress non-fatal messages in silent mode
382     if (type != QAbstractTestLogger::QFatal && QTestLog::verboseLevel() < 0)
383         return;
384
385     printMessage(QTest::messageType2String(type), message, file, line);
386 }
387
388 QT_END_NAMESPACE