From 9fb2faef7f7cf85175f35af32f2ff260d025982b Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Tue, 27 Apr 2010 17:59:05 +0200 Subject: Add unit tests for a number of QList methods. Granted, if this breaks, it will be noticed quickly - but it is desirable to catch such problems at test time rather than in production. Merge-request: 575 Reviewed-by: Benjamin Poulain --- tests/auto/qlist/tst_qlist.cpp | 489 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 489 insertions(+) diff --git a/tests/auto/qlist/tst_qlist.cpp b/tests/auto/qlist/tst_qlist.cpp index e2944cc..ba8aefa 100644 --- a/tests/auto/qlist/tst_qlist.cpp +++ b/tests/auto/qlist/tst_qlist.cpp @@ -62,6 +62,33 @@ private slots: 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 @@ -173,5 +200,467 @@ void tst_QList::mid() const QList() << "bak" << "buck" << "hello"); } +void tst_QList::at() const +{ + // test at() and make sure it functions correctly with some simple list manipulation. + QList 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 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 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 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 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 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 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 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 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 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 list; + list << "foo" << "bar" << "baz"; + + // move an item + list.move(0, list.count() - 1); + QCOMPARE(list, QList() << "bar" << "baz" << "foo"); + + // move it back + list.move(list.count() - 1, 0); + QCOMPARE(list, QList() << "foo" << "bar" << "baz"); + + // move an item in the middle + list.move(1, 0); + QCOMPARE(list, QList() << "bar" << "foo" << "baz"); +} + +void tst_QList::removeAll() const +{ + QList list; + list << "foo" << "bar" << "baz"; + + // remove one instance + list.removeAll(QLatin1String("bar")); + QCOMPARE(list, QList() << "foo" << "baz"); + + // many instances + list << "foo" << "bar" << "baz"; + list << "foo" << "bar" << "baz"; + list << "foo" << "bar" << "baz"; + list.removeAll(QLatin1String("bar")); + QCOMPARE(list, QList() << "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() << "foo" << "baz" << "foo" << "baz" << "foo" << "baz" << "foo" << "baz"); +} + +void tst_QList::removeAt() const +{ + QList list; + list << "foo" << "bar" << "baz"; + + // middle + list.removeAt(1); + QCOMPARE(list, QList() << "foo" << "baz"); + + // start + list.removeAt(0); + QCOMPARE(list, QList() << "baz"); + + // final + list.removeAt(0); + QCOMPARE(list, QList()); +} + +void tst_QList::removeOne() const +{ + QList list; + list << "foo" << "bar" << "baz"; + + // middle + list.removeOne(QLatin1String("bar")); + QCOMPARE(list, QList() << "foo" << "baz"); + + // start + list.removeOne(QLatin1String("foo")); + QCOMPARE(list, QList() << "baz"); + + // last + list.removeOne(QLatin1String("baz")); + QCOMPARE(list, QList()); + + // make sure it really only removes one :) + list << "foo" << "foo"; + list.removeOne("foo"); + QCOMPARE(list, QList() << "foo"); + + // try remove something that doesn't exist + list.removeOne(QLatin1String("you won't remove anything I hope")); + QCOMPARE(list, QList() << "foo"); +} + +void tst_QList::replace() const +{ + QList list; + list << "foo" << "bar" << "baz"; + + // start + list.replace(0, "moo"); + QCOMPARE(list, QList() << "moo" << "bar" << "baz"); + + // middle + list.replace(1, "cow"); + QCOMPARE(list, QList() << "moo" << "cow" << "baz"); + + // end + list.replace(2, "milk"); + QCOMPARE(list, QList() << "moo" << "cow" << "milk"); +} + +void tst_QList::startsWith() const +{ + QList 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 list; + list << "foo" << "bar" << "baz"; + + // swap + list.swap(0, 2); + QCOMPARE(list, QList() << "baz" << "bar" << "foo"); + + // swap again + list.swap(1, 2); + QCOMPARE(list, QList() << "baz" << "foo" << "bar"); +} + +void tst_QList::takeAt() const +{ + QList 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 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 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 list; + list << "foo" << "bar" << "baz"; + + // no duplicates + QCOMPARE(list.toSet(), QSet() << "foo" << "bar" << "baz"); + QCOMPARE(list, QList() << "foo" << "bar" << "baz"); + + // duplicates (is this more of a QSet test?) + list << "foo" << "bar" << "baz"; + QCOMPARE(list.toSet(), QSet() << "foo" << "bar" << "baz"); + QCOMPARE(list, QList() << "foo" << "bar" << "baz" << "foo" << "bar" << "baz"); +} + +void tst_QList::toStdList() const +{ + QList list; + list << "foo" << "bar" << "baz"; + + // yuck. + std::list slist; + slist.push_back(QLatin1String("foo")); + slist.push_back(QLatin1String("bar")); + slist.push_back(QLatin1String("baz")); + + QCOMPARE(list.toStdList(), slist); + QCOMPARE(list, QList() << "foo" << "bar" << "baz"); +} + +void tst_QList::toVector() const +{ + QList list; + list << "foo" << "bar" << "baz"; + + QCOMPARE(list.toVector(), QVector() << "foo" << "bar" << "baz"); +} + +void tst_QList::value() const +{ + QList 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 list; + list << "foo" << "bar" << "baz"; + + QList 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() << "foo" << "bar" << "baz" << "foo" << "bar" << "baz" << "not equal"); + + // = + list = listtwo; + QCOMPARE(list, listtwo); + QCOMPARE(list, QList() << "foo" << "bar" << "baz" << "not equal"); + + // [] + QCOMPARE(list[0], QLatin1String("foo")); + QCOMPARE(list[list.size() - 1], QLatin1String("not equal")); +} + +void tst_QList::testSTLIterators() const +{ + QList list; + + // create a list + list << "foo" << "bar" << "baz"; + QList::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" -- cgit v0.12