diff options
Diffstat (limited to 'tests/auto/corelib/tools/qlist')
-rw-r--r-- | tests/auto/corelib/tools/qlist/tst_qlist.cpp | 365 |
1 files changed, 265 insertions, 100 deletions
diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index aab09cdb9f..1207986dde 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** $QT_END_LICENSE$ @@ -35,6 +35,17 @@ #include <QtTest/QtTest> #include <QList> +template <typename T, class MemoryLayout> +class is_qlist_array_memory_layout { + struct No { char c; }; + struct Yes { No n[2]; }; + Q_STATIC_ASSERT(sizeof(No) != sizeof(Yes)); + static No check(...); + static Yes check(MemoryLayout); +public: + enum { value = sizeof(check(typename QList<T>::MemoryLayout())) == sizeof(Yes) }; +}; + struct Movable { Movable(char input = 'j') : i(input) @@ -79,14 +90,16 @@ private: static int liveCount; enum State { Constructed = 106, Destructed = 110 }; - State state; + uchar state; - static void check(const State state1, const State state2) + static void check(const uchar state1, const uchar state2) { QCOMPARE(state1, state2); } }; +Q_STATIC_ASSERT(sizeof(Movable) < sizeof(void*)); + int Movable::liveCount = 0; QT_BEGIN_NAMESPACE @@ -100,6 +113,75 @@ int qHash(const Movable& movable) return qHash(movable.i); } +struct Optimal +{ + Optimal(char input = 'j') + : i(input), + state(Constructed) + { + ++liveCount; + } + Optimal(const Optimal &other) + : i(other.i), + state(Constructed) + { + check(other.state, Constructed); + ++liveCount; + } + + ~Optimal() + { + check(state, Constructed); + i = 0; + --liveCount; + state = Destructed; + } + + bool operator ==(const Optimal &other) const + { + check(state, Constructed); + check(other.state, Constructed); + return i == other.i; + } + + Optimal &operator=(const Optimal &other) + { + check(state, Constructed); + check(other.state, Constructed); + i = other.i; + return *this; + } + char i; + + static int getLiveCount() { return liveCount; } +private: + static int liveCount; + + enum State { Constructed = 106, Destructed = 110 }; + uchar state; + char padding[sizeof(void*) - 2]; + + static void check(const uchar state1, const uchar state2) + { + QCOMPARE(state1, state2); + } +}; + +Q_STATIC_ASSERT(sizeof(Optimal) == sizeof(void*)); + +int Optimal::liveCount = 0; + +QT_BEGIN_NAMESPACE +Q_DECLARE_TYPEINFO(Optimal, Q_MOVABLE_TYPE); +QT_END_NAMESPACE + +Q_DECLARE_METATYPE(Optimal); + +int qHash(const Optimal& key) +{ + return qHash(key.i); +} + struct Complex { Complex(int val = 0) @@ -160,108 +242,131 @@ Q_STATIC_ASSERT(!QTypeInfo<int>::isStatic); Q_STATIC_ASSERT(!QTypeInfo<int>::isComplex); Q_STATIC_ASSERT(!QTypeInfo<Movable>::isStatic); Q_STATIC_ASSERT(QTypeInfo<Movable>::isComplex); +Q_STATIC_ASSERT(!QTypeInfo<Optimal>::isStatic); +Q_STATIC_ASSERT(QTypeInfo<Optimal>::isComplex); Q_STATIC_ASSERT(QTypeInfo<Complex>::isStatic); Q_STATIC_ASSERT(QTypeInfo<Complex>::isComplex); +// iow: +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<int, QListData::NotIndirectLayout> ::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<int, QListData::IndirectLayout> ::value)); + +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Optimal, QListData::InlineWithPaddingLayout> ::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Optimal, QListData::NotArrayCompatibleLayout>::value)); +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Optimal, QListData::NotIndirectLayout> ::value)); +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Optimal, QListData::ArrayCompatibleLayout> ::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Optimal, QListData::IndirectLayout> ::value)); + +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Movable, QListData::InlineWithPaddingLayout> ::value)); +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Movable, QListData::NotArrayCompatibleLayout>::value)); +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Movable, QListData::NotIndirectLayout> ::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Movable, QListData::ArrayCompatibleLayout> ::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Movable, QListData::IndirectLayout> ::value)); + +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Complex, QListData::InlineWithPaddingLayout> ::value)); +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Complex, QListData::NotArrayCompatibleLayout>::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Complex, QListData::NotIndirectLayout> ::value)); +Q_STATIC_ASSERT((!is_qlist_array_memory_layout<Complex, QListData::ArrayCompatibleLayout> ::value)); +Q_STATIC_ASSERT(( is_qlist_array_memory_layout<Complex, QListData::IndirectLayout> ::value)); class tst_QList : public QObject { Q_OBJECT private slots: - void lengthInt() const; + void lengthOptimal() const; void lengthMovable() const; void lengthComplex() const; void lengthSignature() const; - void appendInt() const; + void appendOptimal() const; void appendMovable() const; void appendComplex() const; void prepend() const; - void midInt() const; + void midOptimal() const; void midMovable() const; void midComplex() const; - void atInt() const; + void atOptimal() const; void atMovable() const; void atComplex() const; - void firstInt() const; + void firstOptimal() const; void firstMovable() const; void firstComplex() const; - void lastInt() const; + void lastOptimal() const; void lastMovable() const; void lastComplex() const; - void beginInt() const; + void beginOptimal() const; void beginMovable() const; void beginComplex() const; - void endInt() const; + void endOptimal() const; void endMovable() const; void endComplex() const; - void containsInt() const; + void containsOptimal() const; void containsMovable() const; void containsComplex() const; - void countInt() const; + void countOptimal() const; void countMovable() const; void countComplex() const; - void emptyInt() const; + void emptyOptimal() const; void emptyMovable() const; void emptyComplex() const; - void endsWithInt() const; + void endsWithOptimal() const; void endsWithMovable() const; void endsWithComplex() const; - void lastIndexOfInt() const; + void lastIndexOfOptimal() const; void lastIndexOfMovable() const; void lastIndexOfComplex() const; - void moveInt() const; + void moveOptimal() const; void moveMovable() const; void moveComplex() const; - void removeAllInt() const; + void removeAllOptimal() const; void removeAllMovable() const; void removeAllComplex() const; - void removeAtInt() const; + void removeAtOptimal() const; void removeAtMovable() const; void removeAtComplex() const; - void removeOneInt() const; + void removeOneOptimal() const; void removeOneMovable() const; void removeOneComplex() const; - void replaceInt() const; + void replaceOptimal() const; void replaceMovable() const; void replaceComplex() const; - void startsWithInt() const; + void startsWithOptimal() const; void startsWithMovable() const; void startsWithComplex() const; - void swapInt() const; + void swapOptimal() const; void swapMovable() const; void swapComplex() const; - void takeAtInt() const; + void takeAtOptimal() const; void takeAtMovable() const; void takeAtComplex() const; - void takeFirstInt() const; + void takeFirstOptimal() const; void takeFirstMovable() const; void takeFirstComplex() const; - void takeLastInt() const; + void takeLastOptimal() const; void takeLastMovable() const; void takeLastComplex() const; - void toSetInt() const; + void toSetOptimal() const; void toSetMovable() const; void toSetComplex() const; - void toStdListInt() const; + void toStdListOptimal() const; void toStdListMovable() const; void toStdListComplex() const; - void toVectorInt() const; + void toVectorOptimal() const; void toVectorMovable() const; void toVectorComplex() const; - void valueInt() const; + void valueOptimal() const; void valueMovable() const; void valueComplex() const; - void testOperatorsInt() const; + void testOperatorsOptimal() const; void testOperatorsMovable() const; void testOperatorsComplex() const; - void testSTLIteratorsInt() const; + void testSTLIteratorsOptimal() const; void testSTLIteratorsMovable() const; void testSTLIteratorsComplex() const; void initializeList() const; - void constSharedNullInt() const; + void constSharedNullOptimal() const; void constSharedNullMovable() const; void constSharedNullComplex() const; void setSharableInt_data() const; @@ -320,7 +425,7 @@ template<typename T> struct SimpleValue }; template<> -const int SimpleValue<int>::values[] = { 10, 20, 30, 40, 100, 101, 102 }; +const Optimal SimpleValue<Optimal>::values[] = { 10, 20, 30, 40, 100, 101, 102 }; template<> const Movable SimpleValue<Movable>::values[] = { 10, 20, 30, 40, 100, 101, 102 }; template<> @@ -369,9 +474,11 @@ void tst_QList::length() const } } -void tst_QList::lengthInt() const +void tst_QList::lengthOptimal() const { - length<int>(); + const int liveCount = Optimal::getLiveCount(); + length<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::lengthMovable() const @@ -421,9 +528,11 @@ void tst_QList::append() const QCOMPARE(list1, listTotal); } -void tst_QList::appendInt() const +void tst_QList::appendOptimal() const { - append<int>(); + const int liveCount = Optimal::getLiveCount(); + append<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::appendMovable() const @@ -486,9 +595,11 @@ void tst_QList::mid() const QCOMPARE(list1.mid(1, 1).length(), 0); } -void tst_QList::midInt() const +void tst_QList::midOptimal() const { - mid<int>(); + const int liveCount = Optimal::getLiveCount(); + mid<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::midMovable() const @@ -534,9 +645,11 @@ void tst_QList::at() const QCOMPARE(list.at(2), T_CAT); } -void tst_QList::atInt() const +void tst_QList::atOptimal() const { - at<int>(); + const int liveCount = Optimal::getLiveCount(); + at<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::atMovable() const @@ -567,9 +680,11 @@ void tst_QList::first() const QCOMPARE(list.first(), T_BAR); } -void tst_QList::firstInt() const +void tst_QList::firstOptimal() const { - first<int>(); + const int liveCount = Optimal::getLiveCount(); + first<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::firstMovable() const @@ -600,9 +715,11 @@ void tst_QList::last() const QCOMPARE(list.last(), T_FOO); } -void tst_QList::lastInt() const +void tst_QList::lastOptimal() const { - last<int>(); + const int liveCount = Optimal::getLiveCount(); + last<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::lastMovable() const @@ -633,9 +750,11 @@ void tst_QList::begin() const QCOMPARE(*list.begin(), T_BAR); } -void tst_QList::beginInt() const +void tst_QList::beginOptimal() const { - begin<int>(); + const int liveCount = Optimal::getLiveCount(); + begin<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::beginMovable() const @@ -666,9 +785,11 @@ void tst_QList::end() const QCOMPARE(*--list.end(), T_FOO); } -void tst_QList::endInt() const +void tst_QList::endOptimal() const { - end<int>(); + const int liveCount = Optimal::getLiveCount(); + end<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::endMovable() const @@ -699,9 +820,11 @@ void tst_QList::contains() const QVERIFY(list.contains(T_BLAH) == true); } -void tst_QList::containsInt() const +void tst_QList::containsOptimal() const { - contains<int>(); + const int liveCount = Optimal::getLiveCount(); + contains<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::containsMovable() const @@ -743,9 +866,11 @@ void tst_QList::count() const QVERIFY(list.count() == 0); } -void tst_QList::countInt() const +void tst_QList::countOptimal() const { - count<int>(); + const int liveCount = Optimal::getLiveCount(); + count<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::countMovable() const @@ -779,9 +904,11 @@ void tst_QList::empty() const QVERIFY(list.empty()); } -void tst_QList::emptyInt() const +void tst_QList::emptyOptimal() const { - empty<int>(); + const int liveCount = Optimal::getLiveCount(); + empty<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::emptyMovable() const @@ -813,9 +940,11 @@ void tst_QList::endsWith() const QVERIFY(list.endsWith(T_BAR)); } -void tst_QList::endsWithInt() const +void tst_QList::endsWithOptimal() const { - endsWith<int>(); + const int liveCount = Optimal::getLiveCount(); + endsWith<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::endsWithMovable() const @@ -856,9 +985,11 @@ void tst_QList::lastIndexOf() const QVERIFY(list.lastIndexOf(T_BAZ, 1) == -1); } -void tst_QList::lastIndexOfInt() const +void tst_QList::lastIndexOfOptimal() const { - lastIndexOf<int>(); + const int liveCount = Optimal::getLiveCount(); + lastIndexOf<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::lastIndexOfMovable() const @@ -894,9 +1025,11 @@ void tst_QList::move() const QCOMPARE(list, QList<T>() << T_BAR << T_FOO << T_BAZ); } -void tst_QList::moveInt() const +void tst_QList::moveOptimal() const { - move<int>(); + const int liveCount = Optimal::getLiveCount(); + move<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::moveMovable() const @@ -933,9 +1066,11 @@ void tst_QList::removeAll() const QCOMPARE(list, QList<T>() << T_FOO << T_BAZ << T_FOO << T_BAZ << T_FOO << T_BAZ << T_FOO << T_BAZ); } -void tst_QList::removeAllInt() const +void tst_QList::removeAllOptimal() const { - removeAll<int>(); + const int liveCount = Optimal::getLiveCount(); + removeAll<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::removeAllMovable() const @@ -971,9 +1106,11 @@ void tst_QList::removeAt() const QCOMPARE(list, QList<T>()); } -void tst_QList::removeAtInt() const +void tst_QList::removeAtOptimal() const { - removeAt<int>(); + const int liveCount = Optimal::getLiveCount(); + removeAt<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::removeAtMovable() const @@ -1018,9 +1155,11 @@ void tst_QList::removeOne() const QCOMPARE(list, QList<T>() << T_FOO); } -void tst_QList::removeOneInt() const +void tst_QList::removeOneOptimal() const { - removeOne<int>(); + const int liveCount = Optimal::getLiveCount(); + removeOne<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::removeOneMovable() const @@ -1059,9 +1198,11 @@ void tst_QList::replace() const << T_DOG << T_BLAH); } -void tst_QList::replaceInt() const +void tst_QList::replaceOptimal() const { - replace<int>(); + const int liveCount = Optimal::getLiveCount(); + replace<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::replaceMovable() const @@ -1092,9 +1233,11 @@ void tst_QList::startsWith() const QVERIFY(list.startsWith(T_BAR)); } -void tst_QList::startsWithInt() const +void tst_QList::startsWithOptimal() const { - startsWith<int>(); + const int liveCount = Optimal::getLiveCount(); + startsWith<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::startsWithMovable() const @@ -1133,9 +1276,11 @@ void tst_QList::swap() const QCOMPARE(list2, QList<T>() << T_BAZ << T_FOO << T_BAR); } -void tst_QList::swapInt() const +void tst_QList::swapOptimal() const { - swap<int>(); + const int liveCount = Optimal::getLiveCount(); + swap<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::swapMovable() const @@ -1166,9 +1311,11 @@ void tst_QList::takeAt() const QVERIFY(list.size() == 0); } -void tst_QList::takeAtInt() const +void tst_QList::takeAtOptimal() const { - takeAt<int>(); + const int liveCount = Optimal::getLiveCount(); + takeAt<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::takeAtMovable() const @@ -1199,9 +1346,11 @@ void tst_QList::takeFirst() const QVERIFY(list.size() == 0); } -void tst_QList::takeFirstInt() const +void tst_QList::takeFirstOptimal() const { - takeFirst<int>(); + const int liveCount = Optimal::getLiveCount(); + takeFirst<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::takeFirstMovable() const @@ -1229,9 +1378,11 @@ void tst_QList::takeLast() const QCOMPARE(list.takeLast(), T_FOO); } -void tst_QList::takeLastInt() const +void tst_QList::takeLastOptimal() const { - takeLast<int>(); + const int liveCount = Optimal::getLiveCount(); + takeLast<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::takeLastMovable() const @@ -1265,9 +1416,11 @@ void tst_QList::toSet() const << T_FOO << T_BAR << T_BAZ); } -void tst_QList::toSetInt() const +void tst_QList::toSetOptimal() const { - toSet<int>(); + const int liveCount = Optimal::getLiveCount(); + toSet<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::toSetMovable() const @@ -1300,9 +1453,11 @@ void tst_QList::toStdList() const QCOMPARE(list, QList<T>() << T_FOO << T_BAR << T_BAZ); } -void tst_QList::toStdListInt() const +void tst_QList::toStdListOptimal() const { - toStdList<int>(); + const int liveCount = Optimal::getLiveCount(); + toStdList<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::toStdListMovable() const @@ -1328,9 +1483,11 @@ void tst_QList::toVector() const QCOMPARE(list.toVector(), QVector<T>() << T_FOO << T_BAR << T_BAZ); } -void tst_QList::toVectorInt() const +void tst_QList::toVectorOptimal() const { - toVector<int>(); + const int liveCount = Optimal::getLiveCount(); + toVector<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::toVectorMovable() const @@ -1367,9 +1524,11 @@ void tst_QList::value() const QCOMPARE(list.value(3, defaultT), defaultT); } -void tst_QList::valueInt() const +void tst_QList::valueOptimal() const { - value<int>(); + const int liveCount = Optimal::getLiveCount(); + value<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::valueMovable() const @@ -1419,9 +1578,11 @@ void tst_QList::testOperators() const QCOMPARE(list[list.size() - 1], T_CAT); } -void tst_QList::testOperatorsInt() const +void tst_QList::testOperatorsOptimal() const { - testOperators<int>(); + const int liveCount = Optimal::getLiveCount(); + testOperators<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::testOperatorsMovable() const @@ -1478,9 +1639,11 @@ void tst_QList::testSTLIterators() const QCOMPARE(*it, T_FOO); } -void tst_QList::testSTLIteratorsInt() const +void tst_QList::testSTLIteratorsOptimal() const { - testSTLIterators<int>(); + const int liveCount = Optimal::getLiveCount(); + testSTLIterators<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::testSTLIteratorsMovable() const @@ -1525,9 +1688,11 @@ void tst_QList::constSharedNull() const QVERIFY(!list2.isDetached()); } -void tst_QList::constSharedNullInt() const +void tst_QList::constSharedNullOptimal() const { - constSharedNull<int>(); + const int liveCount = Optimal::getLiveCount(); + constSharedNull<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); } void tst_QList::constSharedNullMovable() const |