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" 58 #if defined(Q_OS_SYMBIAN) 63 #include <QtCore/QString> 66 #include <QtCore/QByteArray> 67 #include <QtCore/qmath.h> 73 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) 75 static CRITICAL_SECTION outputCriticalSection;
76 static HANDLE hConsole = INVALID_HANDLE_VALUE;
77 static WORD consoleAttributes = 0;
79 static const char *qWinColoredMsg(
int prefix,
int color,
const char *msg)
84 WORD attr = consoleAttributes & ~(FOREGROUND_GREEN | FOREGROUND_BLUE
85 | FOREGROUND_RED | FOREGROUND_INTENSITY);
87 attr |= FOREGROUND_INTENSITY;
89 attr |= FOREGROUND_GREEN;
91 attr |= FOREGROUND_BLUE | FOREGROUND_GREEN;
93 attr |= FOREGROUND_RED | FOREGROUND_INTENSITY;
95 attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
97 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
98 SetConsoleTextAttribute(hConsole, attr);
100 SetConsoleTextAttribute(hConsole, consoleAttributes);
104 # define COLORED_MSG(prefix, color, msg) colored ? qWinColoredMsg(prefix, color, msg) : msg 106 # define COLORED_MSG(prefix, color, msg) colored && QAbstractTestLogger::isTtyOutput() ? "\033["#prefix";"#color"m" msg "\033[0m" : msg 136 static bool colored = ::getenv(
"QTEST_COLORED");
159 #if defined(Q_OS_WINCE) 161 const int maxOutputLength = 255;
164 OutputDebugString((
wchar_t*)tmp.
utf16());
165 strUtf16.
remove(0, maxOutputLength);
169 EnterCriticalSection(&outputCriticalSection);
171 OutputDebugStringA(str);
172 LeaveCriticalSection(&outputCriticalSection);
173 #elif defined(Q_OS_SYMBIAN) 175 TPtrC8
ptr(reinterpret_cast<const TUint8*>(str));
176 _LIT(
format,
"[QTestLib] %S");
177 const int maxBlockSize = 256 - ((
const TDesC &)
format).Length();
178 HBufC* hbuffer = HBufC::New(maxBlockSize);
180 for (
int i = 0; i < ptr.Length(); i += maxBlockSize) {
181 int size = Min(maxBlockSize, ptr.Length() - i);
182 hbuffer->Des().Copy(ptr.Mid(i, size));
183 RDebug::Print(
format, hbuffer);
190 TPtrC8
ptr(reinterpret_cast<const TUint8*>(str));
191 RDebug::RawPrint(ptr);
192 RDebug::RawPrint(_L8(
"\n"));
198 static void printMessage(
const char *
type,
const char *msg,
const char *file = 0,
int line = 0)
211 const char *filler = (tag[0] && gtag[0]) ?
":" :
"";
215 "%s(%d) : failure location\n" 220 msg[0] ?
" " :
"", msg, file, line);
224 msg[0] ?
" " :
"", msg);
228 memcpy(buf.
data(),
type, strlen(type));
232 template <
typename T>
241 while (num / divisor >= 1) {
259 afterDecimalPoint.
remove(0, beforeDecimalPoint.
count() + 1);
261 int beforeUse =
qMin(beforeDecimalPoint.
count(), significantDigits);
262 int beforeRemove = beforeDecimalPoint.
count() - beforeUse;
265 beforeDecimalPoint.
chop(beforeRemove);
266 for (
int i = 0; i < beforeRemove; ++i) {
270 int afterUse = significantDigits - beforeUse;
277 while (i < afterDecimalPoint.
count() && afterDecimalPoint.
at(i) ==
QLatin1Char(
'0')) {
284 int afterRemove = afterDecimalPoint.
count() - afterUse;
285 afterDecimalPoint.
chop(afterRemove);
291 int length = beforeDecimalPoint.
length();
292 for (
int i = beforeDecimalPoint.
length() -1; i >= 1; --i) {
293 if ((length - i) % 3 == 0)
294 beforeDecimalPoint.
insert(i, separator);
298 print = beforeDecimalPoint;
300 print.
append(decimalPoint);
302 print += afterDecimalPoint;
308 template <
typename T>
309 int formatResult(
char * buffer,
int bufferSize, T number,
int significantDigits)
313 int size = result.
count();
324 buf1,
sizeof(buf1),
"%s: %s::%s",
340 fillFormat,
sizeof(fillFormat),
":\n%%%ds", fillLength);
347 char resultBuffer[100] =
"";
352 buf2,
sizeof(buf2),
"%s %s",
362 iterationText.
data());
368 buf3,
sizeof(buf3),
" (total: %s, iterations: %d)",
376 buf,
sizeof(buf),
"%s%s%s%s%s%s\n", buf1, bufTag, fill, buf2, buf2_, buf3);
381 memcpy(buf, bmtag, strlen(bmtag));
387 : randomSeed(9), hasRandomSeed(false)
389 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) 390 InitializeCriticalSection(&QTest::outputCriticalSection);
391 QTest::hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
392 if (QTest::hConsole != INVALID_HANDLE_VALUE) {
393 CONSOLE_SCREEN_BUFFER_INFO
info;
394 if (GetConsoleScreenBufferInfo(QTest::hConsole, &info)) {
395 QTest::consoleAttributes = info.wAttributes;
397 QTest::hConsole = INVALID_HANDLE_VALUE;
405 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) 406 DeleteCriticalSection(&QTest::outputCriticalSection);
421 "********* Start testing of %s *********\n" 426 "********* Start testing of %s *********\n" 443 "Totals: %d passed, %d failed, %d skipped\n" 444 "********* Finished testing of %s *********\n",
465 const char *file,
int line)
481 const char *file,
int line)
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QTest::QBenchmarkMetric metric
static const char * currentTestFunction()
static int verboseLevel()
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static int countSignificantDigits(T num)
static void outputString(const char *msg)
static mach_timebase_info_data_t info
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
const char * benchmarkMetricUnit(QBenchmarkMetric metric)
char * data()
Returns a pointer to the data stored in the byte array.
const QChar at(int i) const
Returns the character at the given index position in the string.
static const char * incidentType2String(QAbstractTestLogger::IncidentTypes type)
The QByteArray class provides an array of bytes.
void chop(int n)
Removes n characters from the end of the string.
int length() const
Returns the number of characters in this string.
int qt_asprintf(QTestCharBuffer *str, const char *format,...)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
void addIncident(IncidentTypes type, const char *description, const char *file=0, int line=0)
static const char * outputFileName()
The QChar class provides a 16-bit Unicode character.
void registerRandomSeed(unsigned int seed)
Q_CORE_EXPORT const char * qVersion()
virtual void stopLogging()
static const char * benchmarkResult2String()
QString formatResult(T number, int significantDigits)
#define QT_BEGIN_NAMESPACE
This macro expands to.
static const char * currentTestObjectName()
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
static const char * messageType2String(QAbstractTestLogger::MessageTypes type)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
void enterTestFunction(const char *function)
const T * ptr(const T &t)
static void outputMessage(const char *str)
#define QTEST_VERSION_STR
void addBenchmarkResult(const QBenchmarkResult &result)
const char * constData() const
Returns a pointer to the data stored in the byte array.
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
static const char * currentGlobalDataTag()
QString & append(QChar c)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
static void printMessage(const char *type, const char *msg, const char *file=0, int line=0)
void addMessage(MessageTypes type, const char *message, const char *file=0, int line=0)
#define QTEST_ASSERT(cond)
QByteArray toAscii() const Q_REQUIRED_RESULT
Returns an 8-bit representation of the string as a QByteArray.
static const char * currentDataTag()
static void printBenchmarkResult(const QBenchmarkResult &result)
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
virtual void startLogging()
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
QBenchmarkContext context
QString & insert(int i, QChar c)
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, uint len)
#define COLORED_MSG(prefix, color, msg)
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.