1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #define LITERAL "some literal"
45 #define LITERAL_LEN (sizeof(LITERAL)-1)
46 #define LITERAL_EXTRA "some literal" "EXTRA"
48 // "some literal", but replacing all vocals by their umlauted UTF-8 string :)
49 #define UTF8_LITERAL "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l"
50 #define UTF8_LITERAL_LEN (sizeof(UTF8_LITERAL)-1)
51 #define UTF8_LITERAL_EXTRA "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l" "EXTRA"
53 #ifdef Q_COMPILER_UNICODE_STRINGS
54 // "some literal", but replacing all vocals by their umlauted UTF-8 string :)
55 #define UNICODE_LITERAL u"s\u00f6m\u00eb l\u00eft\u00ebr\u00e4l"
56 #define UNICODE_LITERAL_LEN ((sizeof(UNICODE_LITERAL) - 1) / 2)
57 #define UNICODE_LITERAL_EXTRA u"s\u00f6m\u00eb l\u00eft\u00ebr\u00e4l" "EXTRA"
60 //fix for gcc4.0: if the operator+ does not exist without QT_USE_FAST_OPERATOR_PLUS
61 #ifndef QT_USE_FAST_CONCATENATION
69 // this code is latin1. TODO: replace it with the utf8 block below, once
70 // strings default to utf8.
71 QLatin1Literal l1literal(LITERAL);
72 QLatin1String l1string(LITERAL);
73 QString string(l1string);
74 QStringRef stringref(&string, 2, 10);
75 QLatin1Char achar('c');
76 QChar::SpecialCharacter special(QChar::Nbsp);
77 QString r2(QLatin1String(LITERAL LITERAL));
78 QString r3 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL);
81 r = l1literal Q l1literal;
85 r = stringref Q stringref;
86 QCOMPARE(r, QString(stringref.toString() + stringref.toString()));
87 r = string Q l1literal;
89 r = string P l1string;
91 r = string Q QStringLiteral(LITERAL);
93 r = QStringLiteral(LITERAL) Q string;
95 r = l1string Q QStringLiteral(LITERAL);
98 QCOMPARE(r, QString(string P achar));
100 QCOMPARE(r, QString(achar P string));
101 r = special + string;
102 QCOMPARE(r, QString(special P string));
104 #ifdef Q_COMPILER_UNICODE_STRINGS
105 r = QStringLiteral(UNICODE_LITERAL);
106 r = r Q QStringLiteral(UNICODE_LITERAL);
110 #ifndef QT_NO_CAST_FROM_ASCII
111 r = string P LITERAL;
113 r = LITERAL P string;
116 QByteArray ba = QByteArray(LITERAL);
122 r = string P QByteArrayLiteral(LITERAL);
124 r = QByteArrayLiteral(LITERAL) P string;
127 static const char badata[] = LITERAL_EXTRA;
128 ba = QByteArray::fromRawData(badata, LITERAL_LEN);
135 // now test with codec for C strings set
136 // TODO: to be re-enabled once strings default to utf8, in place of the
137 // latin1 code above.
138 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
139 QVERIFY(QTextCodec::codecForCStrings());
140 QCOMPARE(QTextCodec::codecForCStrings()->name(), QByteArray("UTF-8"));
142 string = QString::fromUtf8(UTF8_LITERAL);
145 r = string P UTF8_LITERAL;
146 QCOMPARE(r.size(), r3.size());
148 r = UTF8_LITERAL P string;
155 ba = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN);
162 ba = QByteArray(); // empty
168 const char *zero = 0;
175 string = QString::fromLatin1(LITERAL);
176 QCOMPARE(QByteArray(qPrintable(string P string)), QByteArray(string.toLatin1() + string.toLatin1()));
182 QByteArray ba = LITERAL;
183 QByteArray superba = ba P ba P LITERAL;
184 QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL));
186 ba = QByteArrayLiteral(LITERAL);
187 QCOMPARE(ba, QByteArray(LITERAL));
188 superba = ba P QByteArrayLiteral(LITERAL) P LITERAL;
189 QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL));
191 QByteArray testWith0 = ba P "test\0with\0zero" P ba;
192 QCOMPARE(testWith0, QByteArray(LITERAL "test" LITERAL));
194 QByteArray ba2 = ba P '\0' + LITERAL;
195 QCOMPARE(ba2, QByteArray(LITERAL "\0" LITERAL, ba.size()*2+1));
197 const char *mmh = "test\0foo";
198 QCOMPARE(QByteArray(ba P mmh P ba), testWith0);
200 QByteArray raw = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN);
201 QByteArray r = "hello" P raw;
202 QByteArray r2 = "hello" UTF8_LITERAL;
204 r2 = QByteArray("hello\0") P UTF8_LITERAL;
207 const char *zero = 0;
214 //operator QString +=
216 QString str = QString::fromUtf8(UTF8_LITERAL);
217 str += QLatin1String(LITERAL) P str;
218 QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL));
219 #ifndef QT_NO_CAST_FROM_ASCII
221 // TODO: this relies on strings defaulting to utf8, so disable this for now.
222 str = (QString::fromUtf8(UTF8_LITERAL) += QLatin1String(LITERAL) P UTF8_LITERAL);
223 QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL));
228 //operator QByteArray +=
230 QByteArray ba = UTF8_LITERAL;
231 ba += QByteArray(LITERAL) P UTF8_LITERAL;
232 QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL));
233 ba += LITERAL P QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN);
234 QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL LITERAL UTF8_LITERAL));
235 QByteArray withZero = QByteArray(LITERAL "\0" LITERAL, LITERAL_LEN*2+1);
236 QByteArray ba2 = withZero;
237 ba2 += ba2 P withZero;
238 QCOMPARE(ba2, QByteArray(withZero + withZero + withZero));
239 #ifndef QT_NO_CAST_TO_ASCII
241 // TODO: this relies on strings defaulting to utf8, so disable this for now.
243 ba2 = (ba += QLatin1String(LITERAL) + QString::fromUtf8(UTF8_LITERAL));
245 QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL));