1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30#include <qregexp.h>
31#include <qregularexpression.h>
32#include <qstringlist.h>
33#include <qvector.h>
34
35#include <locale.h>
36
37#include <algorithm>
38
39class tst_QStringList : public QObject
40{
41 Q_OBJECT
42private slots:
43 void constructors();
44 void sort();
45 void filter();
46 void replaceInStrings();
47 void removeDuplicates();
48 void removeDuplicates_data();
49 void contains();
50 void indexOf_data();
51 void indexOf();
52 void lastIndexOf_data();
53 void lastIndexOf();
54
55 void indexOf_regExp();
56 void lastIndexOf_regExp();
57
58 void streamingOperator();
59 void assignmentOperator();
60 void join() const;
61 void join_data() const;
62 void joinEmptiness() const;
63 void joinChar() const;
64 void joinChar_data() const;
65
66 void initializeList() const;
67};
68
69extern const char email[];
70
71void tst_QStringList::constructors()
72{
73 {
74 QStringList list;
75 QVERIFY(list.isEmpty());
76 QCOMPARE(list.size(), 0);
77 QVERIFY(list == QStringList());
78 }
79 {
80 QString str = "abc";
81 QStringList list(str);
82 QVERIFY(!list.isEmpty());
83 QCOMPARE(list.size(), 1);
84 QCOMPARE(list.at(0), str);
85 }
86 {
87 QStringList list{ "a", "b", "c" };
88 QVERIFY(!list.isEmpty());
89 QCOMPARE(list.size(), 3);
90 QCOMPARE(list.at(0), "a");
91 QCOMPARE(list.at(1), "b");
92 QCOMPARE(list.at(2), "c");
93 }
94 {
95 const QVector<QString> reference{ "a", "b", "c" };
96 QCOMPARE(reference.size(), 3);
97
98 QStringList list(reference.cbegin(), reference.cend());
99 QCOMPARE(list.size(), reference.size());
100 QVERIFY(std::equal(list.cbegin(), list.cend(), reference.cbegin()));
101 }
102}
103
104void tst_QStringList::indexOf_regExp()
105{
106 QStringList list;
107 list << "harald" << "trond" << "vohi" << "harald";
108 {
109 QRegExp re(".*o.*");
110
111 QCOMPARE(list.indexOf(re), 1);
112 QCOMPARE(list.indexOf(re, 2), 2);
113 QCOMPARE(list.indexOf(re, 3), -1);
114
115 QCOMPARE(list.indexOf(QRegExp(".*x.*")), -1);
116 QCOMPARE(list.indexOf(re, -1), -1);
117 QCOMPARE(list.indexOf(re, -3), 1);
118 QCOMPARE(list.indexOf(re, -9999), 1);
119 QCOMPARE(list.indexOf(re, 9999), -1);
120
121 QCOMPARE(list.indexOf(QRegExp("[aeiou]")), -1);
122 }
123
124 {
125 QRegularExpression re(".*o.*");
126
127 QCOMPARE(list.indexOf(re), 1);
128 QCOMPARE(list.indexOf(re, 2), 2);
129 QCOMPARE(list.indexOf(re, 3), -1);
130
131 QCOMPARE(list.indexOf(QRegularExpression(".*x.*")), -1);
132 QCOMPARE(list.indexOf(re, -1), -1);
133 QCOMPARE(list.indexOf(re, -3), 1);
134 QCOMPARE(list.indexOf(re, -9999), 1);
135 QCOMPARE(list.indexOf(re, 9999), -1);
136
137 QCOMPARE(list.indexOf(QRegularExpression("[aeiou]")), -1);
138 }
139}
140
141void tst_QStringList::lastIndexOf_regExp()
142{
143 QStringList list;
144 list << "harald" << "trond" << "vohi" << "harald";
145
146 {
147 QRegExp re(".*o.*");
148
149 QCOMPARE(list.lastIndexOf(re), 2);
150 QCOMPARE(list.lastIndexOf(re, 2), 2);
151 QCOMPARE(list.lastIndexOf(re, 1), 1);
152
153 QCOMPARE(list.lastIndexOf(QRegExp(".*x.*")), -1);
154 QCOMPARE(list.lastIndexOf(re, -1), 2);
155 QCOMPARE(list.lastIndexOf(re, -3), 1);
156 QCOMPARE(list.lastIndexOf(re, -9999), -1);
157 QCOMPARE(list.lastIndexOf(re, 9999), 2);
158
159 QCOMPARE(list.lastIndexOf(QRegExp("[aeiou]")), -1);
160 }
161
162 {
163 QRegularExpression re(".*o.*");
164
165 QCOMPARE(list.lastIndexOf(re), 2);
166 QCOMPARE(list.lastIndexOf(re, 2), 2);
167 QCOMPARE(list.lastIndexOf(re, 1), 1);
168
169 QCOMPARE(list.lastIndexOf(QRegularExpression(".*x.*")), -1);
170 QCOMPARE(list.lastIndexOf(re, -1), 2);
171 QCOMPARE(list.lastIndexOf(re, -3), 1);
172 QCOMPARE(list.lastIndexOf(re, -9999), -1);
173 QCOMPARE(list.lastIndexOf(re, 9999), 2);
174
175 QCOMPARE(list.lastIndexOf(QRegularExpression("[aeiou]")), -1);
176 }
177
178
179}
180
181void tst_QStringList::indexOf_data()
182{
183 QTest::addColumn<QString>(name: "search");
184 QTest::addColumn<int>(name: "from");
185 QTest::addColumn<int>(name: "expectedResult");
186
187 QTest::newRow(dataTag: "harald") << "harald" << 0 << 0;
188 QTest::newRow(dataTag: "trond") << "trond" << 0 << 1;
189 QTest::newRow(dataTag: "vohi") << "vohi" << 0 << 2;
190 QTest::newRow(dataTag: "harald-1") << "harald" << 1 << 3;
191
192 QTest::newRow(dataTag: "hans") << "hans" << 0 << -1;
193 QTest::newRow(dataTag: "trond-1") << "trond" << 2 << -1;
194 QTest::newRow(dataTag: "harald-2") << "harald" << -1 << 3;
195 QTest::newRow(dataTag: "vohi-1") << "vohi" << -3 << 2;
196}
197
198void tst_QStringList::indexOf()
199{
200 QStringList list;
201 list << "harald" << "trond" << "vohi" << "harald";
202
203 QFETCH(QString, search);
204 QFETCH(int, from);
205 QFETCH(int, expectedResult);
206
207 QCOMPARE(list.indexOf(search, from), expectedResult);
208 QCOMPARE(list.indexOf(QStringView(search), from), expectedResult);
209 QCOMPARE(list.indexOf(QLatin1String(search.toLatin1()), from), expectedResult);
210}
211
212void tst_QStringList::lastIndexOf_data()
213{
214 QTest::addColumn<QString>(name: "search");
215 QTest::addColumn<int>(name: "from");
216 QTest::addColumn<int>(name: "expectedResult");
217
218 QTest::newRow(dataTag: "harald") << "harald" << -1 << 3;
219 QTest::newRow(dataTag: "trond") << "trond" << -1 << 1;
220 QTest::newRow(dataTag: "vohi") << "vohi" << -1 << 2;
221 QTest::newRow(dataTag: "harald-1") << "harald" << 2 << 0;
222
223 QTest::newRow(dataTag: "hans") << "hans" << -1 << -1;
224 QTest::newRow(dataTag: "vohi-1") << "vohi" << 1 << -1;
225 QTest::newRow(dataTag: "vohi-2") << "vohi" << -1 << 2;
226 QTest::newRow(dataTag: "vohi-3") << "vohi" << -3 << -1;
227}
228
229void tst_QStringList::lastIndexOf()
230{
231 QStringList list;
232 list << "harald" << "trond" << "vohi" << "harald";
233
234 QFETCH(QString, search);
235 QFETCH(int, from);
236 QFETCH(int, expectedResult);
237
238 QCOMPARE(list.lastIndexOf(search, from), expectedResult);
239 QCOMPARE(list.lastIndexOf(QStringView(search), from), expectedResult);
240 QCOMPARE(list.lastIndexOf(QLatin1String(search.toLatin1()), from), expectedResult);
241}
242
243void tst_QStringList::filter()
244{
245 QStringList list1, list2;
246 list1 << "Bill Gates" << "Joe Blow" << "Bill Clinton";
247 list1 = list1.filter( str: "Bill" );
248 list2 << "Bill Gates" << "Bill Clinton";
249 QCOMPARE( list1, list2 );
250
251 QStringList list3, list4;
252 list3 << "Bill Gates" << "Joe Blow" << "Bill Clinton";
253 list3 = list3.filter( rx: QRegExp("[i]ll") );
254 list4 << "Bill Gates" << "Bill Clinton";
255 QCOMPARE( list3, list4 );
256
257 QStringList list5, list6;
258 list5 << "Bill Gates" << "Joe Blow" << "Bill Clinton";
259 list5 = list5.filter( rx: QRegularExpression("[i]ll") );
260 list6 << "Bill Gates" << "Bill Clinton";
261 QCOMPARE( list5, list6 );
262
263 QStringList list7, list8;
264 list7 << "Bill Gates" << "Joe Blow" << "Bill Clinton";
265 list7 = list7.filter( str: QStringView(QString("Bill")) );
266 list8 << "Bill Gates" << "Bill Clinton";
267 QCOMPARE( list7, list8 );
268}
269
270void tst_QStringList::sort()
271{
272 QStringList list1, list2;
273 list1 << "alpha" << "beta" << "BETA" << "gamma" << "Gamma" << "gAmma" << "epsilon";
274 list1.sort();
275 list2 << "BETA" << "Gamma" << "alpha" << "beta" << "epsilon" << "gAmma" << "gamma";
276 QCOMPARE( list1, list2 );
277
278 char *current_locale = setlocale(LC_ALL, locale: "C");
279 QStringList list3, list4;
280 list3 << "alpha" << "beta" << "BETA" << "gamma" << "Gamma" << "gAmma" << "epsilon";
281 list3.sort(cs: Qt::CaseInsensitive);
282 list4 << "alpha" << "beta" << "BETA" << "epsilon" << "Gamma" << "gAmma" << "gamma";
283 // with this list, case insensitive sorting can give more than one permutation for "equivalent"
284 // elements; so we check that the sort gave the formally correct result (list[i] <= list[i+1])
285 for (int i = 0; i < list4.count() - 1; ++i)
286 QVERIFY2(QString::compare(list4.at(i), list4.at(i + 1), Qt::CaseInsensitive) <= 0, qPrintable(QString("index %1 failed").arg(i)));
287 // additional checks
288 QCOMPARE(list4.at(0), QString("alpha"));
289 QVERIFY(list4.indexOf("epsilon") > 0);
290 QVERIFY(list4.indexOf("epsilon") < (list4.count() - 1));
291 setlocale(LC_ALL, locale: current_locale);
292}
293
294void tst_QStringList::replaceInStrings()
295{
296 QStringList list1, list2;
297 list1 << "alpha" << "beta" << "gamma" << "epsilon";
298 list1.replaceInStrings( before: "a", after: "o" );
299 list2 << "olpho" << "beto" << "gommo" << "epsilon";
300 QCOMPARE( list1, list2 );
301
302 QStringList list3, list4;
303 list3 << "alpha" << "beta" << "gamma" << "epsilon";
304 list3.replaceInStrings( rx: QRegExp("^a"), after: "o" );
305 list4 << "olpha" << "beta" << "gamma" << "epsilon";
306 QCOMPARE( list3, list4 );
307
308 QStringList list5, list6;
309 list5 << "Bill Clinton" << "Gates, Bill";
310 list6 << "Bill Clinton" << "Bill Gates";
311 list5.replaceInStrings( rx: QRegExp("^(.*), (.*)$"), after: "\\2 \\1" );
312 QCOMPARE( list5, list6 );
313
314 QStringList list7, list8;
315 list7 << "alpha" << "beta" << "gamma" << "epsilon";
316 list7.replaceInStrings( rx: QRegularExpression("^a"), after: "o" );
317 list8 << "olpha" << "beta" << "gamma" << "epsilon";
318 QCOMPARE( list7, list8 );
319
320 QStringList list9, list10;
321 list9 << "Bill Clinton" << "Gates, Bill";
322 list10 << "Bill Clinton" << "Bill Gates";
323 list9.replaceInStrings( rx: QRegularExpression("^(.*), (.*)$"), after: "\\2 \\1" );
324 QCOMPARE( list9, list10 );
325
326 QStringList list11, list12, list13, list14;
327 list11 << "alpha" << "beta" << "gamma" << "epsilon";
328 list12 << "alpha" << "beta" << "gamma" << "epsilon";
329 list13 << "alpha" << "beta" << "gamma" << "epsilon";
330 list11.replaceInStrings( before: QStringView(QString("a")), after: QStringView(QString("o")) );
331 list12.replaceInStrings( before: QStringView(QString("a")), after: QString("o") );
332 list13.replaceInStrings( before: QString("a"), after: QStringView(QString("o")) );
333 list14 << "olpho" << "beto" << "gommo" << "epsilon";
334 QCOMPARE( list11, list12 );
335}
336
337void tst_QStringList::contains()
338{
339 QStringList list;
340 list << "arthur" << "Arthur" << "arthuR" << "ARTHUR" << "Dent" << "Hans Dent";
341
342 QVERIFY(list.contains("arthur"));
343 QVERIFY(!list.contains("ArthuR"));
344 QVERIFY(!list.contains("Hans"));
345 QVERIFY(list.contains("arthur", Qt::CaseInsensitive));
346 QVERIFY(list.contains("ArthuR", Qt::CaseInsensitive));
347 QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive));
348 QVERIFY(list.contains("dent", Qt::CaseInsensitive));
349 QVERIFY(!list.contains("hans", Qt::CaseInsensitive));
350
351 QVERIFY(list.contains(QLatin1String("arthur")));
352 QVERIFY(!list.contains(QLatin1String("ArthuR")));
353 QVERIFY(!list.contains(QLatin1String("Hans")));
354 QVERIFY(list.contains(QLatin1String("arthur"), Qt::CaseInsensitive));
355 QVERIFY(list.contains(QLatin1String("ArthuR"), Qt::CaseInsensitive));
356 QVERIFY(list.contains(QLatin1String("ARTHUR"), Qt::CaseInsensitive));
357 QVERIFY(list.contains(QLatin1String("dent"), Qt::CaseInsensitive));
358 QVERIFY(!list.contains(QLatin1String("hans"), Qt::CaseInsensitive));
359
360 QVERIFY(list.contains(QStringView(QString("arthur"))));
361 QVERIFY(!list.contains(QStringView(QString("ArthuR"))));
362 QVERIFY(!list.contains(QStringView(QString("Hans"))));
363 QVERIFY(list.contains(QStringView(QString("arthur")), Qt::CaseInsensitive));
364 QVERIFY(list.contains(QStringView(QString("ArthuR")), Qt::CaseInsensitive));
365 QVERIFY(list.contains(QStringView(QString("ARTHUR")), Qt::CaseInsensitive));
366 QVERIFY(list.contains(QStringView(QString("dent")), Qt::CaseInsensitive));
367 QVERIFY(!list.contains(QStringView(QString("hans")), Qt::CaseInsensitive));
368}
369
370void tst_QStringList::removeDuplicates_data()
371{
372 QTest::addColumn<QString>(name: "before");
373 QTest::addColumn<QString>(name: "after");
374 QTest::addColumn<int>(name: "count");
375 QTest::addColumn<bool>(name: "detached");
376
377 QTest::newRow(dataTag: "empty-1") << "Hello,Hello" << "Hello" << 1 << true;
378 QTest::newRow(dataTag: "empty-2") << "Hello,World" << "Hello,World" << 0 << false;
379 QTest::newRow(dataTag: "middle") << "Hello,World,Hello" << "Hello,World" << 1 << true;
380}
381
382void tst_QStringList::removeDuplicates()
383{
384 QFETCH(QString, before);
385 QFETCH(QString, after);
386 QFETCH(int, count);
387 QFETCH(bool, detached);
388
389 QStringList lbefore = before.split(sep: ',');
390 const QStringList oldlbefore = lbefore;
391 QStringList lafter = after.split(sep: ',');
392 int removed = lbefore.removeDuplicates();
393
394 QCOMPARE(removed, count);
395 QCOMPARE(lbefore, lafter);
396 QCOMPARE(detached, !oldlbefore.isSharedWith(lbefore));
397}
398
399void tst_QStringList::streamingOperator()
400{
401 QStringList list;
402 list << "hei";
403 list << list << "hopp" << list;
404
405 QList<QString> slist = list;
406 list << slist;
407
408 QCOMPARE(list, QStringList()
409 << "hei" << "hei" << "hopp"
410 << "hei" << "hei" << "hopp"
411 << "hei" << "hei" << "hopp"
412 << "hei" << "hei" << "hopp");
413
414 QStringList list2;
415 list2 << "adam";
416
417 QStringList list3;
418 list3 << "eva";
419
420 QCOMPARE(list2 << list3, QStringList() << "adam" << "eva");
421}
422
423void tst_QStringList::assignmentOperator()
424{
425 // compile-only test
426
427 QStringList adam;
428 adam << "adam";
429 QList<QString> eva;
430 eva << "eva";
431 QStringList result;
432 QStringList &ref1 = (result = adam);
433 QStringList &ref2 = (result = eva);
434 Q_UNUSED(ref1);
435 Q_UNUSED(ref2);
436}
437
438void tst_QStringList::join() const
439{
440 QFETCH(QStringList, input);
441 QFETCH(QString, separator);
442 QFETCH(QString, expectedResult);
443
444 QCOMPARE(input.join(separator), expectedResult);
445 QCOMPARE(input.join(QLatin1String(separator.toLatin1())), expectedResult);
446 QCOMPARE(input.join(QStringView(separator)), expectedResult);
447}
448
449void tst_QStringList::join_data() const
450{
451 QTest::addColumn<QStringList>(name: "input");
452 QTest::addColumn<QString>(name: "separator");
453 QTest::addColumn<QString>(name: "expectedResult");
454
455 QTest::newRow(dataTag: "data1")
456 << QStringList()
457 << QString()
458 << QString();
459
460 QTest::newRow(dataTag: "data2")
461 << QStringList()
462 << QString(QLatin1String("separator"))
463 << QString();
464
465 QTest::newRow(dataTag: "data3")
466 << QStringList("one")
467 << QString(QLatin1String("separator"))
468 << QString("one");
469
470 QTest::newRow(dataTag: "data4")
471 << QStringList("one")
472 << QString(QLatin1String("separator"))
473 << QString("one");
474
475
476 QTest::newRow(dataTag: "data5")
477 << (QStringList()
478 << QLatin1String("a")
479 << QLatin1String("b"))
480 << QString(QLatin1String(" "))
481 << QString("a b");
482
483 QTest::newRow(dataTag: "data6")
484 << (QStringList()
485 << QLatin1String("a")
486 << QLatin1String("b")
487 << QLatin1String("c"))
488 << QString(QLatin1String(" "))
489 << QString("a b c");
490}
491
492void tst_QStringList::joinChar() const
493{
494 QFETCH(QStringList, input);
495 QFETCH(QChar, separator);
496 QFETCH(QString, expectedResult);
497
498 QCOMPARE(input.join(separator), expectedResult);
499}
500
501void tst_QStringList::joinChar_data() const
502{
503 QTest::addColumn<QStringList>(name: "input");
504 QTest::addColumn<QChar>(name: "separator");
505 QTest::addColumn<QString>(name: "expectedResult");
506
507 QTest::newRow(dataTag: "data1")
508 << QStringList()
509 << QChar(QLatin1Char(' '))
510 << QString();
511
512 QTest::newRow(dataTag: "data5")
513 << (QStringList()
514 << QLatin1String("a")
515 << QLatin1String("b"))
516 << QChar(QLatin1Char(' '))
517 << QString("a b");
518
519 QTest::newRow(dataTag: "data6")
520 << (QStringList()
521 << QLatin1String("a")
522 << QLatin1String("b")
523 << QLatin1String("c"))
524 << QChar(QLatin1Char(' '))
525 << QString("a b c");
526
527 QTest::newRow(dataTag: "null separator")
528 << QStringList{QStringLiteral("a"), QStringLiteral("b"), QStringLiteral("c")}
529 << QChar(u'\0')
530 << QStringLiteral("a\0b\0c");
531}
532
533void tst_QStringList::joinEmptiness() const
534{
535 QStringList list;
536 QString string = list.join(sep: QString());
537
538 QVERIFY(string.isEmpty());
539 QVERIFY(string.isNull());
540}
541
542void tst_QStringList::initializeList() const
543{
544
545 QStringList v1{QLatin1String("hello"),"world",QString::fromLatin1(str: "plop")};
546 QCOMPARE(v1, (QStringList() << "hello" << "world" << "plop"));
547 QCOMPARE(v1, (QStringList{"hello","world","plop"}));
548}
549
550QTEST_APPLESS_MAIN(tst_QStringList)
551#include "tst_qstringlist.moc"
552

source code of qtbase/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp