1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QList>
//TESTED_CLASS=QList
//TESTED_FILES=corelib/tools/qlist.h corelib/tools/qlist.cpp
/*!
\class tst_QVector
\internal
\since 4.5
\brief Test Qt's class QList.
*/
class tst_QList : public QObject
{
Q_OBJECT
private slots:
void length() const;
void lengthSignature() const;
void append() const;
void prepend() const;
void mid() const;
void at() const;
void first() const;
void last() const;
void begin() const;
void end() const;
void contains() const;
void count() const;
void empty() const;
void endsWith() const;
void lastIndexOf() const;
void move() const;
void removeAll() const;
void removeAt() const;
void removeOne() const;
void replace() const;
void startsWith() const;
void swap() const;
void takeAt() const;
void takeFirst() const;
void takeLast() const;
void toSet() const;
void toStdList() const;
void toVector() const;
void value() const;
void testSTLIterators() const;
void testOperators() const;
};
void tst_QList::length() const
{
/* Empty list. */
{
const QList<int> list;
QCOMPARE(list.length(), 0);
}
/* One entry. */
{
QList<int> list;
list.append(0);
QCOMPARE(list.length(), 1);
}
/* Two entries. */
{
QList<int> list;
list.append(0);
list.append(1);
QCOMPARE(list.length(), 2);
}
/* Three entries. */
{
QList<int> list;
list.append(0);
list.append(0);
list.append(0);
QCOMPARE(list.length(), 3);
}
}
void tst_QList::lengthSignature() const
{
/* Constness. */
{
const QList<int> list;
/* The function should be const. */
list.length();
}
}
void tst_QList::append() const
{
/* test append(const QList<T> &) function */
QString one("one");
QString two("two");
QString three("three");
QString four("four");
QList<QString> list1;
QList<QString> list2;
QList<QString> listTotal;
list1.append(one);
list1.append(two);
list2.append(three);
list2.append(four);
list1.append(list2);
qDebug() << list1;
listTotal.append(one);
listTotal.append(two);
listTotal.append(three);
listTotal.append(four);
QCOMPARE(list1, listTotal);
}
void tst_QList::prepend() const
{
QList<QString *> list;
QString *str1 = new QString;
list.prepend(str1);
QVERIFY(list.size() == 1);
QVERIFY(list.at(0) == str1);
QString *str2 = new QString;
list.prepend(str2);
QVERIFY(list.size() == 2);
QVERIFY(list.at(0) == str2);
QVERIFY(list.at(1) == str1);
QString *str3 = new QString;
list.prepend(str3);
QVERIFY(list.size() == 3);
QVERIFY(list.at(0) == str3);
QVERIFY(list.at(1) == str2);
QVERIFY(list.at(2) == str1);
list.removeAll(str2);
delete str2;
QVERIFY(list.size() == 2);
QVERIFY(list.at(0) == str3);
QVERIFY(list.at(1) == str1);
QString *str4 = new QString;
list.prepend(str4);
QVERIFY(list.size() == 3);
QVERIFY(list.at(0) == str4);
QVERIFY(list.at(1) == str3);
QVERIFY(list.at(2) == str1);
qDeleteAll(list);
list.clear();
}
void tst_QList::mid() const
{
QList<QString> list;
list << "foo" << "bar" << "baz" << "bak" << "buck" << "hello" << "kitty";
QCOMPARE(list.mid(3, 3),
QList<QString>() << "bak" << "buck" << "hello");
}
void tst_QList::at() const
{
// test at() and make sure it functions correctly with some simple list manipulation.
QList<QString> list;
// create a list
list << "foo" << "bar" << "baz";
QVERIFY(list.size() == 3);
QCOMPARE(list.at(0), QLatin1String("foo"));
QCOMPARE(list.at(1), QLatin1String("bar"));
QCOMPARE(list.at(2), QLatin1String("baz"));
// append an item
list << "hello";
QVERIFY(list.size() == 4);
QCOMPARE(list.at(0), QLatin1String("foo"));
QCOMPARE(list.at(1), QLatin1String("bar"));
QCOMPARE(list.at(2), QLatin1String("baz"));
QCOMPARE(list.at(3), QLatin1String("hello"));
// remove an item
list.removeAt(1);
QVERIFY(list.size() == 3);
QCOMPARE(list.at(0), QLatin1String("foo"));
QCOMPARE(list.at(1), QLatin1String("baz"));
QCOMPARE(list.at(2), QLatin1String("hello"));
}
void tst_QList::first() const
{
QList<QString> list;
list << "foo" << "bar";
QCOMPARE(list.first(), QLatin1String("foo"));
// remove an item, make sure it still works
list.pop_front();
QVERIFY(list.size() == 1);
QCOMPARE(list.first(), QLatin1String("bar"));
}
void tst_QList::last() const
{
QList<QString> list;
list << "foo" << "bar";
QCOMPARE(list.last(), QLatin1String("bar"));
// remove an item, make sure it still works
list.pop_back();
QVERIFY(list.size() == 1);
QCOMPARE(list.last(), QLatin1String("foo"));
}
void tst_QList::begin() const
{
QList<QString> list;
list << "foo" << "bar";
QCOMPARE(*list.begin(), QLatin1String("foo"));
// remove an item, make sure it still works
list.pop_front();
QVERIFY(list.size() == 1);
QCOMPARE(*list.begin(), QLatin1String("bar"));
}
void tst_QList::end() const
{
QList<QString> list;
list << "foo" << "bar";
QCOMPARE(*--list.end(), QLatin1String("bar"));
// remove an item, make sure it still works
list.pop_back();
QVERIFY(list.size() == 1);
QCOMPARE(*--list.end(), QLatin1String("foo"));
}
void tst_QList::contains() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
QVERIFY(list.contains(QLatin1String("foo")) == true);
QVERIFY(list.contains(QLatin1String("pirates")) != true);
// add it and make sure it matches
list.append(QLatin1String("ninjas"));
QVERIFY(list.contains(QLatin1String("ninjas")) == true);
}
void tst_QList::count() const
{
QList<QString> list;
// starts empty
QVERIFY(list.count() == 0);
// goes up
list.append(QLatin1String("foo"));
QVERIFY(list.count() == 1);
// and up
list.append(QLatin1String("bar"));
QVERIFY(list.count() == 2);
// and down
list.pop_back();
QVERIFY(list.count() == 1);
// and empty. :)
list.pop_back();
QVERIFY(list.count() == 0);
}
void tst_QList::empty() const
{
QList<QString> list;
// make sure it starts empty
QVERIFY(list.empty());
// and doesn't stay empty
list.append(QLatin1String("foo"));
QVERIFY(!list.empty());
// and goes back to being empty
list.pop_back();
QVERIFY(list.empty());
}
void tst_QList::endsWith() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// test it returns correctly in both cases
QVERIFY(list.endsWith(QLatin1String("baz")));
QVERIFY(!list.endsWith(QLatin1String("bar")));
// remove an item and make sure the end item changes
list.pop_back();
QVERIFY(list.endsWith(QLatin1String("bar")));
}
void tst_QList::lastIndexOf() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// one instance of the target item
QVERIFY(list.lastIndexOf(QLatin1String("baz")) == 2);
// shouldn't find this
QVERIFY(list.lastIndexOf(QLatin1String("shouldntfindme")) == -1);
// multiple instances
list.append("baz");
list.append("baz");
QVERIFY(list.lastIndexOf(QLatin1String("baz")) == 4);
// search from the middle to find the last one
QVERIFY(list.lastIndexOf(QLatin1String("baz"), 3) == 3);
// try find none
QVERIFY(list.lastIndexOf(QLatin1String("baz"), 1) == -1);
}
void tst_QList::move() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// move an item
list.move(0, list.count() - 1);
QCOMPARE(list, QList<QString>() << "bar" << "baz" << "foo");
// move it back
list.move(list.count() - 1, 0);
QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz");
// move an item in the middle
list.move(1, 0);
QCOMPARE(list, QList<QString>() << "bar" << "foo" << "baz");
}
void tst_QList::removeAll() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// remove one instance
list.removeAll(QLatin1String("bar"));
QCOMPARE(list, QList<QString>() << "foo" << "baz");
// many instances
list << "foo" << "bar" << "baz";
list << "foo" << "bar" << "baz";
list << "foo" << "bar" << "baz";
list.removeAll(QLatin1String("bar"));
QCOMPARE(list, QList<QString>() << "foo" << "baz" << "foo" << "baz" << "foo" << "baz" << "foo" << "baz");
// try remove something that doesn't exist
list.removeAll(QLatin1String("you won't remove anything I hope"));
QCOMPARE(list, QList<QString>() << "foo" << "baz" << "foo" << "baz" << "foo" << "baz" << "foo" << "baz");
}
void tst_QList::removeAt() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// middle
list.removeAt(1);
QCOMPARE(list, QList<QString>() << "foo" << "baz");
// start
list.removeAt(0);
QCOMPARE(list, QList<QString>() << "baz");
// final
list.removeAt(0);
QCOMPARE(list, QList<QString>());
}
void tst_QList::removeOne() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// middle
list.removeOne(QLatin1String("bar"));
QCOMPARE(list, QList<QString>() << "foo" << "baz");
// start
list.removeOne(QLatin1String("foo"));
QCOMPARE(list, QList<QString>() << "baz");
// last
list.removeOne(QLatin1String("baz"));
QCOMPARE(list, QList<QString>());
// make sure it really only removes one :)
list << "foo" << "foo";
list.removeOne("foo");
QCOMPARE(list, QList<QString>() << "foo");
// try remove something that doesn't exist
list.removeOne(QLatin1String("you won't remove anything I hope"));
QCOMPARE(list, QList<QString>() << "foo");
}
void tst_QList::replace() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// start
list.replace(0, "moo");
QCOMPARE(list, QList<QString>() << "moo" << "bar" << "baz");
// middle
list.replace(1, "cow");
QCOMPARE(list, QList<QString>() << "moo" << "cow" << "baz");
// end
list.replace(2, "milk");
QCOMPARE(list, QList<QString>() << "moo" << "cow" << "milk");
}
void tst_QList::startsWith() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// make sure it starts ok
QVERIFY(list.startsWith(QLatin1String("foo")));
// remove an item
list.removeFirst();
QVERIFY(list.startsWith(QLatin1String("bar")));
}
void tst_QList::swap() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// swap
list.swap(0, 2);
QCOMPARE(list, QList<QString>() << "baz" << "bar" << "foo");
// swap again
list.swap(1, 2);
QCOMPARE(list, QList<QString>() << "baz" << "foo" << "bar");
}
void tst_QList::takeAt() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
QCOMPARE(list.takeAt(0), QLatin1String("foo"));
QVERIFY(list.size() == 2);
QCOMPARE(list.takeAt(1), QLatin1String("baz"));
QVERIFY(list.size() == 1);
QCOMPARE(list.takeAt(0), QLatin1String("bar"));
QVERIFY(list.size() == 0);
}
void tst_QList::takeFirst() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
QCOMPARE(list.takeFirst(), QLatin1String("foo"));
QVERIFY(list.size() == 2);
QCOMPARE(list.takeFirst(), QLatin1String("bar"));
QVERIFY(list.size() == 1);
QCOMPARE(list.takeFirst(), QLatin1String("baz"));
QVERIFY(list.size() == 0);
}
void tst_QList::takeLast() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
QCOMPARE(list.takeLast(), QLatin1String("baz"));
QCOMPARE(list.takeLast(), QLatin1String("bar"));
QCOMPARE(list.takeLast(), QLatin1String("foo"));
}
void tst_QList::toSet() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// no duplicates
QCOMPARE(list.toSet(), QSet<QString>() << "foo" << "bar" << "baz");
QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz");
// duplicates (is this more of a QSet test?)
list << "foo" << "bar" << "baz";
QCOMPARE(list.toSet(), QSet<QString>() << "foo" << "bar" << "baz");
QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz" << "foo" << "bar" << "baz");
}
void tst_QList::toStdList() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// yuck.
std::list<QString> slist;
slist.push_back(QLatin1String("foo"));
slist.push_back(QLatin1String("bar"));
slist.push_back(QLatin1String("baz"));
QCOMPARE(list.toStdList(), slist);
QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz");
}
void tst_QList::toVector() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
QCOMPARE(list.toVector(), QVector<QString>() << "foo" << "bar" << "baz");
}
void tst_QList::value() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
// test real values
QCOMPARE(list.value(0), QLatin1String("foo"));
QCOMPARE(list.value(2), QLatin1String("baz"));
// test empty default
QCOMPARE(list.value(3), QString());
QCOMPARE(list.value(-1), QString());
// test defaults
QLatin1String defaultstr("default");
QCOMPARE(list.value(-1, defaultstr), defaultstr);
QCOMPARE(list.value(3, defaultstr), defaultstr);
}
void tst_QList::testOperators() const
{
QList<QString> list;
list << "foo" << "bar" << "baz";
QList<QString> listtwo;
listtwo << "foo" << "bar" << "baz";
// test equal
QVERIFY(list == listtwo);
// not equal
listtwo.append("not equal");
QVERIFY(list != listtwo);
// +=
list += listtwo;
QVERIFY(list.size() == 7);
QVERIFY(listtwo.size() == 4);
QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz" << "foo" << "bar" << "baz" << "not equal");
// =
list = listtwo;
QCOMPARE(list, listtwo);
QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz" << "not equal");
// []
QCOMPARE(list[0], QLatin1String("foo"));
QCOMPARE(list[list.size() - 1], QLatin1String("not equal"));
}
void tst_QList::testSTLIterators() const
{
QList<QString> list;
// create a list
list << "foo" << "bar" << "baz";
QList<QString>::iterator it = list.begin();
QCOMPARE(*it, QLatin1String("foo")); it++;
QCOMPARE(*it, QLatin1String("bar")); it++;
QCOMPARE(*it, QLatin1String("baz")); it++;
QCOMPARE(it, list.end()); it--;
// walk backwards
QCOMPARE(*it, QLatin1String("baz")); it--;
QCOMPARE(*it, QLatin1String("bar")); it--;
QCOMPARE(*it, QLatin1String("foo"));
// test erase
it = list.erase(it);
QVERIFY(list.size() == 2);
QCOMPARE(*it, QLatin1String("bar"));
// test multiple erase
it = list.erase(it, it + 2);
QVERIFY(list.size() == 0);
QCOMPARE(it, list.end());
// insert again
it = list.insert(it, QLatin1String("foo"));
QVERIFY(list.size() == 1);
QCOMPARE(*it, QLatin1String("foo"));
// insert again
it = list.insert(it, QLatin1String("bar"));
QVERIFY(list.size() == 2);
QCOMPARE(*it++, QLatin1String("bar"));
QCOMPARE(*it, QLatin1String("foo"));
}
QTEST_APPLESS_MAIN(tst_QList)
#include "tst_qlist.moc"
|