| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the test suite of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | #include <qbytearray.h> |
| 33 | #include <qfile.h> |
| 34 | #include <qhash.h> |
| 35 | #include <limits.h> |
| 36 | #include <private/qtools_p.h> |
| 37 | |
| 38 | class tst_QByteArray : public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | public: |
| 43 | tst_QByteArray(); |
| 44 | private slots: |
| 45 | void swap(); |
| 46 | void qChecksum_data(); |
| 47 | void qChecksum(); |
| 48 | void qCompress_data(); |
| 49 | #ifndef QT_NO_COMPRESS |
| 50 | void qCompress(); |
| 51 | void qUncompressCorruptedData_data(); |
| 52 | void qUncompressCorruptedData(); |
| 53 | void qCompressionZeroTermination(); |
| 54 | #endif |
| 55 | void constByteArray(); |
| 56 | void leftJustified(); |
| 57 | void rightJustified(); |
| 58 | void setNum(); |
| 59 | void startsWith_data(); |
| 60 | void startsWith(); |
| 61 | void startsWith_char(); |
| 62 | void endsWith_data(); |
| 63 | void endsWith(); |
| 64 | void endsWith_char(); |
| 65 | void reverseIterators(); |
| 66 | void split_data(); |
| 67 | void split(); |
| 68 | void base64_data(); |
| 69 | void base64(); |
| 70 | void fromBase64_data(); |
| 71 | void fromBase64(); |
| 72 | void qvsnprintf(); |
| 73 | void qstrlen(); |
| 74 | void qstrnlen(); |
| 75 | void qstrcpy(); |
| 76 | void qstrncpy(); |
| 77 | void qstricmp_data(); |
| 78 | void qstricmp(); |
| 79 | void qstricmp_singularities(); |
| 80 | void qstrnicmp_singularities(); |
| 81 | void chop_data(); |
| 82 | void chop(); |
| 83 | void prepend(); |
| 84 | void prependExtended_data(); |
| 85 | void prependExtended(); |
| 86 | void append(); |
| 87 | void appendExtended_data(); |
| 88 | void appendExtended(); |
| 89 | void insert(); |
| 90 | void insertExtended_data(); |
| 91 | void insertExtended(); |
| 92 | void remove_data(); |
| 93 | void remove(); |
| 94 | void replace_data(); |
| 95 | void replace(); |
| 96 | void replaceWithSpecifiedLength(); |
| 97 | void indexOf_data(); |
| 98 | void indexOf(); |
| 99 | void lastIndexOf_data(); |
| 100 | void lastIndexOf(); |
| 101 | void toULong_data(); |
| 102 | void toULong(); |
| 103 | void toULongLong_data(); |
| 104 | void toULongLong(); |
| 105 | |
| 106 | void number(); |
| 107 | void toInt_data(); |
| 108 | void toInt(); |
| 109 | void toDouble_data(); |
| 110 | void toDouble(); |
| 111 | void blockSizeCalculations(); |
| 112 | |
| 113 | void resizeAfterFromRawData(); |
| 114 | void appendAfterFromRawData(); |
| 115 | void toFromHex_data(); |
| 116 | void toFromHex(); |
| 117 | void toFromPercentEncoding(); |
| 118 | void fromPercentEncoding_data(); |
| 119 | void fromPercentEncoding(); |
| 120 | void toPercentEncoding_data(); |
| 121 | void toPercentEncoding(); |
| 122 | void toPercentEncoding2_data(); |
| 123 | void toPercentEncoding2(); |
| 124 | |
| 125 | void compare_data(); |
| 126 | void compare(); |
| 127 | void compareCharStar_data(); |
| 128 | void compareCharStar(); |
| 129 | |
| 130 | void repeatedSignature() const; |
| 131 | void repeated() const; |
| 132 | void repeated_data() const; |
| 133 | |
| 134 | void byteRefDetaching() const; |
| 135 | |
| 136 | void reserve(); |
| 137 | void reserveExtended_data(); |
| 138 | void reserveExtended(); |
| 139 | void movablity_data(); |
| 140 | void movablity(); |
| 141 | void literals(); |
| 142 | void toUpperLower_data(); |
| 143 | void toUpperLower(); |
| 144 | void isUpper(); |
| 145 | void isLower(); |
| 146 | |
| 147 | void macTypes(); |
| 148 | |
| 149 | void stdString(); |
| 150 | }; |
| 151 | |
| 152 | static const struct StaticByteArrays { |
| 153 | struct Standard { |
| 154 | QByteArrayData data; |
| 155 | const char string[8]; |
| 156 | } standard; |
| 157 | struct NotNullTerminated { |
| 158 | QByteArrayData data; |
| 159 | const char string[8]; |
| 160 | } notNullTerminated; |
| 161 | struct Shifted { |
| 162 | QByteArrayData data; |
| 163 | const char dummy; // added to change offset of string |
| 164 | const char string[8]; |
| 165 | } shifted; |
| 166 | struct ShiftedNotNullTerminated { |
| 167 | QByteArrayData data; |
| 168 | const char dummy; // added to change offset of string |
| 169 | const char string[8]; |
| 170 | } shiftedNotNullTerminated; |
| 171 | |
| 172 | } statics = {.standard: {Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(4), .string: "data" } |
| 173 | ,.notNullTerminated: {Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(4), .string: "dataBAD" } |
| 174 | ,.shifted: {Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(4, sizeof(QByteArrayData) + sizeof(char)), .dummy: 0, .string: "data" } |
| 175 | ,.shiftedNotNullTerminated: {Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(4, sizeof(QByteArrayData) + sizeof(char)), .dummy: 0, .string: "dataBAD" } |
| 176 | }; |
| 177 | |
| 178 | static const QByteArrayDataPtr staticStandard = { .ptr: const_cast<QByteArrayData *>(&statics.standard.data) }; |
| 179 | static const QByteArrayDataPtr staticNotNullTerminated = { .ptr: const_cast<QByteArrayData *>(&statics.notNullTerminated.data) }; |
| 180 | static const QByteArrayDataPtr staticShifted = { .ptr: const_cast<QByteArrayData *>(&statics.shifted.data) }; |
| 181 | static const QByteArrayDataPtr staticShiftedNotNullTerminated = { .ptr: const_cast<QByteArrayData *>(&statics.shiftedNotNullTerminated.data) }; |
| 182 | |
| 183 | template <class T> const T &verifyZeroTermination(const T &t) { return t; } |
| 184 | |
| 185 | QByteArray verifyZeroTermination(const QByteArray &ba) |
| 186 | { |
| 187 | // This test does some evil stuff, it's all supposed to work. |
| 188 | |
| 189 | QByteArray::DataPtr baDataPtr = const_cast<QByteArray &>(ba).data_ptr(); |
| 190 | |
| 191 | // Skip if isStatic() or fromRawData(), as those offer no guarantees |
| 192 | if (baDataPtr->ref.isStatic() |
| 193 | || baDataPtr->offset != QByteArray().data_ptr()->offset) |
| 194 | return ba; |
| 195 | |
| 196 | int baSize = ba.size(); |
| 197 | char baTerminator = ba.constData()[baSize]; |
| 198 | if ('\0' != baTerminator) |
| 199 | return QString::fromLatin1( |
| 200 | str: "*** Result ('%1') not null-terminated: 0x%2 ***" ).arg(a: QString::fromLatin1(str: ba)) |
| 201 | .arg(a: baTerminator, fieldWidth: 2, base: 16, fillChar: QChar('0')).toLatin1(); |
| 202 | |
| 203 | // Skip mutating checks on shared strings |
| 204 | if (baDataPtr->ref.isShared()) |
| 205 | return ba; |
| 206 | |
| 207 | const char *baData = ba.constData(); |
| 208 | const QByteArray baCopy(baData, baSize); // Deep copy |
| 209 | |
| 210 | const_cast<char *>(baData)[baSize] = 'x'; |
| 211 | if ('x' != ba.constData()[baSize]) { |
| 212 | return "*** Failed to replace null-terminator in " |
| 213 | "result ('" + ba + "') ***" ; |
| 214 | } |
| 215 | if (ba != baCopy) { |
| 216 | return "*** Result ('" + ba + "') differs from its copy " |
| 217 | "after null-terminator was replaced ***" ; |
| 218 | } |
| 219 | const_cast<char *>(baData)[baSize] = '\0'; // Restore sanity |
| 220 | |
| 221 | return ba; |
| 222 | } |
| 223 | |
| 224 | // Overriding QTest's QCOMPARE, to check QByteArray for null termination |
| 225 | #undef QCOMPARE |
| 226 | #define QCOMPARE(actual, expected) \ |
| 227 | do { \ |
| 228 | if (!QTest::qCompare(verifyZeroTermination(actual), expected, \ |
| 229 | #actual, #expected, __FILE__, __LINE__)) \ |
| 230 | return; \ |
| 231 | } while (0) \ |
| 232 | /**/ |
| 233 | #undef QTEST |
| 234 | #define QTEST(actual, testElement) \ |
| 235 | do { \ |
| 236 | if (!QTest::qTest(verifyZeroTermination(actual), testElement, \ |
| 237 | #actual, #testElement, __FILE__, __LINE__)) \ |
| 238 | return; \ |
| 239 | } while (0) \ |
| 240 | /**/ |
| 241 | |
| 242 | Q_DECLARE_METATYPE(QByteArray::Base64DecodingStatus); |
| 243 | |
| 244 | tst_QByteArray::tst_QByteArray() |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | void tst_QByteArray::qChecksum_data() |
| 249 | { |
| 250 | QTest::addColumn<QByteArray>(name: "data" ); |
| 251 | QTest::addColumn<uint>(name: "len" ); |
| 252 | QTest::addColumn<Qt::ChecksumType>(name: "standard" ); |
| 253 | QTest::addColumn<uint>(name: "checksum" ); |
| 254 | |
| 255 | // Examples from ISO 14443-3 |
| 256 | QTest::newRow(dataTag: "1" ) << QByteArray("\x00\x00" ) << 2U << Qt::ChecksumItuV41 << 0x1EA0U; |
| 257 | QTest::newRow(dataTag: "2" ) << QByteArray("\x12\x34" ) << 2U << Qt::ChecksumItuV41 << 0xCF26U; |
| 258 | QTest::newRow(dataTag: "3" ) << QByteArray("\x00\x00\x00" ) << 3U << Qt::ChecksumIso3309 << 0xC6CCU; |
| 259 | QTest::newRow(dataTag: "4" ) << QByteArray("\x0F\xAA\xFF" ) << 3U << Qt::ChecksumIso3309 << 0xD1FCU; |
| 260 | QTest::newRow(dataTag: "5" ) << QByteArray("\x0A\x12\x34\x56" ) << 4U << Qt::ChecksumIso3309 << 0xF62CU; |
| 261 | } |
| 262 | |
| 263 | void tst_QByteArray::qChecksum() |
| 264 | { |
| 265 | QFETCH(QByteArray, data); |
| 266 | QFETCH(uint, len); |
| 267 | QFETCH(Qt::ChecksumType, standard); |
| 268 | QFETCH(uint, checksum); |
| 269 | |
| 270 | if (standard == Qt::ChecksumIso3309) { |
| 271 | QCOMPARE(::qChecksum(data.constData(), len), static_cast<quint16>(checksum)); |
| 272 | } |
| 273 | QCOMPARE(::qChecksum(data.constData(), len, standard), static_cast<quint16>(checksum)); |
| 274 | } |
| 275 | |
| 276 | void tst_QByteArray::qCompress_data() |
| 277 | { |
| 278 | QTest::addColumn<QByteArray>(name: "ba" ); |
| 279 | |
| 280 | const int size1 = 1024*1024; |
| 281 | QByteArray ba1( size1, 0 ); |
| 282 | |
| 283 | QTest::newRow( dataTag: "00" ) << QByteArray(); |
| 284 | |
| 285 | int i; |
| 286 | for ( i=0; i<size1; i++ ) |
| 287 | ba1[i] = (char)( i / 1024 ); |
| 288 | QTest::newRow( dataTag: "01" ) << ba1; |
| 289 | |
| 290 | for ( i=0; i<size1; i++ ) |
| 291 | ba1[i] = (char)( i % 256 ); |
| 292 | QTest::newRow( dataTag: "02" ) << ba1; |
| 293 | |
| 294 | ba1.fill( c: 'A' ); |
| 295 | QTest::newRow( dataTag: "03" ) << ba1; |
| 296 | |
| 297 | QFile file( QFINDTESTDATA("rfc3252.txt" ) ); |
| 298 | QVERIFY( file.open(QIODevice::ReadOnly) ); |
| 299 | QTest::newRow( dataTag: "04" ) << file.readAll(); |
| 300 | } |
| 301 | |
| 302 | #ifndef QT_NO_COMPRESS |
| 303 | void tst_QByteArray::qCompress() |
| 304 | { |
| 305 | QFETCH( QByteArray, ba ); |
| 306 | QByteArray compressed = ::qCompress( data: ba ); |
| 307 | QTEST( ::qUncompress( compressed ), "ba" ); |
| 308 | } |
| 309 | |
| 310 | void tst_QByteArray::qUncompressCorruptedData_data() |
| 311 | { |
| 312 | QTest::addColumn<QByteArray>(name: "in" ); |
| 313 | |
| 314 | QTest::newRow(dataTag: "0x00000000" ) << QByteArray("\x00\x00\x00\x00" , 4); |
| 315 | QTest::newRow(dataTag: "0x000000ff" ) << QByteArray("\x00\x00\x00\xff" , 4); |
| 316 | QTest::newRow(dataTag: "0x3f000000" ) << QByteArray("\x3f\x00\x00\x00" , 4); |
| 317 | QTest::newRow(dataTag: "0x3fffffff" ) << QByteArray("\x3f\xff\xff\xff" , 4); |
| 318 | QTest::newRow(dataTag: "0x7fffff00" ) << QByteArray("\x7f\xff\xff\x00" , 4); |
| 319 | QTest::newRow(dataTag: "0x7fffffff" ) << QByteArray("\x7f\xff\xff\xff" , 4); |
| 320 | QTest::newRow(dataTag: "0x80000000" ) << QByteArray("\x80\x00\x00\x00" , 4); |
| 321 | QTest::newRow(dataTag: "0x800000ff" ) << QByteArray("\x80\x00\x00\xff" , 4); |
| 322 | QTest::newRow(dataTag: "0xcf000000" ) << QByteArray("\xcf\x00\x00\x00" , 4); |
| 323 | QTest::newRow(dataTag: "0xcfffffff" ) << QByteArray("\xcf\xff\xff\xff" , 4); |
| 324 | QTest::newRow(dataTag: "0xffffff00" ) << QByteArray("\xff\xff\xff\x00" , 4); |
| 325 | QTest::newRow(dataTag: "0xffffffff" ) << QByteArray("\xff\xff\xff\xff" , 4); |
| 326 | } |
| 327 | |
| 328 | // Corrupt data causes this test to lock up on HP-UX / PA-RISC with gcc, |
| 329 | // SOLARIS, and Windows. |
| 330 | // This test is expected to produce some warning messages in the test output. |
| 331 | void tst_QByteArray::qUncompressCorruptedData() |
| 332 | { |
| 333 | #if !(defined(Q_OS_HPUX) && !defined(__ia64) && defined(Q_CC_GNU)) && !defined(Q_OS_SOLARIS) && !defined(Q_OS_WIN) |
| 334 | QFETCH(QByteArray, in); |
| 335 | |
| 336 | QByteArray res; |
| 337 | res = ::qUncompress(data: in); |
| 338 | QCOMPARE(res, QByteArray()); |
| 339 | |
| 340 | res = ::qUncompress(data: in + "blah" ); |
| 341 | QCOMPARE(res, QByteArray()); |
| 342 | #else |
| 343 | QSKIP("This test freezes on this platform" ); |
| 344 | #endif |
| 345 | } |
| 346 | |
| 347 | void tst_QByteArray::qCompressionZeroTermination() |
| 348 | { |
| 349 | QString s = "Hello, I'm a string." ; |
| 350 | QByteArray ba = ::qUncompress(data: ::qCompress(data: s.toLocal8Bit())); |
| 351 | QVERIFY((int) *(ba.data() + ba.size()) == 0); |
| 352 | } |
| 353 | |
| 354 | #endif |
| 355 | |
| 356 | void tst_QByteArray::constByteArray() |
| 357 | { |
| 358 | const char *ptr = "abc" ; |
| 359 | QByteArray cba = QByteArray::fromRawData(ptr, size: 3); |
| 360 | QVERIFY(cba.constData() == ptr); |
| 361 | cba.squeeze(); |
| 362 | QVERIFY(cba.constData() == ptr); |
| 363 | cba.detach(); |
| 364 | QVERIFY(cba.size() == 3); |
| 365 | QVERIFY(cba.capacity() == 3); |
| 366 | QVERIFY(cba.constData() != ptr); |
| 367 | QVERIFY(cba.constData()[0] == 'a'); |
| 368 | QVERIFY(cba.constData()[1] == 'b'); |
| 369 | QVERIFY(cba.constData()[2] == 'c'); |
| 370 | QVERIFY(cba.constData()[3] == '\0'); |
| 371 | } |
| 372 | |
| 373 | void tst_QByteArray::leftJustified() |
| 374 | { |
| 375 | QByteArray a; |
| 376 | a = "ABC" ; |
| 377 | QCOMPARE(a.leftJustified(5,'-'), QByteArray("ABC--" )); |
| 378 | QCOMPARE(a.leftJustified(4,'-'), QByteArray("ABC-" )); |
| 379 | QCOMPARE(a.leftJustified(4), QByteArray("ABC " )); |
| 380 | QCOMPARE(a.leftJustified(3), QByteArray("ABC" )); |
| 381 | QCOMPARE(a.leftJustified(2), QByteArray("ABC" )); |
| 382 | QCOMPARE(a.leftJustified(1), QByteArray("ABC" )); |
| 383 | QCOMPARE(a.leftJustified(0), QByteArray("ABC" )); |
| 384 | |
| 385 | QByteArray n; |
| 386 | QVERIFY(!n.leftJustified(3).isNull()); // I expected true |
| 387 | QCOMPARE(a.leftJustified(4,' ',true), QByteArray("ABC " )); |
| 388 | QCOMPARE(a.leftJustified(3,' ',true), QByteArray("ABC" )); |
| 389 | QCOMPARE(a.leftJustified(2,' ',true), QByteArray("AB" )); |
| 390 | QCOMPARE(a.leftJustified(1,' ',true), QByteArray("A" )); |
| 391 | QCOMPARE(a.leftJustified(0,' ',true), QByteArray("" )); |
| 392 | } |
| 393 | |
| 394 | void tst_QByteArray::rightJustified() |
| 395 | { |
| 396 | QByteArray a; |
| 397 | a="ABC" ; |
| 398 | QCOMPARE(a.rightJustified(5,'-'),QByteArray("--ABC" )); |
| 399 | QCOMPARE(a.rightJustified(4,'-'),QByteArray("-ABC" )); |
| 400 | QCOMPARE(a.rightJustified(4),QByteArray(" ABC" )); |
| 401 | QCOMPARE(a.rightJustified(3),QByteArray("ABC" )); |
| 402 | QCOMPARE(a.rightJustified(2),QByteArray("ABC" )); |
| 403 | QCOMPARE(a.rightJustified(1),QByteArray("ABC" )); |
| 404 | QCOMPARE(a.rightJustified(0),QByteArray("ABC" )); |
| 405 | |
| 406 | QByteArray n; |
| 407 | QVERIFY(!n.rightJustified(3).isNull()); // I expected true |
| 408 | QCOMPARE(a.rightJustified(4,'-',true),QByteArray("-ABC" )); |
| 409 | QCOMPARE(a.rightJustified(4,' ',true),QByteArray(" ABC" )); |
| 410 | QCOMPARE(a.rightJustified(3,' ',true),QByteArray("ABC" )); |
| 411 | QCOMPARE(a.rightJustified(2,' ',true),QByteArray("AB" )); |
| 412 | QCOMPARE(a.rightJustified(1,' ',true),QByteArray("A" )); |
| 413 | QCOMPARE(a.rightJustified(0,' ',true),QByteArray("" )); |
| 414 | QCOMPARE(a,QByteArray("ABC" )); |
| 415 | } |
| 416 | |
| 417 | void tst_QByteArray::setNum() |
| 418 | { |
| 419 | QByteArray a; |
| 420 | QCOMPARE(a.setNum(-1), QByteArray("-1" )); |
| 421 | QCOMPARE(a.setNum(0), QByteArray("0" )); |
| 422 | QCOMPARE(a.setNum(0, 2), QByteArray("0" )); |
| 423 | QCOMPARE(a.setNum(0, 36), QByteArray("0" )); |
| 424 | QCOMPARE(a.setNum(1), QByteArray("1" )); |
| 425 | QCOMPARE(a.setNum(35, 36), QByteArray("z" )); |
| 426 | QCOMPARE(a.setNum(37, 2), QByteArray("100101" )); |
| 427 | QCOMPARE(a.setNum(37, 36), QByteArray("11" )); |
| 428 | |
| 429 | // Negative numbers are only properly supported for base 10. |
| 430 | QCOMPARE(a.setNum(short(-1), 16), QByteArray("ffff" )); |
| 431 | QCOMPARE(a.setNum(int(-1), 16), QByteArray("ffffffff" )); |
| 432 | QCOMPARE(a.setNum(qlonglong(-1), 16), QByteArray("ffffffffffffffff" )); |
| 433 | |
| 434 | QCOMPARE(a.setNum(short(-1), 10), QByteArray("-1" )); |
| 435 | QCOMPARE(a.setNum(int(-1), 10), QByteArray("-1" )); |
| 436 | QCOMPARE(a.setNum(qlonglong(-1), 10), QByteArray("-1" )); |
| 437 | |
| 438 | QCOMPARE(a.setNum(-123), QByteArray("-123" )); |
| 439 | QCOMPARE(a.setNum(0x123,16), QByteArray("123" )); |
| 440 | QCOMPARE(a.setNum((short)123), QByteArray("123" )); |
| 441 | |
| 442 | QCOMPARE(a.setNum(1.23), QByteArray("1.23" )); |
| 443 | QCOMPARE(a.setNum(1.234567), QByteArray("1.23457" )); |
| 444 | |
| 445 | // Note that there are no 'long' overloads, so not all of the |
| 446 | // QString::setNum() tests can be re-used. |
| 447 | QCOMPARE(a.setNum(Q_INT64_C(123)), QByteArray("123" )); |
| 448 | // 2^40 == 1099511627776 |
| 449 | QCOMPARE(a.setNum(Q_INT64_C(-1099511627776)), QByteArray("-1099511627776" )); |
| 450 | QCOMPARE(a.setNum(Q_UINT64_C(1099511627776)), QByteArray("1099511627776" )); |
| 451 | QCOMPARE(a.setNum(Q_INT64_C(9223372036854775807)), // LLONG_MAX |
| 452 | QByteArray("9223372036854775807" )); |
| 453 | QCOMPARE(a.setNum(-Q_INT64_C(9223372036854775807) - Q_INT64_C(1)), |
| 454 | QByteArray("-9223372036854775808" )); |
| 455 | QCOMPARE(a.setNum(Q_UINT64_C(18446744073709551615)), // ULLONG_MAX |
| 456 | QByteArray("18446744073709551615" )); |
| 457 | QCOMPARE(a.setNum(0.000000000931322574615478515625), QByteArray("9.31323e-10" )); |
| 458 | } |
| 459 | |
| 460 | void tst_QByteArray::startsWith_data() |
| 461 | { |
| 462 | QTest::addColumn<QByteArray>(name: "ba" ); |
| 463 | QTest::addColumn<QByteArray>(name: "sw" ); |
| 464 | QTest::addColumn<bool>(name: "result" ); |
| 465 | |
| 466 | QTest::newRow(dataTag: "01" ) << QByteArray() << QByteArray() << true; |
| 467 | QTest::newRow(dataTag: "02" ) << QByteArray() << QByteArray("" ) << true; |
| 468 | QTest::newRow(dataTag: "03" ) << QByteArray() << QByteArray("hallo" ) << false; |
| 469 | |
| 470 | QTest::newRow(dataTag: "04" ) << QByteArray("" ) << QByteArray() << true; |
| 471 | QTest::newRow(dataTag: "05" ) << QByteArray("" ) << QByteArray("" ) << true; |
| 472 | QTest::newRow(dataTag: "06" ) << QByteArray("" ) << QByteArray("h" ) << false; |
| 473 | |
| 474 | QTest::newRow(dataTag: "07" ) << QByteArray("hallo" ) << QByteArray("h" ) << true; |
| 475 | QTest::newRow(dataTag: "08" ) << QByteArray("hallo" ) << QByteArray("hallo" ) << true; |
| 476 | QTest::newRow(dataTag: "09" ) << QByteArray("hallo" ) << QByteArray("" ) << true; |
| 477 | QTest::newRow(dataTag: "10" ) << QByteArray("hallo" ) << QByteArray("hallohallo" ) << false; |
| 478 | QTest::newRow(dataTag: "11" ) << QByteArray("hallo" ) << QByteArray() << true; |
| 479 | } |
| 480 | |
| 481 | void tst_QByteArray::startsWith() |
| 482 | { |
| 483 | QFETCH(QByteArray, ba); |
| 484 | QFETCH(QByteArray, sw); |
| 485 | QFETCH(bool, result); |
| 486 | |
| 487 | QVERIFY(ba.startsWith(sw) == result); |
| 488 | |
| 489 | if (sw.isNull()) { |
| 490 | QVERIFY(ba.startsWith((char*)0) == result); |
| 491 | } else { |
| 492 | QVERIFY(ba.startsWith(sw.data()) == result); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void tst_QByteArray::startsWith_char() |
| 497 | { |
| 498 | QVERIFY(QByteArray("hallo" ).startsWith('h')); |
| 499 | QVERIFY(!QByteArray("hallo" ).startsWith('\0')); |
| 500 | QVERIFY(!QByteArray("hallo" ).startsWith('o')); |
| 501 | QVERIFY(QByteArray("h" ).startsWith('h')); |
| 502 | QVERIFY(!QByteArray("h" ).startsWith('\0')); |
| 503 | QVERIFY(!QByteArray("h" ).startsWith('o')); |
| 504 | QVERIFY(!QByteArray("hallo" ).startsWith('l')); |
| 505 | QVERIFY(!QByteArray("" ).startsWith('\0')); |
| 506 | QVERIFY(!QByteArray("" ).startsWith('a')); |
| 507 | QVERIFY(!QByteArray().startsWith('a')); |
| 508 | QVERIFY(!QByteArray().startsWith('\0')); |
| 509 | } |
| 510 | |
| 511 | void tst_QByteArray::endsWith_data() |
| 512 | { |
| 513 | QTest::addColumn<QByteArray>(name: "ba" ); |
| 514 | QTest::addColumn<QByteArray>(name: "sw" ); |
| 515 | QTest::addColumn<bool>(name: "result" ); |
| 516 | |
| 517 | QTest::newRow(dataTag: "01" ) << QByteArray() << QByteArray() << true; |
| 518 | QTest::newRow(dataTag: "02" ) << QByteArray() << QByteArray("" ) << true; |
| 519 | QTest::newRow(dataTag: "03" ) << QByteArray() << QByteArray("hallo" ) << false; |
| 520 | |
| 521 | QTest::newRow(dataTag: "04" ) << QByteArray("" ) << QByteArray() << true; |
| 522 | QTest::newRow(dataTag: "05" ) << QByteArray("" ) << QByteArray("" ) << true; |
| 523 | QTest::newRow(dataTag: "06" ) << QByteArray("" ) << QByteArray("h" ) << false; |
| 524 | |
| 525 | QTest::newRow(dataTag: "07" ) << QByteArray("hallo" ) << QByteArray("o" ) << true; |
| 526 | QTest::newRow(dataTag: "08" ) << QByteArray("hallo" ) << QByteArray("hallo" ) << true; |
| 527 | QTest::newRow(dataTag: "09" ) << QByteArray("hallo" ) << QByteArray("" ) << true; |
| 528 | QTest::newRow(dataTag: "10" ) << QByteArray("hallo" ) << QByteArray("hallohallo" ) << false; |
| 529 | QTest::newRow(dataTag: "11" ) << QByteArray("hallo" ) << QByteArray() << true; |
| 530 | } |
| 531 | |
| 532 | void tst_QByteArray::endsWith() |
| 533 | { |
| 534 | QFETCH(QByteArray, ba); |
| 535 | QFETCH(QByteArray, sw); |
| 536 | QFETCH(bool, result); |
| 537 | |
| 538 | QVERIFY(ba.endsWith(sw) == result); |
| 539 | |
| 540 | if (sw.isNull()) { |
| 541 | QVERIFY(ba.endsWith((char*)0) == result); |
| 542 | } else { |
| 543 | QVERIFY(ba.endsWith(sw.data()) == result); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | void tst_QByteArray::endsWith_char() |
| 548 | { |
| 549 | QVERIFY(QByteArray("hallo" ).endsWith('o')); |
| 550 | QVERIFY(!QByteArray("hallo" ).endsWith('\0')); |
| 551 | QVERIFY(!QByteArray("hallo" ).endsWith('h')); |
| 552 | QVERIFY(QByteArray("h" ).endsWith('h')); |
| 553 | QVERIFY(!QByteArray("h" ).endsWith('\0')); |
| 554 | QVERIFY(!QByteArray("h" ).endsWith('o')); |
| 555 | QVERIFY(!QByteArray("hallo" ).endsWith('l')); |
| 556 | QVERIFY(!QByteArray("" ).endsWith('\0')); |
| 557 | QVERIFY(!QByteArray("" ).endsWith('a')); |
| 558 | QVERIFY(!QByteArray().endsWith('a')); |
| 559 | QVERIFY(!QByteArray().endsWith('\0')); |
| 560 | } |
| 561 | |
| 562 | void tst_QByteArray::reverseIterators() |
| 563 | { |
| 564 | QByteArray s = "1234" ; |
| 565 | QByteArray sr = s; |
| 566 | std::reverse(first: sr.begin(), last: sr.end()); |
| 567 | const QByteArray &csr = sr; |
| 568 | QVERIFY(std::equal(s.begin(), s.end(), sr.rbegin())); |
| 569 | QVERIFY(std::equal(s.begin(), s.end(), sr.crbegin())); |
| 570 | QVERIFY(std::equal(s.begin(), s.end(), csr.rbegin())); |
| 571 | QVERIFY(std::equal(sr.rbegin(), sr.rend(), s.begin())); |
| 572 | QVERIFY(std::equal(sr.crbegin(), sr.crend(), s.begin())); |
| 573 | QVERIFY(std::equal(csr.rbegin(), csr.rend(), s.begin())); |
| 574 | } |
| 575 | |
| 576 | void tst_QByteArray::split_data() |
| 577 | { |
| 578 | QTest::addColumn<QByteArray>(name: "sample" ); |
| 579 | QTest::addColumn<int>(name: "size" ); |
| 580 | |
| 581 | QTest::newRow(dataTag: "1" ) << QByteArray("-rw-r--r-- 1 0 0 519240 Jul 9 2002 bigfile" ) << 14; |
| 582 | QTest::newRow(dataTag: "2" ) << QByteArray("abcde" ) << 1; |
| 583 | QTest::newRow(dataTag: "one empty" ) << QByteArray("" ) << 1; |
| 584 | QTest::newRow(dataTag: "two empty" ) << QByteArray(" " ) << 2; |
| 585 | QTest::newRow(dataTag: "three empty" ) << QByteArray(" " ) << 3; |
| 586 | |
| 587 | } |
| 588 | |
| 589 | void tst_QByteArray::split() |
| 590 | { |
| 591 | QFETCH(QByteArray, sample); |
| 592 | QFETCH(int, size); |
| 593 | |
| 594 | QList<QByteArray> list = sample.split(sep: ' '); |
| 595 | QCOMPARE(list.count(), size); |
| 596 | } |
| 597 | |
| 598 | void tst_QByteArray::swap() |
| 599 | { |
| 600 | QByteArray b1 = "b1" , b2 = "b2" ; |
| 601 | b1.swap(other&: b2); |
| 602 | QCOMPARE(b1, QByteArray("b2" )); |
| 603 | QCOMPARE(b2, QByteArray("b1" )); |
| 604 | } |
| 605 | |
| 606 | void tst_QByteArray::base64_data() |
| 607 | { |
| 608 | QTest::addColumn<QByteArray>(name: "rawdata" ); |
| 609 | QTest::addColumn<QByteArray>(name: "base64" ); |
| 610 | |
| 611 | QTest::newRow(dataTag: "1" ) << QByteArray("" ) << QByteArray("" ); |
| 612 | QTest::newRow(dataTag: "2" ) << QByteArray("1" ) << QByteArray("MQ==" ); |
| 613 | QTest::newRow(dataTag: "3" ) << QByteArray("12" ) << QByteArray("MTI=" ); |
| 614 | QTest::newRow(dataTag: "4" ) << QByteArray("123" ) << QByteArray("MTIz" ); |
| 615 | QTest::newRow(dataTag: "5" ) << QByteArray("1234" ) << QByteArray("MTIzNA==" ); |
| 616 | QTest::newRow(dataTag: "6" ) << QByteArray("\n" ) << QByteArray("Cg==" ); |
| 617 | QTest::newRow(dataTag: "7" ) << QByteArray("a\n" ) << QByteArray("YQo=" ); |
| 618 | QTest::newRow(dataTag: "8" ) << QByteArray("ab\n" ) << QByteArray("YWIK" ); |
| 619 | QTest::newRow(dataTag: "9" ) << QByteArray("abc\n" ) << QByteArray("YWJjCg==" ); |
| 620 | QTest::newRow(dataTag: "a" ) << QByteArray("abcd\n" ) << QByteArray("YWJjZAo=" ); |
| 621 | QTest::newRow(dataTag: "b" ) << QByteArray("abcde\n" ) << QByteArray("YWJjZGUK" ); |
| 622 | QTest::newRow(dataTag: "c" ) << QByteArray("abcdef\n" ) << QByteArray("YWJjZGVmCg==" ); |
| 623 | QTest::newRow(dataTag: "d" ) << QByteArray("abcdefg\n" ) << QByteArray("YWJjZGVmZwo=" ); |
| 624 | QTest::newRow(dataTag: "e" ) << QByteArray("abcdefgh\n" ) << QByteArray("YWJjZGVmZ2gK" ); |
| 625 | |
| 626 | QByteArray ba; |
| 627 | ba.resize(size: 256); |
| 628 | for (int i = 0; i < 256; ++i) |
| 629 | ba[i] = i; |
| 630 | QTest::newRow(dataTag: "f" ) << ba << QByteArray("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==" ); |
| 631 | |
| 632 | QTest::newRow(dataTag: "g" ) << QByteArray("foo\0bar" , 7) << QByteArray("Zm9vAGJhcg==" ); |
| 633 | QTest::newRow(dataTag: "h" ) << QByteArray("f\xd1oo\x9ctar" ) << QByteArray("ZtFvb5x0YXI=" ); |
| 634 | QTest::newRow(dataTag: "i" ) << QByteArray("\"\0\0\0\0\0\0\"" , 8) << QByteArray("IgAAAAAAACI=" ); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | void tst_QByteArray::base64() |
| 639 | { |
| 640 | QFETCH(QByteArray, rawdata); |
| 641 | QFETCH(QByteArray, base64); |
| 642 | QByteArray::FromBase64Result result; |
| 643 | |
| 644 | result = QByteArray::fromBase64Encoding(base64, options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 645 | QVERIFY(result); |
| 646 | QCOMPARE(result.decoded, rawdata); |
| 647 | |
| 648 | QByteArray arr = base64; |
| 649 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 650 | QVERIFY(result); |
| 651 | QCOMPARE(result.decoded, rawdata); |
| 652 | |
| 653 | QByteArray arr64 = rawdata.toBase64(); |
| 654 | QCOMPARE(arr64, base64); |
| 655 | |
| 656 | arr64 = rawdata.toBase64(options: QByteArray::Base64Encoding); |
| 657 | QCOMPARE(arr64, base64); |
| 658 | |
| 659 | QByteArray base64noequals = base64; |
| 660 | base64noequals.replace(before: '=', c: "" ); |
| 661 | arr64 = rawdata.toBase64(options: QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals); |
| 662 | QCOMPARE(arr64, base64noequals); |
| 663 | |
| 664 | QByteArray base64url = base64; |
| 665 | base64url.replace(before: '/', after: '_').replace(before: '+', after: '-'); |
| 666 | arr64 = rawdata.toBase64(options: QByteArray::Base64UrlEncoding); |
| 667 | QCOMPARE(arr64, base64url); |
| 668 | |
| 669 | QByteArray base64urlnoequals = base64url; |
| 670 | base64urlnoequals.replace(before: '=', c: "" ); |
| 671 | arr64 = rawdata.toBase64(options: QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals); |
| 672 | QCOMPARE(arr64, base64urlnoequals); |
| 673 | } |
| 674 | |
| 675 | //different from the previous test as the input are invalid |
| 676 | void tst_QByteArray::fromBase64_data() |
| 677 | { |
| 678 | QTest::addColumn<QByteArray>(name: "rawdata" ); |
| 679 | QTest::addColumn<QByteArray>(name: "base64" ); |
| 680 | QTest::addColumn<QByteArray::Base64DecodingStatus>(name: "status" ); |
| 681 | |
| 682 | QTest::newRow(dataTag: "1" ) << QByteArray("" ) << QByteArray(" " ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 683 | QTest::newRow(dataTag: "2" ) << QByteArray("1" ) << QByteArray("MQ=" ) << QByteArray::Base64DecodingStatus::IllegalInputLength; |
| 684 | QTest::newRow(dataTag: "3" ) << QByteArray("12" ) << QByteArray("MTI " ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 685 | QTest::newRow(dataTag: "4" ) << QByteArray("123" ) << QByteArray("M=TIz" ) << QByteArray::Base64DecodingStatus::IllegalInputLength; |
| 686 | QTest::newRow(dataTag: "5" ) << QByteArray("1234" ) << QByteArray("MTI zN A " ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 687 | QTest::newRow(dataTag: "6" ) << QByteArray("\n" ) << QByteArray("Cg@" ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 688 | QTest::newRow(dataTag: "7" ) << QByteArray("a\n" ) << QByteArray("======YQo=" ) << QByteArray::Base64DecodingStatus::IllegalInputLength; |
| 689 | QTest::newRow(dataTag: "8" ) << QByteArray("ab\n" ) << QByteArray("Y\nWIK " ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 690 | QTest::newRow(dataTag: "9" ) << QByteArray("abc\n" ) << QByteArray("YWJjCg=" ) << QByteArray::Base64DecodingStatus::IllegalInputLength; |
| 691 | QTest::newRow(dataTag: "a" ) << QByteArray("abcd\n" ) << QByteArray("YWJ\1j\x9cZAo=" ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 692 | QTest::newRow(dataTag: "b" ) << QByteArray("abcde\n" ) << QByteArray("YW JjZ\n G\tUK" ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 693 | QTest::newRow(dataTag: "c" ) << QByteArray("abcdef\n" ) << QByteArray("YWJjZGVmCg=" ) << QByteArray::Base64DecodingStatus::IllegalInputLength; |
| 694 | QTest::newRow(dataTag: "d" ) << QByteArray("abcdefg\n" ) << QByteArray("YWJ\rjZGVmZwo" ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 695 | QTest::newRow(dataTag: "e" ) << QByteArray("abcdefgh\n" ) << QByteArray("YWJjZGVmZ2gK====" ) << QByteArray::Base64DecodingStatus::IllegalPadding; |
| 696 | |
| 697 | QByteArray ba; |
| 698 | ba.resize(size: 256); |
| 699 | for (int i = 0; i < 256; ++i) |
| 700 | ba[i] = i; |
| 701 | QTest::newRow(dataTag: "f" ) << ba << QByteArray("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Nj\n" |
| 702 | "c4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1u\n" |
| 703 | "b3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpa\n" |
| 704 | "anqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd\n" |
| 705 | "3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w== " ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 706 | |
| 707 | |
| 708 | QTest::newRow(dataTag: "g" ) << QByteArray("foo\0bar" , 7) << QByteArray("Zm9vAGJhcg=" ) << QByteArray::Base64DecodingStatus::IllegalInputLength; |
| 709 | QTest::newRow(dataTag: "h" ) << QByteArray("f\xd1oo\x9ctar" ) << QByteArray("ZtFvb5x 0YXI" ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 710 | QTest::newRow(dataTag: "i" ) << QByteArray("\"\0\0\0\0\0\0\"" , 8) << QByteArray("IgAAAAAAACI " ) << QByteArray::Base64DecodingStatus::IllegalCharacter; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | void tst_QByteArray::fromBase64() |
| 715 | { |
| 716 | QFETCH(QByteArray, rawdata); |
| 717 | QFETCH(QByteArray, base64); |
| 718 | QFETCH(QByteArray::Base64DecodingStatus, status); |
| 719 | |
| 720 | QByteArray::FromBase64Result result; |
| 721 | |
| 722 | result = QByteArray::fromBase64Encoding(base64); |
| 723 | QVERIFY(result); |
| 724 | QCOMPARE(result.decoded, rawdata); |
| 725 | |
| 726 | result = QByteArray::fromBase64Encoding(base64, options: QByteArray::Base64Encoding); |
| 727 | QVERIFY(result); |
| 728 | QCOMPARE(result.decoded, rawdata); |
| 729 | |
| 730 | result = QByteArray::fromBase64Encoding(base64, options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 731 | QVERIFY(!result); |
| 732 | QCOMPARE(result.decodingStatus, status); |
| 733 | QVERIFY(result.decoded.isEmpty()); |
| 734 | |
| 735 | QByteArray arr = base64; |
| 736 | QVERIFY(!arr.isDetached()); |
| 737 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 738 | QVERIFY(!arr.isEmpty()); |
| 739 | QVERIFY(!result); |
| 740 | QCOMPARE(result.decodingStatus, status); |
| 741 | QVERIFY(result.decoded.isEmpty()); |
| 742 | |
| 743 | arr.detach(); |
| 744 | QVERIFY(arr.isDetached()); |
| 745 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 746 | QVERIFY(arr.isEmpty()); |
| 747 | QVERIFY(!result); |
| 748 | QCOMPARE(result.decodingStatus, status); |
| 749 | QVERIFY(result.decoded.isEmpty()); |
| 750 | |
| 751 | // try "base64url" encoding |
| 752 | QByteArray base64url = base64; |
| 753 | base64url.replace(before: '/', after: '_').replace(before: '+', after: '-'); |
| 754 | result = QByteArray::fromBase64Encoding(base64: base64url, options: QByteArray::Base64UrlEncoding); |
| 755 | QVERIFY(result); |
| 756 | QCOMPARE(result.decoded, rawdata); |
| 757 | |
| 758 | result = QByteArray::fromBase64Encoding(base64: base64url, options: QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors); |
| 759 | QVERIFY(!result); |
| 760 | QCOMPARE(result.decodingStatus, status); |
| 761 | QVERIFY(result.decoded.isEmpty()); |
| 762 | |
| 763 | arr = base64url; |
| 764 | arr.detach(); |
| 765 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors); |
| 766 | QVERIFY(arr.isEmpty()); |
| 767 | QVERIFY(!result); |
| 768 | QCOMPARE(result.decodingStatus, status); |
| 769 | QVERIFY(result.decoded.isEmpty()); |
| 770 | |
| 771 | if (base64 != base64url) { |
| 772 | // check that the invalid decodings fail |
| 773 | result = QByteArray::fromBase64Encoding(base64, options: QByteArray::Base64UrlEncoding); |
| 774 | QVERIFY(result); |
| 775 | QVERIFY(result.decoded != rawdata); |
| 776 | result = QByteArray::fromBase64Encoding(base64: base64url, options: QByteArray::Base64Encoding); |
| 777 | QVERIFY(result); |
| 778 | QVERIFY(result.decoded != rawdata); |
| 779 | |
| 780 | result = QByteArray::fromBase64Encoding(base64, options: QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors); |
| 781 | QVERIFY(!result); |
| 782 | QVERIFY(result.decoded.isEmpty()); |
| 783 | |
| 784 | arr = base64; |
| 785 | arr.detach(); |
| 786 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors); |
| 787 | QVERIFY(arr.isEmpty()); |
| 788 | QVERIFY(!result); |
| 789 | QVERIFY(result.decoded.isEmpty()); |
| 790 | |
| 791 | result = QByteArray::fromBase64Encoding(base64: base64url, options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 792 | QVERIFY(!result); |
| 793 | QVERIFY(result.decoded.isEmpty()); |
| 794 | |
| 795 | arr = base64url; |
| 796 | arr.detach(); |
| 797 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64Encoding | QByteArray::AbortOnBase64DecodingErrors); |
| 798 | QVERIFY(arr.isEmpty()); |
| 799 | QVERIFY(!result); |
| 800 | QVERIFY(result.decoded.isEmpty()); |
| 801 | } |
| 802 | |
| 803 | // also remove padding, if any, and test again. note that by doing |
| 804 | // that we might be sanitizing the illegal input, so we can't assume now |
| 805 | // that result will be invalid in all cases |
| 806 | { |
| 807 | auto rightmostNotEqualSign = std::find_if_not(first: base64url.rbegin(), last: base64url.rend(), pred: [](char c) { return c == '='; }); |
| 808 | base64url.chop(n: std::distance(first: base64url.rbegin(), last: rightmostNotEqualSign)); // no QByteArray::erase... |
| 809 | } |
| 810 | |
| 811 | result = QByteArray::fromBase64Encoding(base64: base64url, options: QByteArray::Base64UrlEncoding); |
| 812 | QVERIFY(result); |
| 813 | QCOMPARE(result.decoded, rawdata); |
| 814 | |
| 815 | result = QByteArray::fromBase64Encoding(base64: base64url, options: QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors); |
| 816 | if (result) { |
| 817 | QCOMPARE(result.decoded, rawdata); |
| 818 | } else { |
| 819 | QCOMPARE(result.decodingStatus, status); |
| 820 | QVERIFY(result.decoded.isEmpty()); |
| 821 | } |
| 822 | |
| 823 | arr = base64url; |
| 824 | arr.detach(); |
| 825 | result = QByteArray::fromBase64Encoding(base64: std::move(arr), options: QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors); |
| 826 | QVERIFY(arr.isEmpty()); |
| 827 | if (result) { |
| 828 | QCOMPARE(result.decoded, rawdata); |
| 829 | } else { |
| 830 | QCOMPARE(result.decodingStatus, status); |
| 831 | QVERIFY(result.decoded.isEmpty()); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | void tst_QByteArray::qvsnprintf() |
| 836 | { |
| 837 | char buf[20]; |
| 838 | memset(s: buf, c: 42, n: sizeof(buf)); |
| 839 | |
| 840 | QCOMPARE(::qsnprintf(buf, 10, "%s" , "bubu" ), 4); |
| 841 | QCOMPARE(static_cast<const char *>(buf), "bubu" ); |
| 842 | #ifndef Q_CC_MSVC |
| 843 | // MSVC implementation of vsnprintf overwrites bytes after null terminator so this would fail. |
| 844 | QCOMPARE(buf[5], char(42)); |
| 845 | #endif |
| 846 | |
| 847 | memset(s: buf, c: 42, n: sizeof(buf)); |
| 848 | QCOMPARE(::qsnprintf(buf, 5, "%s" , "bubu" ), 4); |
| 849 | QCOMPARE(static_cast<const char *>(buf), "bubu" ); |
| 850 | QCOMPARE(buf[5], char(42)); |
| 851 | |
| 852 | memset(s: buf, c: 42, n: sizeof(buf)); |
| 853 | #ifdef Q_OS_WIN |
| 854 | // VS 2005 uses the Qt implementation of vsnprintf. |
| 855 | # if defined(_MSC_VER) |
| 856 | QCOMPARE(::qsnprintf(buf, 3, "%s" , "bubu" ), -1); |
| 857 | QCOMPARE(static_cast<const char*>(buf), "bu" ); |
| 858 | # else |
| 859 | // windows has to do everything different, of course. |
| 860 | QCOMPARE(::qsnprintf(buf, 3, "%s" , "bubu" ), -1); |
| 861 | buf[19] = '\0'; |
| 862 | QCOMPARE(static_cast<const char *>(buf), "bub****************" ); |
| 863 | # endif |
| 864 | #else |
| 865 | QCOMPARE(::qsnprintf(buf, 3, "%s" , "bubu" ), 4); |
| 866 | QCOMPARE(static_cast<const char*>(buf), "bu" ); |
| 867 | #endif |
| 868 | QCOMPARE(buf[4], char(42)); |
| 869 | |
| 870 | #ifndef Q_OS_WIN |
| 871 | memset(s: buf, c: 42, n: sizeof(buf)); |
| 872 | QCOMPARE(::qsnprintf(buf, 10, "" ), 0); |
| 873 | #endif |
| 874 | } |
| 875 | |
| 876 | |
| 877 | void tst_QByteArray::qstrlen() |
| 878 | { |
| 879 | const char *src = "Something about ... \0 a string." ; |
| 880 | QCOMPARE(::qstrlen((char*)0), (uint)0); |
| 881 | QCOMPARE(::qstrlen(src), (uint)20); |
| 882 | } |
| 883 | |
| 884 | void tst_QByteArray::qstrnlen() |
| 885 | { |
| 886 | const char *src = "Something about ... \0 a string." ; |
| 887 | QCOMPARE(::qstrnlen((char*)0, 1), (uint)0); |
| 888 | QCOMPARE(::qstrnlen(src, 31), (uint)20); |
| 889 | QCOMPARE(::qstrnlen(src, 19), (uint)19); |
| 890 | QCOMPARE(::qstrnlen(src, 21), (uint)20); |
| 891 | QCOMPARE(::qstrnlen(src, 20), (uint)20); |
| 892 | } |
| 893 | |
| 894 | void tst_QByteArray::qstrcpy() |
| 895 | { |
| 896 | const char *src = "Something about ... \0 a string." ; |
| 897 | const char *expected = "Something about ... " ; |
| 898 | char dst[128]; |
| 899 | |
| 900 | QCOMPARE(::qstrcpy(0, 0), (char*)0); |
| 901 | QCOMPARE(::qstrcpy(dst, 0), (char*)0); |
| 902 | |
| 903 | QCOMPARE(::qstrcpy(dst ,src), (char *)dst); |
| 904 | QCOMPARE((char *)dst, const_cast<char *>(expected)); |
| 905 | } |
| 906 | |
| 907 | void tst_QByteArray::qstrncpy() |
| 908 | { |
| 909 | QByteArray src(1024, 'a'), dst(1024, 'b'); |
| 910 | |
| 911 | // dst == nullptr |
| 912 | QCOMPARE(::qstrncpy(0, src.data(), 0), (char*)0); |
| 913 | QCOMPARE(::qstrncpy(0, src.data(), 10), (char*)0); |
| 914 | |
| 915 | // src == nullptr |
| 916 | QCOMPARE(::qstrncpy(dst.data(), 0, 0), (char*)0); |
| 917 | QCOMPARE(::qstrncpy(dst.data(), 0, 10), (char*)0); |
| 918 | |
| 919 | // valid pointers, but len == 0 |
| 920 | QCOMPARE(::qstrncpy(dst.data(), src.data(), 0), dst.data()); |
| 921 | QCOMPARE(*dst.data(), 'b'); // must not have written to dst |
| 922 | |
| 923 | // normal copy |
| 924 | QCOMPARE(::qstrncpy(dst.data(), src.data(), src.size()), dst.data()); |
| 925 | |
| 926 | src = QByteArray( "Tumdelidum" ); |
| 927 | QCOMPARE(QByteArray(::qstrncpy(dst.data(), src.data(), src.size())), |
| 928 | QByteArray("Tumdelidu" )); |
| 929 | |
| 930 | // normal copy with length is longer than necessary |
| 931 | src = QByteArray( "Tumdelidum\0foo" ); |
| 932 | dst.resize(size: 128*1024); |
| 933 | QCOMPARE(QByteArray(::qstrncpy(dst.data(), src.data(), dst.size())), |
| 934 | QByteArray("Tumdelidum" )); |
| 935 | } |
| 936 | |
| 937 | void tst_QByteArray::qstricmp_data() |
| 938 | { |
| 939 | QTest::addColumn<QString>(name: "str1" ); |
| 940 | QTest::addColumn<QString>(name: "str2" ); |
| 941 | |
| 942 | QTest::newRow(dataTag: "equal 1" ) << "abcEdb" << "abcEdb" ; |
| 943 | QTest::newRow(dataTag: "equal 2" ) << "abcEdb" << "ABCeDB" ; |
| 944 | QTest::newRow(dataTag: "equal 3" ) << "ABCEDB" << "abcedb" ; |
| 945 | QTest::newRow(dataTag: "less 1" ) << "abcdef" << "abcdefg" ; |
| 946 | QTest::newRow(dataTag: "less 2" ) << "abcdeF" << "abcdef" ; |
| 947 | QTest::newRow(dataTag: "greater 1" ) << "abcdef" << "abcdeF" ; |
| 948 | QTest::newRow(dataTag: "greater 2" ) << "abcdefg" << "abcdef" ; |
| 949 | } |
| 950 | |
| 951 | void tst_QByteArray::qstricmp() |
| 952 | { |
| 953 | QFETCH(QString, str1); |
| 954 | QFETCH(QString, str2); |
| 955 | |
| 956 | int expected = strcmp(s1: str1.toUpper().toLatin1(), |
| 957 | s2: str2.toUpper().toLatin1()); |
| 958 | if ( expected != 0 ) { |
| 959 | expected = (expected < 0 ? -1 : 1); |
| 960 | } |
| 961 | int actual = ::qstricmp(str1.toLatin1(), str2.toLatin1()); |
| 962 | if ( actual != 0 ) { |
| 963 | actual = (actual < 0 ? -1 : 1); |
| 964 | } |
| 965 | QCOMPARE(actual, expected); |
| 966 | |
| 967 | actual = ::qstricmp("012345679abcd" + str1.toLatin1(), "012345679AbCd" + str2.toLatin1()); |
| 968 | if ( actual != 0 ) { |
| 969 | actual = (actual < 0 ? -1 : 1); |
| 970 | } |
| 971 | QCOMPARE(actual, expected); |
| 972 | |
| 973 | actual = str1.toLatin1().compare(a: str2.toLatin1(), cs: Qt::CaseInsensitive); |
| 974 | if ( actual != 0 ) { |
| 975 | actual = (actual < 0 ? -1 : 1); |
| 976 | } |
| 977 | QCOMPARE(actual, expected); |
| 978 | |
| 979 | actual = str1.toLatin1().compare(c: str2.toLatin1().constData(), cs: Qt::CaseInsensitive); |
| 980 | if ( actual != 0 ) { |
| 981 | actual = (actual < 0 ? -1 : 1); |
| 982 | } |
| 983 | QCOMPARE(actual, expected); |
| 984 | } |
| 985 | |
| 986 | void tst_QByteArray::qstricmp_singularities() |
| 987 | { |
| 988 | QCOMPARE(::qstricmp(0, 0), 0); |
| 989 | QVERIFY(::qstricmp(0, "a" ) < 0); |
| 990 | QVERIFY(::qstricmp("a" , 0) > 0); |
| 991 | QCOMPARE(::qstricmp("" , "" ), 0); |
| 992 | QCOMPARE(QByteArray().compare(nullptr, Qt::CaseInsensitive), 0); |
| 993 | QCOMPARE(QByteArray().compare("" , Qt::CaseInsensitive), 0); |
| 994 | QVERIFY(QByteArray("a" ).compare(nullptr, Qt::CaseInsensitive) > 0); |
| 995 | QVERIFY(QByteArray("a" ).compare("" , Qt::CaseInsensitive) > 0); |
| 996 | QVERIFY(QByteArray().compare("a" , Qt::CaseInsensitive) < 0); |
| 997 | } |
| 998 | |
| 999 | void tst_QByteArray::qstrnicmp_singularities() |
| 1000 | { |
| 1001 | QCOMPARE(::qstrnicmp(0, 0, 123), 0); |
| 1002 | QVERIFY(::qstrnicmp(0, "a" , 123) != 0); |
| 1003 | QVERIFY(::qstrnicmp("a" , 0, 123) != 0); |
| 1004 | QCOMPARE(::qstrnicmp("" , "" , 123), 0); |
| 1005 | QCOMPARE(::qstrnicmp("a" , "B" , 0), 0); |
| 1006 | QCOMPARE(QByteArray().compare(QByteArray(), Qt::CaseInsensitive), 0); |
| 1007 | QVERIFY(QByteArray().compare(QByteArray("a" ), Qt::CaseInsensitive) < 0); |
| 1008 | QVERIFY(QByteArray("a" ).compare(QByteArray(), Qt::CaseInsensitive) > 0); |
| 1009 | } |
| 1010 | |
| 1011 | void tst_QByteArray::chop_data() |
| 1012 | { |
| 1013 | QTest::addColumn<QByteArray>(name: "src" ); |
| 1014 | QTest::addColumn<int>(name: "choplength" ); |
| 1015 | QTest::addColumn<QByteArray>(name: "expected" ); |
| 1016 | |
| 1017 | QTest::newRow(dataTag: "1" ) << QByteArray("short1" ) << 128 << QByteArray(); |
| 1018 | QTest::newRow(dataTag: "2" ) << QByteArray("short2" ) << int(strlen(s: "short2" )) |
| 1019 | << QByteArray(); |
| 1020 | QTest::newRow(dataTag: "3" ) << QByteArray("abcdef\0foo" , 10) << 2 |
| 1021 | << QByteArray("abcdef\0f" , 8); |
| 1022 | QTest::newRow(dataTag: "4" ) << QByteArray("STARTTLS\r\n" ) << 2 |
| 1023 | << QByteArray("STARTTLS" ); |
| 1024 | QTest::newRow(dataTag: "5" ) << QByteArray("" ) << 1 << QByteArray(); |
| 1025 | QTest::newRow(dataTag: "6" ) << QByteArray("foo" ) << 0 << QByteArray("foo" ); |
| 1026 | QTest::newRow(dataTag: "7" ) << QByteArray(0) << 28 << QByteArray(); |
| 1027 | } |
| 1028 | |
| 1029 | void tst_QByteArray::chop() |
| 1030 | { |
| 1031 | QFETCH(QByteArray, src); |
| 1032 | QFETCH(int, choplength); |
| 1033 | QFETCH(QByteArray, expected); |
| 1034 | |
| 1035 | src.chop(n: choplength); |
| 1036 | QCOMPARE(src, expected); |
| 1037 | } |
| 1038 | |
| 1039 | void tst_QByteArray::prepend() |
| 1040 | { |
| 1041 | QByteArray ba("foo" ); |
| 1042 | QCOMPARE(ba.prepend((char*)0), QByteArray("foo" )); |
| 1043 | QCOMPARE(ba.prepend(QByteArray()), QByteArray("foo" )); |
| 1044 | QCOMPARE(ba.prepend("1" ), QByteArray("1foo" )); |
| 1045 | QCOMPARE(ba.prepend(QByteArray("2" )), QByteArray("21foo" )); |
| 1046 | QCOMPARE(ba.prepend('3'), QByteArray("321foo" )); |
| 1047 | QCOMPARE(ba.prepend(-1, 'x'), QByteArray("321foo" )); |
| 1048 | QCOMPARE(ba.prepend(3, 'x'), QByteArray("xxx321foo" )); |
| 1049 | QCOMPARE(ba.prepend("\0 " , 2), QByteArray::fromRawData("\0 xxx321foo" , 11)); |
| 1050 | } |
| 1051 | |
| 1052 | void tst_QByteArray::prependExtended_data() |
| 1053 | { |
| 1054 | QTest::addColumn<QByteArray>(name: "array" ); |
| 1055 | QTest::newRow(dataTag: "literal" ) << QByteArray(QByteArrayLiteral("data" )); |
| 1056 | QTest::newRow(dataTag: "standard" ) << QByteArray(staticStandard); |
| 1057 | QTest::newRow(dataTag: "shifted" ) << QByteArray(staticShifted); |
| 1058 | QTest::newRow(dataTag: "notNullTerminated" ) << QByteArray(staticNotNullTerminated); |
| 1059 | QTest::newRow(dataTag: "shiftedNotNullTerminated" ) << QByteArray(staticShiftedNotNullTerminated); |
| 1060 | QTest::newRow(dataTag: "non static data" ) << QByteArray("data" ); |
| 1061 | QTest::newRow(dataTag: "from raw data" ) << QByteArray::fromRawData("data" , size: 4); |
| 1062 | QTest::newRow(dataTag: "from raw data not terminated" ) << QByteArray::fromRawData("dataBAD" , size: 4); |
| 1063 | } |
| 1064 | |
| 1065 | void tst_QByteArray::prependExtended() |
| 1066 | { |
| 1067 | QFETCH(QByteArray, array); |
| 1068 | |
| 1069 | QCOMPARE(QByteArray().prepend(array), QByteArray("data" )); |
| 1070 | QCOMPARE(QByteArray("" ).prepend(array), QByteArray("data" )); |
| 1071 | |
| 1072 | QCOMPARE(array.prepend((char*)0), QByteArray("data" )); |
| 1073 | QCOMPARE(array.prepend(QByteArray()), QByteArray("data" )); |
| 1074 | QCOMPARE(array.prepend("1" ), QByteArray("1data" )); |
| 1075 | QCOMPARE(array.prepend(QByteArray("2" )), QByteArray("21data" )); |
| 1076 | QCOMPARE(array.prepend('3'), QByteArray("321data" )); |
| 1077 | QCOMPARE(array.prepend(-1, 'x'), QByteArray("321data" )); |
| 1078 | QCOMPARE(array.prepend(3, 'x'), QByteArray("xxx321data" )); |
| 1079 | QCOMPARE(array.prepend("\0 " , 2), QByteArray::fromRawData("\0 xxx321data" , 12)); |
| 1080 | QCOMPARE(array.size(), 12); |
| 1081 | } |
| 1082 | |
| 1083 | void tst_QByteArray::append() |
| 1084 | { |
| 1085 | QByteArray ba("foo" ); |
| 1086 | QCOMPARE(ba.append((char*)0), QByteArray("foo" )); |
| 1087 | QCOMPARE(ba.append(QByteArray()), QByteArray("foo" )); |
| 1088 | QCOMPARE(ba.append("1" ), QByteArray("foo1" )); |
| 1089 | QCOMPARE(ba.append(QByteArray("2" )), QByteArray("foo12" )); |
| 1090 | QCOMPARE(ba.append('3'), QByteArray("foo123" )); |
| 1091 | QCOMPARE(ba.append(-1, 'x'), QByteArray("foo123" )); |
| 1092 | QCOMPARE(ba.append(3, 'x'), QByteArray("foo123xxx" )); |
| 1093 | QCOMPARE(ba.append("\0" ), QByteArray("foo123xxx" )); |
| 1094 | QCOMPARE(ba.append("\0" , 1), QByteArray::fromRawData("foo123xxx\0" , 10)); |
| 1095 | QCOMPARE(ba.size(), 10); |
| 1096 | } |
| 1097 | |
| 1098 | void tst_QByteArray::appendExtended_data() |
| 1099 | { |
| 1100 | prependExtended_data(); |
| 1101 | } |
| 1102 | |
| 1103 | void tst_QByteArray::appendExtended() |
| 1104 | { |
| 1105 | QFETCH(QByteArray, array); |
| 1106 | |
| 1107 | QCOMPARE(QByteArray().append(array), QByteArray("data" )); |
| 1108 | QCOMPARE(QByteArray("" ).append(array), QByteArray("data" )); |
| 1109 | |
| 1110 | QCOMPARE(array.append((char*)0), QByteArray("data" )); |
| 1111 | QCOMPARE(array.append(QByteArray()), QByteArray("data" )); |
| 1112 | QCOMPARE(array.append("1" ), QByteArray("data1" )); |
| 1113 | QCOMPARE(array.append(QByteArray("2" )), QByteArray("data12" )); |
| 1114 | QCOMPARE(array.append('3'), QByteArray("data123" )); |
| 1115 | QCOMPARE(array.append(-1, 'x'), QByteArray("data123" )); |
| 1116 | QCOMPARE(array.append(3, 'x'), QByteArray("data123xxx" )); |
| 1117 | QCOMPARE(array.append("\0" ), QByteArray("data123xxx" )); |
| 1118 | QCOMPARE(array.append("\0" , 1), QByteArray::fromRawData("data123xxx\0" , 11)); |
| 1119 | QCOMPARE(array.size(), 11); |
| 1120 | } |
| 1121 | |
| 1122 | void tst_QByteArray::insert() |
| 1123 | { |
| 1124 | QByteArray ba("Meal" ); |
| 1125 | QCOMPARE(ba.insert(1, QByteArray("ontr" )), QByteArray("Montreal" )); |
| 1126 | QCOMPARE(ba.insert(ba.size(), "foo" ), QByteArray("Montrealfoo" )); |
| 1127 | |
| 1128 | ba = QByteArray("13" ); |
| 1129 | QCOMPARE(ba.insert(1, QByteArray("2" )), QByteArray("123" )); |
| 1130 | |
| 1131 | ba = "ac" ; |
| 1132 | QCOMPARE(ba.insert(1, 'b'), QByteArray("abc" )); |
| 1133 | QCOMPARE(ba.size(), 3); |
| 1134 | |
| 1135 | ba = "ac" ; |
| 1136 | QCOMPARE(ba.insert(-1, 3, 'x'), QByteArray("ac" )); |
| 1137 | QCOMPARE(ba.insert(1, 3, 'x'), QByteArray("axxxc" )); |
| 1138 | QCOMPARE(ba.insert(6, 3, 'x'), QByteArray("axxxc xxx" )); |
| 1139 | QCOMPARE(ba.size(), 9); |
| 1140 | |
| 1141 | ba = "ikl" ; |
| 1142 | QCOMPARE(ba.insert(1, "j" ), QByteArray("ijkl" )); |
| 1143 | QCOMPARE(ba.size(), 4); |
| 1144 | |
| 1145 | ba = "ab" ; |
| 1146 | QCOMPARE(ba.insert(1, "\0X\0" , 3), QByteArray::fromRawData("a\0X\0b" , 5)); |
| 1147 | QCOMPARE(ba.size(), 5); |
| 1148 | } |
| 1149 | |
| 1150 | void tst_QByteArray::insertExtended_data() |
| 1151 | { |
| 1152 | prependExtended_data(); |
| 1153 | } |
| 1154 | |
| 1155 | void tst_QByteArray::insertExtended() |
| 1156 | { |
| 1157 | QFETCH(QByteArray, array); |
| 1158 | QCOMPARE(array.insert(1, "i" ), QByteArray("diata" )); |
| 1159 | QCOMPARE(array.insert(1, 3, 'x'), QByteArray("dxxxiata" )); |
| 1160 | QCOMPARE(array.size(), 8); |
| 1161 | } |
| 1162 | |
| 1163 | void tst_QByteArray::remove_data() |
| 1164 | { |
| 1165 | QTest::addColumn<QByteArray>(name: "src" ); |
| 1166 | QTest::addColumn<int>(name: "position" ); |
| 1167 | QTest::addColumn<int>(name: "length" ); |
| 1168 | QTest::addColumn<QByteArray>(name: "expected" ); |
| 1169 | |
| 1170 | QTest::newRow(dataTag: "1" ) << QByteArray("Montreal" ) << 1 << 4 |
| 1171 | << QByteArray("Meal" ); |
| 1172 | QTest::newRow(dataTag: "2" ) << QByteArray() << 10 << 10 << QByteArray(); |
| 1173 | QTest::newRow(dataTag: "3" ) << QByteArray("hi" ) << 0 << 10 << QByteArray(); |
| 1174 | QTest::newRow(dataTag: "4" ) << QByteArray("Montreal" ) << 4 << 100 |
| 1175 | << QByteArray("Mont" ); |
| 1176 | |
| 1177 | // index out of range |
| 1178 | QTest::newRow(dataTag: "5" ) << QByteArray("Montreal" ) << 8 << 1 |
| 1179 | << QByteArray("Montreal" ); |
| 1180 | QTest::newRow(dataTag: "6" ) << QByteArray("Montreal" ) << 18 << 4 |
| 1181 | << QByteArray("Montreal" ); |
| 1182 | } |
| 1183 | |
| 1184 | void tst_QByteArray::remove() |
| 1185 | { |
| 1186 | QFETCH(QByteArray, src); |
| 1187 | QFETCH(int, position); |
| 1188 | QFETCH(int, length); |
| 1189 | QFETCH(QByteArray, expected); |
| 1190 | QCOMPARE(src.remove(position, length), expected); |
| 1191 | } |
| 1192 | |
| 1193 | void tst_QByteArray::replace_data() |
| 1194 | { |
| 1195 | QTest::addColumn<QByteArray>(name: "src" ); |
| 1196 | QTest::addColumn<int>(name: "pos" ); |
| 1197 | QTest::addColumn<int>(name: "len" ); |
| 1198 | QTest::addColumn<QByteArray>(name: "after" ); |
| 1199 | QTest::addColumn<QByteArray>(name: "expected" ); |
| 1200 | |
| 1201 | QTest::newRow(dataTag: "1" ) << QByteArray("Say yes!" ) << 4 << 3 |
| 1202 | << QByteArray("no" ) << QByteArray("Say no!" ); |
| 1203 | QTest::newRow(dataTag: "2" ) << QByteArray("rock and roll" ) << 5 << 3 |
| 1204 | << QByteArray("&" ) << QByteArray("rock & roll" ); |
| 1205 | QTest::newRow(dataTag: "3" ) << QByteArray("foo" ) << 3 << 0 << QByteArray("bar" ) |
| 1206 | << QByteArray("foobar" ); |
| 1207 | QTest::newRow(dataTag: "4" ) << QByteArray() << 0 << 0 << QByteArray() << QByteArray(); |
| 1208 | // index out of range |
| 1209 | QTest::newRow(dataTag: "5" ) << QByteArray() << 3 << 0 << QByteArray("hi" ) |
| 1210 | << QByteArray(" hi" ); |
| 1211 | // Optimized path |
| 1212 | QTest::newRow(dataTag: "6" ) << QByteArray("abcdef" ) << 3 << 12 << QByteArray("abcdefghijkl" ) << QByteArray("abcabcdefghijkl" ); |
| 1213 | QTest::newRow(dataTag: "7" ) << QByteArray("abcdef" ) << 3 << 4 << QByteArray("abcdefghijkl" ) << QByteArray("abcabcdefghijkl" ); |
| 1214 | QTest::newRow(dataTag: "8" ) << QByteArray("abcdef" ) << 3 << 3 << QByteArray("abcdefghijkl" ) << QByteArray("abcabcdefghijkl" ); |
| 1215 | QTest::newRow(dataTag: "9" ) << QByteArray("abcdef" ) << 3 << 2 << QByteArray("abcdefghijkl" ) << QByteArray("abcabcdefghijklf" ); |
| 1216 | QTest::newRow(dataTag: "10" ) << QByteArray("abcdef" ) << 2 << 2 << QByteArray("xx" ) << QByteArray("abxxef" ); |
| 1217 | } |
| 1218 | |
| 1219 | void tst_QByteArray::replace() |
| 1220 | { |
| 1221 | QFETCH(QByteArray, src); |
| 1222 | QFETCH(int, pos); |
| 1223 | QFETCH(int, len); |
| 1224 | QFETCH(QByteArray, after); |
| 1225 | QFETCH(QByteArray, expected); |
| 1226 | |
| 1227 | QByteArray str1 = src; |
| 1228 | QByteArray str2 = src; |
| 1229 | |
| 1230 | QCOMPARE(str1.replace(pos, len, after).constData(), expected.constData()); |
| 1231 | QCOMPARE(str2.replace(pos, len, after.data()), expected); |
| 1232 | } |
| 1233 | |
| 1234 | void tst_QByteArray::replaceWithSpecifiedLength() |
| 1235 | { |
| 1236 | const char after[] = "zxc\0vbnmqwert" ; |
| 1237 | int lenAfter = 6; |
| 1238 | QByteArray ba("abcdefghjk" ); |
| 1239 | ba.replace(index: 0,len: 2,s: after,alen: lenAfter); |
| 1240 | |
| 1241 | const char _expected[] = "zxc\0vbcdefghjk" ; |
| 1242 | QByteArray expected(_expected,sizeof(_expected)-1); |
| 1243 | QCOMPARE(ba,expected); |
| 1244 | } |
| 1245 | |
| 1246 | void tst_QByteArray::indexOf_data() |
| 1247 | { |
| 1248 | QTest::addColumn<QByteArray>(name: "haystack" ); |
| 1249 | QTest::addColumn<QByteArray>(name: "needle" ); |
| 1250 | QTest::addColumn<int>(name: "startpos" ); |
| 1251 | QTest::addColumn<int>(name: "expected" ); |
| 1252 | |
| 1253 | QTest::newRow( dataTag: "1" ) << QByteArray("abc" ) << QByteArray("a" ) << 0 << 0; |
| 1254 | QTest::newRow( dataTag: "2" ) << QByteArray("abc" ) << QByteArray("A" ) << 0 << -1; |
| 1255 | QTest::newRow( dataTag: "3" ) << QByteArray("abc" ) << QByteArray("a" ) << 1 << -1; |
| 1256 | QTest::newRow( dataTag: "4" ) << QByteArray("abc" ) << QByteArray("A" ) << 1 << -1; |
| 1257 | QTest::newRow( dataTag: "5" ) << QByteArray("abc" ) << QByteArray("b" ) << 0 << 1; |
| 1258 | QTest::newRow( dataTag: "6" ) << QByteArray("abc" ) << QByteArray("B" ) << 0 << -1; |
| 1259 | QTest::newRow( dataTag: "7" ) << QByteArray("abc" ) << QByteArray("b" ) << 1 << 1; |
| 1260 | QTest::newRow( dataTag: "8" ) << QByteArray("abc" ) << QByteArray("B" ) << 1 << -1; |
| 1261 | QTest::newRow( dataTag: "9" ) << QByteArray("abc" ) << QByteArray("b" ) << 2 << -1; |
| 1262 | QTest::newRow( dataTag: "10" ) << QByteArray("abc" ) << QByteArray("c" ) << 0 << 2; |
| 1263 | QTest::newRow( dataTag: "11" ) << QByteArray("abc" ) << QByteArray("C" ) << 0 << -1; |
| 1264 | QTest::newRow( dataTag: "12" ) << QByteArray("abc" ) << QByteArray("c" ) << 1 << 2; |
| 1265 | QTest::newRow( dataTag: "13" ) << QByteArray("abc" ) << QByteArray("C" ) << 1 << -1; |
| 1266 | QTest::newRow( dataTag: "14" ) << QByteArray("abc" ) << QByteArray("c" ) << 2 << 2; |
| 1267 | QTest::newRow( dataTag: "15" ) << QByteArray("aBc" ) << QByteArray("bc" ) << 0 << -1; |
| 1268 | QTest::newRow( dataTag: "16" ) << QByteArray("aBc" ) << QByteArray("Bc" ) << 0 << 1; |
| 1269 | QTest::newRow( dataTag: "17" ) << QByteArray("aBc" ) << QByteArray("bC" ) << 0 << -1; |
| 1270 | QTest::newRow( dataTag: "18" ) << QByteArray("aBc" ) << QByteArray("BC" ) << 0 << -1; |
| 1271 | |
| 1272 | static const char h19[] = {'x', 0x00, (char)0xe7, 0x25, 0x1c, 0x0a}; |
| 1273 | static const char n19[] = {0x00, 0x00, 0x01, 0x00}; |
| 1274 | QTest::newRow( dataTag: "19" ) << QByteArray(h19, sizeof(h19)) |
| 1275 | << QByteArray(n19, sizeof(n19)) << 0 << -1; |
| 1276 | |
| 1277 | QTest::newRow( dataTag: "empty" ) << QByteArray("" ) << QByteArray("x" ) << 0 << -1; |
| 1278 | QTest::newRow( dataTag: "null" ) << QByteArray() << QByteArray("x" ) << 0 << -1; |
| 1279 | QTest::newRow( dataTag: "null-in-null" ) << QByteArray() << QByteArray() << 0 << 0; |
| 1280 | QTest::newRow( dataTag: "empty-in-null" ) << QByteArray() << QByteArray("" ) << 0 << 0; |
| 1281 | QTest::newRow( dataTag: "null-in-empty" ) << QByteArray("" ) << QByteArray() << 0 << 0; |
| 1282 | QTest::newRow( dataTag: "empty-in-empty" ) << QByteArray("" ) << QByteArray("" ) << 0 << 0; |
| 1283 | |
| 1284 | QByteArray veryBigHaystack(500, 'a'); |
| 1285 | veryBigHaystack += 'B'; |
| 1286 | QTest::newRow(dataTag: "BoyerMooreStressTest" ) << veryBigHaystack << veryBigHaystack << 0 << 0; |
| 1287 | QTest::newRow(dataTag: "BoyerMooreStressTest2" ) << QByteArray(veryBigHaystack + 'c') << QByteArray(veryBigHaystack) << 0 << 0; |
| 1288 | QTest::newRow(dataTag: "BoyerMooreStressTest3" ) << QByteArray('c' + veryBigHaystack) << QByteArray(veryBigHaystack) << 0 << 1; |
| 1289 | QTest::newRow(dataTag: "BoyerMooreStressTest4" ) << QByteArray(veryBigHaystack) << QByteArray(veryBigHaystack + 'c') << 0 << -1; |
| 1290 | QTest::newRow(dataTag: "BoyerMooreStressTest5" ) << QByteArray(veryBigHaystack) << QByteArray('c' + veryBigHaystack) << 0 << -1; |
| 1291 | QTest::newRow(dataTag: "BoyerMooreStressTest6" ) << QByteArray('d' + veryBigHaystack) << QByteArray('c' + veryBigHaystack) << 0 << -1; |
| 1292 | QTest::newRow(dataTag: "BoyerMooreStressTest7" ) << QByteArray(veryBigHaystack + 'c') << QByteArray('c' + veryBigHaystack) << 0 << -1; |
| 1293 | } |
| 1294 | |
| 1295 | void tst_QByteArray::indexOf() |
| 1296 | { |
| 1297 | QFETCH( QByteArray, haystack ); |
| 1298 | QFETCH( QByteArray, needle ); |
| 1299 | QFETCH( int, startpos ); |
| 1300 | QFETCH( int, expected ); |
| 1301 | |
| 1302 | bool hasNull = needle.contains(c: '\0'); |
| 1303 | |
| 1304 | QCOMPARE( haystack.indexOf(needle, startpos), expected ); |
| 1305 | if (!hasNull) |
| 1306 | QCOMPARE( haystack.indexOf(needle.data(), startpos), expected ); |
| 1307 | if (needle.size() == 1) |
| 1308 | QCOMPARE( haystack.indexOf(needle.at(0), startpos), expected ); |
| 1309 | |
| 1310 | if (startpos == 0) { |
| 1311 | QCOMPARE( haystack.indexOf(needle), expected ); |
| 1312 | if (!hasNull) |
| 1313 | QCOMPARE( haystack.indexOf(needle.data()), expected ); |
| 1314 | if (needle.size() == 1) |
| 1315 | QCOMPARE( haystack.indexOf(needle.at(0)), expected ); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | void tst_QByteArray::lastIndexOf_data() |
| 1320 | { |
| 1321 | QTest::addColumn<QByteArray>(name: "haystack" ); |
| 1322 | QTest::addColumn<QByteArray>(name: "needle" ); |
| 1323 | QTest::addColumn<int>(name: "startpos" ); |
| 1324 | QTest::addColumn<int>(name: "expected" ); |
| 1325 | |
| 1326 | QTest::newRow( dataTag: "1" ) << QByteArray("abc" ) << QByteArray("a" ) << 0 << 0; |
| 1327 | QTest::newRow( dataTag: "2" ) << QByteArray("abc" ) << QByteArray("A" ) << 0 << -1; |
| 1328 | QTest::newRow( dataTag: "3" ) << QByteArray("abc" ) << QByteArray("a" ) << 1 << 0; |
| 1329 | QTest::newRow( dataTag: "4" ) << QByteArray("abc" ) << QByteArray("A" ) << 1 << -1; |
| 1330 | QTest::newRow( dataTag: "5" ) << QByteArray("abc" ) << QByteArray("a" ) << -1 << 0; |
| 1331 | QTest::newRow( dataTag: "6" ) << QByteArray("abc" ) << QByteArray("b" ) << 0 << -1; |
| 1332 | QTest::newRow( dataTag: "7" ) << QByteArray("abc" ) << QByteArray("B" ) << 0 << -1; |
| 1333 | QTest::newRow( dataTag: "8" ) << QByteArray("abc" ) << QByteArray("b" ) << 1 << 1; |
| 1334 | QTest::newRow( dataTag: "9" ) << QByteArray("abc" ) << QByteArray("B" ) << 1 << -1; |
| 1335 | QTest::newRow( dataTag: "10" ) << QByteArray("abc" ) << QByteArray("b" ) << 2 << 1; |
| 1336 | QTest::newRow( dataTag: "11" ) << QByteArray("abc" ) << QByteArray("b" ) << -1 << 1; |
| 1337 | QTest::newRow( dataTag: "12" ) << QByteArray("abc" ) << QByteArray("c" ) << 0 << -1; |
| 1338 | QTest::newRow( dataTag: "13" ) << QByteArray("abc" ) << QByteArray("C" ) << 0 << -1; |
| 1339 | QTest::newRow( dataTag: "14" ) << QByteArray("abc" ) << QByteArray("c" ) << 1 << -1; |
| 1340 | QTest::newRow( dataTag: "15" ) << QByteArray("abc" ) << QByteArray("C" ) << 1 << -1; |
| 1341 | QTest::newRow( dataTag: "16" ) << QByteArray("abc" ) << QByteArray("c" ) << 2 << 2; |
| 1342 | QTest::newRow( dataTag: "17" ) << QByteArray("abc" ) << QByteArray("c" ) << -1 << 2; |
| 1343 | QTest::newRow( dataTag: "18" ) << QByteArray("aBc" ) << QByteArray("bc" ) << 0 << -1; |
| 1344 | QTest::newRow( dataTag: "19" ) << QByteArray("aBc" ) << QByteArray("Bc" ) << 0 << -1; |
| 1345 | QTest::newRow( dataTag: "20" ) << QByteArray("aBc" ) << QByteArray("Bc" ) << 2 << 1; |
| 1346 | QTest::newRow( dataTag: "21" ) << QByteArray("aBc" ) << QByteArray("Bc" ) << 1 << 1; |
| 1347 | QTest::newRow( dataTag: "22" ) << QByteArray("aBc" ) << QByteArray("Bc" ) << -1 << 1; |
| 1348 | QTest::newRow( dataTag: "23" ) << QByteArray("aBc" ) << QByteArray("bC" ) << 0 << -1; |
| 1349 | QTest::newRow( dataTag: "24" ) << QByteArray("aBc" ) << QByteArray("BC" ) << 0 << -1; |
| 1350 | |
| 1351 | static const char h25[] = {0x00, (char)0xbc, 0x03, 0x10, 0x0a }; |
| 1352 | static const char n25[] = {0x00, 0x00, 0x01, 0x00}; |
| 1353 | QTest::newRow( dataTag: "25" ) << QByteArray(h25, sizeof(h25)) |
| 1354 | << QByteArray(n25, sizeof(n25)) << 0 << -1; |
| 1355 | |
| 1356 | QTest::newRow( dataTag: "empty" ) << QByteArray("" ) << QByteArray("x" ) << -1 << -1; |
| 1357 | QTest::newRow( dataTag: "null" ) << QByteArray() << QByteArray("x" ) << -1 << -1; |
| 1358 | QTest::newRow( dataTag: "null-in-null" ) << QByteArray() << QByteArray() << -1 << 0; |
| 1359 | QTest::newRow( dataTag: "empty-in-null" ) << QByteArray() << QByteArray("" ) << -1 << 0; |
| 1360 | QTest::newRow( dataTag: "null-in-empty" ) << QByteArray("" ) << QByteArray() << -1 << 0; |
| 1361 | QTest::newRow( dataTag: "empty-in-empty" ) << QByteArray("" ) << QByteArray("" ) << -1 << 0; |
| 1362 | } |
| 1363 | |
| 1364 | void tst_QByteArray::lastIndexOf() |
| 1365 | { |
| 1366 | QFETCH( QByteArray, haystack ); |
| 1367 | QFETCH( QByteArray, needle ); |
| 1368 | QFETCH( int, startpos ); |
| 1369 | QFETCH( int, expected ); |
| 1370 | |
| 1371 | bool hasNull = needle.contains(c: '\0'); |
| 1372 | |
| 1373 | QCOMPARE( haystack.lastIndexOf(needle, startpos), expected ); |
| 1374 | if (!hasNull) |
| 1375 | QCOMPARE( haystack.lastIndexOf(needle.data(), startpos), expected ); |
| 1376 | if (needle.size() == 1) |
| 1377 | QCOMPARE( haystack.lastIndexOf(needle.at(0), startpos), expected ); |
| 1378 | |
| 1379 | if (startpos == -1) { |
| 1380 | QCOMPARE( haystack.lastIndexOf(needle), expected ); |
| 1381 | if (!hasNull) |
| 1382 | QCOMPARE( haystack.lastIndexOf(needle.data()), expected ); |
| 1383 | if (needle.size() == 1) |
| 1384 | QCOMPARE( haystack.lastIndexOf(needle.at(0)), expected ); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | void tst_QByteArray::number() |
| 1389 | { |
| 1390 | QCOMPARE(QString(QByteArray::number((quint64) 0)), |
| 1391 | QString(QByteArray("0" ))); |
| 1392 | QCOMPARE(QString(QByteArray::number(Q_UINT64_C(0xFFFFFFFFFFFFFFFF))), |
| 1393 | QString(QByteArray("18446744073709551615" ))); |
| 1394 | QCOMPARE(QString(QByteArray::number(Q_INT64_C(0xFFFFFFFFFFFFFFFF))), |
| 1395 | QString(QByteArray("-1" ))); |
| 1396 | QCOMPARE(QString(QByteArray::number(qint64(0))), |
| 1397 | QString(QByteArray("0" ))); |
| 1398 | QCOMPARE(QString(QByteArray::number(Q_INT64_C(0x7FFFFFFFFFFFFFFF))), |
| 1399 | QString(QByteArray("9223372036854775807" ))); |
| 1400 | QCOMPARE(QString(QByteArray::number(Q_INT64_C(0x8000000000000000))), |
| 1401 | QString(QByteArray("-9223372036854775808" ))); |
| 1402 | } |
| 1403 | |
| 1404 | // defined later |
| 1405 | extern const char globalChar; |
| 1406 | |
| 1407 | void tst_QByteArray::toInt_data() |
| 1408 | { |
| 1409 | QTest::addColumn<QByteArray>(name: "string" ); |
| 1410 | QTest::addColumn<int>(name: "base" ); |
| 1411 | QTest::addColumn<int>(name: "expectednumber" ); |
| 1412 | QTest::addColumn<bool>(name: "expectedok" ); |
| 1413 | |
| 1414 | QTest::newRow(dataTag: "base 10" ) << QByteArray("100" ) << 10 << int(100) << true; |
| 1415 | QTest::newRow(dataTag: "base 16-1" ) << QByteArray("100" ) << 16 << int(256) << true; |
| 1416 | QTest::newRow(dataTag: "base 16-2" ) << QByteArray("0400" ) << 16 << int(1024) << true; |
| 1417 | QTest::newRow(dataTag: "base 2" ) << QByteArray("1111" ) << 2 << int(15) << true; |
| 1418 | QTest::newRow(dataTag: "base 8" ) << QByteArray("100" ) << 8 << int(64) << true; |
| 1419 | QTest::newRow(dataTag: "base 0-1" ) << QByteArray("0x10" ) << 0 << int(16) << true; |
| 1420 | QTest::newRow(dataTag: "base 0-2" ) << QByteArray("10" ) << 0 << int(10) << true; |
| 1421 | QTest::newRow(dataTag: "base 0-3" ) << QByteArray("010" ) << 0 << int(8) << true; |
| 1422 | QTest::newRow(dataTag: "empty" ) << QByteArray() << 0 << int(0) << false; |
| 1423 | |
| 1424 | QTest::newRow(dataTag: "leading space" ) << QByteArray(" 100" ) << 10 << int(100) << true; |
| 1425 | QTest::newRow(dataTag: "trailing space" ) << QByteArray("100 " ) << 10 << int(100) << true; |
| 1426 | QTest::newRow(dataTag: "leading junk" ) << QByteArray("x100" ) << 10 << int(0) << false; |
| 1427 | QTest::newRow(dataTag: "trailing junk" ) << QByteArray("100x" ) << 10 << int(0) << false; |
| 1428 | |
| 1429 | // using fromRawData |
| 1430 | QTest::newRow(dataTag: "raw1" ) << QByteArray::fromRawData("1" , size: 1) << 10 << 1 << true; |
| 1431 | QTest::newRow(dataTag: "raw2" ) << QByteArray::fromRawData("1foo" , size: 1) << 10 << 1 << true; |
| 1432 | QTest::newRow(dataTag: "raw3" ) << QByteArray::fromRawData("12" , size: 1) << 10 << 1 << true; |
| 1433 | QTest::newRow(dataTag: "raw4" ) << QByteArray::fromRawData("123456789" , size: 1) << 10 << 1 << true; |
| 1434 | QTest::newRow(dataTag: "raw5" ) << QByteArray::fromRawData("123456789" , size: 2) << 10 << 12 << true; |
| 1435 | |
| 1436 | QTest::newRow(dataTag: "raw-static" ) << QByteArray::fromRawData(&globalChar, size: 1) << 10 << 1 << true; |
| 1437 | } |
| 1438 | |
| 1439 | void tst_QByteArray::toInt() |
| 1440 | { |
| 1441 | QFETCH( QByteArray, string ); |
| 1442 | QFETCH( int, base ); |
| 1443 | QFETCH( int, expectednumber ); |
| 1444 | QFETCH( bool, expectedok ); |
| 1445 | |
| 1446 | bool ok; |
| 1447 | int number = string.toInt(ok: &ok, base); |
| 1448 | |
| 1449 | QCOMPARE( ok, expectedok ); |
| 1450 | QCOMPARE( number, expectednumber ); |
| 1451 | } |
| 1452 | |
| 1453 | void tst_QByteArray::toDouble_data() |
| 1454 | { |
| 1455 | QTest::addColumn<QByteArray>(name: "string" ); |
| 1456 | QTest::addColumn<double>(name: "expectedNumber" ); |
| 1457 | QTest::addColumn<bool>(name: "expectedOk" ); |
| 1458 | |
| 1459 | QTest::newRow(dataTag: "decimal" ) << QByteArray("1.2345" ) << 1.2345 << true; |
| 1460 | QTest::newRow(dataTag: "exponent lowercase" ) << QByteArray("1.2345e+01" ) << 12.345 << true; |
| 1461 | QTest::newRow(dataTag: "exponent uppercase" ) << QByteArray("1.2345E+02" ) << 123.45 << true; |
| 1462 | QTest::newRow(dataTag: "leading spaces" ) << QByteArray(" \n\r\t1.2345" ) << 1.2345 << true; |
| 1463 | QTest::newRow(dataTag: "trailing spaces" ) << QByteArray("1.2345 \n\r\t" ) << 1.2345 << true; |
| 1464 | QTest::newRow(dataTag: "leading junk" ) << QByteArray("x1.2345" ) << 0.0 << false; |
| 1465 | QTest::newRow(dataTag: "trailing junk" ) << QByteArray("1.2345x" ) << 0.0 << false; |
| 1466 | |
| 1467 | QTest::newRow(dataTag: "raw, null plus junk" ) << QByteArray::fromRawData("1.2\0 junk" , size: 9) << 0.0 << false; |
| 1468 | QTest::newRow(dataTag: "raw, null-terminator not included" ) << QByteArray::fromRawData("2.3" , size: 3) << 2.3 << true; |
| 1469 | } |
| 1470 | |
| 1471 | void tst_QByteArray::toDouble() |
| 1472 | { |
| 1473 | QFETCH(QByteArray, string); |
| 1474 | QFETCH(double, expectedNumber); |
| 1475 | QFETCH(bool, expectedOk); |
| 1476 | |
| 1477 | bool ok; |
| 1478 | const double number = string.toDouble(ok: &ok); |
| 1479 | |
| 1480 | QCOMPARE(ok, expectedOk); |
| 1481 | QCOMPARE(number, expectedNumber); |
| 1482 | } |
| 1483 | |
| 1484 | void tst_QByteArray::toULong_data() |
| 1485 | { |
| 1486 | QTest::addColumn<QByteArray>(name: "str" ); |
| 1487 | QTest::addColumn<int>(name: "base" ); |
| 1488 | QTest::addColumn<ulong>(name: "result" ); |
| 1489 | QTest::addColumn<bool>(name: "ok" ); |
| 1490 | |
| 1491 | ulong LongMaxPlusOne = (ulong)LONG_MAX + 1; |
| 1492 | QTest::newRow(dataTag: "LONG_MAX+1" ) << QString::number(LongMaxPlusOne).toLatin1() << 10 << LongMaxPlusOne << true; |
| 1493 | QTest::newRow(dataTag: "default" ) << QByteArray() << 10 << 0UL << false; |
| 1494 | QTest::newRow(dataTag: "empty" ) << QByteArray("" ) << 10 << 0UL << false; |
| 1495 | QTest::newRow(dataTag: "ulong1" ) << QByteArray("3234567890" ) << 10 << 3234567890UL << true; |
| 1496 | QTest::newRow(dataTag: "ulong2" ) << QByteArray("fFFfFfFf" ) << 16 << 0xFFFFFFFFUL << true; |
| 1497 | |
| 1498 | QTest::newRow(dataTag: "leading spaces" ) << QByteArray(" \n\r\t100" ) << 10 << 100UL << true; |
| 1499 | QTest::newRow(dataTag: "trailing spaces" ) << QByteArray("100 \n\r\t" ) << 10 << 100UL << true; |
| 1500 | QTest::newRow(dataTag: "leading junk" ) << QByteArray("x100" ) << 10 << 0UL << false; |
| 1501 | QTest::newRow(dataTag: "trailing junk" ) << QByteArray("100x" ) << 10 << 0UL << false; |
| 1502 | } |
| 1503 | |
| 1504 | void tst_QByteArray::toULong() |
| 1505 | { |
| 1506 | QFETCH(QByteArray, str); |
| 1507 | QFETCH(int, base); |
| 1508 | QFETCH(ulong, result); |
| 1509 | QFETCH(bool, ok); |
| 1510 | |
| 1511 | bool b; |
| 1512 | QCOMPARE(str.toULong(0, base), result); |
| 1513 | QCOMPARE(str.toULong(&b, base), result); |
| 1514 | QCOMPARE(b, ok); |
| 1515 | } |
| 1516 | |
| 1517 | void tst_QByteArray::toULongLong_data() |
| 1518 | { |
| 1519 | QTest::addColumn<QByteArray>(name: "str" ); |
| 1520 | QTest::addColumn<int>(name: "base" ); |
| 1521 | QTest::addColumn<qulonglong>(name: "result" ); |
| 1522 | QTest::addColumn<bool>(name: "ok" ); |
| 1523 | |
| 1524 | QTest::newRow(dataTag: "default" ) << QByteArray() << 10 << (qulonglong)0 << false; |
| 1525 | QTest::newRow(dataTag: "out of base bound" ) << QByteArray("c" ) << 10 << (qulonglong)0 << false; |
| 1526 | |
| 1527 | QTest::newRow(dataTag: "leading spaces" ) << QByteArray(" \n\r\t100" ) << 10 << qulonglong(100) << true; |
| 1528 | QTest::newRow(dataTag: "trailing spaces" ) << QByteArray("100 \n\r\t" ) << 10 << qulonglong(100) << true; |
| 1529 | QTest::newRow(dataTag: "leading junk" ) << QByteArray("x100" ) << 10 << qulonglong(0) << false; |
| 1530 | QTest::newRow(dataTag: "trailing junk" ) << QByteArray("100x" ) << 10 << qulonglong(0) << false; |
| 1531 | } |
| 1532 | |
| 1533 | void tst_QByteArray::toULongLong() |
| 1534 | { |
| 1535 | QFETCH(QByteArray, str); |
| 1536 | QFETCH(int, base); |
| 1537 | QFETCH(qulonglong, result); |
| 1538 | QFETCH(bool, ok); |
| 1539 | |
| 1540 | bool b; |
| 1541 | QCOMPARE(str.toULongLong(0, base), result); |
| 1542 | QCOMPARE(str.toULongLong(&b, base), result); |
| 1543 | QCOMPARE(b, ok); |
| 1544 | } |
| 1545 | |
| 1546 | static bool checkSize(size_t value, size_t min) |
| 1547 | { |
| 1548 | return value >= min && value <= INT_MAX; |
| 1549 | } |
| 1550 | |
| 1551 | // global functions defined in qbytearray.cpp |
| 1552 | void tst_QByteArray::blockSizeCalculations() |
| 1553 | { |
| 1554 | // Not very important, but please behave :-) |
| 1555 | QCOMPARE(qCalculateBlockSize(0, 1), size_t(0)); |
| 1556 | QVERIFY(qCalculateGrowingBlockSize(0, 1).size <= MaxAllocSize); |
| 1557 | QVERIFY(qCalculateGrowingBlockSize(0, 1).elementCount <= MaxAllocSize); |
| 1558 | |
| 1559 | // boundary condition |
| 1560 | QCOMPARE(qCalculateBlockSize(MaxAllocSize, 1), size_t(MaxAllocSize)); |
| 1561 | QCOMPARE(qCalculateBlockSize(MaxAllocSize/2, 2), size_t(MaxAllocSize) - 1); |
| 1562 | QCOMPARE(qCalculateBlockSize(MaxAllocSize/2, 2, 1), size_t(MaxAllocSize)); |
| 1563 | QCOMPARE(qCalculateGrowingBlockSize(MaxAllocSize, 1).size, size_t(MaxAllocSize)); |
| 1564 | QCOMPARE(qCalculateGrowingBlockSize(MaxAllocSize, 1).elementCount, size_t(MaxAllocSize)); |
| 1565 | QCOMPARE(qCalculateGrowingBlockSize(MaxAllocSize/2, 2, 1).size, size_t(MaxAllocSize)); |
| 1566 | QCOMPARE(qCalculateGrowingBlockSize(MaxAllocSize/2, 2, 1).elementCount, size_t(MaxAllocSize)/2); |
| 1567 | |
| 1568 | // error conditions |
| 1569 | QCOMPARE(qCalculateBlockSize(uint(MaxAllocSize) + 1, 1), size_t(~0)); |
| 1570 | QCOMPARE(qCalculateBlockSize(size_t(-1), 1), size_t(~0)); |
| 1571 | QCOMPARE(qCalculateBlockSize(MaxAllocSize, 1, 1), size_t(~0)); |
| 1572 | QCOMPARE(qCalculateBlockSize(MaxAllocSize/2 + 1, 2), size_t(~0)); |
| 1573 | QCOMPARE(qCalculateGrowingBlockSize(uint(MaxAllocSize) + 1, 1).size, size_t(~0)); |
| 1574 | QCOMPARE(qCalculateGrowingBlockSize(MaxAllocSize/2 + 1, 2).size, size_t(~0)); |
| 1575 | |
| 1576 | // overflow conditions |
| 1577 | // on 32-bit platforms, (1 << 16) * (1 << 16) = (1 << 32) which is zero |
| 1578 | QCOMPARE(qCalculateBlockSize(1 << 16, 1 << 16), size_t(~0)); |
| 1579 | QCOMPARE(qCalculateBlockSize(MaxAllocSize/4, 16), size_t(~0)); |
| 1580 | // on 32-bit platforms, (1 << 30) * 3 + (1 << 30) would overflow to zero |
| 1581 | QCOMPARE(qCalculateBlockSize(1U << 30, 3, 1U << 30), size_t(~0)); |
| 1582 | |
| 1583 | // exact block sizes |
| 1584 | for (int i = 1; i < 1 << 31; i <<= 1) { |
| 1585 | QCOMPARE(qCalculateBlockSize(0, 1, i), size_t(i)); |
| 1586 | QCOMPARE(qCalculateBlockSize(i, 1), size_t(i)); |
| 1587 | QCOMPARE(qCalculateBlockSize(i + i/2, 1), size_t(i + i/2)); |
| 1588 | } |
| 1589 | for (int i = 1; i < 1 << 30; i <<= 1) { |
| 1590 | QCOMPARE(qCalculateBlockSize(i, 2), 2 * size_t(i)); |
| 1591 | QCOMPARE(qCalculateBlockSize(i, 2, 1), 2 * size_t(i) + 1); |
| 1592 | QCOMPARE(qCalculateBlockSize(i, 2, 16), 2 * size_t(i) + 16); |
| 1593 | } |
| 1594 | |
| 1595 | // growing sizes |
| 1596 | for (int i = 1; i < 1 << 31; i <<= 1) { |
| 1597 | QVERIFY(checkSize(qCalculateGrowingBlockSize(i, 1).size, i)); |
| 1598 | QVERIFY(checkSize(qCalculateGrowingBlockSize(i, 1).elementCount, i)); |
| 1599 | QVERIFY(checkSize(qCalculateGrowingBlockSize(i, 1, 16).size, i)); |
| 1600 | QVERIFY(checkSize(qCalculateGrowingBlockSize(i, 1, 16).elementCount, i)); |
| 1601 | QVERIFY(checkSize(qCalculateGrowingBlockSize(i, 1, 24).size, i)); |
| 1602 | QVERIFY(checkSize(qCalculateGrowingBlockSize(i, 1, 16).elementCount, i)); |
| 1603 | } |
| 1604 | |
| 1605 | // growth should be limited |
| 1606 | for (int elementSize = 1; elementSize < (1<<8); elementSize <<= 1) { |
| 1607 | size_t alloc = 1; |
| 1608 | forever { |
| 1609 | QVERIFY(checkSize(qCalculateGrowingBlockSize(alloc, elementSize).size, alloc * elementSize)); |
| 1610 | size_t newAlloc = qCalculateGrowingBlockSize(elementCount: alloc, elementSize).elementCount; |
| 1611 | QVERIFY(checkSize(newAlloc, alloc)); |
| 1612 | if (newAlloc == alloc) |
| 1613 | break; // no growth, we're at limit |
| 1614 | alloc = newAlloc; |
| 1615 | } |
| 1616 | QVERIFY(checkSize(alloc, size_t(MaxAllocSize) / elementSize)); |
| 1617 | |
| 1618 | // the next allocation should be invalid |
| 1619 | QCOMPARE(qCalculateGrowingBlockSize(alloc + 1, elementSize).size, size_t(~0)); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | void tst_QByteArray::resizeAfterFromRawData() |
| 1624 | { |
| 1625 | QByteArray buffer("hello world" ); |
| 1626 | |
| 1627 | QByteArray array = QByteArray::fromRawData(buffer.constData(), size: buffer.size()); |
| 1628 | QVERIFY(array.constData() == buffer.constData()); |
| 1629 | array.resize(size: 5); |
| 1630 | QVERIFY(array.constData() == buffer.constData()); |
| 1631 | } |
| 1632 | |
| 1633 | void tst_QByteArray::appendAfterFromRawData() |
| 1634 | { |
| 1635 | QByteArray arr; |
| 1636 | { |
| 1637 | char data[] = "X" ; |
| 1638 | arr += QByteArray::fromRawData(data, size: sizeof(data)); |
| 1639 | data[0] = 'Y'; |
| 1640 | } |
| 1641 | QCOMPARE(arr.at(0), 'X'); |
| 1642 | } |
| 1643 | |
| 1644 | void tst_QByteArray::toFromHex_data() |
| 1645 | { |
| 1646 | QTest::addColumn<QByteArray>(name: "str" ); |
| 1647 | QTest::addColumn<char>(name: "sep" ); |
| 1648 | QTest::addColumn<QByteArray>(name: "hex" ); |
| 1649 | QTest::addColumn<QByteArray>(name: "hex_alt1" ); |
| 1650 | |
| 1651 | QTest::newRow(dataTag: "Qt is great! (default)" ) |
| 1652 | << QByteArray("Qt is great!" ) |
| 1653 | << '\0' |
| 1654 | << QByteArray("517420697320677265617421" ) |
| 1655 | << QByteArray("51 74 20 69 73 20 67 72 65 61 74 21" ); |
| 1656 | |
| 1657 | QTest::newRow(dataTag: "Qt is great! (with space)" ) |
| 1658 | << QByteArray("Qt is great!" ) |
| 1659 | << ' ' |
| 1660 | << QByteArray("51 74 20 69 73 20 67 72 65 61 74 21" ) |
| 1661 | << QByteArray("51 74 20 69 73 20 67 72 65 61 74 21" ); |
| 1662 | |
| 1663 | QTest::newRow(dataTag: "Qt is great! (with minus)" ) |
| 1664 | << QByteArray("Qt is great!" ) |
| 1665 | << '-' |
| 1666 | << QByteArray("51-74-20-69-73-20-67-72-65-61-74-21" ) |
| 1667 | << QByteArray("51-74-20-69-73-20-67-72-65-61-74-21" ); |
| 1668 | |
| 1669 | QTest::newRow(dataTag: "Qt is so great!" ) |
| 1670 | << QByteArray("Qt is so great!" ) |
| 1671 | << '\0' |
| 1672 | << QByteArray("517420697320736f20677265617421" ) |
| 1673 | << QByteArray("51 74 20 69 73 20 73 6f 20 67 72 65 61 74 21" ); |
| 1674 | |
| 1675 | QTest::newRow(dataTag: "default-constructed" ) |
| 1676 | << QByteArray() |
| 1677 | << '\0' |
| 1678 | << QByteArray() |
| 1679 | << QByteArray(); |
| 1680 | |
| 1681 | QTest::newRow(dataTag: "default-constructed (with space)" ) |
| 1682 | << QByteArray() |
| 1683 | << ' ' |
| 1684 | << QByteArray() |
| 1685 | << QByteArray(); |
| 1686 | |
| 1687 | QTest::newRow(dataTag: "empty" ) |
| 1688 | << QByteArray("" ) |
| 1689 | << '\0' |
| 1690 | << QByteArray("" ) |
| 1691 | << QByteArray("" ); |
| 1692 | |
| 1693 | QTest::newRow(dataTag: "empty (with space)" ) |
| 1694 | << QByteArray("" ) |
| 1695 | << ' ' |
| 1696 | << QByteArray("" ) |
| 1697 | << QByteArray("" ); |
| 1698 | |
| 1699 | QTest::newRow(dataTag: "array-of-null" ) |
| 1700 | << QByteArray("\0" , 1) |
| 1701 | << '\0' |
| 1702 | << QByteArray("00" ) |
| 1703 | << QByteArray("0" ); |
| 1704 | |
| 1705 | QTest::newRow(dataTag: "no-leading-zero" ) |
| 1706 | << QByteArray("\xf" ) |
| 1707 | << '\0' |
| 1708 | << QByteArray("0f" ) |
| 1709 | << QByteArray("f" ); |
| 1710 | |
| 1711 | QTest::newRow(dataTag: "single-byte" ) |
| 1712 | << QByteArray("\xaf" ) |
| 1713 | << '\0' |
| 1714 | << QByteArray("af" ) |
| 1715 | << QByteArray("xaf" ); |
| 1716 | |
| 1717 | QTest::newRow(dataTag: "no-leading-zero" ) |
| 1718 | << QByteArray("\xd\xde\xad\xc0\xde" ) |
| 1719 | << '\0' |
| 1720 | << QByteArray("0ddeadc0de" ) |
| 1721 | << QByteArray("ddeadc0de" ); |
| 1722 | |
| 1723 | QTest::newRow(dataTag: "garbage" ) |
| 1724 | << QByteArray("\xC\xde\xeC\xea\xee\xDe\xee\xee" ) |
| 1725 | << '\0' |
| 1726 | << QByteArray("0cdeeceaeedeeeee" ) |
| 1727 | << QByteArray("Code less. Create more. Deploy everywhere." ); |
| 1728 | |
| 1729 | QTest::newRow(dataTag: "under-defined-1" ) |
| 1730 | << QByteArray("\x1\x23" ) |
| 1731 | << '\0' |
| 1732 | << QByteArray("0123" ) |
| 1733 | << QByteArray("x123" ); |
| 1734 | |
| 1735 | QTest::newRow(dataTag: "under-defined-2" ) |
| 1736 | << QByteArray("\x12\x34" ) |
| 1737 | << '\0' |
| 1738 | << QByteArray("1234" ) |
| 1739 | << QByteArray("x1234" ); |
| 1740 | } |
| 1741 | |
| 1742 | void tst_QByteArray::toFromHex() |
| 1743 | { |
| 1744 | QFETCH(QByteArray, str); |
| 1745 | QFETCH(char, sep); |
| 1746 | QFETCH(QByteArray, hex); |
| 1747 | QFETCH(QByteArray, hex_alt1); |
| 1748 | |
| 1749 | if (sep == 0) { |
| 1750 | const QByteArray th = str.toHex(); |
| 1751 | QCOMPARE(th.size(), hex.size()); |
| 1752 | QCOMPARE(th, hex); |
| 1753 | } |
| 1754 | |
| 1755 | { |
| 1756 | const QByteArray th = str.toHex(separator: sep); |
| 1757 | QCOMPARE(th.size(), hex.size()); |
| 1758 | QCOMPARE(th, hex); |
| 1759 | } |
| 1760 | |
| 1761 | { |
| 1762 | const QByteArray fh = QByteArray::fromHex(hexEncoded: hex); |
| 1763 | QCOMPARE(fh.size(), str.size()); |
| 1764 | QCOMPARE(fh, str); |
| 1765 | } |
| 1766 | |
| 1767 | QCOMPARE(QByteArray::fromHex(hex_alt1), str); |
| 1768 | } |
| 1769 | |
| 1770 | void tst_QByteArray::toFromPercentEncoding() |
| 1771 | { |
| 1772 | QByteArray arr("Qt is great!" ); |
| 1773 | |
| 1774 | QByteArray data = arr.toPercentEncoding(); |
| 1775 | QCOMPARE(QString(data), QString("Qt%20is%20great%21" )); |
| 1776 | QCOMPARE(QByteArray::fromPercentEncoding(data), arr); |
| 1777 | |
| 1778 | data = arr.toPercentEncoding(exclude: "! " , include: "Qt" ); |
| 1779 | QCOMPARE(QString(data), QString("%51%74 is grea%74!" )); |
| 1780 | QCOMPARE(QByteArray::fromPercentEncoding(data), arr); |
| 1781 | |
| 1782 | data = arr.toPercentEncoding(exclude: QByteArray(), include: "abcdefghijklmnopqrstuvwxyz" , percent: 'Q'); |
| 1783 | QCOMPARE(QString(data), QString("Q51Q74Q20Q69Q73Q20Q67Q72Q65Q61Q74Q21" )); |
| 1784 | QCOMPARE(QByteArray::fromPercentEncoding(data, 'Q'), arr); |
| 1785 | |
| 1786 | // verify that to/from percent encoding preserves nullity |
| 1787 | arr = "" ; |
| 1788 | QVERIFY(arr.isEmpty()); |
| 1789 | QVERIFY(!arr.isNull()); |
| 1790 | QVERIFY(arr.toPercentEncoding().isEmpty()); |
| 1791 | QVERIFY(!arr.toPercentEncoding().isNull()); |
| 1792 | QVERIFY(QByteArray::fromPercentEncoding("" ).isEmpty()); |
| 1793 | QVERIFY(!QByteArray::fromPercentEncoding("" ).isNull()); |
| 1794 | |
| 1795 | arr = QByteArray(); |
| 1796 | QVERIFY(arr.isEmpty()); |
| 1797 | QVERIFY(arr.isNull()); |
| 1798 | QVERIFY(arr.toPercentEncoding().isEmpty()); |
| 1799 | QVERIFY(arr.toPercentEncoding().isNull()); |
| 1800 | QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isEmpty()); |
| 1801 | QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isNull()); |
| 1802 | } |
| 1803 | |
| 1804 | void tst_QByteArray::fromPercentEncoding_data() |
| 1805 | { |
| 1806 | QTest::addColumn<QByteArray>(name: "encodedString" ); |
| 1807 | QTest::addColumn<QByteArray>(name: "decodedString" ); |
| 1808 | |
| 1809 | QTest::newRow(dataTag: "NormalString" ) << QByteArray("filename" ) << QByteArray("filename" ); |
| 1810 | QTest::newRow(dataTag: "NormalStringEncoded" ) << QByteArray("file%20name" ) << QByteArray("file name" ); |
| 1811 | QTest::newRow(dataTag: "JustEncoded" ) << QByteArray("%20" ) << QByteArray(" " ); |
| 1812 | QTest::newRow(dataTag: "HTTPUrl" ) << QByteArray("http://qt-project.org" ) << QByteArray("http://qt-project.org" ); |
| 1813 | QTest::newRow(dataTag: "HTTPUrlEncoded" ) << QByteArray("http://qt-project%20org" ) << QByteArray("http://qt-project org" ); |
| 1814 | QTest::newRow(dataTag: "EmptyString" ) << QByteArray("" ) << QByteArray("" ); |
| 1815 | QTest::newRow(dataTag: "Task27166" ) << QByteArray("Fran%C3%A7aise" ) << QByteArray("Française" ); |
| 1816 | } |
| 1817 | |
| 1818 | void tst_QByteArray::fromPercentEncoding() |
| 1819 | { |
| 1820 | QFETCH(QByteArray, encodedString); |
| 1821 | QFETCH(QByteArray, decodedString); |
| 1822 | |
| 1823 | QCOMPARE(QByteArray::fromPercentEncoding(encodedString), decodedString); |
| 1824 | } |
| 1825 | |
| 1826 | void tst_QByteArray::toPercentEncoding_data() |
| 1827 | { |
| 1828 | QTest::addColumn<QByteArray>(name: "decodedString" ); |
| 1829 | QTest::addColumn<QByteArray>(name: "encodedString" ); |
| 1830 | |
| 1831 | QTest::newRow(dataTag: "NormalString" ) << QByteArray("filename" ) << QByteArray("filename" ); |
| 1832 | QTest::newRow(dataTag: "NormalStringEncoded" ) << QByteArray("file name" ) << QByteArray("file%20name" ); |
| 1833 | QTest::newRow(dataTag: "JustEncoded" ) << QByteArray(" " ) << QByteArray("%20" ); |
| 1834 | QTest::newRow(dataTag: "HTTPUrl" ) << QByteArray("http://qt-project.org" ) << QByteArray("http%3A//qt-project.org" ); |
| 1835 | QTest::newRow(dataTag: "HTTPUrlEncoded" ) << QByteArray("http://qt-project org" ) << QByteArray("http%3A//qt-project%20org" ); |
| 1836 | QTest::newRow(dataTag: "EmptyString" ) << QByteArray("" ) << QByteArray("" ); |
| 1837 | QTest::newRow(dataTag: "Task27166" ) << QByteArray("Française" ) << QByteArray("Fran%C3%A7aise" ); |
| 1838 | } |
| 1839 | |
| 1840 | void tst_QByteArray::toPercentEncoding() |
| 1841 | { |
| 1842 | QFETCH(QByteArray, decodedString); |
| 1843 | QFETCH(QByteArray, encodedString); |
| 1844 | |
| 1845 | QCOMPARE(decodedString.toPercentEncoding("/." ).constData(), encodedString.constData()); |
| 1846 | } |
| 1847 | |
| 1848 | void tst_QByteArray::toPercentEncoding2_data() |
| 1849 | { |
| 1850 | QTest::addColumn<QByteArray>(name: "original" ); |
| 1851 | QTest::addColumn<QByteArray>(name: "encoded" ); |
| 1852 | QTest::addColumn<QByteArray>(name: "excludeInEncoding" ); |
| 1853 | QTest::addColumn<QByteArray>(name: "includeInEncoding" ); |
| 1854 | |
| 1855 | QTest::newRow(dataTag: "test_01" ) << QByteArray("abcdevghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678-._~" ) |
| 1856 | << QByteArray("abcdevghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678-._~" ) |
| 1857 | << QByteArray("" ) |
| 1858 | << QByteArray("" ); |
| 1859 | QTest::newRow(dataTag: "test_02" ) << QByteArray("{\t\n\r^\"abc}" ) |
| 1860 | << QByteArray("%7B%09%0A%0D%5E%22abc%7D" ) |
| 1861 | << QByteArray("" ) |
| 1862 | << QByteArray("" ); |
| 1863 | QTest::newRow(dataTag: "test_03" ) << QByteArray("://?#[]@!$&'()*+,;=" ) |
| 1864 | << QByteArray("%3A%2F%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D" ) |
| 1865 | << QByteArray("" ) |
| 1866 | << QByteArray("" ); |
| 1867 | QTest::newRow(dataTag: "test_04" ) << QByteArray("://?#[]@!$&'()*+,;=" ) |
| 1868 | << QByteArray("%3A%2F%2F%3F%23%5B%5D%40!$&'()*+,;=" ) |
| 1869 | << QByteArray("!$&'()*+,;=" ) |
| 1870 | << QByteArray("" ); |
| 1871 | QTest::newRow(dataTag: "test_05" ) << QByteArray("abcd" ) |
| 1872 | << QByteArray("a%62%63d" ) |
| 1873 | << QByteArray("" ) |
| 1874 | << QByteArray("bc" ); |
| 1875 | } |
| 1876 | |
| 1877 | void tst_QByteArray::toPercentEncoding2() |
| 1878 | { |
| 1879 | QFETCH(QByteArray, original); |
| 1880 | QFETCH(QByteArray, encoded); |
| 1881 | QFETCH(QByteArray, excludeInEncoding); |
| 1882 | QFETCH(QByteArray, includeInEncoding); |
| 1883 | |
| 1884 | QByteArray encodedData = original.toPercentEncoding(exclude: excludeInEncoding, include: includeInEncoding); |
| 1885 | QCOMPARE(encodedData.constData(), encoded.constData()); |
| 1886 | QCOMPARE(original, QByteArray::fromPercentEncoding(encodedData)); |
| 1887 | } |
| 1888 | |
| 1889 | void tst_QByteArray::compare_data() |
| 1890 | { |
| 1891 | QTest::addColumn<QByteArray>(name: "str1" ); |
| 1892 | QTest::addColumn<QByteArray>(name: "str2" ); |
| 1893 | QTest::addColumn<int>(name: "result" ); |
| 1894 | |
| 1895 | QTest::newRow(dataTag: "null" ) << QByteArray() << QByteArray() << 0; |
| 1896 | QTest::newRow(dataTag: "null-empty" )<< QByteArray() << QByteArray("" ) << 0; |
| 1897 | QTest::newRow(dataTag: "empty-null" )<< QByteArray("" ) << QByteArray() << 0; |
| 1898 | QTest::newRow(dataTag: "null-full" ) << QByteArray() << QByteArray("abc" ) << -1; |
| 1899 | QTest::newRow(dataTag: "full-null" ) << QByteArray("abc" ) << QByteArray() << +1; |
| 1900 | QTest::newRow(dataTag: "empty-full" )<< QByteArray("" ) << QByteArray("abc" ) << -1; |
| 1901 | QTest::newRow(dataTag: "full-empty" )<< QByteArray("abc" ) << QByteArray("" ) << +1; |
| 1902 | QTest::newRow(dataTag: "rawempty-full" ) << QByteArray::fromRawData("abc" , size: 0) << QByteArray("abc" ) << -1; |
| 1903 | QTest::newRow(dataTag: "full-rawempty" ) << QByteArray("abc" ) << QByteArray::fromRawData("abc" , size: 0) << +1; |
| 1904 | |
| 1905 | QTest::newRow(dataTag: "equal 1" ) << QByteArray("abc" ) << QByteArray("abc" ) << 0; |
| 1906 | QTest::newRow(dataTag: "equal 2" ) << QByteArray::fromRawData("abc" , size: 3) << QByteArray("abc" ) << 0; |
| 1907 | QTest::newRow(dataTag: "equal 3" ) << QByteArray::fromRawData("abcdef" , size: 3) << QByteArray("abc" ) << 0; |
| 1908 | QTest::newRow(dataTag: "equal 4" ) << QByteArray("abc" ) << QByteArray::fromRawData("abc" , size: 3) << 0; |
| 1909 | QTest::newRow(dataTag: "equal 5" ) << QByteArray("abc" ) << QByteArray::fromRawData("abcdef" , size: 3) << 0; |
| 1910 | QTest::newRow(dataTag: "equal 6" ) << QByteArray("a\0bc" , 4) << QByteArray("a\0bc" , 4) << 0; |
| 1911 | QTest::newRow(dataTag: "equal 7" ) << QByteArray::fromRawData("a\0bcdef" , size: 4) << QByteArray("a\0bc" , 4) << 0; |
| 1912 | QTest::newRow(dataTag: "equal 8" ) << QByteArray("a\0bc" , 4) << QByteArray::fromRawData("a\0bcdef" , size: 4) << 0; |
| 1913 | |
| 1914 | QTest::newRow(dataTag: "less 1" ) << QByteArray("000" ) << QByteArray("abc" ) << -1; |
| 1915 | QTest::newRow(dataTag: "less 2" ) << QByteArray::fromRawData("00" , size: 3) << QByteArray("abc" ) << -1; |
| 1916 | QTest::newRow(dataTag: "less 3" ) << QByteArray("000" ) << QByteArray::fromRawData("abc" , size: 3) << -1; |
| 1917 | QTest::newRow(dataTag: "less 4" ) << QByteArray("abc" , 3) << QByteArray("abc" , 4) << -1; |
| 1918 | QTest::newRow(dataTag: "less 5" ) << QByteArray::fromRawData("abc\0" , size: 3) << QByteArray("abc\0" , 4) << -1; |
| 1919 | QTest::newRow(dataTag: "less 6" ) << QByteArray("a\0bc" , 4) << QByteArray("a\0bd" , 4) << -1; |
| 1920 | |
| 1921 | QTest::newRow(dataTag: "greater 1" ) << QByteArray("abc" ) << QByteArray("000" ) << +1; |
| 1922 | QTest::newRow(dataTag: "greater 2" ) << QByteArray("abc" ) << QByteArray::fromRawData("00" , size: 3) << +1; |
| 1923 | QTest::newRow(dataTag: "greater 3" ) << QByteArray("abcd" ) << QByteArray::fromRawData("abcd" , size: 3) << +1; |
| 1924 | QTest::newRow(dataTag: "greater 4" ) << QByteArray("a\0bc" , 4) << QByteArray("a\0bb" , 4) << +1; |
| 1925 | } |
| 1926 | |
| 1927 | void tst_QByteArray::compare() |
| 1928 | { |
| 1929 | QFETCH(QByteArray, str1); |
| 1930 | QFETCH(QByteArray, str2); |
| 1931 | QFETCH(int, result); |
| 1932 | |
| 1933 | const bool isEqual = result == 0; |
| 1934 | const bool isLess = result < 0; |
| 1935 | const bool isGreater = result > 0; |
| 1936 | |
| 1937 | int cmp = str1.compare(a: str2); |
| 1938 | if (cmp) |
| 1939 | cmp = (cmp < 0 ? -1 : 1); |
| 1940 | |
| 1941 | QCOMPARE(cmp, result); |
| 1942 | |
| 1943 | // basic tests: |
| 1944 | QCOMPARE(str1 == str2, isEqual); |
| 1945 | QCOMPARE(str1 < str2, isLess); |
| 1946 | QCOMPARE(str1 > str2, isGreater); |
| 1947 | |
| 1948 | // composed tests: |
| 1949 | QCOMPARE(str1 <= str2, isLess || isEqual); |
| 1950 | QCOMPARE(str1 >= str2, isGreater || isEqual); |
| 1951 | QCOMPARE(str1 != str2, !isEqual); |
| 1952 | |
| 1953 | // inverted tests: |
| 1954 | QCOMPARE(str2 == str1, isEqual); |
| 1955 | QCOMPARE(str2 < str1, isGreater); |
| 1956 | QCOMPARE(str2 > str1, isLess); |
| 1957 | |
| 1958 | // composed, inverted tests: |
| 1959 | QCOMPARE(str2 <= str1, isGreater || isEqual); |
| 1960 | QCOMPARE(str2 >= str1, isLess || isEqual); |
| 1961 | QCOMPARE(str2 != str1, !isEqual); |
| 1962 | |
| 1963 | if (isEqual) |
| 1964 | QVERIFY(qHash(str1) == qHash(str2)); |
| 1965 | } |
| 1966 | |
| 1967 | void tst_QByteArray::compareCharStar_data() |
| 1968 | { |
| 1969 | QTest::addColumn<QByteArray>(name: "str1" ); |
| 1970 | QTest::addColumn<QString>(name: "string2" ); |
| 1971 | QTest::addColumn<int>(name: "result" ); |
| 1972 | |
| 1973 | QTest::newRow(dataTag: "null-null" ) << QByteArray() << QString() << 0; |
| 1974 | QTest::newRow(dataTag: "null-empty" ) << QByteArray() << "" << 0; |
| 1975 | QTest::newRow(dataTag: "null-full" ) << QByteArray() << "abc" << -1; |
| 1976 | QTest::newRow(dataTag: "empty-null" ) << QByteArray("" ) << QString() << 0; |
| 1977 | QTest::newRow(dataTag: "empty-empty" ) << QByteArray("" ) << "" << 0; |
| 1978 | QTest::newRow(dataTag: "empty-full" ) << QByteArray("" ) << "abc" << -1; |
| 1979 | QTest::newRow(dataTag: "raw-null" ) << QByteArray::fromRawData("abc" , size: 0) << QString() << 0; |
| 1980 | QTest::newRow(dataTag: "raw-empty" ) << QByteArray::fromRawData("abc" , size: 0) << QString("" ) << 0; |
| 1981 | QTest::newRow(dataTag: "raw-full" ) << QByteArray::fromRawData("abc" , size: 0) << "abc" << -1; |
| 1982 | |
| 1983 | QTest::newRow(dataTag: "full-null" ) << QByteArray("abc" ) << QString() << +1; |
| 1984 | QTest::newRow(dataTag: "full-empty" ) << QByteArray("abc" ) << "" << +1; |
| 1985 | |
| 1986 | QTest::newRow(dataTag: "equal1" ) << QByteArray("abc" ) << "abc" << 0; |
| 1987 | QTest::newRow(dataTag: "equal2" ) << QByteArray("abcd" , 3) << "abc" << 0; |
| 1988 | QTest::newRow(dataTag: "equal3" ) << QByteArray::fromRawData("abcd" , size: 3) << "abc" << 0; |
| 1989 | |
| 1990 | QTest::newRow(dataTag: "less1" ) << QByteArray("ab" ) << "abc" << -1; |
| 1991 | QTest::newRow(dataTag: "less2" ) << QByteArray("abb" ) << "abc" << -1; |
| 1992 | QTest::newRow(dataTag: "less3" ) << QByteArray::fromRawData("abc" , size: 2) << "abc" << -1; |
| 1993 | QTest::newRow(dataTag: "less4" ) << QByteArray("" , 1) << "abc" << -1; |
| 1994 | QTest::newRow(dataTag: "less5" ) << QByteArray::fromRawData("" , size: 1) << "abc" << -1; |
| 1995 | QTest::newRow(dataTag: "less6" ) << QByteArray("a\0bc" , 4) << "a.bc" << -1; |
| 1996 | |
| 1997 | QTest::newRow(dataTag: "greater1" ) << QByteArray("ac" ) << "abc" << +1; |
| 1998 | QTest::newRow(dataTag: "greater2" ) << QByteArray("abd" ) << "abc" << +1; |
| 1999 | QTest::newRow(dataTag: "greater3" ) << QByteArray("abcd" ) << "abc" << +1; |
| 2000 | QTest::newRow(dataTag: "greater4" ) << QByteArray::fromRawData("abcd" , size: 4) << "abc" << +1; |
| 2001 | } |
| 2002 | |
| 2003 | void tst_QByteArray::compareCharStar() |
| 2004 | { |
| 2005 | QFETCH(QByteArray, str1); |
| 2006 | QFETCH(QString, string2); |
| 2007 | QFETCH(int, result); |
| 2008 | |
| 2009 | const bool isEqual = result == 0; |
| 2010 | const bool isLess = result < 0; |
| 2011 | const bool isGreater = result > 0; |
| 2012 | QByteArray qba = string2.toLatin1(); |
| 2013 | const char *str2 = qba.constData(); |
| 2014 | if (string2.isNull()) |
| 2015 | str2 = 0; |
| 2016 | |
| 2017 | // basic tests: |
| 2018 | QCOMPARE(str1 == str2, isEqual); |
| 2019 | QCOMPARE(str1 < str2, isLess); |
| 2020 | QCOMPARE(str1 > str2, isGreater); |
| 2021 | |
| 2022 | // composed tests: |
| 2023 | QCOMPARE(str1 <= str2, isLess || isEqual); |
| 2024 | QCOMPARE(str1 >= str2, isGreater || isEqual); |
| 2025 | QCOMPARE(str1 != str2, !isEqual); |
| 2026 | |
| 2027 | // inverted tests: |
| 2028 | QCOMPARE(str2 == str1, isEqual); |
| 2029 | QCOMPARE(str2 < str1, isGreater); |
| 2030 | QCOMPARE(str2 > str1, isLess); |
| 2031 | |
| 2032 | // composed, inverted tests: |
| 2033 | QCOMPARE(str2 <= str1, isGreater || isEqual); |
| 2034 | QCOMPARE(str2 >= str1, isLess || isEqual); |
| 2035 | QCOMPARE(str2 != str1, !isEqual); |
| 2036 | } |
| 2037 | |
| 2038 | void tst_QByteArray::repeatedSignature() const |
| 2039 | { |
| 2040 | /* repated() should be a const member. */ |
| 2041 | const QByteArray string; |
| 2042 | (void)string.repeated(times: 3); |
| 2043 | } |
| 2044 | |
| 2045 | void tst_QByteArray::repeated() const |
| 2046 | { |
| 2047 | QFETCH(QByteArray, string); |
| 2048 | QFETCH(QByteArray, expected); |
| 2049 | QFETCH(int, count); |
| 2050 | |
| 2051 | QCOMPARE(string.repeated(count), expected); |
| 2052 | } |
| 2053 | |
| 2054 | void tst_QByteArray::repeated_data() const |
| 2055 | { |
| 2056 | QTest::addColumn<QByteArray>(name: "string" ); |
| 2057 | QTest::addColumn<QByteArray>(name: "expected" ); |
| 2058 | QTest::addColumn<int>(name: "count" ); |
| 2059 | |
| 2060 | /* Empty strings. */ |
| 2061 | QTest::newRow(dataTag: "data1" ) |
| 2062 | << QByteArray() |
| 2063 | << QByteArray() |
| 2064 | << 0; |
| 2065 | |
| 2066 | QTest::newRow(dataTag: "data2" ) |
| 2067 | << QByteArray() |
| 2068 | << QByteArray() |
| 2069 | << -1004; |
| 2070 | |
| 2071 | QTest::newRow(dataTag: "data3" ) |
| 2072 | << QByteArray() |
| 2073 | << QByteArray() |
| 2074 | << 1; |
| 2075 | |
| 2076 | QTest::newRow(dataTag: "data4" ) |
| 2077 | << QByteArray() |
| 2078 | << QByteArray() |
| 2079 | << 5; |
| 2080 | |
| 2081 | /* On simple string. */ |
| 2082 | QTest::newRow(dataTag: "data5" ) |
| 2083 | << QByteArray("abc" ) |
| 2084 | << QByteArray() |
| 2085 | << -1004; |
| 2086 | |
| 2087 | QTest::newRow(dataTag: "data6" ) |
| 2088 | << QByteArray("abc" ) |
| 2089 | << QByteArray() |
| 2090 | << -1; |
| 2091 | |
| 2092 | QTest::newRow(dataTag: "data7" ) |
| 2093 | << QByteArray("abc" ) |
| 2094 | << QByteArray() |
| 2095 | << 0; |
| 2096 | |
| 2097 | QTest::newRow(dataTag: "data8" ) |
| 2098 | << QByteArray("abc" ) |
| 2099 | << QByteArray("abc" ) |
| 2100 | << 1; |
| 2101 | |
| 2102 | QTest::newRow(dataTag: "data9" ) |
| 2103 | << QByteArray(("abc" )) |
| 2104 | << QByteArray(("abcabc" )) |
| 2105 | << 2; |
| 2106 | |
| 2107 | QTest::newRow(dataTag: "data10" ) |
| 2108 | << QByteArray(("abc" )) |
| 2109 | << QByteArray(("abcabcabc" )) |
| 2110 | << 3; |
| 2111 | |
| 2112 | QTest::newRow(dataTag: "data11" ) |
| 2113 | << QByteArray(("abc" )) |
| 2114 | << QByteArray(("abcabcabcabc" )) |
| 2115 | << 4; |
| 2116 | |
| 2117 | QTest::newRow(dataTag: "static not null terminated" ) |
| 2118 | << QByteArray(staticNotNullTerminated) |
| 2119 | << QByteArray("datadatadatadata" ) |
| 2120 | << 4; |
| 2121 | QTest::newRow(dataTag: "static standard" ) |
| 2122 | << QByteArray(staticStandard) |
| 2123 | << QByteArray("datadatadatadata" ) |
| 2124 | << 4; |
| 2125 | QTest::newRow(dataTag: "static shifted not null terminated" ) |
| 2126 | << QByteArray(staticShiftedNotNullTerminated) |
| 2127 | << QByteArray("datadatadatadata" ) |
| 2128 | << 4; |
| 2129 | QTest::newRow(dataTag: "static shifted" ) |
| 2130 | << QByteArray(staticShifted) |
| 2131 | << QByteArray("datadatadatadata" ) |
| 2132 | << 4; |
| 2133 | } |
| 2134 | |
| 2135 | void tst_QByteArray::byteRefDetaching() const |
| 2136 | { |
| 2137 | { |
| 2138 | QByteArray str = "str" ; |
| 2139 | QByteArray copy; |
| 2140 | copy[0] = 'S'; |
| 2141 | |
| 2142 | QCOMPARE(str, QByteArray("str" )); |
| 2143 | } |
| 2144 | |
| 2145 | { |
| 2146 | char buf[] = { 's', 't', 'r' }; |
| 2147 | QByteArray str = QByteArray::fromRawData(buf, size: 3); |
| 2148 | str[0] = 'S'; |
| 2149 | |
| 2150 | QCOMPARE(buf[0], char('s')); |
| 2151 | } |
| 2152 | |
| 2153 | { |
| 2154 | static const char buf[] = { 's', 't', 'r' }; |
| 2155 | QByteArray str = QByteArray::fromRawData(buf, size: 3); |
| 2156 | |
| 2157 | // this causes a crash in most systems if the detaching doesn't work |
| 2158 | str[0] = 'S'; |
| 2159 | |
| 2160 | QCOMPARE(buf[0], char('s')); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | void tst_QByteArray::reserve() |
| 2165 | { |
| 2166 | int capacity = 100; |
| 2167 | QByteArray qba; |
| 2168 | qba.reserve(asize: capacity); |
| 2169 | QVERIFY(qba.capacity() == capacity); |
| 2170 | char *data = qba.data(); |
| 2171 | |
| 2172 | for (int i = 0; i < capacity; i++) { |
| 2173 | qba.resize(size: i); |
| 2174 | QVERIFY(capacity == qba.capacity()); |
| 2175 | QVERIFY(data == qba.data()); |
| 2176 | } |
| 2177 | |
| 2178 | qba.resize(size: capacity); |
| 2179 | |
| 2180 | QByteArray copy = qba; |
| 2181 | qba.reserve(asize: capacity / 2); |
| 2182 | QCOMPARE(qba.size(), capacity); // we didn't shrink the size! |
| 2183 | QCOMPARE(qba.capacity(), capacity); |
| 2184 | QCOMPARE(copy.capacity(), capacity); |
| 2185 | |
| 2186 | qba = copy; |
| 2187 | qba.reserve(asize: capacity * 2); |
| 2188 | QCOMPARE(qba.size(), capacity); |
| 2189 | QCOMPARE(qba.capacity(), capacity * 2); |
| 2190 | QCOMPARE(copy.capacity(), capacity); |
| 2191 | QVERIFY(qba.constData() != data); |
| 2192 | |
| 2193 | QByteArray nil1, nil2; |
| 2194 | nil1.reserve(asize: 0); |
| 2195 | nil2.squeeze(); |
| 2196 | nil1.squeeze(); |
| 2197 | nil2.reserve(asize: 0); |
| 2198 | } |
| 2199 | |
| 2200 | void tst_QByteArray::reserveExtended_data() |
| 2201 | { |
| 2202 | prependExtended_data(); |
| 2203 | } |
| 2204 | |
| 2205 | void tst_QByteArray::reserveExtended() |
| 2206 | { |
| 2207 | QFETCH(QByteArray, array); |
| 2208 | array.reserve(asize: 1024); |
| 2209 | QVERIFY(array.capacity() == 1024); |
| 2210 | QCOMPARE(array, QByteArray("data" )); |
| 2211 | array.squeeze(); |
| 2212 | QCOMPARE(array, QByteArray("data" )); |
| 2213 | QCOMPARE(array.capacity(), array.size()); |
| 2214 | } |
| 2215 | |
| 2216 | void tst_QByteArray::movablity_data() |
| 2217 | { |
| 2218 | QTest::addColumn<QByteArray>(name: "array" ); |
| 2219 | |
| 2220 | QTest::newRow(dataTag: "0x00000000" ) << QByteArray("\x00\x00\x00\x00" , 4); |
| 2221 | QTest::newRow(dataTag: "0x000000ff" ) << QByteArray("\x00\x00\x00\xff" , 4); |
| 2222 | QTest::newRow(dataTag: "0xffffffff" ) << QByteArray("\xff\xff\xff\xff" , 4); |
| 2223 | QTest::newRow(dataTag: "empty" ) << QByteArray("" ); |
| 2224 | QTest::newRow(dataTag: "null" ) << QByteArray(); |
| 2225 | QTest::newRow(dataTag: "sss" ) << QByteArray(3, 's'); |
| 2226 | |
| 2227 | prependExtended_data(); |
| 2228 | } |
| 2229 | |
| 2230 | void tst_QByteArray::movablity() |
| 2231 | { |
| 2232 | QFETCH(QByteArray, array); |
| 2233 | |
| 2234 | Q_STATIC_ASSERT(!QTypeInfo<QByteArray>::isStatic); |
| 2235 | |
| 2236 | const int size = array.size(); |
| 2237 | const bool isEmpty = array.isEmpty(); |
| 2238 | const bool isNull = array.isNull(); |
| 2239 | const int capacity = array.capacity(); |
| 2240 | |
| 2241 | QByteArray memSpace; |
| 2242 | |
| 2243 | // we need only memory space not the instance |
| 2244 | memSpace.~QByteArray(); |
| 2245 | // move array -> memSpace |
| 2246 | memcpy(dest: (void *)&memSpace, src: (const void *)&array, n: sizeof(QByteArray)); |
| 2247 | // reconstruct empty QByteArray |
| 2248 | new (&array) QByteArray; |
| 2249 | |
| 2250 | QCOMPARE(memSpace.size(), size); |
| 2251 | QCOMPARE(memSpace.isEmpty(), isEmpty); |
| 2252 | QCOMPARE(memSpace.isNull(), isNull); |
| 2253 | QCOMPARE(memSpace.capacity(), capacity); |
| 2254 | |
| 2255 | // try to not crash |
| 2256 | (void)memSpace.toLower(); |
| 2257 | (void)memSpace.toUpper(); |
| 2258 | memSpace.prepend(c: 'a'); |
| 2259 | memSpace.append(s: "b" , len: 1); |
| 2260 | memSpace.squeeze(); |
| 2261 | memSpace.reserve(asize: array.size() + 16); |
| 2262 | |
| 2263 | QByteArray copy(memSpace); |
| 2264 | |
| 2265 | // reinitialize base values |
| 2266 | const int newSize = size + 2; |
| 2267 | const bool newIsEmpty = false; |
| 2268 | const bool newIsNull = false; |
| 2269 | const int newCapacity = 16; |
| 2270 | |
| 2271 | // move back memSpace -> array |
| 2272 | array.~QByteArray(); |
| 2273 | memcpy(dest: (void *)&array, src: (const void *)&memSpace, n: sizeof(QByteArray)); |
| 2274 | // reconstruct empty QByteArray |
| 2275 | new (&memSpace) QByteArray; |
| 2276 | |
| 2277 | QCOMPARE(array.size(), newSize); |
| 2278 | QCOMPARE(array.isEmpty(), newIsEmpty); |
| 2279 | QCOMPARE(array.isNull(), newIsNull); |
| 2280 | QCOMPARE(array.capacity(), newCapacity); |
| 2281 | QVERIFY(array.startsWith('a')); |
| 2282 | QVERIFY(array.endsWith('b')); |
| 2283 | |
| 2284 | QCOMPARE(copy.size(), newSize); |
| 2285 | QCOMPARE(copy.isEmpty(), newIsEmpty); |
| 2286 | QCOMPARE(copy.isNull(), newIsNull); |
| 2287 | QCOMPARE(copy.capacity(), newCapacity); |
| 2288 | QVERIFY(copy.startsWith('a')); |
| 2289 | QVERIFY(copy.endsWith('b')); |
| 2290 | |
| 2291 | // try to not crash |
| 2292 | array.squeeze(); |
| 2293 | array.reserve(asize: array.size() + 3); |
| 2294 | QVERIFY(true); |
| 2295 | } |
| 2296 | |
| 2297 | // Only tested on c++0x compliant compiler or gcc |
| 2298 | void tst_QByteArray::literals() |
| 2299 | { |
| 2300 | QByteArray str(QByteArrayLiteral("abcd" )); |
| 2301 | |
| 2302 | QVERIFY(str.length() == 4); |
| 2303 | QVERIFY(str == "abcd" ); |
| 2304 | QVERIFY(str.data_ptr()->ref.isStatic()); |
| 2305 | QVERIFY(str.data_ptr()->offset == sizeof(QByteArrayData)); |
| 2306 | |
| 2307 | const char *s = str.constData(); |
| 2308 | QByteArray str2 = str; |
| 2309 | QVERIFY(str2.constData() == s); |
| 2310 | |
| 2311 | // detach on non const access |
| 2312 | QVERIFY(str.data() != s); |
| 2313 | |
| 2314 | QVERIFY(str2.constData() == s); |
| 2315 | QVERIFY(str2.data() != s); |
| 2316 | } |
| 2317 | |
| 2318 | void tst_QByteArray::toUpperLower_data() |
| 2319 | { |
| 2320 | QTest::addColumn<QByteArray>(name: "input" ); |
| 2321 | QTest::addColumn<QByteArray>(name: "upper" ); |
| 2322 | QTest::addColumn<QByteArray>(name: "lower" ); |
| 2323 | |
| 2324 | QTest::newRow(dataTag: "empty" ) << QByteArray() << QByteArray() << QByteArray(); |
| 2325 | QTest::newRow(dataTag: "literal" ) << QByteArrayLiteral("Hello World" ) |
| 2326 | << QByteArrayLiteral("HELLO WORLD" ) |
| 2327 | << QByteArrayLiteral("hello world" ); |
| 2328 | QTest::newRow(dataTag: "ascii" ) << QByteArray("Hello World, this is a STRING" ) |
| 2329 | << QByteArray("HELLO WORLD, THIS IS A STRING" ) |
| 2330 | << QByteArray("hello world, this is a string" ); |
| 2331 | QTest::newRow(dataTag: "latin1" ) << QByteArray("R\311sum\351" ) |
| 2332 | << QByteArray("R\311SUM\311" ) |
| 2333 | << QByteArray("r\351sum\351" ); |
| 2334 | QTest::newRow(dataTag: "nul" ) << QByteArray("a\0B" , 3) << QByteArray("A\0B" , 3) << QByteArray("a\0b" , 3); |
| 2335 | } |
| 2336 | |
| 2337 | void tst_QByteArray::toUpperLower() |
| 2338 | { |
| 2339 | QFETCH(QByteArray, input); |
| 2340 | QFETCH(QByteArray, upper); |
| 2341 | QFETCH(QByteArray, lower); |
| 2342 | QCOMPARE(lower.toLower(), lower); |
| 2343 | QCOMPARE(upper.toUpper(), upper); |
| 2344 | QCOMPARE(input.toUpper(), upper); |
| 2345 | QCOMPARE(input.toLower(), lower); |
| 2346 | |
| 2347 | QByteArray copy = input; |
| 2348 | QCOMPARE(std::move(copy).toUpper(), upper); |
| 2349 | copy = input; |
| 2350 | copy.detach(); |
| 2351 | QCOMPARE(std::move(copy).toUpper(), upper); |
| 2352 | |
| 2353 | copy = input; |
| 2354 | QCOMPARE(std::move(copy).toLower(), lower); |
| 2355 | copy = input; |
| 2356 | copy.detach(); |
| 2357 | QCOMPARE(std::move(copy).toLower(), lower); |
| 2358 | |
| 2359 | copy = lower; |
| 2360 | QCOMPARE(std::move(copy).toLower(), lower); |
| 2361 | copy = lower; |
| 2362 | copy.detach(); |
| 2363 | QCOMPARE(std::move(copy).toLower(), lower); |
| 2364 | |
| 2365 | copy = upper; |
| 2366 | QCOMPARE(std::move(copy).toUpper(), upper); |
| 2367 | copy = upper; |
| 2368 | copy.detach(); |
| 2369 | QCOMPARE(std::move(copy).toUpper(), upper); |
| 2370 | } |
| 2371 | |
| 2372 | void tst_QByteArray::isUpper() |
| 2373 | { |
| 2374 | QVERIFY(!QByteArray().isUpper()); |
| 2375 | QVERIFY(!QByteArray("" ).isUpper()); |
| 2376 | QVERIFY(QByteArray("TEXT" ).isUpper()); |
| 2377 | QVERIFY(QByteArray("\xD0\xDE" ).isUpper()); |
| 2378 | QVERIFY(!QByteArray("\xD7" ).isUpper()); // multiplication sign is not upper |
| 2379 | QVERIFY(!QByteArray("\xDF" ).isUpper()); // sz ligature is not upper |
| 2380 | QVERIFY(!QByteArray("text" ).isUpper()); |
| 2381 | QVERIFY(!QByteArray("Text" ).isUpper()); |
| 2382 | QVERIFY(!QByteArray("tExt" ).isUpper()); |
| 2383 | QVERIFY(!QByteArray("teXt" ).isUpper()); |
| 2384 | QVERIFY(!QByteArray("texT" ).isUpper()); |
| 2385 | QVERIFY(!QByteArray("TExt" ).isUpper()); |
| 2386 | QVERIFY(!QByteArray("teXT" ).isUpper()); |
| 2387 | QVERIFY(!QByteArray("tEXt" ).isUpper()); |
| 2388 | QVERIFY(!QByteArray("tExT" ).isUpper()); |
| 2389 | QVERIFY(!QByteArray("@ABYZ[" ).isUpper()); |
| 2390 | QVERIFY(!QByteArray("@abyz[" ).isUpper()); |
| 2391 | QVERIFY(!QByteArray("`ABYZ{" ).isUpper()); |
| 2392 | QVERIFY(!QByteArray("`abyz{" ).isUpper()); |
| 2393 | } |
| 2394 | |
| 2395 | void tst_QByteArray::isLower() |
| 2396 | { |
| 2397 | QVERIFY(!QByteArray().isLower()); |
| 2398 | QVERIFY(!QByteArray("" ).isLower()); |
| 2399 | QVERIFY(QByteArray("text" ).isLower()); |
| 2400 | QVERIFY(QByteArray("\xE0\xFF" ).isLower()); |
| 2401 | QVERIFY(!QByteArray("\xF7" ).isLower()); // division sign is not lower |
| 2402 | QVERIFY(!QByteArray("Text" ).isLower()); |
| 2403 | QVERIFY(!QByteArray("tExt" ).isLower()); |
| 2404 | QVERIFY(!QByteArray("teXt" ).isLower()); |
| 2405 | QVERIFY(!QByteArray("texT" ).isLower()); |
| 2406 | QVERIFY(!QByteArray("TExt" ).isLower()); |
| 2407 | QVERIFY(!QByteArray("teXT" ).isLower()); |
| 2408 | QVERIFY(!QByteArray("tEXt" ).isLower()); |
| 2409 | QVERIFY(!QByteArray("tExT" ).isLower()); |
| 2410 | QVERIFY(!QByteArray("TEXT" ).isLower()); |
| 2411 | QVERIFY(!QByteArray("@ABYZ[" ).isLower()); |
| 2412 | QVERIFY(!QByteArray("@abyz[" ).isLower()); |
| 2413 | QVERIFY(!QByteArray("`ABYZ{" ).isLower()); |
| 2414 | QVERIFY(!QByteArray("`abyz{" ).isLower()); |
| 2415 | } |
| 2416 | |
| 2417 | void tst_QByteArray::macTypes() |
| 2418 | { |
| 2419 | #ifndef Q_OS_MAC |
| 2420 | QSKIP("This is a Apple-only test" ); |
| 2421 | #else |
| 2422 | extern void tst_QByteArray_macTypes(); // in qbytearray_mac.mm |
| 2423 | tst_QByteArray_macTypes(); |
| 2424 | #endif |
| 2425 | } |
| 2426 | |
| 2427 | void tst_QByteArray::stdString() |
| 2428 | { |
| 2429 | std::string stdstr( "QByteArray" ); |
| 2430 | |
| 2431 | const QByteArray stlqt = QByteArray::fromStdString(s: stdstr); |
| 2432 | QCOMPARE(stlqt.length(), int(stdstr.length())); |
| 2433 | QCOMPARE(stlqt.data(), stdstr.c_str()); |
| 2434 | QCOMPARE(stlqt.toStdString(), stdstr); |
| 2435 | |
| 2436 | std::string utf8str( "Nøt æscii" ); |
| 2437 | const QByteArray u8 = QByteArray::fromStdString(s: utf8str); |
| 2438 | const QByteArray l1 = QString::fromUtf8(str: u8).toLatin1(); |
| 2439 | std::string l1str = l1.toStdString(); |
| 2440 | QVERIFY(l1str.length() < utf8str.length()); |
| 2441 | } |
| 2442 | |
| 2443 | |
| 2444 | const char globalChar = '1'; |
| 2445 | |
| 2446 | QTEST_MAIN(tst_QByteArray) |
| 2447 | #include "tst_qbytearray.moc" |
| 2448 | |