From 11b3092cc4672f9832bfa26d5c31b63927d0e696 Mon Sep 17 00:00:00 2001 From: Lars Schmertmann Date: Mon, 29 Jun 2020 16:23:28 +0200 Subject: [PATCH] Refactor tst_Collections This improves the readability and avoids code duplication in tst_Collections::forwardDeclared. Also some warnings are fixed: * qSort is deprecated. * The = operator for LargeStatic needs to be implemented explicitly when a copy constructor is given. * QMap::insertMulti is deprecated, a MultiMap is required. Task-number: QTBUG-82978 Change-Id: I577f851394edfaa30154bd3417ce391635cc546d Reviewed-by: Friedemann Kleint --- .../tools/collections/tst_collections.cpp | 70 +++++++++++++------ 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/tests/auto/corelib/tools/collections/tst_collections.cpp b/tests/auto/corelib/tools/collections/tst_collections.cpp index ea68b21898a..f7b6f478cff 100644 --- a/tests/auto/corelib/tools/collections/tst_collections.cpp +++ b/tests/auto/corelib/tools/collections/tst_collections.cpp @@ -129,6 +129,12 @@ struct LargeStatic { static int count; LargeStatic():c(count) { ++count; } LargeStatic(const LargeStatic& o):c(o.c) { ++count; } + LargeStatic &operator=(const LargeStatic &o) + { + c = o.c; + ++count; + return *this; + }; ~LargeStatic() { --count; } int c; int data[8]; @@ -1326,19 +1332,19 @@ void tst_Collections::hash() hash1.unite(hash2); QCOMPARE(hash1.size(), 5); auto values = hash1.values(); - qSort(values); + std::sort(values.begin(), values.end()); QList expected; expected << "Gamma" << "Gamma" << "Beta" << "Gamma" << "Alpha"; - qSort(expected); + std::sort(expected.begin(), expected.end()); QCOMPARE(values, expected); hash2 = hash1; hash2.unite(hash2); QCOMPARE(hash2.size(), 10); values = hash2.values(); - qSort(values); + std::sort(values.begin(), values.end()); expected += expected; - qSort(expected); + std::sort(expected.begin(), expected.end()); QCOMPARE(values, expected); } } @@ -1645,12 +1651,12 @@ void tst_Collections::map() } { - QMap map1, map2; - map1.insertMulti(1, "Alpha"); - map1.insertMulti(1, "Gamma"); - map2.insertMulti(1, "Beta"); - map2.insertMulti(1, "Gamma"); - map2.insertMulti(1, "Gamma"); + QMultiMap map1, map2; + map1.insert(1, "Alpha"); + map1.insert(1, "Gamma"); + map2.insert(1, "Beta"); + map2.insert(1, "Gamma"); + map2.insert(1, "Gamma"); map1.unite(map2); QCOMPARE(map1.size(), 5); @@ -2895,16 +2901,40 @@ class T2; void tst_Collections::forwardDeclared() { - { typedef QHash C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } - { typedef QMultiHash C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } - { typedef QMap C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } - { typedef QMultiMap C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } - { typedef QPair C; C *x = 0; Q_UNUSED(x) } - { typedef QList C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } - { typedef QVector C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) Q_UNUSED(i) Q_UNUSED(j) } - { typedef QStack C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) Q_UNUSED(i) Q_UNUSED(j) } - { typedef QQueue C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } - { typedef QSet C; C *x = 0; /* C::iterator i; */ C::const_iterator j; Q_UNUSED(x) } +#define COMMA , +#define TEST(type) do { \ + using C = type; \ + C *x = nullptr; \ + C::iterator i; \ + C::const_iterator j; \ + Q_UNUSED(x); \ + Q_UNUSED(i); \ + Q_UNUSED(j); \ + } while (false) + + TEST(QHash); + TEST(QMap); + TEST(QMultiMap); + TEST(QList); + TEST(QVector); + TEST(QStack); + TEST(QQueue); +#undef TEST +#undef COMMA + + { + using C = QPair; + C *x = nullptr; + Q_UNUSED(x); + } + + { + using C = QSet; + C *x = nullptr; + C::const_iterator j; + Q_UNUSED(x); + Q_UNUSED(j); + } } class alignas(4) Aligned4