| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2014 by Southwest Research Institute (R) |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module 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 | #include <qbytearraylist.h> |
| 32 | |
| 33 | #include <qmetatype.h> |
| 34 | |
| 35 | Q_DECLARE_METATYPE(QByteArrayList) |
| 36 | |
| 37 | class tst_QByteArrayList : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | private slots: |
| 41 | void join() const; |
| 42 | void join_data() const; |
| 43 | void joinByteArray() const; |
| 44 | void joinByteArray_data() const; |
| 45 | void joinChar() const; |
| 46 | void joinChar_data() const; |
| 47 | void joinEmptiness() const; |
| 48 | |
| 49 | void operator_plus() const; |
| 50 | void operator_plus_data() const; |
| 51 | |
| 52 | void indexOf_data() const; |
| 53 | void indexOf() const; |
| 54 | |
| 55 | void initializerList() const; |
| 56 | }; |
| 57 | |
| 58 | void tst_QByteArrayList::join() const |
| 59 | { |
| 60 | QFETCH(QByteArrayList, input); |
| 61 | QFETCH(QByteArray, expectedResult); |
| 62 | |
| 63 | QCOMPARE(input.join(), expectedResult); |
| 64 | } |
| 65 | |
| 66 | void tst_QByteArrayList::join_data() const |
| 67 | { |
| 68 | QTest::addColumn<QByteArrayList>(name: "input" ); |
| 69 | QTest::addColumn<QByteArray>(name: "expectedResult" ); |
| 70 | |
| 71 | QTest::newRow(dataTag: "data1" ) << QByteArrayList() |
| 72 | << QByteArray(); |
| 73 | |
| 74 | QTest::newRow(dataTag: "data2" ) << (QByteArrayList() << "one" ) |
| 75 | << QByteArray("one" ); |
| 76 | |
| 77 | QTest::newRow(dataTag: "data3" ) << (QByteArrayList() << "a" << "b" ) |
| 78 | << QByteArray("ab" ); |
| 79 | |
| 80 | QTest::newRow(dataTag: "data4" ) << (QByteArrayList() << "a" << "b" << "c" ) |
| 81 | << QByteArray("abc" ); |
| 82 | } |
| 83 | |
| 84 | void tst_QByteArrayList::joinByteArray() const |
| 85 | { |
| 86 | QFETCH(QByteArrayList, input); |
| 87 | QFETCH(QByteArray, separator); |
| 88 | QFETCH(QByteArray, expectedResult); |
| 89 | |
| 90 | QCOMPARE(input.join(separator), expectedResult); |
| 91 | } |
| 92 | |
| 93 | void tst_QByteArrayList::joinByteArray_data() const |
| 94 | { |
| 95 | QTest::addColumn<QByteArrayList>(name: "input" ); |
| 96 | QTest::addColumn<QByteArray>(name: "separator" ); |
| 97 | QTest::addColumn<QByteArray>(name: "expectedResult" ); |
| 98 | |
| 99 | QTest::newRow(dataTag: "data1" ) << QByteArrayList() |
| 100 | << QByteArray() |
| 101 | << QByteArray(); |
| 102 | |
| 103 | QTest::newRow(dataTag: "data2" ) << QByteArrayList() |
| 104 | << QByteArray("separator" ) |
| 105 | << QByteArray(); |
| 106 | |
| 107 | QTest::newRow(dataTag: "data3" ) << (QByteArrayList() << "one" ) |
| 108 | << QByteArray("separator" ) |
| 109 | << QByteArray("one" ); |
| 110 | |
| 111 | QTest::newRow(dataTag: "data4" ) << (QByteArrayList() << "a" << "b" ) |
| 112 | << QByteArray(" " ) |
| 113 | << QByteArray("a b" ); |
| 114 | |
| 115 | QTest::newRow(dataTag: "data5" ) << (QByteArrayList() << "a" << "b" << "c" ) |
| 116 | << QByteArray(" " ) |
| 117 | << QByteArray("a b c" ); |
| 118 | |
| 119 | QTest::newRow(dataTag: "data6" ) << (QByteArrayList() << "a" << "b" << "c" ) |
| 120 | << QByteArray() |
| 121 | << QByteArray("abc" ); |
| 122 | |
| 123 | QTest::newRow(dataTag: "data7" ) << (QByteArrayList() << "a" << "b" << "c" ) |
| 124 | << QByteArray("" ) //empty |
| 125 | << QByteArray("abc" ); |
| 126 | } |
| 127 | |
| 128 | void tst_QByteArrayList::joinChar() const |
| 129 | { |
| 130 | QFETCH(QByteArrayList, input); |
| 131 | QFETCH(char, separator); |
| 132 | QFETCH(QByteArray, expectedResult); |
| 133 | |
| 134 | QCOMPARE(input.join(separator), expectedResult); |
| 135 | } |
| 136 | |
| 137 | void tst_QByteArrayList::joinChar_data() const |
| 138 | { |
| 139 | QTest::addColumn<QByteArrayList>(name: "input" ); |
| 140 | QTest::addColumn<char>(name: "separator" ); |
| 141 | QTest::addColumn<QByteArray>(name: "expectedResult" ); |
| 142 | |
| 143 | QTest::newRow(dataTag: "data1" ) << QByteArrayList() |
| 144 | << ' ' |
| 145 | << QByteArray(); |
| 146 | |
| 147 | QTest::newRow(dataTag: "data2" ) << (QByteArrayList() << "a a" << "b" ) |
| 148 | << ' ' |
| 149 | << QByteArray("a a b" ); |
| 150 | |
| 151 | QTest::newRow(dataTag: "data3" ) << (QByteArrayList() << "a" << "b" << "c c" ) |
| 152 | << ' ' |
| 153 | << QByteArray("a b c c" ); |
| 154 | } |
| 155 | |
| 156 | void tst_QByteArrayList::joinEmptiness() const |
| 157 | { |
| 158 | QByteArrayList list; |
| 159 | QByteArray string = list.join(sep: QByteArray()); |
| 160 | |
| 161 | QVERIFY(string.isEmpty()); |
| 162 | QVERIFY(string.isNull()); |
| 163 | } |
| 164 | |
| 165 | void tst_QByteArrayList::operator_plus() const |
| 166 | { |
| 167 | QFETCH(QByteArrayList, lhs); |
| 168 | QFETCH(QByteArrayList, rhs); |
| 169 | QFETCH(QByteArrayList, expectedResult); |
| 170 | |
| 171 | // operator+ for const lvalues |
| 172 | { |
| 173 | const QByteArrayList bal1 = lhs; |
| 174 | const QByteArrayList bal2 = rhs; |
| 175 | QCOMPARE(bal1 + bal2, expectedResult); |
| 176 | } |
| 177 | { |
| 178 | const QList<QByteArray> lba1 = lhs; |
| 179 | const QByteArrayList bal2 = rhs; |
| 180 | QCOMPARE(lba1 + bal2, expectedResult); |
| 181 | } |
| 182 | { |
| 183 | const QByteArrayList bal1 = lhs; |
| 184 | const QList<QByteArray> lba2 = rhs; |
| 185 | QCOMPARE(bal1 + lba2, expectedResult); |
| 186 | } |
| 187 | { |
| 188 | const QList<QByteArray> lba1 = lhs; |
| 189 | const QList<QByteArray> lba2 = rhs; |
| 190 | QCOMPARE(lba1 + lba2, QList<QByteArray>(expectedResult)); // check we don't mess with old code |
| 191 | } |
| 192 | |
| 193 | // operator+ for rvalues (only lhs) |
| 194 | { |
| 195 | QByteArrayList bal1 = lhs; |
| 196 | const QByteArrayList bal2 = rhs; |
| 197 | QCOMPARE(std::move(bal1) + bal2, expectedResult); |
| 198 | } |
| 199 | { |
| 200 | QList<QByteArray> lba1 = lhs; |
| 201 | const QByteArrayList bal2 = rhs; |
| 202 | QCOMPARE(std::move(lba1) + bal2, expectedResult); |
| 203 | } |
| 204 | { |
| 205 | QByteArrayList bal1 = lhs; |
| 206 | const QList<QByteArray> lba2 = rhs; |
| 207 | QCOMPARE(std::move(bal1) + lba2, expectedResult); |
| 208 | } |
| 209 | { |
| 210 | QList<QByteArray> lba1 = lhs; |
| 211 | const QList<QByteArray> lba2 = rhs; |
| 212 | QCOMPARE(std::move(lba1) + lba2, QList<QByteArray>(expectedResult)); // check we don't mess with old code |
| 213 | } |
| 214 | |
| 215 | // operator += for const lvalues |
| 216 | { |
| 217 | QByteArrayList bal1 = lhs; |
| 218 | const QByteArrayList bal2 = rhs; |
| 219 | QCOMPARE(bal1 += bal2, expectedResult); |
| 220 | } |
| 221 | { |
| 222 | QByteArrayList bal1 = lhs; |
| 223 | const QList<QByteArray> lba2 = rhs; |
| 224 | QCOMPARE(bal1 += lba2, expectedResult); |
| 225 | } |
| 226 | { |
| 227 | QList<QByteArray> lba1 = lhs; |
| 228 | const QByteArrayList bal2 = rhs; |
| 229 | QCOMPARE(lba1 += bal2, QList<QByteArray>(expectedResult)); |
| 230 | } |
| 231 | |
| 232 | QByteArrayList t1 = lhs; |
| 233 | QByteArrayList t2 = rhs; |
| 234 | |
| 235 | QCOMPARE(std::move(t1) + t2, expectedResult); |
| 236 | } |
| 237 | |
| 238 | void tst_QByteArrayList::operator_plus_data() const |
| 239 | { |
| 240 | QTest::addColumn<QByteArrayList>(name: "lhs" ); |
| 241 | QTest::addColumn<QByteArrayList>(name: "rhs" ); |
| 242 | QTest::addColumn<QByteArrayList>(name: "expectedResult" ); |
| 243 | |
| 244 | QTest::newRow(dataTag: "simpl" ) << ( QByteArrayList() << "a" ) |
| 245 | << ( QByteArrayList() << "b" << "c" ) |
| 246 | << ( QByteArrayList() << "a" << "b" << "c" ); |
| 247 | |
| 248 | QTest::newRow(dataTag: "blank1" ) << QByteArrayList() |
| 249 | << QByteArrayList() |
| 250 | << QByteArrayList(); |
| 251 | |
| 252 | QTest::newRow(dataTag: "blank2" ) << ( QByteArrayList() ) |
| 253 | << ( QByteArrayList() << "b" << "c" ) |
| 254 | << ( QByteArrayList() << "b" << "c" ); |
| 255 | |
| 256 | QTest::newRow(dataTag: "empty1" ) << ( QByteArrayList() << "" ) |
| 257 | << ( QByteArrayList() << "b" << "c" ) |
| 258 | << ( QByteArrayList() << "" << "b" << "c" ); |
| 259 | |
| 260 | QTest::newRow(dataTag: "empty2" ) << ( QByteArrayList() << "a" ) |
| 261 | << ( QByteArrayList() << "" << "c" ) |
| 262 | << ( QByteArrayList() << "a" << "" << "c" ); |
| 263 | } |
| 264 | |
| 265 | void tst_QByteArrayList::indexOf_data() const |
| 266 | { |
| 267 | QTest::addColumn<QByteArrayList>(name: "list" ); |
| 268 | QTest::addColumn<QByteArray>(name: "item" ); |
| 269 | QTest::addColumn<int>(name: "expectedResult" ); |
| 270 | |
| 271 | QTest::newRow(dataTag: "empty" ) << QByteArrayList() << QByteArray("a" ) << -1; |
| 272 | QTest::newRow(dataTag: "found_1" ) << ( QByteArrayList() << "a" ) << QByteArray("a" ) << 0; |
| 273 | QTest::newRow(dataTag: "not_found_1" ) << ( QByteArrayList() << "a" ) << QByteArray("b" ) << -1; |
| 274 | QTest::newRow(dataTag: "found_2" ) << ( QByteArrayList() << "hello" << "world" ) << QByteArray("world" ) << 1; |
| 275 | QTest::newRow(dataTag: "returns_first" ) << ( QByteArrayList() << "hello" << "world" << "hello" << "again" ) << QByteArray("hello" ) << 0; |
| 276 | } |
| 277 | |
| 278 | void tst_QByteArrayList::indexOf() const |
| 279 | { |
| 280 | QFETCH(QByteArrayList, list); |
| 281 | QFETCH(QByteArray, item); |
| 282 | QFETCH(int, expectedResult); |
| 283 | |
| 284 | QCOMPARE(list.indexOf(item), expectedResult); |
| 285 | QCOMPARE(list.indexOf(item.constData()), expectedResult); |
| 286 | } |
| 287 | |
| 288 | void tst_QByteArrayList::initializerList() const |
| 289 | { |
| 290 | // constructor |
| 291 | QByteArrayList v1 = {QByteArray("hello" ),"world" ,QByteArray("plop" )}; |
| 292 | QCOMPARE(v1, (QByteArrayList() << "hello" << "world" << "plop" )); |
| 293 | QCOMPARE(v1, (QByteArrayList{"hello" ,"world" ,"plop" })); |
| 294 | // assignment operator (through implicit temporary) |
| 295 | QByteArrayList v2; |
| 296 | v2 = {QByteArray("hello" ),"world" ,QByteArray("plop" )}; |
| 297 | QCOMPARE(v2, v1); |
| 298 | } |
| 299 | |
| 300 | QTEST_APPLESS_MAIN(tst_QByteArrayList) |
| 301 | #include "tst_qbytearraylist.moc" |
| 302 | |