author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qbytearray.h> |
|
46 |
#include <qfile.h> |
|
47 |
#include <limits.h> |
|
48 |
#include <private/qtools_p.h> |
|
49 |
#if defined(Q_OS_WINCE) |
|
50 |
#include <qcoreapplication.h> |
|
51 |
#endif |
|
52 |
||
53 |
//TESTED_CLASS= |
|
54 |
//TESTED_FILES= |
|
55 |
||
56 |
#if defined(Q_OS_SYMBIAN) |
|
57 |
#define SRCDIR "" |
|
58 |
#endif |
|
59 |
||
60 |
class tst_QByteArray : public QObject |
|
61 |
{ |
|
62 |
Q_OBJECT |
|
63 |
||
64 |
public: |
|
65 |
tst_QByteArray(); |
|
66 |
virtual ~tst_QByteArray(); |
|
67 |
||
68 |
public slots: |
|
69 |
void initTestCase(); |
|
70 |
void cleanupTestCase(); |
|
71 |
void init(); |
|
72 |
void cleanup(); |
|
73 |
private slots: |
|
74 |
void qCompress_data(); |
|
75 |
#ifndef QT_NO_COMPRESS |
|
76 |
void qCompress(); |
|
77 |
void qUncompress_data(); |
|
78 |
void qUncompress(); |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
79 |
void qCompressionZeroTermination(); |
0 | 80 |
#endif |
81 |
void constByteArray(); |
|
82 |
void leftJustified(); |
|
83 |
void rightJustified(); |
|
84 |
void startsWith_data(); |
|
85 |
void startsWith(); |
|
86 |
void startsWith_char(); |
|
87 |
void endsWith_data(); |
|
88 |
void endsWith(); |
|
89 |
void endsWith_char(); |
|
90 |
void split_data(); |
|
91 |
void split(); |
|
92 |
void base64_data(); |
|
93 |
void base64(); |
|
94 |
void fromBase64_data(); |
|
95 |
void fromBase64(); |
|
96 |
void qvsnprintf(); |
|
97 |
void qstrlen(); |
|
98 |
void qstrnlen(); |
|
99 |
void qstrcpy(); |
|
100 |
void qstrncpy(); |
|
101 |
void qstricmp_data(); |
|
102 |
void qstricmp(); |
|
103 |
void qstricmp_singularities(); |
|
104 |
void qstrnicmp_singularities(); |
|
105 |
void chop_data(); |
|
106 |
void chop(); |
|
107 |
void prepend(); |
|
108 |
void append(); |
|
109 |
void insert(); |
|
110 |
void remove_data(); |
|
111 |
void remove(); |
|
112 |
void replace_data(); |
|
113 |
void replace(); |
|
114 |
void indexOf_data(); |
|
115 |
void indexOf(); |
|
116 |
void lastIndexOf_data(); |
|
117 |
void lastIndexOf(); |
|
118 |
void toULong_data(); |
|
119 |
void toULong(); |
|
120 |
void toULongLong_data(); |
|
121 |
void toULongLong(); |
|
122 |
||
123 |
void number(); |
|
124 |
void toInt_data(); |
|
125 |
void toInt(); |
|
126 |
void qAllocMore(); |
|
127 |
||
128 |
void resizeAfterFromRawData(); |
|
129 |
void appendAfterFromRawData(); |
|
130 |
void toFromHex_data(); |
|
131 |
void toFromHex(); |
|
132 |
void toFromPercentEncoding(); |
|
133 |
||
134 |
void compare_data(); |
|
135 |
void compare(); |
|
136 |
void compareCharStar_data(); |
|
137 |
void compareCharStar(); |
|
138 |
||
139 |
void repeatedSignature() const; |
|
140 |
void repeated() const; |
|
141 |
void repeated_data() const; |
|
142 |
||
143 |
void byteRefDetaching() const; |
|
144 |
}; |
|
145 |
||
146 |
tst_QByteArray::tst_QByteArray() |
|
147 |
{ |
|
148 |
qRegisterMetaType<qulonglong>("qulonglong"); |
|
149 |
} |
|
150 |
||
151 |
tst_QByteArray::~tst_QByteArray() |
|
152 |
{ |
|
153 |
} |
|
154 |
||
155 |
void tst_QByteArray::initTestCase() |
|
156 |
{ |
|
157 |
} |
|
158 |
||
159 |
void tst_QByteArray::cleanupTestCase() |
|
160 |
{ |
|
161 |
} |
|
162 |
||
163 |
void tst_QByteArray::init() |
|
164 |
{ |
|
165 |
} |
|
166 |
||
167 |
void tst_QByteArray::cleanup() |
|
168 |
{ |
|
169 |
} |
|
170 |
||
171 |
void tst_QByteArray::qCompress_data() |
|
172 |
{ |
|
173 |
QTest::addColumn<QByteArray>("ba"); |
|
174 |
||
175 |
#ifndef Q_OS_WINCE |
|
176 |
const int size1 = 1024*1024; |
|
177 |
#else |
|
178 |
const int size1 = 1024; |
|
179 |
#endif |
|
180 |
QByteArray ba1( size1, 0 ); |
|
181 |
||
182 |
QTest::newRow( "00" ) << QByteArray(); |
|
183 |
||
184 |
int i; |
|
185 |
for ( i=0; i<size1; i++ ) |
|
186 |
ba1[i] = (char)( i / 1024 ); |
|
187 |
QTest::newRow( "01" ) << ba1; |
|
188 |
||
189 |
for ( i=0; i<size1; i++ ) |
|
190 |
ba1[i] = (char)( i % 256 ); |
|
191 |
QTest::newRow( "02" ) << ba1; |
|
192 |
||
193 |
ba1.fill( 'A' ); |
|
194 |
QTest::newRow( "03" ) << ba1; |
|
195 |
||
196 |
#if defined(Q_OS_WINCE) |
|
197 |
int tmpArgc = 0; |
|
198 |
char** tmpArgv = 0; |
|
199 |
QCoreApplication app(tmpArgc, tmpArgv); |
|
200 |
#endif |
|
201 |
QFile file( SRCDIR "rfc3252.txt" ); |
|
202 |
QVERIFY( file.open(QIODevice::ReadOnly) ); |
|
203 |
QTest::newRow( "04" ) << file.readAll(); |
|
204 |
} |
|
205 |
||
206 |
#ifndef QT_NO_COMPRESS |
|
207 |
void tst_QByteArray::qCompress() |
|
208 |
{ |
|
209 |
QFETCH( QByteArray, ba ); |
|
210 |
QByteArray compressed = ::qCompress( ba ); |
|
211 |
//qDebug( "size uncompressed: %d size compressed: %d", ba.size(), compressed.size() ); |
|
212 |
QTEST( ::qUncompress( compressed ), "ba" ); |
|
213 |
} |
|
214 |
||
215 |
/* |
|
216 |
Just making sure it doesn't crash on invalid data. |
|
217 |
*/ |
|
218 |
void tst_QByteArray::qUncompress_data() |
|
219 |
{ |
|
220 |
QTest::addColumn<QByteArray>("in"); |
|
221 |
QTest::addColumn<QByteArray>("out"); |
|
222 |
||
223 |
QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00") << QByteArray(); |
|
224 |
QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff") << QByteArray(); |
|
225 |
QTest::newRow("0x3f000000") << QByteArray("\x3f\x00\x00\x00") << QByteArray(); |
|
226 |
QTest::newRow("0x3fffffff") << QByteArray("\x3f\xff\xff\xff") << QByteArray(); |
|
227 |
QTest::newRow("0x7fffff00") << QByteArray("\x7f\xff\xff\x00") << QByteArray(); |
|
228 |
QTest::newRow("0x7fffffff") << QByteArray("\x7f\xff\xff\xff") << QByteArray(); |
|
229 |
QTest::newRow("0x80000000") << QByteArray("\x80\x00\x00\x00") << QByteArray(); |
|
230 |
QTest::newRow("0x800000ff") << QByteArray("\x80\x00\x00\xff") << QByteArray(); |
|
231 |
QTest::newRow("0xcf000000") << QByteArray("\xcf\x00\x00\x00") << QByteArray(); |
|
232 |
QTest::newRow("0xcfffffff") << QByteArray("\xcf\xff\xff\xff") << QByteArray(); |
|
233 |
QTest::newRow("0xffffff00") << QByteArray("\xff\xff\xff\x00") << QByteArray(); |
|
234 |
QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff") << QByteArray(); |
|
235 |
} |
|
236 |
||
237 |
void tst_QByteArray::qUncompress() |
|
238 |
{ |
|
239 |
QFETCH(QByteArray, in); |
|
240 |
QFETCH(QByteArray, out); |
|
241 |
||
242 |
#if defined Q_OS_HPUX && !defined __ia64 && defined Q_CC_GNU |
|
243 |
QSKIP("Corrupt data causes this tests to lock up on HP-UX / PA-RISC with gcc", SkipAll); |
|
244 |
#elif defined Q_OS_SOLARIS |
|
245 |
QSKIP("Corrupt data causes this tests to lock up on Solaris", SkipAll); |
|
246 |
#elif defined Q_OS_QNX |
|
247 |
QSKIP("Corrupt data causes this test to lock up on QNX", SkipAll); |
|
248 |
#endif |
|
249 |
||
250 |
QByteArray res; |
|
251 |
QT_TRY { |
|
252 |
res = ::qUncompress(in); |
|
253 |
} QT_CATCH(const std::bad_alloc &) { |
|
254 |
res = QByteArray(); |
|
255 |
} |
|
256 |
QCOMPARE(res, out); |
|
257 |
||
258 |
QT_TRY { |
|
259 |
res = ::qUncompress(in + "blah"); |
|
260 |
} QT_CATCH(const std::bad_alloc &) { |
|
261 |
res = QByteArray(); |
|
262 |
} |
|
263 |
QCOMPARE(res, out); |
|
264 |
} |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
265 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
266 |
void tst_QByteArray::qCompressionZeroTermination() |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
267 |
{ |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
268 |
QString s = "Hello, I'm a string."; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
269 |
QByteArray ba = ::qUncompress(::qCompress(s.toLocal8Bit())); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
270 |
QVERIFY((int) *(ba.data() + ba.size()) == 0); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
271 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
272 |
|
0 | 273 |
#endif |
274 |
||
275 |
void tst_QByteArray::constByteArray() |
|
276 |
{ |
|
277 |
const char *ptr = "abc"; |
|
278 |
QByteArray cba = QByteArray::fromRawData(ptr, 3); |
|
279 |
QVERIFY(cba.constData() == ptr); |
|
280 |
cba.squeeze(); |
|
281 |
QVERIFY(cba.constData() == ptr); |
|
282 |
cba.detach(); |
|
283 |
QVERIFY(cba.size() == 3); |
|
284 |
QVERIFY(cba.capacity() == 3); |
|
285 |
QVERIFY(cba.constData() != ptr); |
|
286 |
QVERIFY(cba.constData()[0] == 'a'); |
|
287 |
QVERIFY(cba.constData()[1] == 'b'); |
|
288 |
QVERIFY(cba.constData()[2] == 'c'); |
|
289 |
QVERIFY(cba.constData()[3] == '\0'); |
|
290 |
} |
|
291 |
||
292 |
void tst_QByteArray::leftJustified() |
|
293 |
{ |
|
294 |
QByteArray a; |
|
295 |
a = "ABC"; |
|
296 |
QCOMPARE(a.leftJustified(5,'-'), QByteArray("ABC--")); |
|
297 |
QCOMPARE(a.leftJustified(4,'-'), QByteArray("ABC-")); |
|
298 |
QCOMPARE(a.leftJustified(4), QByteArray("ABC ")); |
|
299 |
QCOMPARE(a.leftJustified(3), QByteArray("ABC")); |
|
300 |
QCOMPARE(a.leftJustified(2), QByteArray("ABC")); |
|
301 |
QCOMPARE(a.leftJustified(1), QByteArray("ABC")); |
|
302 |
QCOMPARE(a.leftJustified(0), QByteArray("ABC")); |
|
303 |
||
304 |
QByteArray n; |
|
305 |
QVERIFY(!n.leftJustified(3).isNull()); // I expected true |
|
306 |
QCOMPARE(a.leftJustified(4,' ',true), QByteArray("ABC ")); |
|
307 |
QCOMPARE(a.leftJustified(3,' ',true), QByteArray("ABC")); |
|
308 |
QCOMPARE(a.leftJustified(2,' ',true), QByteArray("AB")); |
|
309 |
QCOMPARE(a.leftJustified(1,' ',true), QByteArray("A")); |
|
310 |
QCOMPARE(a.leftJustified(0,' ',true), QByteArray("")); |
|
311 |
} |
|
312 |
||
313 |
void tst_QByteArray::rightJustified() |
|
314 |
{ |
|
315 |
QByteArray a; |
|
316 |
a="ABC"; |
|
317 |
QCOMPARE(a.rightJustified(5,'-'),QByteArray("--ABC")); |
|
318 |
QCOMPARE(a.rightJustified(4,'-'),QByteArray("-ABC")); |
|
319 |
QCOMPARE(a.rightJustified(4),QByteArray(" ABC")); |
|
320 |
QCOMPARE(a.rightJustified(3),QByteArray("ABC")); |
|
321 |
QCOMPARE(a.rightJustified(2),QByteArray("ABC")); |
|
322 |
QCOMPARE(a.rightJustified(1),QByteArray("ABC")); |
|
323 |
QCOMPARE(a.rightJustified(0),QByteArray("ABC")); |
|
324 |
||
325 |
QByteArray n; |
|
326 |
QVERIFY(!n.rightJustified(3).isNull()); // I expected true |
|
327 |
QCOMPARE(a.rightJustified(4,'-',true),QByteArray("-ABC")); |
|
328 |
QCOMPARE(a.rightJustified(4,' ',true),QByteArray(" ABC")); |
|
329 |
QCOMPARE(a.rightJustified(3,' ',true),QByteArray("ABC")); |
|
330 |
QCOMPARE(a.rightJustified(2,' ',true),QByteArray("AB")); |
|
331 |
QCOMPARE(a.rightJustified(1,' ',true),QByteArray("A")); |
|
332 |
QCOMPARE(a.rightJustified(0,' ',true),QByteArray("")); |
|
333 |
QCOMPARE(a,QByteArray("ABC")); |
|
334 |
} |
|
335 |
||
336 |
void tst_QByteArray::startsWith_data() |
|
337 |
{ |
|
338 |
QTest::addColumn<QByteArray>("ba"); |
|
339 |
QTest::addColumn<QByteArray>("sw"); |
|
340 |
QTest::addColumn<bool>("result"); |
|
341 |
||
342 |
QTest::newRow("01") << QByteArray() << QByteArray() << true; |
|
343 |
QTest::newRow("02") << QByteArray() << QByteArray("") << true; |
|
344 |
QTest::newRow("03") << QByteArray() << QByteArray("hallo") << false; |
|
345 |
||
346 |
QTest::newRow("04") << QByteArray("") << QByteArray() << true; |
|
347 |
QTest::newRow("05") << QByteArray("") << QByteArray("") << true; |
|
348 |
QTest::newRow("06") << QByteArray("") << QByteArray("h") << false; |
|
349 |
||
350 |
QTest::newRow("07") << QByteArray("hallo") << QByteArray("h") << true; |
|
351 |
QTest::newRow("08") << QByteArray("hallo") << QByteArray("hallo") << true; |
|
352 |
QTest::newRow("09") << QByteArray("hallo") << QByteArray("") << true; |
|
353 |
QTest::newRow("10") << QByteArray("hallo") << QByteArray("hallohallo") << false; |
|
354 |
QTest::newRow("11") << QByteArray("hallo") << QByteArray() << true; |
|
355 |
} |
|
356 |
||
357 |
void tst_QByteArray::startsWith() |
|
358 |
{ |
|
359 |
QFETCH(QByteArray, ba); |
|
360 |
QFETCH(QByteArray, sw); |
|
361 |
QFETCH(bool, result); |
|
362 |
||
363 |
QVERIFY(ba.startsWith(sw) == result); |
|
364 |
||
365 |
if (sw.isNull()) { |
|
366 |
QVERIFY(ba.startsWith((char*)0) == result); |
|
367 |
} else { |
|
368 |
QVERIFY(ba.startsWith(sw.data()) == result); |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
void tst_QByteArray::startsWith_char() |
|
373 |
{ |
|
374 |
QVERIFY(QByteArray("hallo").startsWith('h')); |
|
375 |
QVERIFY(!QByteArray("hallo").startsWith('\0')); |
|
376 |
QVERIFY(!QByteArray("hallo").startsWith('o')); |
|
377 |
QVERIFY(QByteArray("h").startsWith('h')); |
|
378 |
QVERIFY(!QByteArray("h").startsWith('\0')); |
|
379 |
QVERIFY(!QByteArray("h").startsWith('o')); |
|
380 |
QVERIFY(!QByteArray("hallo").startsWith('l')); |
|
381 |
QVERIFY(!QByteArray("").startsWith('\0')); |
|
382 |
QVERIFY(!QByteArray("").startsWith('a')); |
|
383 |
QVERIFY(!QByteArray().startsWith('a')); |
|
384 |
QVERIFY(!QByteArray().startsWith('\0')); |
|
385 |
} |
|
386 |
||
387 |
void tst_QByteArray::endsWith_data() |
|
388 |
{ |
|
389 |
QTest::addColumn<QByteArray>("ba"); |
|
390 |
QTest::addColumn<QByteArray>("sw"); |
|
391 |
QTest::addColumn<bool>("result"); |
|
392 |
||
393 |
QTest::newRow("01") << QByteArray() << QByteArray() << true; |
|
394 |
QTest::newRow("02") << QByteArray() << QByteArray("") << true; |
|
395 |
QTest::newRow("03") << QByteArray() << QByteArray("hallo") << false; |
|
396 |
||
397 |
QTest::newRow("04") << QByteArray("") << QByteArray() << true; |
|
398 |
QTest::newRow("05") << QByteArray("") << QByteArray("") << true; |
|
399 |
QTest::newRow("06") << QByteArray("") << QByteArray("h") << false; |
|
400 |
||
401 |
QTest::newRow("07") << QByteArray("hallo") << QByteArray("o") << true; |
|
402 |
QTest::newRow("08") << QByteArray("hallo") << QByteArray("hallo") << true; |
|
403 |
QTest::newRow("09") << QByteArray("hallo") << QByteArray("") << true; |
|
404 |
QTest::newRow("10") << QByteArray("hallo") << QByteArray("hallohallo") << false; |
|
405 |
QTest::newRow("11") << QByteArray("hallo") << QByteArray() << true; |
|
406 |
} |
|
407 |
||
408 |
void tst_QByteArray::endsWith() |
|
409 |
{ |
|
410 |
QFETCH(QByteArray, ba); |
|
411 |
QFETCH(QByteArray, sw); |
|
412 |
QFETCH(bool, result); |
|
413 |
||
414 |
QVERIFY(ba.endsWith(sw) == result); |
|
415 |
||
416 |
if (sw.isNull()) { |
|
417 |
QVERIFY(ba.endsWith((char*)0) == result); |
|
418 |
} else { |
|
419 |
QVERIFY(ba.endsWith(sw.data()) == result); |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
void tst_QByteArray::endsWith_char() |
|
424 |
{ |
|
425 |
QVERIFY(QByteArray("hallo").endsWith('o')); |
|
426 |
QVERIFY(!QByteArray("hallo").endsWith('\0')); |
|
427 |
QVERIFY(!QByteArray("hallo").endsWith('h')); |
|
428 |
QVERIFY(QByteArray("h").endsWith('h')); |
|
429 |
QVERIFY(!QByteArray("h").endsWith('\0')); |
|
430 |
QVERIFY(!QByteArray("h").endsWith('o')); |
|
431 |
QVERIFY(!QByteArray("hallo").endsWith('l')); |
|
432 |
QVERIFY(!QByteArray("").endsWith('\0')); |
|
433 |
QVERIFY(!QByteArray("").endsWith('a')); |
|
434 |
QVERIFY(!QByteArray().endsWith('a')); |
|
435 |
QVERIFY(!QByteArray().endsWith('\0')); |
|
436 |
} |
|
437 |
||
438 |
void tst_QByteArray::split_data() |
|
439 |
{ |
|
440 |
QTest::addColumn<QByteArray>("sample"); |
|
441 |
QTest::addColumn<int>("size"); |
|
442 |
||
443 |
QTest::newRow("1") << QByteArray("-rw-r--r-- 1 0 0 519240 Jul 9 2002 bigfile") |
|
444 |
<< 14; |
|
445 |
QTest::newRow("2") << QByteArray("abcde") << 1; |
|
446 |
QTest::newRow("one empty") << QByteArray("") << 1; |
|
447 |
QTest::newRow("two empty") << QByteArray(" ") << 2; |
|
448 |
QTest::newRow("three empty") << QByteArray(" ") << 3; |
|
449 |
||
450 |
} |
|
451 |
||
452 |
void tst_QByteArray::split() |
|
453 |
{ |
|
454 |
QFETCH(QByteArray, sample); |
|
455 |
QFETCH(int, size); |
|
456 |
||
457 |
QList<QByteArray> list = sample.split(' '); |
|
458 |
QCOMPARE(list.count(), size); |
|
459 |
} |
|
460 |
||
461 |
void tst_QByteArray::base64_data() |
|
462 |
{ |
|
463 |
QTest::addColumn<QByteArray>("rawdata"); |
|
464 |
QTest::addColumn<QByteArray>("base64"); |
|
465 |
||
466 |
QTest::newRow("1") << QByteArray("") << QByteArray(""); |
|
467 |
QTest::newRow("2") << QByteArray("1") << QByteArray("MQ=="); |
|
468 |
QTest::newRow("3") << QByteArray("12") << QByteArray("MTI="); |
|
469 |
QTest::newRow("4") << QByteArray("123") << QByteArray("MTIz"); |
|
470 |
QTest::newRow("5") << QByteArray("1234") << QByteArray("MTIzNA=="); |
|
471 |
QTest::newRow("6") << QByteArray("\n") << QByteArray("Cg=="); |
|
472 |
QTest::newRow("7") << QByteArray("a\n") << QByteArray("YQo="); |
|
473 |
QTest::newRow("8") << QByteArray("ab\n") << QByteArray("YWIK"); |
|
474 |
QTest::newRow("9") << QByteArray("abc\n") << QByteArray("YWJjCg=="); |
|
475 |
QTest::newRow("a") << QByteArray("abcd\n") << QByteArray("YWJjZAo="); |
|
476 |
QTest::newRow("b") << QByteArray("abcde\n") << QByteArray("YWJjZGUK"); |
|
477 |
QTest::newRow("c") << QByteArray("abcdef\n") << QByteArray("YWJjZGVmCg=="); |
|
478 |
QTest::newRow("d") << QByteArray("abcdefg\n") << QByteArray("YWJjZGVmZwo="); |
|
479 |
QTest::newRow("e") << QByteArray("abcdefgh\n") << QByteArray("YWJjZGVmZ2gK"); |
|
480 |
||
481 |
QByteArray ba; |
|
482 |
ba.resize(256); |
|
483 |
for (int i = 0; i < 256; ++i) |
|
484 |
ba[i] = i; |
|
485 |
QTest::newRow("f") << ba << QByteArray("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="); |
|
486 |
||
487 |
QTest::newRow("g") << QByteArray("foo\0bar", 7) << QByteArray("Zm9vAGJhcg=="); |
|
488 |
QTest::newRow("h") << QByteArray("f\xd1oo\x9ctar") << QByteArray("ZtFvb5x0YXI="); |
|
489 |
QTest::newRow("i") << QByteArray("\"\0\0\0\0\0\0\"", 8) << QByteArray("IgAAAAAAACI="); |
|
490 |
} |
|
491 |
||
492 |
||
493 |
void tst_QByteArray::base64() |
|
494 |
{ |
|
495 |
QFETCH(QByteArray, rawdata); |
|
496 |
QFETCH(QByteArray, base64); |
|
497 |
||
498 |
QByteArray arr = QByteArray::fromBase64(base64); |
|
499 |
QCOMPARE(arr, rawdata); |
|
500 |
||
501 |
QByteArray arr64 = rawdata.toBase64(); |
|
502 |
QCOMPARE(arr64, base64); |
|
503 |
} |
|
504 |
||
505 |
//different from the previous test as the input are invalid |
|
506 |
void tst_QByteArray::fromBase64_data() |
|
507 |
{ |
|
508 |
QTest::addColumn<QByteArray>("rawdata"); |
|
509 |
QTest::addColumn<QByteArray>("base64"); |
|
510 |
||
511 |
QTest::newRow("1") << QByteArray("") << QByteArray(" "); |
|
512 |
QTest::newRow("2") << QByteArray("1") << QByteArray("MQ"); |
|
513 |
QTest::newRow("3") << QByteArray("12") << QByteArray("MTI "); |
|
514 |
QTest::newRow("4") << QByteArray("123") << QByteArray("M=TIz"); |
|
515 |
QTest::newRow("5") << QByteArray("1234") << QByteArray("MTI zN A "); |
|
516 |
QTest::newRow("6") << QByteArray("\n") << QByteArray("Cg"); |
|
517 |
QTest::newRow("7") << QByteArray("a\n") << QByteArray("======YQo="); |
|
518 |
QTest::newRow("8") << QByteArray("ab\n") << QByteArray("Y\nWIK"); |
|
519 |
QTest::newRow("9") << QByteArray("abc\n") << QByteArray("YWJjCg=="); |
|
520 |
QTest::newRow("a") << QByteArray("abcd\n") << QByteArray("YWJ\1j\x9cZAo="); |
|
521 |
QTest::newRow("b") << QByteArray("abcde\n") << QByteArray("YW JjZ\n G\tUK"); |
|
522 |
QTest::newRow("c") << QByteArray("abcdef\n") << QByteArray("YWJjZGVmCg="); |
|
523 |
QTest::newRow("d") << QByteArray("abcdefg\n") << QByteArray("YWJ\rjZGVmZwo"); |
|
524 |
QTest::newRow("e") << QByteArray("abcdefgh\n") << QByteArray("YWJjZGVmZ2gK"); |
|
525 |
||
526 |
QByteArray ba; |
|
527 |
ba.resize(256); |
|
528 |
for (int i = 0; i < 256; ++i) |
|
529 |
ba[i] = i; |
|
530 |
QTest::newRow("f") << ba << QByteArray("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Nj\n" |
|
531 |
"c4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1u\n" |
|
532 |
"b3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpa\n" |
|
533 |
"anqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd\n" |
|
534 |
"3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w== "); |
|
535 |
||
536 |
||
537 |
QTest::newRow("g") << QByteArray("foo\0bar", 7) << QByteArray("Zm9vAGJhcg"); |
|
538 |
QTest::newRow("h") << QByteArray("f\xd1oo\x9ctar") << QByteArray("ZtFvb5x0YXI="); |
|
539 |
QTest::newRow("i") << QByteArray("\"\0\0\0\0\0\0\"", 8) << QByteArray("IgAAAAAAACI"); |
|
540 |
||
541 |
} |
|
542 |
||
543 |
||
544 |
void tst_QByteArray::fromBase64() |
|
545 |
{ |
|
546 |
QFETCH(QByteArray, rawdata); |
|
547 |
QFETCH(QByteArray, base64); |
|
548 |
||
549 |
QByteArray arr = QByteArray::fromBase64(base64); |
|
550 |
QCOMPARE(arr, rawdata); |
|
551 |
} |
|
552 |
||
553 |
void tst_QByteArray::qvsnprintf() |
|
554 |
{ |
|
555 |
char buf[20]; |
|
556 |
qMemSet(buf, 42, sizeof(buf)); |
|
557 |
||
558 |
QCOMPARE(::qsnprintf(buf, 10, "%s", "bubu"), 4); |
|
559 |
QCOMPARE(static_cast<const char *>(buf), "bubu"); |
|
560 |
QCOMPARE(buf[5], char(42)); |
|
561 |
||
562 |
qMemSet(buf, 42, sizeof(buf)); |
|
563 |
QCOMPARE(::qsnprintf(buf, 5, "%s", "bubu"), 4); |
|
564 |
QCOMPARE(static_cast<const char *>(buf), "bubu"); |
|
565 |
QCOMPARE(buf[5], char(42)); |
|
566 |
||
567 |
qMemSet(buf, 42, sizeof(buf)); |
|
568 |
#ifdef Q_OS_WIN |
|
569 |
// VS 2005 uses the Qt implementation of vsnprintf. |
|
570 |
# if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE) |
|
571 |
QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), -1); |
|
572 |
QCOMPARE(static_cast<const char*>(buf), "bu"); |
|
573 |
# else |
|
574 |
// windows has to do everything different, of course. |
|
575 |
QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), -1); |
|
576 |
buf[19] = '\0'; |
|
577 |
QCOMPARE(static_cast<const char *>(buf), "bub****************"); |
|
578 |
# endif |
|
579 |
#else |
|
580 |
#ifdef Q_OS_IRIX |
|
581 |
// Irix reports back the amount of characters written without the \0 |
|
582 |
QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), 2); |
|
583 |
#else |
|
584 |
// Every other system in this world reports the amount of data that could have been written |
|
585 |
QCOMPARE(::qsnprintf(buf, 3, "%s", "bubu"), 4); |
|
586 |
#endif |
|
587 |
QCOMPARE(static_cast<const char*>(buf), "bu"); |
|
588 |
#endif |
|
589 |
QCOMPARE(buf[4], char(42)); |
|
590 |
||
591 |
#ifndef Q_OS_WIN |
|
592 |
qMemSet(buf, 42, sizeof(buf)); |
|
593 |
QCOMPARE(::qsnprintf(buf, 10, ""), 0); |
|
594 |
#endif |
|
595 |
} |
|
596 |
||
597 |
||
598 |
void tst_QByteArray::qstrlen() |
|
599 |
{ |
|
600 |
const char *src = "Something about ... \0 a string."; |
|
601 |
QCOMPARE(::qstrlen((char*)0), (uint)0); |
|
602 |
QCOMPARE(::qstrlen(src), (uint)20); |
|
603 |
} |
|
604 |
||
605 |
void tst_QByteArray::qstrnlen() |
|
606 |
{ |
|
607 |
const char *src = "Something about ... \0 a string."; |
|
608 |
QCOMPARE(::qstrnlen((char*)0, 1), (uint)0); |
|
609 |
QCOMPARE(::qstrnlen(src, 31), (uint)20); |
|
610 |
QCOMPARE(::qstrnlen(src, 19), (uint)19); |
|
611 |
QCOMPARE(::qstrnlen(src, 21), (uint)20); |
|
612 |
QCOMPARE(::qstrnlen(src, 20), (uint)20); |
|
613 |
} |
|
614 |
||
615 |
void tst_QByteArray::qstrcpy() |
|
616 |
{ |
|
617 |
const char *src = "Something about ... \0 a string."; |
|
618 |
const char *expected = "Something about ... "; |
|
619 |
char dst[128]; |
|
620 |
||
621 |
QCOMPARE(::qstrcpy(0, 0), (char*)0); |
|
622 |
QCOMPARE(::qstrcpy(dst, 0), (char*)0); |
|
623 |
||
624 |
QCOMPARE(::qstrcpy(dst ,src), (char *)dst); |
|
625 |
QCOMPARE((char *)dst, const_cast<char *>(expected)); |
|
626 |
} |
|
627 |
||
628 |
void tst_QByteArray::qstrncpy() |
|
629 |
{ |
|
630 |
QByteArray src(1024, 'a'), dst(1024, 'b'); |
|
631 |
||
632 |
// singularities |
|
633 |
QCOMPARE(::qstrncpy(0, 0,0), (char*)0); |
|
634 |
QCOMPARE(::qstrncpy(dst.data(), 0, 0), (char*)0); |
|
635 |
||
636 |
// normal copy |
|
637 |
QCOMPARE(::qstrncpy(dst.data(), src.data(), src.size()), dst.data()); |
|
638 |
||
639 |
src = QByteArray( "Tumdelidum" ); |
|
640 |
QCOMPARE(QByteArray(::qstrncpy(dst.data(), src.data(), src.size())), |
|
641 |
QByteArray("Tumdelidu")); |
|
642 |
||
643 |
// normal copy with length is longer than neccessary |
|
644 |
src = QByteArray( "Tumdelidum\0foo" ); |
|
645 |
dst.resize(128*1024); |
|
646 |
QCOMPARE(QByteArray(::qstrncpy(dst.data(), src.data(), dst.size())), |
|
647 |
QByteArray("Tumdelidum")); |
|
648 |
} |
|
649 |
||
650 |
void tst_QByteArray::qstricmp_data() |
|
651 |
{ |
|
652 |
QTest::addColumn<QString>("str1"); |
|
653 |
QTest::addColumn<QString>("str2"); |
|
654 |
||
655 |
QTest::newRow("equal 1") << "abcEdb" << "abcEdb"; |
|
656 |
QTest::newRow("equal 2") << "abcEdb" << "ABCeDB"; |
|
657 |
QTest::newRow("equal 3") << "ABCEDB" << "abcedb"; |
|
658 |
QTest::newRow("less 1") << "abcdef" << "abcdefg"; |
|
659 |
QTest::newRow("less 2") << "abcdeF" << "abcdef"; |
|
660 |
QTest::newRow("greater 1") << "abcdef" << "abcdeF"; |
|
661 |
QTest::newRow("greater 2") << "abcdefg" << "abcdef"; |
|
662 |
} |
|
663 |
||
664 |
void tst_QByteArray::qstricmp() |
|
665 |
{ |
|
666 |
QFETCH(QString, str1); |
|
667 |
QFETCH(QString, str2); |
|
668 |
||
669 |
int expected = strcmp(str1.toUpper().toLatin1(), |
|
670 |
str2.toUpper().toLatin1()); |
|
671 |
if ( expected != 0 ) { |
|
672 |
expected = (expected < 0 ? -1 : 1); |
|
673 |
} |
|
674 |
int actual = ::qstricmp(str1.toLatin1(), str2.toLatin1()); |
|
675 |
if ( actual != 0 ) { |
|
676 |
actual = (actual < 0 ? -1 : 1); |
|
677 |
} |
|
678 |
QCOMPARE(expected, actual); |
|
679 |
} |
|
680 |
||
681 |
void tst_QByteArray::qstricmp_singularities() |
|
682 |
{ |
|
683 |
QCOMPARE(::qstricmp(0, 0), 0); |
|
684 |
QVERIFY(::qstricmp(0, "a") != 0); |
|
685 |
QVERIFY(::qstricmp("a", 0) != 0); |
|
686 |
QCOMPARE(::qstricmp("", ""), 0); |
|
687 |
} |
|
688 |
||
689 |
void tst_QByteArray::qstrnicmp_singularities() |
|
690 |
{ |
|
691 |
QCOMPARE(::qstrnicmp(0, 0, 123), 0); |
|
692 |
QVERIFY(::qstrnicmp(0, "a", 123) != 0); |
|
693 |
QVERIFY(::qstrnicmp("a", 0, 123) != 0); |
|
694 |
QCOMPARE(::qstrnicmp("", "", 123), 0); |
|
695 |
QCOMPARE(::qstrnicmp("a", "B", 0), 0); |
|
696 |
} |
|
697 |
||
698 |
void tst_QByteArray::chop_data() |
|
699 |
{ |
|
700 |
QTest::addColumn<QByteArray>("src"); |
|
701 |
QTest::addColumn<int>("choplength"); |
|
702 |
QTest::addColumn<QByteArray>("expected"); |
|
703 |
||
704 |
QTest::newRow("1") << QByteArray("short1") << 128 << QByteArray(); |
|
705 |
QTest::newRow("2") << QByteArray("short2") << int(strlen("short2")) |
|
706 |
<< QByteArray(); |
|
707 |
QTest::newRow("3") << QByteArray("abcdef\0foo", 10) << 2 |
|
708 |
<< QByteArray("abcdef\0f", 8); |
|
709 |
QTest::newRow("4") << QByteArray("STARTTLS\r\n") << 2 |
|
710 |
<< QByteArray("STARTTLS"); |
|
711 |
QTest::newRow("5") << QByteArray("") << 1 << QByteArray(); |
|
712 |
QTest::newRow("6") << QByteArray("foo") << 0 << QByteArray("foo"); |
|
713 |
QTest::newRow("7") << QByteArray(0) << 28 << QByteArray(); |
|
714 |
} |
|
715 |
||
716 |
void tst_QByteArray::chop() |
|
717 |
{ |
|
718 |
QFETCH(QByteArray, src); |
|
719 |
QFETCH(int, choplength); |
|
720 |
QFETCH(QByteArray, expected); |
|
721 |
||
722 |
src.chop(choplength); |
|
723 |
QCOMPARE(src, expected); |
|
724 |
} |
|
725 |
||
726 |
void tst_QByteArray::prepend() |
|
727 |
{ |
|
728 |
QByteArray ba("foo"); |
|
729 |
QCOMPARE(ba.prepend((char*)0), QByteArray("foo")); |
|
730 |
QCOMPARE(ba.prepend(QByteArray()), QByteArray("foo")); |
|
731 |
QCOMPARE(ba.prepend("1"), QByteArray("1foo")); |
|
732 |
QCOMPARE(ba.prepend(QByteArray("2")), QByteArray("21foo")); |
|
733 |
QCOMPARE(ba.prepend('3'), QByteArray("321foo")); |
|
734 |
QCOMPARE(ba.prepend("\0 ", 2), QByteArray::fromRawData("\0 321foo", 8)); |
|
735 |
} |
|
736 |
||
737 |
void tst_QByteArray::append() |
|
738 |
{ |
|
739 |
QByteArray ba("foo"); |
|
740 |
QCOMPARE(ba.append((char*)0), QByteArray("foo")); |
|
741 |
QCOMPARE(ba.append(QByteArray()), QByteArray("foo")); |
|
742 |
QCOMPARE(ba.append("1"), QByteArray("foo1")); |
|
743 |
QCOMPARE(ba.append(QByteArray("2")), QByteArray("foo12")); |
|
744 |
QCOMPARE(ba.append('3'), QByteArray("foo123")); |
|
745 |
QCOMPARE(ba.append("\0"), QByteArray("foo123")); |
|
746 |
QCOMPARE(ba.append("\0", 1), QByteArray::fromRawData("foo123\0", 7)); |
|
747 |
QCOMPARE(ba.size(), 7); |
|
748 |
} |
|
749 |
||
750 |
void tst_QByteArray::insert() |
|
751 |
{ |
|
752 |
QByteArray ba("Meal"); |
|
753 |
QCOMPARE(ba.insert(1, QByteArray("ontr")), QByteArray("Montreal")); |
|
754 |
QCOMPARE(ba.insert(ba.size(), "foo"), QByteArray("Montrealfoo")); |
|
755 |
||
756 |
ba = QByteArray("13"); |
|
757 |
QCOMPARE(ba.insert(1, QByteArray("2")), QByteArray("123")); |
|
758 |
||
759 |
ba = "ac"; |
|
760 |
QCOMPARE(ba.insert(1, 'b'), QByteArray("abc")); |
|
761 |
QCOMPARE(ba.size(), 3); |
|
762 |
||
763 |
ba = "ikl"; |
|
764 |
QCOMPARE(ba.insert(1, "j"), QByteArray("ijkl")); |
|
765 |
QCOMPARE(ba.size(), 4); |
|
766 |
||
767 |
ba = "ab"; |
|
768 |
QCOMPARE(ba.insert(1, "\0X\0", 3), QByteArray::fromRawData("a\0X\0b", 5)); |
|
769 |
QCOMPARE(ba.size(), 5); |
|
770 |
} |
|
771 |
||
772 |
void tst_QByteArray::remove_data() |
|
773 |
{ |
|
774 |
QTest::addColumn<QByteArray>("src"); |
|
775 |
QTest::addColumn<int>("position"); |
|
776 |
QTest::addColumn<int>("length"); |
|
777 |
QTest::addColumn<QByteArray>("expected"); |
|
778 |
||
779 |
QTest::newRow("1") << QByteArray("Montreal") << 1 << 4 |
|
780 |
<< QByteArray("Meal"); |
|
781 |
QTest::newRow("2") << QByteArray() << 10 << 10 << QByteArray(); |
|
782 |
QTest::newRow("3") << QByteArray("hi") << 0 << 10 << QByteArray(); |
|
783 |
QTest::newRow("4") << QByteArray("Montreal") << 4 << 100 |
|
784 |
<< QByteArray("Mont"); |
|
785 |
||
786 |
// index out of range |
|
787 |
QTest::newRow("5") << QByteArray("Montreal") << 8 << 1 |
|
788 |
<< QByteArray("Montreal"); |
|
789 |
QTest::newRow("6") << QByteArray("Montreal") << 18 << 4 |
|
790 |
<< QByteArray("Montreal"); |
|
791 |
} |
|
792 |
||
793 |
void tst_QByteArray::remove() |
|
794 |
{ |
|
795 |
QFETCH(QByteArray, src); |
|
796 |
QFETCH(int, position); |
|
797 |
QFETCH(int, length); |
|
798 |
QFETCH(QByteArray, expected); |
|
799 |
QCOMPARE(src.remove(position, length), expected); |
|
800 |
} |
|
801 |
||
802 |
void tst_QByteArray::replace_data() |
|
803 |
{ |
|
804 |
QTest::addColumn<QByteArray>("src"); |
|
805 |
QTest::addColumn<int>("pos"); |
|
806 |
QTest::addColumn<int>("len"); |
|
807 |
QTest::addColumn<QByteArray>("after"); |
|
808 |
QTest::addColumn<QByteArray>("expected"); |
|
809 |
||
810 |
QTest::newRow("1") << QByteArray("Say yes!") << 4 << 3 |
|
811 |
<< QByteArray("no") << QByteArray("Say no!"); |
|
812 |
QTest::newRow("2") << QByteArray("rock and roll") << 5 << 3 |
|
813 |
<< QByteArray("&") << QByteArray("rock & roll"); |
|
814 |
QTest::newRow("3") << QByteArray("foo") << 3 << 0 << QByteArray("bar") |
|
815 |
<< QByteArray("foobar"); |
|
816 |
QTest::newRow("4") << QByteArray() << 0 << 0 << QByteArray() << QByteArray(); |
|
817 |
// index out of range |
|
818 |
QTest::newRow("5") << QByteArray() << 3 << 0 << QByteArray("hi") |
|
819 |
<< QByteArray(" hi"); |
|
820 |
// Optimized path |
|
821 |
QTest::newRow("6") << QByteArray("abcdef") << 3 << 12 << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijkl"); |
|
822 |
QTest::newRow("7") << QByteArray("abcdef") << 3 << 4 << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijkl"); |
|
823 |
QTest::newRow("8") << QByteArray("abcdef") << 3 << 3 << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijkl"); |
|
824 |
QTest::newRow("9") << QByteArray("abcdef") << 3 << 2 << QByteArray("abcdefghijkl") << QByteArray("abcabcdefghijklf"); |
|
825 |
QTest::newRow("10") << QByteArray("abcdef") << 2 << 2 << QByteArray("xx") << QByteArray("abxxef"); |
|
826 |
} |
|
827 |
||
828 |
void tst_QByteArray::replace() |
|
829 |
{ |
|
830 |
QFETCH(QByteArray, src); |
|
831 |
QFETCH(int, pos); |
|
832 |
QFETCH(int, len); |
|
833 |
QFETCH(QByteArray, after); |
|
834 |
QFETCH(QByteArray, expected); |
|
835 |
||
836 |
QByteArray str1 = src; |
|
837 |
QByteArray str2 = src; |
|
838 |
||
839 |
QCOMPARE(str1.replace(pos, len, after).constData(), expected.constData()); |
|
840 |
QCOMPARE(str2.replace(pos, len, after.data()), expected); |
|
841 |
} |
|
842 |
||
843 |
void tst_QByteArray::indexOf_data() |
|
844 |
{ |
|
845 |
QTest::addColumn<QByteArray>("haystack"); |
|
846 |
QTest::addColumn<QByteArray>("needle"); |
|
847 |
QTest::addColumn<int>("startpos"); |
|
848 |
QTest::addColumn<int>("expected"); |
|
849 |
||
850 |
QTest::newRow( "1" ) << QByteArray("abc") << QByteArray("a") << 0 << 0; |
|
851 |
QTest::newRow( "2" ) << QByteArray("abc") << QByteArray("A") << 0 << -1; |
|
852 |
QTest::newRow( "3" ) << QByteArray("abc") << QByteArray("a") << 1 << -1; |
|
853 |
QTest::newRow( "4" ) << QByteArray("abc") << QByteArray("A") << 1 << -1; |
|
854 |
QTest::newRow( "5" ) << QByteArray("abc") << QByteArray("b") << 0 << 1; |
|
855 |
QTest::newRow( "6" ) << QByteArray("abc") << QByteArray("B") << 0 << -1; |
|
856 |
QTest::newRow( "7" ) << QByteArray("abc") << QByteArray("b") << 1 << 1; |
|
857 |
QTest::newRow( "8" ) << QByteArray("abc") << QByteArray("B") << 1 << -1; |
|
858 |
QTest::newRow( "9" ) << QByteArray("abc") << QByteArray("b") << 2 << -1; |
|
859 |
QTest::newRow( "10" ) << QByteArray("abc") << QByteArray("c") << 0 << 2; |
|
860 |
QTest::newRow( "11" ) << QByteArray("abc") << QByteArray("C") << 0 << -1; |
|
861 |
QTest::newRow( "12" ) << QByteArray("abc") << QByteArray("c") << 1 << 2; |
|
862 |
QTest::newRow( "13" ) << QByteArray("abc") << QByteArray("C") << 1 << -1; |
|
863 |
QTest::newRow( "14" ) << QByteArray("abc") << QByteArray("c") << 2 << 2; |
|
864 |
QTest::newRow( "15" ) << QByteArray("aBc") << QByteArray("bc") << 0 << -1; |
|
865 |
QTest::newRow( "16" ) << QByteArray("aBc") << QByteArray("Bc") << 0 << 1; |
|
866 |
QTest::newRow( "17" ) << QByteArray("aBc") << QByteArray("bC") << 0 << -1; |
|
867 |
QTest::newRow( "18" ) << QByteArray("aBc") << QByteArray("BC") << 0 << -1; |
|
868 |
||
869 |
// task 203692 |
|
870 |
static const char h19[] = {'x', 0x00, 0xe7, 0x25, 0x1c, 0x0a}; |
|
871 |
static const char n19[] = {0x00, 0x00, 0x01, 0x00}; |
|
872 |
QTest::newRow( "19" ) << QByteArray(h19, sizeof(h19)) |
|
873 |
<< QByteArray(n19, sizeof(n19)) << 0 << -1; |
|
874 |
||
875 |
QTest::newRow( "empty" ) << QByteArray("") << QByteArray("x") << 0 << -1; |
|
876 |
QTest::newRow( "null" ) << QByteArray() << QByteArray("x") << 0 << -1; |
|
877 |
QTest::newRow( "null-in-null") << QByteArray() << QByteArray() << 0 << 0; |
|
878 |
QTest::newRow( "empty-in-null") << QByteArray() << QByteArray("") << 0 << 0; |
|
879 |
QTest::newRow( "null-in-empty") << QByteArray("") << QByteArray() << 0 << 0; |
|
880 |
QTest::newRow( "empty-in-empty") << QByteArray("") << QByteArray("") << 0 << 0; |
|
881 |
||
882 |
QByteArray veryBigHaystack(500, 'a'); |
|
883 |
veryBigHaystack += 'B'; |
|
884 |
QTest::newRow("BoyerMooreStressTest") << veryBigHaystack << veryBigHaystack << 0 << 0; |
|
885 |
QTest::newRow("BoyerMooreStressTest2") << veryBigHaystack + 'c' << veryBigHaystack << 0 << 0; |
|
886 |
QTest::newRow("BoyerMooreStressTest3") << 'c' + veryBigHaystack << veryBigHaystack << 0 << 1; |
|
887 |
QTest::newRow("BoyerMooreStressTest4") << veryBigHaystack << veryBigHaystack + 'c' << 0 << -1; |
|
888 |
QTest::newRow("BoyerMooreStressTest5") << veryBigHaystack << 'c' + veryBigHaystack << 0 << -1; |
|
889 |
QTest::newRow("BoyerMooreStressTest6") << 'd' + veryBigHaystack << 'c' + veryBigHaystack << 0 << -1; |
|
890 |
QTest::newRow("BoyerMooreStressTest6") << veryBigHaystack + 'c' << 'c' + veryBigHaystack << 0 << -1; |
|
891 |
} |
|
892 |
||
893 |
void tst_QByteArray::indexOf() |
|
894 |
{ |
|
895 |
QFETCH( QByteArray, haystack ); |
|
896 |
QFETCH( QByteArray, needle ); |
|
897 |
QFETCH( int, startpos ); |
|
898 |
QFETCH( int, expected ); |
|
899 |
||
900 |
bool hasNull = needle.contains('\0'); |
|
901 |
||
902 |
QCOMPARE( haystack.indexOf(needle, startpos), expected ); |
|
903 |
if (!hasNull) |
|
904 |
QCOMPARE( haystack.indexOf(needle.data(), startpos), expected ); |
|
905 |
if (needle.size() == 1) |
|
906 |
QCOMPARE( haystack.indexOf(needle.at(0), startpos), expected ); |
|
907 |
||
908 |
if (startpos == 0) { |
|
909 |
QCOMPARE( haystack.indexOf(needle), expected ); |
|
910 |
if (!hasNull) |
|
911 |
QCOMPARE( haystack.indexOf(needle.data()), expected ); |
|
912 |
if (needle.size() == 1) |
|
913 |
QCOMPARE( haystack.indexOf(needle.at(0)), expected ); |
|
914 |
} |
|
915 |
} |
|
916 |
||
917 |
void tst_QByteArray::lastIndexOf_data() |
|
918 |
{ |
|
919 |
QTest::addColumn<QByteArray>("haystack"); |
|
920 |
QTest::addColumn<QByteArray>("needle"); |
|
921 |
QTest::addColumn<int>("startpos"); |
|
922 |
QTest::addColumn<int>("expected"); |
|
923 |
||
924 |
QTest::newRow( "1" ) << QByteArray("abc") << QByteArray("a") << 0 << 0; |
|
925 |
QTest::newRow( "2" ) << QByteArray("abc") << QByteArray("A") << 0 << -1; |
|
926 |
QTest::newRow( "3" ) << QByteArray("abc") << QByteArray("a") << 1 << 0; |
|
927 |
QTest::newRow( "4" ) << QByteArray("abc") << QByteArray("A") << 1 << -1; |
|
928 |
QTest::newRow( "5" ) << QByteArray("abc") << QByteArray("a") << -1 << 0; |
|
929 |
QTest::newRow( "6" ) << QByteArray("abc") << QByteArray("b") << 0 << -1; |
|
930 |
QTest::newRow( "7" ) << QByteArray("abc") << QByteArray("B") << 0 << -1; |
|
931 |
QTest::newRow( "8" ) << QByteArray("abc") << QByteArray("b") << 1 << 1; |
|
932 |
QTest::newRow( "9" ) << QByteArray("abc") << QByteArray("B") << 1 << -1; |
|
933 |
QTest::newRow( "10" ) << QByteArray("abc") << QByteArray("b") << 2 << 1; |
|
934 |
QTest::newRow( "11" ) << QByteArray("abc") << QByteArray("b") << -1 << 1; |
|
935 |
QTest::newRow( "12" ) << QByteArray("abc") << QByteArray("c") << 0 << -1; |
|
936 |
QTest::newRow( "13" ) << QByteArray("abc") << QByteArray("C") << 0 << -1; |
|
937 |
QTest::newRow( "14" ) << QByteArray("abc") << QByteArray("c") << 1 << -1; |
|
938 |
QTest::newRow( "15" ) << QByteArray("abc") << QByteArray("C") << 1 << -1; |
|
939 |
QTest::newRow( "16" ) << QByteArray("abc") << QByteArray("c") << 2 << 2; |
|
940 |
QTest::newRow( "17" ) << QByteArray("abc") << QByteArray("c") << -1 << 2; |
|
941 |
QTest::newRow( "18" ) << QByteArray("aBc") << QByteArray("bc") << 0 << -1; |
|
942 |
QTest::newRow( "19" ) << QByteArray("aBc") << QByteArray("Bc") << 0 << -1; |
|
943 |
QTest::newRow( "20" ) << QByteArray("aBc") << QByteArray("Bc") << 2 << 1; |
|
944 |
QTest::newRow( "21" ) << QByteArray("aBc") << QByteArray("Bc") << 1 << 1; |
|
945 |
QTest::newRow( "22" ) << QByteArray("aBc") << QByteArray("Bc") << -1 << 1; |
|
946 |
QTest::newRow( "23" ) << QByteArray("aBc") << QByteArray("bC") << 0 << -1; |
|
947 |
QTest::newRow( "24" ) << QByteArray("aBc") << QByteArray("BC") << 0 << -1; |
|
948 |
||
949 |
// task 203692 |
|
950 |
static const char h25[] = {0x00, 0xbc, 0x03, 0x10, 0x0a }; |
|
951 |
static const char n25[] = {0x00, 0x00, 0x01, 0x00}; |
|
952 |
QTest::newRow( "25" ) << QByteArray(h25, sizeof(h25)) |
|
953 |
<< QByteArray(n25, sizeof(n25)) << 0 << -1; |
|
954 |
||
955 |
QTest::newRow( "empty" ) << QByteArray("") << QByteArray("x") << -1 << -1; |
|
956 |
QTest::newRow( "null" ) << QByteArray() << QByteArray("x") << -1 << -1; |
|
957 |
QTest::newRow( "null-in-null") << QByteArray() << QByteArray() << -1 << 0; |
|
958 |
QTest::newRow( "empty-in-null") << QByteArray() << QByteArray("") << -1 << 0; |
|
959 |
QTest::newRow( "null-in-empty") << QByteArray("") << QByteArray() << -1 << 0; |
|
960 |
QTest::newRow( "empty-in-empty") << QByteArray("") << QByteArray("") << -1 << 0; |
|
961 |
} |
|
962 |
||
963 |
void tst_QByteArray::lastIndexOf() |
|
964 |
{ |
|
965 |
QFETCH( QByteArray, haystack ); |
|
966 |
QFETCH( QByteArray, needle ); |
|
967 |
QFETCH( int, startpos ); |
|
968 |
QFETCH( int, expected ); |
|
969 |
||
970 |
bool hasNull = needle.contains('\0'); |
|
971 |
||
972 |
QCOMPARE( haystack.lastIndexOf(needle, startpos), expected ); |
|
973 |
if (!hasNull) |
|
974 |
QCOMPARE( haystack.lastIndexOf(needle.data(), startpos), expected ); |
|
975 |
if (needle.size() == 1) |
|
976 |
QCOMPARE( haystack.lastIndexOf(needle.at(0), startpos), expected ); |
|
977 |
||
978 |
if (startpos == -1) { |
|
979 |
QCOMPARE( haystack.lastIndexOf(needle), expected ); |
|
980 |
if (!hasNull) |
|
981 |
QCOMPARE( haystack.lastIndexOf(needle.data()), expected ); |
|
982 |
if (needle.size() == 1) |
|
983 |
QCOMPARE( haystack.lastIndexOf(needle.at(0)), expected ); |
|
984 |
} |
|
985 |
} |
|
986 |
||
987 |
void tst_QByteArray::number() |
|
988 |
{ |
|
989 |
QCOMPARE(QString(QByteArray::number((quint64) 0)), |
|
990 |
QString(QByteArray("0"))); |
|
991 |
QCOMPARE(QString(QByteArray::number(Q_UINT64_C(0xFFFFFFFFFFFFFFFF))), |
|
992 |
QString(QByteArray("18446744073709551615"))); |
|
993 |
QCOMPARE(QString(QByteArray::number(Q_INT64_C(0xFFFFFFFFFFFFFFFF))), |
|
994 |
QString(QByteArray("-1"))); |
|
995 |
QCOMPARE(QString(QByteArray::number(qint64(0))), |
|
996 |
QString(QByteArray("0"))); |
|
997 |
QCOMPARE(QString(QByteArray::number(Q_INT64_C(0x7FFFFFFFFFFFFFFF))), |
|
998 |
QString(QByteArray("9223372036854775807"))); |
|
999 |
QCOMPARE(QString(QByteArray::number(Q_INT64_C(0x8000000000000000))), |
|
1000 |
QString(QByteArray("-9223372036854775808"))); |
|
1001 |
} |
|
1002 |
||
1003 |
// defined later |
|
1004 |
extern const char globalChar; |
|
1005 |
||
1006 |
void tst_QByteArray::toInt_data() |
|
1007 |
{ |
|
1008 |
QTest::addColumn<QByteArray>("string"); |
|
1009 |
QTest::addColumn<int>("base"); |
|
1010 |
QTest::addColumn<int>("expectednumber"); |
|
1011 |
QTest::addColumn<bool>("expectedok"); |
|
1012 |
||
1013 |
QTest::newRow("base 10") << QByteArray("100") << 10 << int(100) << true; |
|
1014 |
QTest::newRow("base 16") << QByteArray("100") << 16 << int(256) << true; |
|
1015 |
QTest::newRow("base 16") << QByteArray("0400") << 16 << int(1024) << true; |
|
1016 |
QTest::newRow("base 2") << QByteArray("1111") << 2 << int(15) << true; |
|
1017 |
QTest::newRow("base 8") << QByteArray("100") << 8 << int(64) << true; |
|
1018 |
QTest::newRow("base 0") << QByteArray("0x10") << 0 << int(16) << true; |
|
1019 |
QTest::newRow("base 0") << QByteArray("10") << 0 << int(10) << true; |
|
1020 |
QTest::newRow("base 0") << QByteArray("010") << 0 << int(8) << true; |
|
1021 |
QTest::newRow("empty") << QByteArray() << 0 << int(0) << false; |
|
1022 |
||
1023 |
// using fromRawData |
|
1024 |
QTest::newRow("raw1") << QByteArray::fromRawData("1", 1) << 10 << 1 << true; |
|
1025 |
QTest::newRow("raw2") << QByteArray::fromRawData("1foo", 1) << 10 << 1 << true; |
|
1026 |
QTest::newRow("raw3") << QByteArray::fromRawData("12", 1) << 10 << 1 << true; |
|
1027 |
QTest::newRow("raw4") << QByteArray::fromRawData("123456789", 1) << 10 << 1 << true; |
|
1028 |
QTest::newRow("raw4") << QByteArray::fromRawData("123456789", 2) << 10 << 12 << true; |
|
1029 |
||
1030 |
QTest::newRow("raw-static") << QByteArray::fromRawData(&globalChar, 1) << 10 << 1 << true; |
|
1031 |
} |
|
1032 |
||
1033 |
void tst_QByteArray::toInt() |
|
1034 |
{ |
|
1035 |
QFETCH( QByteArray, string ); |
|
1036 |
QFETCH( int, base ); |
|
1037 |
QFETCH( int, expectednumber ); |
|
1038 |
QFETCH( bool, expectedok ); |
|
1039 |
||
1040 |
bool ok; |
|
1041 |
int number = string.toInt(&ok, base); |
|
1042 |
||
1043 |
QCOMPARE( ok, expectedok ); |
|
1044 |
QCOMPARE( number, expectednumber ); |
|
1045 |
} |
|
1046 |
||
1047 |
Q_DECLARE_METATYPE(qulonglong) |
|
1048 |
void tst_QByteArray::toULong_data() |
|
1049 |
{ |
|
1050 |
QTest::addColumn<QByteArray>("str"); |
|
1051 |
QTest::addColumn<int>("base"); |
|
1052 |
QTest::addColumn<ulong>("result"); |
|
1053 |
QTest::addColumn<bool>("ok"); |
|
1054 |
||
1055 |
ulong LongMaxPlusOne = (ulong)LONG_MAX + 1; |
|
1056 |
QTest::newRow("LONG_MAX+1") << QString::number(LongMaxPlusOne).toLatin1() << 10 << LongMaxPlusOne << true; |
|
1057 |
QTest::newRow("default") << QByteArray() << 10 << 0UL << false; |
|
1058 |
QTest::newRow("empty") << QByteArray("") << 10 << 0UL << false; |
|
1059 |
QTest::newRow("ulong1") << QByteArray("3234567890") << 10 << 3234567890UL << true; |
|
1060 |
QTest::newRow("ulong2") << QByteArray("fFFfFfFf") << 16 << 0xFFFFFFFFUL << true; |
|
1061 |
} |
|
1062 |
||
1063 |
void tst_QByteArray::toULong() |
|
1064 |
{ |
|
1065 |
QFETCH(QByteArray, str); |
|
1066 |
QFETCH(int, base); |
|
1067 |
QFETCH(ulong, result); |
|
1068 |
QFETCH(bool, ok); |
|
1069 |
||
1070 |
bool b; |
|
1071 |
QCOMPARE(str.toULong(0, base), result); |
|
1072 |
QCOMPARE(str.toULong(&b, base), result); |
|
1073 |
QCOMPARE(b, ok); |
|
1074 |
} |
|
1075 |
||
1076 |
void tst_QByteArray::toULongLong_data() |
|
1077 |
{ |
|
1078 |
QTest::addColumn<QByteArray>("str"); |
|
1079 |
QTest::addColumn<int>("base"); |
|
1080 |
QTest::addColumn<qulonglong>("result"); |
|
1081 |
QTest::addColumn<bool>("ok"); |
|
1082 |
||
1083 |
QTest::newRow("default") << QByteArray() << 10 << (qulonglong)0 << false; |
|
1084 |
QTest::newRow("out of base bound") << QByteArray("c") << 10 << (qulonglong)0 << false; |
|
1085 |
||
1086 |
} |
|
1087 |
||
1088 |
void tst_QByteArray::toULongLong() |
|
1089 |
{ |
|
1090 |
QFETCH(QByteArray, str); |
|
1091 |
QFETCH(int, base); |
|
1092 |
QFETCH(qulonglong, result); |
|
1093 |
QFETCH(bool, ok); |
|
1094 |
||
1095 |
bool b; |
|
1096 |
QCOMPARE(str.toULongLong(0, base), result); |
|
1097 |
QCOMPARE(str.toULongLong(&b, base), result); |
|
1098 |
QCOMPARE(b, ok); |
|
1099 |
} |
|
1100 |
||
1101 |
// global function defined in qbytearray.cpp |
|
1102 |
void tst_QByteArray::qAllocMore() |
|
1103 |
{ |
|
1104 |
static const int t[] = { |
|
1105 |
INT_MIN, INT_MIN + 1, -1234567, -66000, -1025, |
|
1106 |
-3, -1, 0, +1, +3, +1025, +66000, +1234567, INT_MAX - 1, INT_MAX, |
|
1107 |
INT_MAX/3 |
|
1108 |
}; |
|
1109 |
static const int N = sizeof(t)/sizeof(t[0]); |
|
1110 |
||
1111 |
// make sure qAllocMore() doesn't loop infinitely on any input |
|
1112 |
for (int i = 0; i < N; ++i) { |
|
1113 |
for (int j = 0; j < N; ++j) { |
|
1114 |
::qAllocMore(t[i], t[j]); |
|
1115 |
} |
|
1116 |
} |
|
1117 |
} |
|
1118 |
||
1119 |
void tst_QByteArray::resizeAfterFromRawData() |
|
1120 |
{ |
|
1121 |
QByteArray buffer("hello world"); |
|
1122 |
||
1123 |
QByteArray array = QByteArray::fromRawData(buffer.constData(), buffer.size()); |
|
1124 |
QVERIFY(array.constData() == buffer.constData()); |
|
1125 |
array.resize(5); |
|
1126 |
QVERIFY(array.constData() == buffer.constData()); |
|
1127 |
} |
|
1128 |
||
1129 |
void tst_QByteArray::appendAfterFromRawData() |
|
1130 |
{ |
|
1131 |
QByteArray arr; |
|
1132 |
{ |
|
1133 |
char data[] = "X"; |
|
1134 |
arr += QByteArray::fromRawData(data, sizeof(data)); |
|
1135 |
data[0] = 'Y'; |
|
1136 |
} |
|
1137 |
QVERIFY(arr.at(0) == 'X'); |
|
1138 |
} |
|
1139 |
||
1140 |
void tst_QByteArray::toFromHex_data() |
|
1141 |
{ |
|
1142 |
QTest::addColumn<QByteArray>("str"); |
|
1143 |
QTest::addColumn<QByteArray>("hex"); |
|
1144 |
QTest::addColumn<QByteArray>("hex_alt1"); |
|
1145 |
||
1146 |
QTest::newRow("Qt is great!") |
|
1147 |
<< QByteArray("Qt is great!") |
|
1148 |
<< QByteArray("517420697320677265617421") |
|
1149 |
<< QByteArray("51 74 20 69 73 20 67 72 65 61 74 21"); |
|
1150 |
||
1151 |
QTest::newRow("Qt is so great!") |
|
1152 |
<< QByteArray("Qt is so great!") |
|
1153 |
<< QByteArray("517420697320736f20677265617421") |
|
1154 |
<< QByteArray("51 74 20 69 73 20 73 6f 20 67 72 65 61 74 21"); |
|
1155 |
||
1156 |
QTest::newRow("default-constructed") |
|
1157 |
<< QByteArray() |
|
1158 |
<< QByteArray() |
|
1159 |
<< QByteArray(); |
|
1160 |
||
1161 |
QTest::newRow("empty") |
|
1162 |
<< QByteArray("") |
|
1163 |
<< QByteArray("") |
|
1164 |
<< QByteArray(""); |
|
1165 |
||
1166 |
QTest::newRow("array-of-null") |
|
1167 |
<< QByteArray("\0", 1) |
|
1168 |
<< QByteArray("00") |
|
1169 |
<< QByteArray("0"); |
|
1170 |
||
1171 |
QTest::newRow("no-leading-zero") |
|
1172 |
<< QByteArray("\xf") |
|
1173 |
<< QByteArray("0f") |
|
1174 |
<< QByteArray("f"); |
|
1175 |
||
1176 |
QTest::newRow("single-byte") |
|
1177 |
<< QByteArray("\xaf") |
|
1178 |
<< QByteArray("af") |
|
1179 |
<< QByteArray("xaf"); |
|
1180 |
||
1181 |
QTest::newRow("no-leading-zero") |
|
1182 |
<< QByteArray("\xd\xde\xad\xc0\xde") |
|
1183 |
<< QByteArray("0ddeadc0de") |
|
1184 |
<< QByteArray("ddeadc0de"); |
|
1185 |
||
1186 |
QTest::newRow("garbage") |
|
1187 |
<< QByteArray("\xC\xde\xeC\xea\xee\xDe\xee\xee") |
|
1188 |
<< QByteArray("0cdeeceaeedeeeee") |
|
1189 |
<< QByteArray("Code less. Create more. Deploy everywhere."); |
|
1190 |
||
1191 |
QTest::newRow("under-defined-1") |
|
1192 |
<< QByteArray("\x1\x23") |
|
1193 |
<< QByteArray("0123") |
|
1194 |
<< QByteArray("x123"); |
|
1195 |
||
1196 |
QTest::newRow("under-defined-2") |
|
1197 |
<< QByteArray("\x12\x34") |
|
1198 |
<< QByteArray("1234") |
|
1199 |
<< QByteArray("x1234"); |
|
1200 |
} |
|
1201 |
||
1202 |
void tst_QByteArray::toFromHex() |
|
1203 |
{ |
|
1204 |
QFETCH(QByteArray, str); |
|
1205 |
QFETCH(QByteArray, hex); |
|
1206 |
QFETCH(QByteArray, hex_alt1); |
|
1207 |
||
1208 |
{ |
|
1209 |
const QByteArray th = str.toHex(); |
|
1210 |
QCOMPARE(th.size(), hex.size()); |
|
1211 |
QCOMPARE(th, hex); |
|
1212 |
} |
|
1213 |
||
1214 |
{ |
|
1215 |
const QByteArray fh = QByteArray::fromHex(hex); |
|
1216 |
QCOMPARE(fh.size(), str.size()); |
|
1217 |
QCOMPARE(fh, str); |
|
1218 |
} |
|
1219 |
||
1220 |
QCOMPARE(QByteArray::fromHex(hex_alt1), str); |
|
1221 |
} |
|
1222 |
||
1223 |
void tst_QByteArray::toFromPercentEncoding() |
|
1224 |
{ |
|
1225 |
QByteArray arr("Qt is great!"); |
|
1226 |
/* |
|
1227 |
QByteArray data = arr.toPercentEncoding(); |
|
1228 |
QCOMPARE(QString(data), QString("Qt%20is%20great%21")); |
|
1229 |
QCOMPARE(QByteArray::fromPercentEncoding(data), arr); |
|
1230 |
*/ |
|
1231 |
QByteArray data = arr.toPercentEncoding("! ", "Qt"); |
|
1232 |
QCOMPARE(QString(data), QString("%51%74 is grea%74!")); |
|
1233 |
QCOMPARE(QByteArray::fromPercentEncoding(data), arr); |
|
1234 |
||
1235 |
data = arr.toPercentEncoding(QByteArray(), "abcdefghijklmnopqrstuvwxyz", 'Q'); |
|
1236 |
QCOMPARE(QString(data), QString("Q51Q74Q20Q69Q73Q20Q67Q72Q65Q61Q74Q21")); |
|
1237 |
QCOMPARE(QByteArray::fromPercentEncoding(data, 'Q'), arr); |
|
1238 |
||
1239 |
// verify that to/from percent encoding preserves nullity |
|
1240 |
arr = ""; |
|
1241 |
QVERIFY(arr.isEmpty()); |
|
1242 |
QVERIFY(!arr.isNull()); |
|
1243 |
QVERIFY(arr.toPercentEncoding().isEmpty()); |
|
1244 |
QVERIFY(!arr.toPercentEncoding().isNull()); |
|
1245 |
QVERIFY(QByteArray::fromPercentEncoding("").isEmpty()); |
|
1246 |
QVERIFY(!QByteArray::fromPercentEncoding("").isNull()); |
|
1247 |
||
1248 |
arr = QByteArray(); |
|
1249 |
QVERIFY(arr.isEmpty()); |
|
1250 |
QVERIFY(arr.isNull()); |
|
1251 |
QVERIFY(arr.toPercentEncoding().isEmpty()); |
|
1252 |
QVERIFY(arr.toPercentEncoding().isNull()); |
|
1253 |
QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isEmpty()); |
|
1254 |
QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isNull()); |
|
1255 |
} |
|
1256 |
||
1257 |
void tst_QByteArray::compare_data() |
|
1258 |
{ |
|
1259 |
QTest::addColumn<QByteArray>("str1"); |
|
1260 |
QTest::addColumn<QByteArray>("str2"); |
|
1261 |
QTest::addColumn<int>("result"); |
|
1262 |
||
1263 |
QTest::newRow("null") << QByteArray() << QByteArray() << 0; |
|
1264 |
QTest::newRow("null-empty")<< QByteArray() << QByteArray("") << 0; |
|
1265 |
QTest::newRow("empty-null")<< QByteArray("") << QByteArray() << 0; |
|
1266 |
QTest::newRow("null-full") << QByteArray() << QByteArray("abc") << -1; |
|
1267 |
QTest::newRow("full-null") << QByteArray("abc") << QByteArray() << +1; |
|
1268 |
QTest::newRow("empty-full")<< QByteArray("") << QByteArray("abc") << -1; |
|
1269 |
QTest::newRow("full-empty")<< QByteArray("abc") << QByteArray("") << +1; |
|
1270 |
QTest::newRow("rawempty-full") << QByteArray::fromRawData("abc", 0) << QByteArray("abc") << -1; |
|
1271 |
QTest::newRow("full-rawempty") << QByteArray("abc") << QByteArray::fromRawData("abc", 0) << +1; |
|
1272 |
||
1273 |
QTest::newRow("equal 1") << QByteArray("abc") << QByteArray("abc") << 0; |
|
1274 |
QTest::newRow("equal 2") << QByteArray::fromRawData("abc", 3) << QByteArray("abc") << 0; |
|
1275 |
QTest::newRow("equal 3") << QByteArray::fromRawData("abcdef", 3) << QByteArray("abc") << 0; |
|
1276 |
QTest::newRow("equal 4") << QByteArray("abc") << QByteArray::fromRawData("abc", 3) << 0; |
|
1277 |
QTest::newRow("equal 5") << QByteArray("abc") << QByteArray::fromRawData("abcdef", 3) << 0; |
|
1278 |
QTest::newRow("equal 6") << QByteArray("a\0bc", 4) << QByteArray("a\0bc", 4) << 0; |
|
1279 |
QTest::newRow("equal 7") << QByteArray::fromRawData("a\0bcdef", 4) << QByteArray("a\0bc", 4) << 0; |
|
1280 |
QTest::newRow("equal 8") << QByteArray("a\0bc", 4) << QByteArray::fromRawData("a\0bcdef", 4) << 0; |
|
1281 |
||
1282 |
QTest::newRow("less 1") << QByteArray("000") << QByteArray("abc") << -1; |
|
1283 |
QTest::newRow("less 2") << QByteArray::fromRawData("00", 3) << QByteArray("abc") << -1; |
|
1284 |
QTest::newRow("less 3") << QByteArray("000") << QByteArray::fromRawData("abc", 3) << -1; |
|
1285 |
QTest::newRow("less 4") << QByteArray("abc", 3) << QByteArray("abc", 4) << -1; |
|
1286 |
QTest::newRow("less 5") << QByteArray::fromRawData("abc\0", 3) << QByteArray("abc\0", 4) << -1; |
|
1287 |
QTest::newRow("less 6") << QByteArray("a\0bc", 4) << QByteArray("a\0bd", 4) << -1; |
|
1288 |
||
1289 |
QTest::newRow("greater 1") << QByteArray("abc") << QByteArray("000") << +1; |
|
1290 |
QTest::newRow("greater 2") << QByteArray("abc") << QByteArray::fromRawData("00", 3) << +1; |
|
1291 |
QTest::newRow("greater 3") << QByteArray("abcd") << QByteArray::fromRawData("abcd", 3) << +1; |
|
1292 |
QTest::newRow("greater 4") << QByteArray("a\0bc", 4) << QByteArray("a\0bb", 4) << +1; |
|
1293 |
} |
|
1294 |
||
1295 |
void tst_QByteArray::compare() |
|
1296 |
{ |
|
1297 |
QFETCH(QByteArray, str1); |
|
1298 |
QFETCH(QByteArray, str2); |
|
1299 |
QFETCH(int, result); |
|
1300 |
||
1301 |
const bool isEqual = result == 0; |
|
1302 |
const bool isLess = result < 0; |
|
1303 |
const bool isGreater = result > 0; |
|
1304 |
||
1305 |
// basic tests: |
|
1306 |
QCOMPARE(str1 == str2, isEqual); |
|
1307 |
QCOMPARE(str1 < str2, isLess); |
|
1308 |
QCOMPARE(str1 > str2, isGreater); |
|
1309 |
||
1310 |
// composed tests: |
|
1311 |
QCOMPARE(str1 <= str2, isLess || isEqual); |
|
1312 |
QCOMPARE(str1 >= str2, isGreater || isEqual); |
|
1313 |
QCOMPARE(str1 != str2, !isEqual); |
|
1314 |
||
1315 |
// inverted tests: |
|
1316 |
QCOMPARE(str2 == str1, isEqual); |
|
1317 |
QCOMPARE(str2 < str1, isGreater); |
|
1318 |
QCOMPARE(str2 > str1, isLess); |
|
1319 |
||
1320 |
// composed, inverted tests: |
|
1321 |
QCOMPARE(str2 <= str1, isGreater || isEqual); |
|
1322 |
QCOMPARE(str2 >= str1, isLess || isEqual); |
|
1323 |
QCOMPARE(str2 != str1, !isEqual); |
|
1324 |
} |
|
1325 |
||
1326 |
void tst_QByteArray::compareCharStar_data() |
|
1327 |
{ |
|
1328 |
QTest::addColumn<QByteArray>("str1"); |
|
1329 |
QTest::addColumn<QString>("string2"); |
|
1330 |
QTest::addColumn<int>("result"); |
|
1331 |
||
1332 |
QTest::newRow("null-null") << QByteArray() << QString() << 0; |
|
1333 |
QTest::newRow("null-empty") << QByteArray() << "" << 0; |
|
1334 |
QTest::newRow("null-full") << QByteArray() << "abc" << -1; |
|
1335 |
QTest::newRow("empty-null") << QByteArray("") << QString() << 0; |
|
1336 |
QTest::newRow("empty-empty") << QByteArray("") << "" << 0; |
|
1337 |
QTest::newRow("empty-full") << QByteArray("") << "abc" << -1; |
|
1338 |
QTest::newRow("raw-null") << QByteArray::fromRawData("abc", 0) << QString() << 0; |
|
1339 |
QTest::newRow("raw-empty") << QByteArray::fromRawData("abc", 0) << QString("") << 0; |
|
1340 |
QTest::newRow("raw-full") << QByteArray::fromRawData("abc", 0) << "abc" << -1; |
|
1341 |
||
1342 |
QTest::newRow("full-null") << QByteArray("abc") << QString() << +1; |
|
1343 |
QTest::newRow("full-empty") << QByteArray("abc") << "" << +1; |
|
1344 |
||
1345 |
QTest::newRow("equal1") << QByteArray("abc") << "abc" << 0; |
|
1346 |
QTest::newRow("equal2") << QByteArray("abcd", 3) << "abc" << 0; |
|
1347 |
QTest::newRow("equal3") << QByteArray::fromRawData("abcd", 3) << "abc" << 0; |
|
1348 |
||
1349 |
QTest::newRow("less1") << QByteArray("ab") << "abc" << -1; |
|
1350 |
QTest::newRow("less2") << QByteArray("abb") << "abc" << -1; |
|
1351 |
QTest::newRow("less3") << QByteArray::fromRawData("abc", 2) << "abc" << -1; |
|
1352 |
QTest::newRow("less4") << QByteArray("", 1) << "abc" << -1; |
|
1353 |
QTest::newRow("less5") << QByteArray::fromRawData("", 1) << "abc" << -1; |
|
1354 |
QTest::newRow("less6") << QByteArray("a\0bc", 4) << "a.bc" << -1; |
|
1355 |
||
1356 |
QTest::newRow("greater1") << QByteArray("ac") << "abc" << +1; |
|
1357 |
QTest::newRow("greater2") << QByteArray("abd") << "abc" << +1; |
|
1358 |
QTest::newRow("greater3") << QByteArray("abcd") << "abc" << +1; |
|
1359 |
QTest::newRow("greater4") << QByteArray::fromRawData("abcd", 4) << "abc" << +1; |
|
1360 |
} |
|
1361 |
||
1362 |
void tst_QByteArray::compareCharStar() |
|
1363 |
{ |
|
1364 |
QFETCH(QByteArray, str1); |
|
1365 |
QFETCH(QString, string2); |
|
1366 |
QFETCH(int, result); |
|
1367 |
||
1368 |
const bool isEqual = result == 0; |
|
1369 |
const bool isLess = result < 0; |
|
1370 |
const bool isGreater = result > 0; |
|
1371 |
QByteArray qba = string2.toLatin1(); |
|
1372 |
const char *str2 = qba.constData(); |
|
1373 |
if (string2.isNull()) |
|
1374 |
str2 = 0; |
|
1375 |
||
1376 |
// basic tests: |
|
1377 |
QCOMPARE(str1 == str2, isEqual); |
|
1378 |
QCOMPARE(str1 < str2, isLess); |
|
1379 |
QCOMPARE(str1 > str2, isGreater); |
|
1380 |
||
1381 |
// composed tests: |
|
1382 |
QCOMPARE(str1 <= str2, isLess || isEqual); |
|
1383 |
QCOMPARE(str1 >= str2, isGreater || isEqual); |
|
1384 |
QCOMPARE(str1 != str2, !isEqual); |
|
1385 |
||
1386 |
// inverted tests: |
|
1387 |
QCOMPARE(str2 == str1, isEqual); |
|
1388 |
QCOMPARE(str2 < str1, isGreater); |
|
1389 |
QCOMPARE(str2 > str1, isLess); |
|
1390 |
||
1391 |
// composed, inverted tests: |
|
1392 |
QCOMPARE(str2 <= str1, isGreater || isEqual); |
|
1393 |
QCOMPARE(str2 >= str1, isLess || isEqual); |
|
1394 |
QCOMPARE(str2 != str1, !isEqual); |
|
1395 |
} |
|
1396 |
||
1397 |
void tst_QByteArray::repeatedSignature() const |
|
1398 |
{ |
|
1399 |
/* repated() should be a const member. */ |
|
1400 |
const QByteArray string; |
|
1401 |
string.repeated(3); |
|
1402 |
} |
|
1403 |
||
1404 |
void tst_QByteArray::repeated() const |
|
1405 |
{ |
|
1406 |
QFETCH(QByteArray, string); |
|
1407 |
QFETCH(QByteArray, expected); |
|
1408 |
QFETCH(int, count); |
|
1409 |
||
1410 |
QCOMPARE(string.repeated(count), expected); |
|
1411 |
} |
|
1412 |
||
1413 |
void tst_QByteArray::repeated_data() const |
|
1414 |
{ |
|
1415 |
QTest::addColumn<QByteArray>("string" ); |
|
1416 |
QTest::addColumn<QByteArray>("expected" ); |
|
1417 |
QTest::addColumn<int>("count" ); |
|
1418 |
||
1419 |
/* Empty strings. */ |
|
1420 |
QTest::newRow("") |
|
1421 |
<< QByteArray() |
|
1422 |
<< QByteArray() |
|
1423 |
<< 0; |
|
1424 |
||
1425 |
QTest::newRow("") |
|
1426 |
<< QByteArray() |
|
1427 |
<< QByteArray() |
|
1428 |
<< -1004; |
|
1429 |
||
1430 |
QTest::newRow("") |
|
1431 |
<< QByteArray() |
|
1432 |
<< QByteArray() |
|
1433 |
<< 1; |
|
1434 |
||
1435 |
QTest::newRow("") |
|
1436 |
<< QByteArray() |
|
1437 |
<< QByteArray() |
|
1438 |
<< 5; |
|
1439 |
||
1440 |
/* On simple string. */ |
|
1441 |
QTest::newRow("") |
|
1442 |
<< QByteArray("abc") |
|
1443 |
<< QByteArray() |
|
1444 |
<< -1004; |
|
1445 |
||
1446 |
QTest::newRow("") |
|
1447 |
<< QByteArray("abc") |
|
1448 |
<< QByteArray() |
|
1449 |
<< -1; |
|
1450 |
||
1451 |
QTest::newRow("") |
|
1452 |
<< QByteArray("abc") |
|
1453 |
<< QByteArray() |
|
1454 |
<< 0; |
|
1455 |
||
1456 |
QTest::newRow("") |
|
1457 |
<< QByteArray("abc") |
|
1458 |
<< QByteArray("abc") |
|
1459 |
<< 1; |
|
1460 |
||
1461 |
QTest::newRow("") |
|
1462 |
<< QByteArray(("abc")) |
|
1463 |
<< QByteArray(("abcabc")) |
|
1464 |
<< 2; |
|
1465 |
||
1466 |
QTest::newRow("") |
|
1467 |
<< QByteArray(("abc")) |
|
1468 |
<< QByteArray(("abcabcabc")) |
|
1469 |
<< 3; |
|
1470 |
||
1471 |
QTest::newRow("") |
|
1472 |
<< QByteArray(("abc")) |
|
1473 |
<< QByteArray(("abcabcabcabc")) |
|
1474 |
<< 4; |
|
1475 |
} |
|
1476 |
||
1477 |
void tst_QByteArray::byteRefDetaching() const |
|
1478 |
{ |
|
1479 |
{ |
|
1480 |
QByteArray str = "str"; |
|
1481 |
QByteArray copy; |
|
1482 |
copy[0] = 'S'; |
|
1483 |
||
1484 |
QCOMPARE(str, QByteArray("str")); |
|
1485 |
} |
|
1486 |
||
1487 |
{ |
|
1488 |
char buf[] = { 's', 't', 'r' }; |
|
1489 |
QByteArray str = QByteArray::fromRawData(buf, 3); |
|
1490 |
str[0] = 'S'; |
|
1491 |
||
1492 |
QCOMPARE(buf[0], char('s')); |
|
1493 |
} |
|
1494 |
||
1495 |
{ |
|
1496 |
static const char buf[] = { 's', 't', 'r' }; |
|
1497 |
QByteArray str = QByteArray::fromRawData(buf, 3); |
|
1498 |
||
1499 |
// this causes a crash in most systems if the detaching doesn't work |
|
1500 |
str[0] = 'S'; |
|
1501 |
||
1502 |
QCOMPARE(buf[0], char('s')); |
|
1503 |
} |
|
1504 |
} |
|
1505 |
||
1506 |
const char globalChar = '1'; |
|
1507 |
||
1508 |
QTEST_APPLESS_MAIN(tst_QByteArray) |
|
1509 |
#include "tst_qbytearray.moc" |