diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-06-27 11:14:32 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-06 21:31:25 +0200 |
commit | 792cb3989af2918f6a2878c22db51a009310756f (patch) | |
tree | 463b61ee3fc5deea2eb58fc8ecb30f8a5638ae74 /tests/auto/corelib/tools/qlist/tst_qlist.cpp | |
parent | 9a9a1a2a2e0c8bcbbaa178be3d67add3876df9b7 (diff) |
Add support for first(n), last(n) and sliced() to QList
This keeps the API symmetric with what we have in our string
classes.
Change-Id: I94c5b39b718ca2472f9ca645e7a42e4314636f67
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qlist/tst_qlist.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qlist/tst_qlist.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index ddf5e9c30a..fbd5cf6402 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -267,6 +267,7 @@ private slots: void last() const; void lastIndexOf() const; void mid() const; + void sliced() const; void moveInt() const; void moveMovable() const; void moveCustom() const; @@ -1287,6 +1288,13 @@ void tst_QList::first() const myvec.prepend(23); QCOMPARE(myvec.first(), 23); QCOMPARE(myvec.constFirst(), 23); + + + QCOMPARE(QList<int>().first(0), QList<int>()); + QCOMPARE(myvec.first(0), QList<int>()); + QCOMPARE(myvec.first(1), (QList<int>{23})); + QCOMPARE(myvec.first(2), (QList<int>{23, 42})); + QCOMPARE(myvec.first(3), myvec); } void tst_QList::constFirst() const @@ -1546,6 +1554,12 @@ void tst_QList::last() const myvec.remove(3); QCOMPARE(myvec.last(), QLatin1String("C")); QCOMPARE(myvec.constLast(), QLatin1String("C")); + + QCOMPARE(QList<QString>().last(0), QList<QString>()); + QCOMPARE(myvec.last(0), QList<QString>()); + QCOMPARE(myvec.last(1), (QList<QString>{QLatin1String("C")})); + QCOMPARE(myvec.last(2), (QList<QString>{QLatin1String("B"), QLatin1String("C")})); + QCOMPARE(myvec.last(3), myvec); } void tst_QList::constLast() const @@ -1648,6 +1662,22 @@ void tst_QList::mid() const QCOMPARE(list.mid(4), QList<QString>() << "buck" << "hello" << "kitty"); } +void tst_QList::sliced() const +{ + QList<QString> list; + list << "foo" << "bar" << "baz" << "bak" << "buck" << "hello" << "kitty"; + + QCOMPARE(QList<QString>().sliced(0), QList<QString>()); + QCOMPARE(QList<QString>().sliced(0, 0), QList<QString>()); + QCOMPARE(list.sliced(3, 3), QList<QString>() << "bak" << "buck" << "hello"); + QCOMPARE(list.sliced(3), QList<QString>() << "bak" << "buck" << "hello" << "kitty"); + QCOMPARE(list.sliced(6, 1), QList<QString>() << "kitty"); + QCOMPARE(list.sliced(6), QList<QString>() << "kitty"); + QCOMPARE(list.sliced(0, list.size()), list); + QCOMPARE(list.sliced(0), list); + QCOMPARE(list.sliced(4), QList<QString>() << "buck" << "hello" << "kitty"); +} + template <typename T> void tst_QList::qhash() const { |