From: Thiago Macieira Date: Thu, 21 Jul 2011 14:33:40 +0000 (+0200) Subject: Add unit tests to QStringLiteral and QByteArrayLiteral with operator % X-Git-Tag: qt-v5.0.0-alpha1~3928 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a6d87b51141e362f39e323bf329491d27565b137;p=profile%2Fivi%2Fqtbase.git Add unit tests to QStringLiteral and QByteArrayLiteral with operator % This requires a fix for QByteArrayLiteral to work too. Change-Id: I3c2a50ad431d5b0c014a341e675fa54e7b206e70 Merge-request: 27 Reviewed-by: Olivier Goffart Reviewed-on: http://codereview.qt.nokia.com/1967 Reviewed-by: Qt Sanity Bot --- diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 6d998b6..63c487e 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -364,6 +364,27 @@ template <> struct QConcatenable : private QAbstractConcatenable } }; +template struct QConcatenable > : private QAbstractConcatenable +{ + typedef QConstByteArrayDataPtr type; + typedef QByteArray ConvertTo; + enum { ExactSize = false }; + static int size(const type &) { return N; } +#ifndef QT_NO_CAST_FROM_ASCII + static inline QT_ASCII_CAST_WARN void appendTo(const type &a, QChar *&out) + { + // adding 1 because convertFromAscii expects the size including the null-termination + QAbstractConcatenable::convertFromAscii(a.ptr->data, N + 1, out); + } +#endif + static inline void appendTo(const type &ba, char *&out) + { + const char *a = ba.ptr->data; + while (*a) + *out++ = *a++; + } +}; + namespace QtStringBuilder { template struct ConvertToTypeHelper { typedef A ConvertTo; }; diff --git a/tests/auto/qstringbuilder1/stringbuilder.cpp b/tests/auto/qstringbuilder1/stringbuilder.cpp index de7ad65..4d453b0 100644 --- a/tests/auto/qstringbuilder1/stringbuilder.cpp +++ b/tests/auto/qstringbuilder1/stringbuilder.cpp @@ -39,6 +39,8 @@ ** ****************************************************************************/ +#include + #define LITERAL "some literal" #define LITERAL_LEN (sizeof(LITERAL)-1) #define LITERAL_EXTRA "some literal" "EXTRA" @@ -48,6 +50,12 @@ #define UTF8_LITERAL_LEN (sizeof(UTF8_LITERAL)-1) #define UTF8_LITERAL_EXTRA "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l" "EXTRA" +#ifdef Q_COMPILER_UNICODE_STRINGS +// "some literal", but replacing all vocals by their umlauted UTF-8 string :) +#define UNICODE_LITERAL u"s\u00f6m\u00eb l\u00eft\u00ebr\u00e4l" +#define UNICODE_LITERAL_LEN ((sizeof(UNICODE_LITERAL) - 1) / 2) +#define UNICODE_LITERAL_EXTRA u"s\u00f6m\u00eb l\u00eft\u00ebr\u00e4l" "EXTRA" +#endif //fix for gcc4.0: if the operator+ does not exist without QT_USE_FAST_OPERATOR_PLUS #ifndef QT_USE_FAST_CONCATENATION @@ -68,6 +76,7 @@ void runScenario() QStringRef stringref(&string, 2, 10); QLatin1Char achar('c'); QString r2(QLatin1String(LITERAL LITERAL)); + QString r3 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL); QString r; r = l1literal Q l1literal; @@ -80,10 +89,23 @@ void runScenario() QCOMPARE(r, r2); r = string P l1string; QCOMPARE(r, r2); + r = string Q QStringLiteral(LITERAL); + QCOMPARE(r, r2); + r = QStringLiteral(LITERAL) Q string; + QCOMPARE(r, r2); + r = l1string Q QStringLiteral(LITERAL); + QCOMPARE(r, r2); r = string + achar; QCOMPARE(r, QString(string P achar)); r = achar + string; QCOMPARE(r, QString(achar P string)); + +#ifdef Q_COMPILER_UNICODE_STRINGS + r = QStringLiteral(UNICODE_LITERAL); + r = r Q QStringLiteral(UNICODE_LITERAL); + QCOMPARE(r, r3); +#endif + #ifndef QT_NO_CAST_FROM_ASCII r = string P LITERAL; QCOMPARE(r, r2); @@ -96,6 +118,11 @@ void runScenario() r = string P ba; QCOMPARE(r, r2); + r = string P QByteArrayLiteral(LITERAL); + QCOMPARE(r, r2); + r = QByteArrayLiteral(LITERAL) P string; + QCOMPARE(r, r2); + static const char badata[] = LITERAL_EXTRA; ba = QByteArray::fromRawData(badata, LITERAL_LEN); r = ba P string; @@ -109,24 +136,23 @@ void runScenario() QCOMPARE(QTextCodec::codecForCStrings()->name(), QByteArray("UTF-8")); string = QString::fromUtf8(UTF8_LITERAL); - r2 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL); ba = UTF8_LITERAL; r = string P UTF8_LITERAL; - QCOMPARE(r.size(), r2.size()); - QCOMPARE(r, r2); + QCOMPARE(r.size(), r3.size()); + QCOMPARE(r, r3); r = UTF8_LITERAL P string; - QCOMPARE(r, r2); + QCOMPARE(r, r3); r = ba P string; - QCOMPARE(r, r2); + QCOMPARE(r, r3); r = string P ba; - QCOMPARE(r, r2); + QCOMPARE(r, r3); ba = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); r = ba P string; - QCOMPARE(r, r2); + QCOMPARE(r, r3); r = string P ba; - QCOMPARE(r, r2); + QCOMPARE(r, r3); ba = QByteArray(); // empty r = ba P string; @@ -152,6 +178,11 @@ void runScenario() QByteArray superba = ba P ba P LITERAL; QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); + ba = QByteArrayLiteral(LITERAL); + QCOMPARE(ba, QByteArray(LITERAL)); + superba = ba P QByteArrayLiteral(LITERAL) P LITERAL; + QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); + QByteArray testWith0 = ba P "test\0with\0zero" P ba; QCOMPARE(testWith0, QByteArray(LITERAL "test" LITERAL));