127 listTotal.append(four); |
156 listTotal.append(four); |
128 QCOMPARE(list1, listTotal); |
157 QCOMPARE(list1, listTotal); |
129 |
158 |
130 } |
159 } |
131 |
160 |
|
161 void tst_QList::prepend() const |
|
162 { |
|
163 QList<QString *> list; |
|
164 QString *str1 = new QString; |
|
165 list.prepend(str1); |
|
166 QVERIFY(list.size() == 1); |
|
167 QVERIFY(list.at(0) == str1); |
|
168 QString *str2 = new QString; |
|
169 list.prepend(str2); |
|
170 QVERIFY(list.size() == 2); |
|
171 QVERIFY(list.at(0) == str2); |
|
172 QVERIFY(list.at(1) == str1); |
|
173 QString *str3 = new QString; |
|
174 list.prepend(str3); |
|
175 QVERIFY(list.size() == 3); |
|
176 QVERIFY(list.at(0) == str3); |
|
177 QVERIFY(list.at(1) == str2); |
|
178 QVERIFY(list.at(2) == str1); |
|
179 list.removeAll(str2); |
|
180 delete str2; |
|
181 QVERIFY(list.size() == 2); |
|
182 QVERIFY(list.at(0) == str3); |
|
183 QVERIFY(list.at(1) == str1); |
|
184 QString *str4 = new QString; |
|
185 list.prepend(str4); |
|
186 QVERIFY(list.size() == 3); |
|
187 QVERIFY(list.at(0) == str4); |
|
188 QVERIFY(list.at(1) == str3); |
|
189 QVERIFY(list.at(2) == str1); |
|
190 qDeleteAll(list); |
|
191 list.clear(); |
|
192 } |
|
193 |
|
194 void tst_QList::mid() const |
|
195 { |
|
196 QList<QString> list; |
|
197 list << "foo" << "bar" << "baz" << "bak" << "buck" << "hello" << "kitty"; |
|
198 |
|
199 QCOMPARE(list.mid(3, 3), |
|
200 QList<QString>() << "bak" << "buck" << "hello"); |
|
201 } |
|
202 |
|
203 void tst_QList::at() const |
|
204 { |
|
205 // test at() and make sure it functions correctly with some simple list manipulation. |
|
206 QList<QString> list; |
|
207 |
|
208 // create a list |
|
209 list << "foo" << "bar" << "baz"; |
|
210 QVERIFY(list.size() == 3); |
|
211 QCOMPARE(list.at(0), QLatin1String("foo")); |
|
212 QCOMPARE(list.at(1), QLatin1String("bar")); |
|
213 QCOMPARE(list.at(2), QLatin1String("baz")); |
|
214 |
|
215 // append an item |
|
216 list << "hello"; |
|
217 QVERIFY(list.size() == 4); |
|
218 QCOMPARE(list.at(0), QLatin1String("foo")); |
|
219 QCOMPARE(list.at(1), QLatin1String("bar")); |
|
220 QCOMPARE(list.at(2), QLatin1String("baz")); |
|
221 QCOMPARE(list.at(3), QLatin1String("hello")); |
|
222 |
|
223 // remove an item |
|
224 list.removeAt(1); |
|
225 QVERIFY(list.size() == 3); |
|
226 QCOMPARE(list.at(0), QLatin1String("foo")); |
|
227 QCOMPARE(list.at(1), QLatin1String("baz")); |
|
228 QCOMPARE(list.at(2), QLatin1String("hello")); |
|
229 } |
|
230 |
|
231 void tst_QList::first() const |
|
232 { |
|
233 QList<QString> list; |
|
234 list << "foo" << "bar"; |
|
235 |
|
236 QCOMPARE(list.first(), QLatin1String("foo")); |
|
237 |
|
238 // remove an item, make sure it still works |
|
239 list.pop_front(); |
|
240 QVERIFY(list.size() == 1); |
|
241 QCOMPARE(list.first(), QLatin1String("bar")); |
|
242 } |
|
243 |
|
244 void tst_QList::last() const |
|
245 { |
|
246 QList<QString> list; |
|
247 list << "foo" << "bar"; |
|
248 |
|
249 QCOMPARE(list.last(), QLatin1String("bar")); |
|
250 |
|
251 // remove an item, make sure it still works |
|
252 list.pop_back(); |
|
253 QVERIFY(list.size() == 1); |
|
254 QCOMPARE(list.last(), QLatin1String("foo")); |
|
255 } |
|
256 |
|
257 void tst_QList::begin() const |
|
258 { |
|
259 QList<QString> list; |
|
260 list << "foo" << "bar"; |
|
261 |
|
262 QCOMPARE(*list.begin(), QLatin1String("foo")); |
|
263 |
|
264 // remove an item, make sure it still works |
|
265 list.pop_front(); |
|
266 QVERIFY(list.size() == 1); |
|
267 QCOMPARE(*list.begin(), QLatin1String("bar")); |
|
268 } |
|
269 |
|
270 void tst_QList::end() const |
|
271 { |
|
272 QList<QString> list; |
|
273 list << "foo" << "bar"; |
|
274 |
|
275 QCOMPARE(*--list.end(), QLatin1String("bar")); |
|
276 |
|
277 // remove an item, make sure it still works |
|
278 list.pop_back(); |
|
279 QVERIFY(list.size() == 1); |
|
280 QCOMPARE(*--list.end(), QLatin1String("foo")); |
|
281 } |
|
282 |
|
283 void tst_QList::contains() const |
|
284 { |
|
285 QList<QString> list; |
|
286 list << "foo" << "bar" << "baz"; |
|
287 |
|
288 QVERIFY(list.contains(QLatin1String("foo")) == true); |
|
289 QVERIFY(list.contains(QLatin1String("pirates")) != true); |
|
290 |
|
291 // add it and make sure it matches |
|
292 list.append(QLatin1String("ninjas")); |
|
293 QVERIFY(list.contains(QLatin1String("ninjas")) == true); |
|
294 } |
|
295 |
|
296 void tst_QList::count() const |
|
297 { |
|
298 QList<QString> list; |
|
299 |
|
300 // starts empty |
|
301 QVERIFY(list.count() == 0); |
|
302 |
|
303 // goes up |
|
304 list.append(QLatin1String("foo")); |
|
305 QVERIFY(list.count() == 1); |
|
306 |
|
307 // and up |
|
308 list.append(QLatin1String("bar")); |
|
309 QVERIFY(list.count() == 2); |
|
310 |
|
311 // and down |
|
312 list.pop_back(); |
|
313 QVERIFY(list.count() == 1); |
|
314 |
|
315 // and empty. :) |
|
316 list.pop_back(); |
|
317 QVERIFY(list.count() == 0); |
|
318 } |
|
319 |
|
320 void tst_QList::empty() const |
|
321 { |
|
322 QList<QString> list; |
|
323 |
|
324 // make sure it starts empty |
|
325 QVERIFY(list.empty()); |
|
326 |
|
327 // and doesn't stay empty |
|
328 list.append(QLatin1String("foo")); |
|
329 QVERIFY(!list.empty()); |
|
330 |
|
331 // and goes back to being empty |
|
332 list.pop_back(); |
|
333 QVERIFY(list.empty()); |
|
334 } |
|
335 |
|
336 void tst_QList::endsWith() const |
|
337 { |
|
338 QList<QString> list; |
|
339 list << "foo" << "bar" << "baz"; |
|
340 |
|
341 // test it returns correctly in both cases |
|
342 QVERIFY(list.endsWith(QLatin1String("baz"))); |
|
343 QVERIFY(!list.endsWith(QLatin1String("bar"))); |
|
344 |
|
345 // remove an item and make sure the end item changes |
|
346 list.pop_back(); |
|
347 QVERIFY(list.endsWith(QLatin1String("bar"))); |
|
348 } |
|
349 |
|
350 void tst_QList::lastIndexOf() const |
|
351 { |
|
352 QList<QString> list; |
|
353 list << "foo" << "bar" << "baz"; |
|
354 |
|
355 // one instance of the target item |
|
356 QVERIFY(list.lastIndexOf(QLatin1String("baz")) == 2); |
|
357 |
|
358 // shouldn't find this |
|
359 QVERIFY(list.lastIndexOf(QLatin1String("shouldntfindme")) == -1); |
|
360 |
|
361 // multiple instances |
|
362 list.append("baz"); |
|
363 list.append("baz"); |
|
364 QVERIFY(list.lastIndexOf(QLatin1String("baz")) == 4); |
|
365 |
|
366 // search from the middle to find the last one |
|
367 QVERIFY(list.lastIndexOf(QLatin1String("baz"), 3) == 3); |
|
368 |
|
369 // try find none |
|
370 QVERIFY(list.lastIndexOf(QLatin1String("baz"), 1) == -1); |
|
371 } |
|
372 |
|
373 void tst_QList::move() const |
|
374 { |
|
375 QList<QString> list; |
|
376 list << "foo" << "bar" << "baz"; |
|
377 |
|
378 // move an item |
|
379 list.move(0, list.count() - 1); |
|
380 QCOMPARE(list, QList<QString>() << "bar" << "baz" << "foo"); |
|
381 |
|
382 // move it back |
|
383 list.move(list.count() - 1, 0); |
|
384 QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz"); |
|
385 |
|
386 // move an item in the middle |
|
387 list.move(1, 0); |
|
388 QCOMPARE(list, QList<QString>() << "bar" << "foo" << "baz"); |
|
389 } |
|
390 |
|
391 void tst_QList::removeAll() const |
|
392 { |
|
393 QList<QString> list; |
|
394 list << "foo" << "bar" << "baz"; |
|
395 |
|
396 // remove one instance |
|
397 list.removeAll(QLatin1String("bar")); |
|
398 QCOMPARE(list, QList<QString>() << "foo" << "baz"); |
|
399 |
|
400 // many instances |
|
401 list << "foo" << "bar" << "baz"; |
|
402 list << "foo" << "bar" << "baz"; |
|
403 list << "foo" << "bar" << "baz"; |
|
404 list.removeAll(QLatin1String("bar")); |
|
405 QCOMPARE(list, QList<QString>() << "foo" << "baz" << "foo" << "baz" << "foo" << "baz" << "foo" << "baz"); |
|
406 |
|
407 // try remove something that doesn't exist |
|
408 list.removeAll(QLatin1String("you won't remove anything I hope")); |
|
409 QCOMPARE(list, QList<QString>() << "foo" << "baz" << "foo" << "baz" << "foo" << "baz" << "foo" << "baz"); |
|
410 } |
|
411 |
|
412 void tst_QList::removeAt() const |
|
413 { |
|
414 QList<QString> list; |
|
415 list << "foo" << "bar" << "baz"; |
|
416 |
|
417 // middle |
|
418 list.removeAt(1); |
|
419 QCOMPARE(list, QList<QString>() << "foo" << "baz"); |
|
420 |
|
421 // start |
|
422 list.removeAt(0); |
|
423 QCOMPARE(list, QList<QString>() << "baz"); |
|
424 |
|
425 // final |
|
426 list.removeAt(0); |
|
427 QCOMPARE(list, QList<QString>()); |
|
428 } |
|
429 |
|
430 void tst_QList::removeOne() const |
|
431 { |
|
432 QList<QString> list; |
|
433 list << "foo" << "bar" << "baz"; |
|
434 |
|
435 // middle |
|
436 list.removeOne(QLatin1String("bar")); |
|
437 QCOMPARE(list, QList<QString>() << "foo" << "baz"); |
|
438 |
|
439 // start |
|
440 list.removeOne(QLatin1String("foo")); |
|
441 QCOMPARE(list, QList<QString>() << "baz"); |
|
442 |
|
443 // last |
|
444 list.removeOne(QLatin1String("baz")); |
|
445 QCOMPARE(list, QList<QString>()); |
|
446 |
|
447 // make sure it really only removes one :) |
|
448 list << "foo" << "foo"; |
|
449 list.removeOne("foo"); |
|
450 QCOMPARE(list, QList<QString>() << "foo"); |
|
451 |
|
452 // try remove something that doesn't exist |
|
453 list.removeOne(QLatin1String("you won't remove anything I hope")); |
|
454 QCOMPARE(list, QList<QString>() << "foo"); |
|
455 } |
|
456 |
|
457 void tst_QList::replace() const |
|
458 { |
|
459 QList<QString> list; |
|
460 list << "foo" << "bar" << "baz"; |
|
461 |
|
462 // start |
|
463 list.replace(0, "moo"); |
|
464 QCOMPARE(list, QList<QString>() << "moo" << "bar" << "baz"); |
|
465 |
|
466 // middle |
|
467 list.replace(1, "cow"); |
|
468 QCOMPARE(list, QList<QString>() << "moo" << "cow" << "baz"); |
|
469 |
|
470 // end |
|
471 list.replace(2, "milk"); |
|
472 QCOMPARE(list, QList<QString>() << "moo" << "cow" << "milk"); |
|
473 } |
|
474 |
|
475 void tst_QList::startsWith() const |
|
476 { |
|
477 QList<QString> list; |
|
478 list << "foo" << "bar" << "baz"; |
|
479 |
|
480 // make sure it starts ok |
|
481 QVERIFY(list.startsWith(QLatin1String("foo"))); |
|
482 |
|
483 // remove an item |
|
484 list.removeFirst(); |
|
485 QVERIFY(list.startsWith(QLatin1String("bar"))); |
|
486 } |
|
487 |
|
488 void tst_QList::swap() const |
|
489 { |
|
490 QList<QString> list; |
|
491 list << "foo" << "bar" << "baz"; |
|
492 |
|
493 // swap |
|
494 list.swap(0, 2); |
|
495 QCOMPARE(list, QList<QString>() << "baz" << "bar" << "foo"); |
|
496 |
|
497 // swap again |
|
498 list.swap(1, 2); |
|
499 QCOMPARE(list, QList<QString>() << "baz" << "foo" << "bar"); |
|
500 } |
|
501 |
|
502 void tst_QList::takeAt() const |
|
503 { |
|
504 QList<QString> list; |
|
505 list << "foo" << "bar" << "baz"; |
|
506 |
|
507 QCOMPARE(list.takeAt(0), QLatin1String("foo")); |
|
508 QVERIFY(list.size() == 2); |
|
509 QCOMPARE(list.takeAt(1), QLatin1String("baz")); |
|
510 QVERIFY(list.size() == 1); |
|
511 QCOMPARE(list.takeAt(0), QLatin1String("bar")); |
|
512 QVERIFY(list.size() == 0); |
|
513 } |
|
514 |
|
515 void tst_QList::takeFirst() const |
|
516 { |
|
517 QList<QString> list; |
|
518 list << "foo" << "bar" << "baz"; |
|
519 |
|
520 QCOMPARE(list.takeFirst(), QLatin1String("foo")); |
|
521 QVERIFY(list.size() == 2); |
|
522 QCOMPARE(list.takeFirst(), QLatin1String("bar")); |
|
523 QVERIFY(list.size() == 1); |
|
524 QCOMPARE(list.takeFirst(), QLatin1String("baz")); |
|
525 QVERIFY(list.size() == 0); |
|
526 } |
|
527 |
|
528 void tst_QList::takeLast() const |
|
529 { |
|
530 QList<QString> list; |
|
531 list << "foo" << "bar" << "baz"; |
|
532 |
|
533 QCOMPARE(list.takeLast(), QLatin1String("baz")); |
|
534 QCOMPARE(list.takeLast(), QLatin1String("bar")); |
|
535 QCOMPARE(list.takeLast(), QLatin1String("foo")); |
|
536 } |
|
537 |
|
538 void tst_QList::toSet() const |
|
539 { |
|
540 QList<QString> list; |
|
541 list << "foo" << "bar" << "baz"; |
|
542 |
|
543 // no duplicates |
|
544 QCOMPARE(list.toSet(), QSet<QString>() << "foo" << "bar" << "baz"); |
|
545 QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz"); |
|
546 |
|
547 // duplicates (is this more of a QSet test?) |
|
548 list << "foo" << "bar" << "baz"; |
|
549 QCOMPARE(list.toSet(), QSet<QString>() << "foo" << "bar" << "baz"); |
|
550 QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz" << "foo" << "bar" << "baz"); |
|
551 } |
|
552 |
|
553 void tst_QList::toStdList() const |
|
554 { |
|
555 QList<QString> list; |
|
556 list << "foo" << "bar" << "baz"; |
|
557 |
|
558 // yuck. |
|
559 std::list<QString> slist; |
|
560 slist.push_back(QLatin1String("foo")); |
|
561 slist.push_back(QLatin1String("bar")); |
|
562 slist.push_back(QLatin1String("baz")); |
|
563 |
|
564 QCOMPARE(list.toStdList(), slist); |
|
565 QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz"); |
|
566 } |
|
567 |
|
568 void tst_QList::toVector() const |
|
569 { |
|
570 QList<QString> list; |
|
571 list << "foo" << "bar" << "baz"; |
|
572 |
|
573 QCOMPARE(list.toVector(), QVector<QString>() << "foo" << "bar" << "baz"); |
|
574 } |
|
575 |
|
576 void tst_QList::value() const |
|
577 { |
|
578 QList<QString> list; |
|
579 list << "foo" << "bar" << "baz"; |
|
580 |
|
581 // test real values |
|
582 QCOMPARE(list.value(0), QLatin1String("foo")); |
|
583 QCOMPARE(list.value(2), QLatin1String("baz")); |
|
584 |
|
585 // test empty default |
|
586 QCOMPARE(list.value(3), QString()); |
|
587 QCOMPARE(list.value(-1), QString()); |
|
588 |
|
589 // test defaults |
|
590 QLatin1String defaultstr("default"); |
|
591 QCOMPARE(list.value(-1, defaultstr), defaultstr); |
|
592 QCOMPARE(list.value(3, defaultstr), defaultstr); |
|
593 } |
|
594 |
|
595 void tst_QList::testOperators() const |
|
596 { |
|
597 QList<QString> list; |
|
598 list << "foo" << "bar" << "baz"; |
|
599 |
|
600 QList<QString> listtwo; |
|
601 listtwo << "foo" << "bar" << "baz"; |
|
602 |
|
603 // test equal |
|
604 QVERIFY(list == listtwo); |
|
605 |
|
606 // not equal |
|
607 listtwo.append("not equal"); |
|
608 QVERIFY(list != listtwo); |
|
609 |
|
610 // += |
|
611 list += listtwo; |
|
612 QVERIFY(list.size() == 7); |
|
613 QVERIFY(listtwo.size() == 4); |
|
614 QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz" << "foo" << "bar" << "baz" << "not equal"); |
|
615 |
|
616 // = |
|
617 list = listtwo; |
|
618 QCOMPARE(list, listtwo); |
|
619 QCOMPARE(list, QList<QString>() << "foo" << "bar" << "baz" << "not equal"); |
|
620 |
|
621 // [] |
|
622 QCOMPARE(list[0], QLatin1String("foo")); |
|
623 QCOMPARE(list[list.size() - 1], QLatin1String("not equal")); |
|
624 } |
|
625 |
|
626 void tst_QList::testSTLIterators() const |
|
627 { |
|
628 QList<QString> list; |
|
629 |
|
630 // create a list |
|
631 list << "foo" << "bar" << "baz"; |
|
632 QList<QString>::iterator it = list.begin(); |
|
633 QCOMPARE(*it, QLatin1String("foo")); it++; |
|
634 QCOMPARE(*it, QLatin1String("bar")); it++; |
|
635 QCOMPARE(*it, QLatin1String("baz")); it++; |
|
636 QCOMPARE(it, list.end()); it--; |
|
637 |
|
638 // walk backwards |
|
639 QCOMPARE(*it, QLatin1String("baz")); it--; |
|
640 QCOMPARE(*it, QLatin1String("bar")); it--; |
|
641 QCOMPARE(*it, QLatin1String("foo")); |
|
642 |
|
643 // test erase |
|
644 it = list.erase(it); |
|
645 QVERIFY(list.size() == 2); |
|
646 QCOMPARE(*it, QLatin1String("bar")); |
|
647 |
|
648 // test multiple erase |
|
649 it = list.erase(it, it + 2); |
|
650 QVERIFY(list.size() == 0); |
|
651 QCOMPARE(it, list.end()); |
|
652 |
|
653 // insert again |
|
654 it = list.insert(it, QLatin1String("foo")); |
|
655 QVERIFY(list.size() == 1); |
|
656 QCOMPARE(*it, QLatin1String("foo")); |
|
657 |
|
658 // insert again |
|
659 it = list.insert(it, QLatin1String("bar")); |
|
660 QVERIFY(list.size() == 2); |
|
661 QCOMPARE(*it++, QLatin1String("bar")); |
|
662 QCOMPARE(*it, QLatin1String("foo")); |
|
663 } |
|
664 |
132 QTEST_APPLESS_MAIN(tst_QList) |
665 QTEST_APPLESS_MAIN(tst_QList) |
133 #include "tst_qlist.moc" |
666 #include "tst_qlist.moc" |