diff options
author | Leander Beernaert <leander.beernaert@qt.io> | 2020-01-22 13:47:08 +0100 |
---|---|---|
committer | Leander Beernaert <leander.beernaert@qt.io> | 2020-01-24 13:17:33 +0100 |
commit | 502d3d6744913899da87acfda5ebdab42c40329e (patch) | |
tree | 16658a328503bfd5a62b4fd5d69ffb66e9854b18 /tests/auto/corelib/tools | |
parent | d1be8b9ceb2c7b20bbe53a07154c79699540ea3d (diff) | |
parent | 06bb315beb6c2c398223cfe52cbc7f66e14a8557 (diff) |
Merge remote-tracking branch 'origin/dev' into merge-dev
Change-Id: I31b761cfd5ea01373c60d02a5da8c33398d34739
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r-- | tests/auto/corelib/tools/collections/tst_collections.cpp | 24 | ||||
-rw-r--r-- | tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp | 4 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qarraydata/simplevector.h | 49 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp | 307 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp | 93 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qflatmap/qflatmap.pro | 4 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp | 453 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qhash/tst_qhash.cpp | 73 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qmap/tst_qmap.cpp | 96 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp | 2 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qvector/tst_qvector.cpp | 91 | ||||
-rw-r--r-- | tests/auto/corelib/tools/tools.pro | 1 |
12 files changed, 872 insertions, 325 deletions
diff --git a/tests/auto/corelib/tools/collections/tst_collections.cpp b/tests/auto/corelib/tools/collections/tst_collections.cpp index 5ba8545b61..023b22b4ba 100644 --- a/tests/auto/corelib/tools/collections/tst_collections.cpp +++ b/tests/auto/corelib/tools/collections/tst_collections.cpp @@ -3583,30 +3583,8 @@ template<class Container> void insert_remove_loop_impl() } -//Add insert(int, int, T) so it has the same interface as QVector and QVarLengthArray for the test. template<typename T> -struct ExtList : QList<T> { - using QList<T>::insert; - void insert(int before, int n, const T&x) { - while (n--) { - this->insert(before, x ); - } - } - void insert(typename QList<T>::iterator before, int n, const T&x) { - while (n--) { - before = this->insert(before, x); - } - } - - void remove(int i) { - this->removeAt(i); - } - void remove(int i, int n) { - while (n--) { - this->removeAt(i); - } - } -}; +using ExtList = QList<T>; void tst_Collections::insert_remove_loop() { diff --git a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp index 7a1e13e83d..0e4517e740 100644 --- a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp +++ b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp @@ -51,7 +51,7 @@ #ifdef Q_CC_MSVC #define COMPILER_HAS_STDLIB_INCLUDE(x) 1 #else -#define COMPILER_HAS_STDLIB_INCLUDE(x) QT_HAS_INCLUDE(x) +#define COMPILER_HAS_STDLIB_INCLUDE(x) __has_include(x) #endif #if COMPILER_HAS_STDLIB_INCLUDE(<forward_list>) @@ -795,8 +795,6 @@ template <> QStringView make(int size) { return QStringView(s_string).left(siz template <> QLatin1String make(int size) { return QLatin1String("\1\2\3\4\5\6\7", size); } template <typename T> T clean(T &&t) { return std::forward<T>(t); } -inline QChar clean(QCharRef ch) { return ch; } -inline char clean(QByteRef ch) { return ch; } inline char clean(QLatin1Char ch) { return ch.toLatin1(); } template <typename Container> diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index e587ad27b5..94cee5d887 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -76,21 +76,32 @@ public: { } - explicit SimpleVector(Data *ptr) - : d(ptr) + template <size_t N> + explicit SimpleVector(QStaticArrayData<T, N> &ptr) + : d(static_cast<Data *>(&ptr.header), ptr.data, N) + { + } + + SimpleVector(Data *header, T *data, size_t len = 0) + : d(header, data, len) + { + } + + explicit SimpleVector(QPair<Data*, T*> ptr, size_t len = 0) + : d(ptr, len) { } - bool empty() const { return d->size == 0; } + bool empty() const { return d.size == 0; } bool isNull() const { return d.isNull(); } bool isEmpty() const { return this->empty(); } - bool isStatic() const { return d->ref.isStatic(); } - bool isShared() const { return d->ref.isShared(); } + bool isStatic() const { return d->isStatic(); } + bool isShared() const { return d->isShared(); } bool isSharedWith(const SimpleVector &other) const { return d == other.d; } - size_t size() const { return d->size; } - size_t capacity() const { return d->alloc; } + size_t size() const { return d.size; } + size_t capacity() const { return d->constAllocatedCapacity(); } iterator begin() { detach(); return d->begin(); } iterator end() { detach(); return d->end(); } @@ -139,10 +150,10 @@ public: return; if (n <= capacity()) { - if (d->capacityReserved) + if (d->flags() & Data::CapacityReserved) return; - if (!d->ref.isShared()) { - d->capacityReserved = 1; + if (!d->isShared()) { + d->flags() |= Data::CapacityReserved; return; } } @@ -159,7 +170,7 @@ public: if (size() == newSize) return; - if (d.needsDetach() || newSize > capacity()) { + if (d->needsDetach() || newSize > capacity()) { SimpleVector detached(Data::allocate( d->detachCapacity(newSize), d->detachFlags())); if (newSize) { @@ -195,11 +206,11 @@ public: return; T *const begin = d->begin(); - if (d.needsDetach() + if (d->needsDetach() || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::Grow)); + d->detachFlags() | Data::GrowsForward)); detached.d->copyAppend(first, last); detached.d->copyAppend(begin, begin + d->size); @@ -216,11 +227,11 @@ public: if (first == last) return; - if (d.needsDetach() + if (d->needsDetach() || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::Grow)); + d->detachFlags() | Data::GrowsForward)); if (d->size) { const T *const begin = constBegin(); @@ -256,11 +267,11 @@ public: const iterator begin = d->begin(); const iterator where = begin + position; const iterator end = begin + d->size; - if (d.needsDetach() + if (d->needsDetach() || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::Grow)); + d->detachFlags() | Data::GrowsForward)); if (position) detached.d->copyAppend(begin, where); @@ -294,7 +305,7 @@ public: const T *const begin = d->begin(); const T *const end = begin + d->size; - if (d.needsDetach()) { + if (d->needsDetach()) { SimpleVector detached(Data::allocate( d->detachCapacity(size() - (last - first)), d->detachFlags())); @@ -328,7 +339,7 @@ public: } static SimpleVector fromRawData(const T *data, size_t size, - QArrayData::AllocationOptions options = Data::Default) + QArrayData::ArrayOptions options = Data::DefaultRawFlags) { return SimpleVector(Data::fromRawData(data, size, options)); } diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 47bdc2fd38..1366eebf97 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -37,8 +37,8 @@ struct SharedNullVerifier { SharedNullVerifier() { - Q_ASSERT(QArrayData::shared_null[0].ref.isStatic()); - Q_ASSERT(QArrayData::shared_null[0].ref.isShared()); + Q_ASSERT(QArrayData::shared_null[0].isStatic()); + Q_ASSERT(QArrayData::shared_null[0].isShared()); } }; @@ -56,7 +56,6 @@ class tst_QArrayData : public QObject private slots: void referenceCounting(); void sharedNullEmpty(); - void staticData(); void simpleVector(); void simpleVectorReserve_data(); void simpleVectorReserve(); @@ -84,43 +83,42 @@ void tst_QArrayData::referenceCounting() { { // Reference counting initialized to 1 (owned) - QArrayData array = { { Q_BASIC_ATOMIC_INITIALIZER(1) }, 0, 0, 0, 0 }; + QArrayData array = { Q_BASIC_ATOMIC_INITIALIZER(1), QArrayData::DefaultRawFlags, 0 }; - QCOMPARE(array.ref.atomic.loadRelaxed(), 1); + QCOMPARE(array.ref_.loadRelaxed(), 1); - QVERIFY(!array.ref.isStatic()); + QVERIFY(!array.isStatic()); - QVERIFY(array.ref.ref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), 2); + QVERIFY(array.ref()); + QCOMPARE(array.ref_.loadRelaxed(), 2); - QVERIFY(array.ref.deref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), 1); + QVERIFY(array.deref()); + QCOMPARE(array.ref_.loadRelaxed(), 1); - QVERIFY(array.ref.ref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), 2); + QVERIFY(array.ref()); + QCOMPARE(array.ref_.loadRelaxed(), 2); - QVERIFY(array.ref.deref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), 1); + QVERIFY(array.deref()); + QCOMPARE(array.ref_.loadRelaxed(), 1); - QVERIFY(!array.ref.deref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), 0); + QVERIFY(!array.deref()); + QCOMPARE(array.ref_.loadRelaxed(), 0); // Now would be a good time to free/release allocated data } - { // Reference counting initialized to -1 (static read-only data) - QArrayData array = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; + QArrayData array = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }; - QCOMPARE(array.ref.atomic.loadRelaxed(), -1); + QCOMPARE(array.ref_.loadRelaxed(), -1); - QVERIFY(array.ref.isStatic()); + QVERIFY(array.isStatic()); - QVERIFY(array.ref.ref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), -1); + QVERIFY(array.ref()); + QCOMPARE(array.ref_.loadRelaxed(), -1); - QVERIFY(array.ref.deref()); - QCOMPARE(array.ref.atomic.loadRelaxed(), -1); + QVERIFY(array.deref()); + QCOMPARE(array.ref_.loadRelaxed(), -1); } } @@ -128,69 +126,39 @@ void tst_QArrayData::referenceCounting() void tst_QArrayData::sharedNullEmpty() { QArrayData *null = const_cast<QArrayData *>(QArrayData::shared_null); - QArrayData *empty = QArrayData::allocate(1, alignof(QArrayData), 0); - - QVERIFY(null->ref.isStatic()); - QVERIFY(null->ref.isShared()); + QArrayData *empty; + QArrayData::allocate(&empty, 1, alignof(QArrayData), 0); - QVERIFY(empty->ref.isStatic()); - QVERIFY(empty->ref.isShared()); + QVERIFY(null->isStatic()); + QVERIFY(null->isShared()); - QCOMPARE(null->ref.atomic.loadRelaxed(), -1); - QCOMPARE(empty->ref.atomic.loadRelaxed(), -1); + QVERIFY(empty->isStatic()); + QVERIFY(empty->isShared()); - QVERIFY(null->ref.ref()); - QVERIFY(empty->ref.ref()); + QCOMPARE(null->ref_.loadRelaxed(), -1); + QCOMPARE(empty->ref_.loadRelaxed(), -1); - QCOMPARE(null->ref.atomic.loadRelaxed(), -1); - QCOMPARE(empty->ref.atomic.loadRelaxed(), -1); + QCOMPARE(null->ref_.loadRelaxed(), -1); + QCOMPARE(empty->ref_.loadRelaxed(), -1); - QVERIFY(null->ref.deref()); - QVERIFY(empty->ref.deref()); + QVERIFY(null->deref()); + QVERIFY(empty->deref()); - QCOMPARE(null->ref.atomic.loadRelaxed(), -1); - QCOMPARE(empty->ref.atomic.loadRelaxed(), -1); + QCOMPARE(null->ref_.loadRelaxed(), -1); + QCOMPARE(empty->ref_.loadRelaxed(), -1); QVERIFY(null != empty); - QCOMPARE(null->size, 0); - QCOMPARE(null->alloc, 0u); - QCOMPARE(null->capacityReserved, 0u); + QCOMPARE(null->allocatedCapacity(), size_t(0)); - QCOMPARE(empty->size, 0); - QCOMPARE(empty->alloc, 0u); - QCOMPARE(empty->capacityReserved, 0u); -} - -void tst_QArrayData::staticData() -{ - QStaticArrayData<char, 10> charArray = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(char, 10), - { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } - }; - QStaticArrayData<int, 10> intArray = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 10), - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } - }; - QStaticArrayData<double, 10> doubleArray = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(double, 10), - { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f } - }; - - QCOMPARE(charArray.header.size, 10); - QCOMPARE(intArray.header.size, 10); - QCOMPARE(doubleArray.header.size, 10); - - QCOMPARE(charArray.header.data(), reinterpret_cast<void *>(&charArray.data)); - QCOMPARE(intArray.header.data(), reinterpret_cast<void *>(&intArray.data)); - QCOMPARE(doubleArray.header.data(), reinterpret_cast<void *>(&doubleArray.data)); + QCOMPARE(empty->allocatedCapacity(), size_t(0)); } void tst_QArrayData::simpleVector() { - QArrayData data0 = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; + QArrayData data0 = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }; QStaticArrayData<int, 7> data1 = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 7), + { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }, { 0, 1, 2, 3, 4, 5, 6 } }; @@ -198,10 +166,10 @@ void tst_QArrayData::simpleVector() SimpleVector<int> v1; SimpleVector<int> v2(v1); - SimpleVector<int> v3(static_cast<QTypedArrayData<int> *>(&data0)); - SimpleVector<int> v4(static_cast<QTypedArrayData<int> *>(&data1.header)); - SimpleVector<int> v5(static_cast<QTypedArrayData<int> *>(&data0)); - SimpleVector<int> v6(static_cast<QTypedArrayData<int> *>(&data1.header)); + SimpleVector<int> v3(static_cast<QTypedArrayData<int> *>(&data0), 0, 0); + SimpleVector<int> v4(data1); + SimpleVector<int> v5(static_cast<QTypedArrayData<int> *>(&data0), 0, 0); + SimpleVector<int> v6(data1); SimpleVector<int> v7(10, 5); SimpleVector<int> v8(array, array + sizeof(array)/sizeof(*array)); @@ -461,11 +429,13 @@ void tst_QArrayData::simpleVectorReserve_data() QTest::newRow("non-empty") << SimpleVector<int>(5, 42) << size_t(5) << size_t(5); static const QStaticArrayData<int, 15> array = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 15), + { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } }; - QArrayDataPointerRef<int> p = { + const QArrayDataPointerRef<int> p = { static_cast<QTypedArrayData<int> *>( - const_cast<QArrayData *>(&array.header)) }; + const_cast<QArrayData *>(&array.header)), + const_cast<int *>(array.data), + sizeof(array.data) / sizeof(array.data[0]) }; QTest::newRow("static") << SimpleVector<int>(p) << size_t(0) << size_t(15); QTest::newRow("raw-data") << SimpleVector<int>::fromRawData(array.data, 15) << size_t(0) << size_t(15); @@ -528,13 +498,13 @@ struct Deallocator }; Q_DECLARE_METATYPE(const QArrayData *) -Q_DECLARE_METATYPE(QArrayData::AllocationOptions) +Q_DECLARE_METATYPE(QArrayData::ArrayOptions) void tst_QArrayData::allocate_data() { QTest::addColumn<size_t>("objectSize"); QTest::addColumn<size_t>("alignment"); - QTest::addColumn<QArrayData::AllocationOptions>("allocateOptions"); + QTest::addColumn<QArrayData::ArrayOptions>("allocateOptions"); QTest::addColumn<bool>("isCapacityReserved"); QTest::addColumn<const QArrayData *>("commonEmpty"); @@ -548,18 +518,19 @@ void tst_QArrayData::allocate_data() { "void *", sizeof(void *), alignof(void *) } }; - QArrayData *shared_empty = QArrayData::allocate(0, alignof(QArrayData), 0); + QArrayData *shared_empty; + QArrayData::allocate(&shared_empty, 1, alignof(QArrayData), 0); QVERIFY(shared_empty); struct { char const *description; - QArrayData::AllocationOptions allocateOptions; + QArrayData::ArrayOptions allocateOptions; bool isCapacityReserved; const QArrayData *commonEmpty; } options[] = { - { "Default", QArrayData::Default, false, shared_empty }, + { "Default", QArrayData::DefaultAllocationFlags, false, shared_empty }, { "Reserved", QArrayData::CapacityReserved, true, shared_empty }, - { "Grow", QArrayData::Grow, false, shared_empty } + { "Grow", QArrayData::GrowsForward, false, shared_empty } }; for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i) @@ -577,7 +548,7 @@ void tst_QArrayData::allocate() { QFETCH(size_t, objectSize); QFETCH(size_t, alignment); - QFETCH(QArrayData::AllocationOptions, allocateOptions); + QFETCH(QArrayData::ArrayOptions, allocateOptions); QFETCH(bool, isCapacityReserved); QFETCH(const QArrayData *, commonEmpty); @@ -586,27 +557,29 @@ void tst_QArrayData::allocate() size_t minAlignment = qMax(alignment, alignof(QArrayData)); // Shared Empty - QCOMPARE(QArrayData::allocate(objectSize, minAlignment, 0, - QArrayData::AllocationOptions(allocateOptions)), commonEmpty); + QArrayData *empty; + QCOMPARE((QArrayData::allocate(&empty, objectSize, minAlignment, 0, + QArrayData::ArrayOptions(allocateOptions)), empty), commonEmpty); Deallocator keeper(objectSize, minAlignment); keeper.headers.reserve(1024); for (int capacity = 1; capacity <= 1024; capacity <<= 1) { - QArrayData *data = QArrayData::allocate(objectSize, minAlignment, - capacity, QArrayData::AllocationOptions(allocateOptions)); + QArrayData *data; + void *dataPointer = QArrayData::allocate(&data, objectSize, minAlignment, + capacity, QArrayData::ArrayOptions(allocateOptions)); + keeper.headers.append(data); - QCOMPARE(data->size, 0); - if (allocateOptions & QArrayData::Grow) - QVERIFY(data->alloc > uint(capacity)); + if (allocateOptions & QArrayData::GrowsForward) + QVERIFY(data->allocatedCapacity() > uint(capacity)); else - QCOMPARE(data->alloc, uint(capacity)); - QCOMPARE(data->capacityReserved, uint(isCapacityReserved)); + QCOMPARE(data->allocatedCapacity(), size_t(capacity)); + QCOMPARE(bool(data->flags & QArrayData::CapacityReserved), isCapacityReserved); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(data->data(), 'A', objectSize * capacity); + ::memset(dataPointer, 'A', objectSize * capacity); } } @@ -614,43 +587,40 @@ void tst_QArrayData::reallocate() { QFETCH(size_t, objectSize); QFETCH(size_t, alignment); - QFETCH(QArrayData::AllocationOptions, allocateOptions); + QFETCH(QArrayData::ArrayOptions, allocateOptions); QFETCH(bool, isCapacityReserved); - // Maximum alignment that can be requested is that of QArrayData, - // otherwise, we can't use reallocate(). - Q_ASSERT(alignment <= alignof(QArrayData)); - // Minimum alignment that can be requested is that of QArrayData. // Typically, this alignment is sizeof(void *) and ensured by malloc. size_t minAlignment = qMax(alignment, alignof(QArrayData)); int capacity = 10; Deallocator keeper(objectSize, minAlignment); - QArrayData *data = QArrayData::allocate(objectSize, minAlignment, capacity, - QArrayData::AllocationOptions(allocateOptions) & ~QArrayData::Grow); + QArrayData *data; + void *dataPointer = QArrayData::allocate(&data, objectSize, minAlignment, capacity, + QArrayData::ArrayOptions(allocateOptions) & ~QArrayData::GrowsForward); keeper.headers.append(data); - memset(data->data(), 'A', objectSize * capacity); - data->size = capacity; + memset(dataPointer, 'A', objectSize * capacity); // now try to reallocate int newCapacity = 40; - data = QArrayData::reallocateUnaligned(data, objectSize, newCapacity, - QArrayData::AllocationOptions(allocateOptions)); + auto pair = QArrayData::reallocateUnaligned(data, dataPointer, objectSize, newCapacity, + QArrayData::ArrayOptions(allocateOptions)); + data = pair.first; + dataPointer = pair.second; QVERIFY(data); keeper.headers.clear(); keeper.headers.append(data); - QCOMPARE(data->size, capacity); - if (allocateOptions & QArrayData::Grow) - QVERIFY(data->alloc > uint(newCapacity)); + if (allocateOptions & QArrayData::GrowsForward) + QVERIFY(data->allocatedCapacity() > size_t(newCapacity)); else - QCOMPARE(data->alloc, uint(newCapacity)); - QCOMPARE(data->capacityReserved, uint(isCapacityReserved)); + QCOMPARE(data->allocatedCapacity(), size_t(newCapacity)); + QCOMPARE(!(data->flags & QArrayData::CapacityReserved), !isCapacityReserved); for (int i = 0; i < capacity; ++i) - QCOMPARE(static_cast<char *>(data->data())[i], 'A'); + QCOMPARE(static_cast<char *>(dataPointer)[i], 'A'); } class Unaligned @@ -680,89 +650,46 @@ void tst_QArrayData::alignment() keeper.headers.reserve(100); for (int i = 0; i < 100; ++i) { - QArrayData *data = QArrayData::allocate(sizeof(Unaligned), - minAlignment, 8, QArrayData::Default); + QArrayData *data; + void *dataPointer = QArrayData::allocate(&data, sizeof(Unaligned), + minAlignment, 8, QArrayData::DefaultAllocationFlags); keeper.headers.append(data); QVERIFY(data); - QCOMPARE(data->size, 0); - QVERIFY(data->alloc >= uint(8)); + QVERIFY(data->allocatedCapacity() >= uint(8)); // These conditions should hold as long as header and array are // allocated together - QVERIFY(data->offset >= qptrdiff(sizeof(QArrayData))); - QVERIFY(data->offset <= qptrdiff(sizeof(QArrayData) + qptrdiff offset = reinterpret_cast<char *>(dataPointer) - + reinterpret_cast<char *>(data); + QVERIFY(offset >= qptrdiff(sizeof(QArrayData))); + QVERIFY(offset <= qptrdiff(sizeof(QArrayData) + minAlignment - alignof(QArrayData))); // Data is aligned - QCOMPARE(quintptr(quintptr(data->data()) % alignment), quintptr(0u)); + QCOMPARE(quintptr(quintptr(dataPointer) % alignment), quintptr(0u)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(data->data(), 'A', sizeof(Unaligned) * 8); + ::memset(dataPointer, 'A', sizeof(Unaligned) * 8); } } void tst_QArrayData::typedData() { - QStaticArrayData<int, 10> data = { - Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 10), - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } - }; - QCOMPARE(data.header.size, 10); - - { - QTypedArrayData<int> *array = - static_cast<QTypedArrayData<int> *>(&data.header); - QCOMPARE(array->data(), data.data); - - int j = 0; - for (QTypedArrayData<int>::iterator iter = array->begin(); - iter != array->end(); ++iter, ++j) - QCOMPARE((const int *)iter, data.data + j); - QCOMPARE(j, 10); - } - - { - const QTypedArrayData<int> *array = - static_cast<const QTypedArrayData<int> *>(&data.header); - - QCOMPARE(array->data(), data.data); - - int j = 0; - for (QTypedArrayData<int>::const_iterator iter = array->begin(); - iter != array->end(); ++iter, ++j) - QCOMPARE((const int *)iter, data.data + j); - QCOMPARE(j, 10); - } - - { - QTypedArrayData<int> *null = QTypedArrayData<int>::sharedNull(); - QTypedArrayData<int> *empty = QTypedArrayData<int>::allocate(0); - - QVERIFY(null != empty); - - QCOMPARE(null->size, 0); - QCOMPARE(empty->size, 0); - - QCOMPARE(null->begin(), null->end()); - QCOMPARE(empty->begin(), empty->end()); - } - - { Deallocator keeper(sizeof(char), alignof(QTypedArrayData<char>::AlignmentDummy)); - QArrayData *array = QTypedArrayData<char>::allocate(10); + QPair<QTypedArrayData<char> *, char *> pair = QTypedArrayData<char>::allocate(10); + QArrayData *array = pair.first; keeper.headers.append(array); QVERIFY(array); - QCOMPARE(array->size, 0); - QCOMPARE(array->alloc, 10u); + QCOMPARE(array->allocatedCapacity(), size_t(10)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(array->data(), 0, 10 * sizeof(char)); + ::memset(pair.second, 0, 10 * sizeof(char)); keeper.headers.clear(); QTypedArrayData<short>::deallocate(array); @@ -773,16 +700,16 @@ void tst_QArrayData::typedData() { Deallocator keeper(sizeof(short), alignof(QTypedArrayData<short>::AlignmentDummy)); - QArrayData *array = QTypedArrayData<short>::allocate(10); + QPair<QTypedArrayData<short> *, short *> pair = QTypedArrayData<short>::allocate(10); + QArrayData *array = pair.first; keeper.headers.append(array); QVERIFY(array); - QCOMPARE(array->size, 0); - QCOMPARE(array->alloc, 10u); + QCOMPARE(array->allocatedCapacity(), size_t(10)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(array->data(), 0, 10 * sizeof(short)); + ::memset(pair.second, 0, 10 * sizeof(short)); keeper.headers.clear(); QTypedArrayData<short>::deallocate(array); @@ -793,16 +720,16 @@ void tst_QArrayData::typedData() { Deallocator keeper(sizeof(double), alignof(QTypedArrayData<double>::AlignmentDummy)); - QArrayData *array = QTypedArrayData<double>::allocate(10); + QPair<QTypedArrayData<double> *, double *> pair = QTypedArrayData<double>::allocate(10); + QArrayData *array = pair.first; keeper.headers.append(array); QVERIFY(array); - QCOMPARE(array->size, 0); - QCOMPARE(array->alloc, 10u); + QCOMPARE(array->allocatedCapacity(), size_t(10)); // Check that the allocated array can be used. Best tested with a // memory checker, such as valgrind, running. - ::memset(array->data(), 0, 10 * sizeof(double)); + ::memset(pair.second, 0, 10 * sizeof(double)); keeper.headers.clear(); QTypedArrayData<double>::deallocate(array); @@ -1253,7 +1180,7 @@ void fromRawData_impl() { // Default: Immutable, sharable SimpleVector<T> raw = SimpleVector<T>::fromRawData(array, - sizeof(array)/sizeof(array[0]), QArrayData::Default); + sizeof(array)/sizeof(array[0]), QArrayData::DefaultRawFlags); QCOMPARE(raw.size(), size_t(11)); QCOMPARE((const T *)raw.constBegin(), array); @@ -1298,17 +1225,17 @@ void tst_QArrayData::literals() { { QArrayDataPointer<char> d = Q_ARRAY_LITERAL(char, "ABCDEFGHIJ"); - QCOMPARE(d->size, 10 + 1); + QCOMPARE(d.size, 10u + 1u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], char('A' + i)); + QCOMPARE(d.data()[i], char('A' + i)); } { // wchar_t is not necessarily 2-bytes QArrayDataPointer<wchar_t> d = Q_ARRAY_LITERAL(wchar_t, L"ABCDEFGHIJ"); - QCOMPARE(d->size, 10 + 1); + QCOMPARE(d.size, 10u + 1u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], wchar_t('A' + i)); + QCOMPARE(d.data()[i], wchar_t('A' + i)); } { @@ -1346,26 +1273,26 @@ void tst_QArrayData::variadicLiterals() { QArrayDataPointer<int> d = Q_ARRAY_LITERAL(int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); - QCOMPARE(d->size, 10); + QCOMPARE(d.size, 10u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], i); + QCOMPARE(d.data()[i], i); } { QArrayDataPointer<char> d = Q_ARRAY_LITERAL(char, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); - QCOMPARE(d->size, 10); + QCOMPARE(d.size, 10u); for (int i = 0; i < 10; ++i) - QCOMPARE(d->data()[i], char('A' + i)); + QCOMPARE(d.data()[i], char('A' + i)); } { QArrayDataPointer<const char *> d = Q_ARRAY_LITERAL(const char *, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"); - QCOMPARE(d->size, 10); + QCOMPARE(d.size, 10u); for (int i = 0; i < 10; ++i) { - QCOMPARE(d->data()[i][0], char('A' + i)); - QCOMPARE(d->data()[i][1], '\0'); + QCOMPARE(d.data()[i][0], char('A' + i)); + QCOMPARE(d.data()[i][1], '\0'); } } diff --git a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp index 9a7c099228..b2c7915aa0 100644 --- a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp +++ b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp @@ -86,6 +86,9 @@ private slots: void resize(); void fromBits_data(); void fromBits(); + + void toUInt32_data(); + void toUInt32(); }; void tst_QBitArray::size_data() @@ -667,5 +670,95 @@ void tst_QBitArray::fromBits() QCOMPARE(QBitArray::fromBits(fromBits.bits(), fromBits.size()), expected); } +void tst_QBitArray::toUInt32_data() +{ + QTest::addColumn<QBitArray>("data"); + QTest::addColumn<int>("endianness"); + QTest::addColumn<bool>("check"); + QTest::addColumn<quint32>("result"); + + QTest::newRow("ctor") << QBitArray() + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(0); + + QTest::newRow("empty") << QBitArray(0) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(0); + + QTest::newRow("LittleEndian4") << QStringToQBitArray(QString("0111")) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(14); + + QTest::newRow("BigEndian4") << QStringToQBitArray(QString("0111")) + << static_cast<int>(QSysInfo::Endian::BigEndian) + << true + << quint32(7); + + QTest::newRow("LittleEndian8") << QStringToQBitArray(QString("01111111")) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(254); + + QTest::newRow("BigEndian8") << QStringToQBitArray(QString("01111111")) + << static_cast<int>(QSysInfo::Endian::BigEndian) + << true + << quint32(127); + + QTest::newRow("LittleEndian16") << QStringToQBitArray(QString("0111111111111111")) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(65534); + + QTest::newRow("BigEndian16") << QStringToQBitArray(QString("0111111111111111")) + << static_cast<int>(QSysInfo::Endian::BigEndian) + << true + << quint32(32767); + + QTest::newRow("LittleEndian31") << QBitArray(31, true) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(2147483647); + + QTest::newRow("BigEndian31") << QBitArray(31, true) + << static_cast<int>(QSysInfo::Endian::BigEndian) + << true + << quint32(2147483647); + + QTest::newRow("LittleEndian32") << QBitArray(32, true) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << true + << quint32(4294967295); + + QTest::newRow("BigEndian32") << QBitArray(32, true) + << static_cast<int>(QSysInfo::Endian::BigEndian) + << true + << quint32(4294967295); + + QTest::newRow("LittleEndian33") << QBitArray(33, true) + << static_cast<int>(QSysInfo::Endian::LittleEndian) + << false + << quint32(0); + + QTest::newRow("BigEndian33") << QBitArray(33, true) + << static_cast<int>(QSysInfo::Endian::BigEndian) + << false + << quint32(0); +} + +void tst_QBitArray::toUInt32() +{ + QFETCH(QBitArray, data); + QFETCH(int, endianness); + QFETCH(bool, check); + QFETCH(quint32, result); + bool ok = false; + + QCOMPARE(data.toUInt32(static_cast<QSysInfo::Endian>(endianness), &ok), result); + QCOMPARE(ok, check); +} + QTEST_APPLESS_MAIN(tst_QBitArray) #include "tst_qbitarray.moc" diff --git a/tests/auto/corelib/tools/qflatmap/qflatmap.pro b/tests/auto/corelib/tools/qflatmap/qflatmap.pro new file mode 100644 index 0000000000..3927cee30c --- /dev/null +++ b/tests/auto/corelib/tools/qflatmap/qflatmap.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qflatmap +QT = core-private testlib +SOURCES = tst_qflatmap.cpp diff --git a/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp b/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp new file mode 100644 index 0000000000..a5ae6f5f44 --- /dev/null +++ b/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp @@ -0,0 +1,453 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <private/qflatmap_p.h> +#include <qbytearray.h> +#include <qstring.h> +#include <qstringview.h> +#include <qvarlengtharray.h> +#include <qvector.h> + +#include <algorithm> +#include <list> +#include <tuple> + +class tst_QFlatMap : public QObject +{ + Q_OBJECT +private slots: + void constructing(); + void constAccess(); + void insertion(); + void removal(); + void extraction(); + void iterators(); + void statefulComparator(); + void transparency(); + void viewIterators(); + void varLengthArray(); +}; + +void tst_QFlatMap::constructing() +{ + using Map = QFlatMap<int, QByteArray>; + Map fmDefault; + QVERIFY(fmDefault.isEmpty()); + QCOMPARE(fmDefault.size(), Map::size_type(0)); + QCOMPARE(fmDefault.size(), fmDefault.count()); + + auto key_compare = fmDefault.key_comp(); + auto selfbuilt_value_compare + = [&key_compare](const Map::value_type &a, const Map::value_type &b) + { + return key_compare(a.first, b.first); + }; + auto value_compare = fmDefault.value_comp(); + + Map::key_container_type kv = { 6, 2, 1 }; + Map::mapped_container_type mv = { "foo", "bar", "baz" }; + Map fmCopy{kv, mv}; + QCOMPARE(fmCopy.size(), Map::size_type(3)); + QVERIFY(std::is_sorted(fmCopy.begin(), fmCopy.end(), selfbuilt_value_compare)); + QVERIFY(std::is_sorted(fmCopy.begin(), fmCopy.end(), value_compare)); + + Map fmMove{ + Map::key_container_type{ 6, 2, 1 }, + Map::mapped_container_type{ "foo", "bar", "baz" } + }; + QCOMPARE(fmMove.size(), Map::size_type(3)); + QVERIFY(std::is_sorted(fmMove.begin(), fmMove.end(), value_compare)); + + auto fmInitList = Map{ { 1, 2 }, { "foo", "bar" } }; + QVERIFY(std::is_sorted(fmInitList.begin(), fmInitList.end(), value_compare)); + + auto fmRange = Map(fmCopy.begin(), fmCopy.end()); + QVERIFY(std::is_sorted(fmRange.begin(), fmRange.end(), value_compare)); + + kv.clear(); + mv.clear(); + std::vector<Map::value_type> sv; + for (auto it = fmRange.begin(); it != fmRange.end(); ++it) { + kv.push_back(it->first); + mv.push_back(it->second); + sv.push_back(*it); + } + auto fmFromSortedVectorCopy = Map(Qt::OrderedUniqueRange, kv, mv); + auto fmFromSortedVectorMove = Map(Qt::OrderedUniqueRange, Map::key_container_type(kv), + Map::mapped_container_type(mv)); + auto fmFromSortedInitList = Map(Qt::OrderedUniqueRange, { { 1, "foo" }, { 2, "bar" } }); + auto fmFromSortedRange = Map(Qt::OrderedUniqueRange, sv.begin(), sv.end()); +} + +void tst_QFlatMap::constAccess() +{ + using Map = QFlatMap<QByteArray, QByteArray>; + const Map m{ { { "foo", "FOO" }, { "bar", "BAR" } } }; + + const std::vector<Map::value_type> v{ { "foo", "FOO" }, { "bar", "BAR" } }; + + QCOMPARE(m.value("foo").data(), "FOO"); + QCOMPARE(m.value("bar").data(), "BAR"); + QCOMPARE(m.value("nix"), QByteArray()); + QCOMPARE(m.value("nix", "NIX").data(), "NIX"); + QCOMPARE(m["foo"].data(), "FOO"); + QCOMPARE(m["bar"].data(), "BAR"); + QCOMPARE(m["nix"], QByteArray()); + QVERIFY(m.contains("foo")); + QVERIFY(!m.contains("nix")); +} + +void tst_QFlatMap::insertion() +{ + using Map = QFlatMap<QByteArray, QByteArray>; + Map m; + QByteArray foo = "foo"; + m[foo] = foo.toUpper(); + m["bar"] = "BAR"; + m["baz"] = "BAZ"; + QVERIFY(m.insert("oof", "eek").second); + QVERIFY(!m.insert("oof", "OOF").second); + const std::vector<Map::value_type> container = { { "bla", "BLA" }, { "blubb", "BLUBB" } }; + m.insert(container.begin(), container.end()); + QCOMPARE(m.value("foo").data(), "FOO"); + QCOMPARE(m.value("bar").data(), "BAR"); + QCOMPARE(m.value("baz").data(), "BAZ"); + QCOMPARE(m.value("oof").data(), "OOF"); + QCOMPARE(m.value("bla").data(), "BLA"); + QCOMPARE(m.value("blubb").data(), "BLUBB"); + + Map::value_type a1[] = { { "narf", "NARF" }, + { "zort", "ZORT" }, + { "troz", "TROZ" } }; + Map::value_type a2[] = { { "gnampf", "GNAMPF" }, + { "narf", "NARFFFF" }, + { "narf", "NARFFFFF" }, + { "narf", "NARFFFFFF" } }; + m.insert(std::begin(a1), std::end(a1)); + m.insert(Qt::OrderedUniqueRange, std::begin(a2), std::end(a2)); + QCOMPARE(m.size(), 10); + QCOMPARE(m.value("narf").data(), "NARFFFFFF"); + QCOMPARE(m.value("gnampf").data(), "GNAMPF"); +} + +void tst_QFlatMap::extraction() +{ + using Map = QFlatMap<int, QByteArray>; + Map::key_container_type expectedKeys = { 1, 2, 3 }; + Map::mapped_container_type expectedValues = { "een", "twee", "dree" }; + Map m(expectedKeys, expectedValues); + auto keys = m.keys(); + auto values = m.values(); + QCOMPARE(keys, expectedKeys); + QCOMPARE(values, expectedValues); + Map::containers c = std::move(m).extract(); + QCOMPARE(c.keys, expectedKeys); + QCOMPARE(c.values, expectedValues); +} + +void tst_QFlatMap::iterators() +{ + using Map = QFlatMap<int, QByteArray>; + auto m = Map{ { 1, "foo" }, { 2, "bar" }, { 3, "baz" } }; + { + // forward / backward + Map::iterator a = m.begin(); + QVERIFY(a != m.end()); + QCOMPARE(a.key(), 1); + QCOMPARE(a.value(), "foo"); + ++a; + QCOMPARE(a.key(), 2); + QCOMPARE(a.value(), "bar"); + Map::iterator b = a++; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + QCOMPARE(std::tie(b.key(), b.value()), std::make_tuple(2, "bar")); + QCOMPARE(++a, m.end()); + --a; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + a.value() = "buzz"; + b = a--; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(2, "bar")); + QCOMPARE(std::tie(b.key(), b.value()), std::make_tuple(3, "buzz")); + b.value() = "baz"; + + // random access + a = m.begin(); + a += 2; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + a = m.begin() + 1; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(2, "bar")); + a = 1 + m.begin(); + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(2, "bar")); + a = m.end() - 1; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + b = m.end(); + b -= 1; + QCOMPARE(std::tie(b.key(), b.value()), std::make_tuple(3, "baz")); + QCOMPARE(m.end() - m.begin(), m.size()); + + // comparison + a = m.begin() + m.size() - 1; + b = m.end() - 1; + QVERIFY(a == b); + a = m.begin(); + b = m.end(); + QVERIFY(a < b); + QVERIFY(a <= b); + QVERIFY(b > a); + QVERIFY(b >= a); + a = b; + QVERIFY(!(a < b)); + QVERIFY(a <= b); + QVERIFY(!(b > a)); + QVERIFY(b >= a); + + // de-referencing + a = m.begin(); + auto ref0 = *a; + QCOMPARE(ref0.first, 1); + QCOMPARE(ref0.second, "foo"); + auto ref1 = a[1]; + QCOMPARE(ref1.first, 2); + QCOMPARE(ref1.second, "bar"); + } + { + // forward / backward + Map::const_iterator a = m.cbegin(); + QVERIFY(a != m.cend()); + QCOMPARE(a.key(), 1); + QCOMPARE(a.value(), "foo"); + ++a; + QCOMPARE(a.key(), 2); + QCOMPARE(a.value(), "bar"); + Map::const_iterator b = a++; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + QCOMPARE(std::tie(b.key(), b.value()), std::make_tuple(2, "bar")); + QCOMPARE(++a, m.cend()); + --a; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + b = a--; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(2, "bar")); + + // random access + a = m.cbegin(); + a += 2; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + a = m.cbegin() + 1; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(2, "bar")); + a = 1 + m.cbegin(); + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(2, "bar")); + a = m.cend() - 1; + QCOMPARE(std::tie(a.key(), a.value()), std::make_tuple(3, "baz")); + b = m.cend(); + b -= 1; + QCOMPARE(std::tie(b.key(), b.value()), std::make_tuple(3, "baz")); + QCOMPARE(m.cend() - m.cbegin(), m.size()); + + // comparison + a = m.cbegin() + m.size() - 1; + b = m.cend() - 1; + QVERIFY(a == b); + a = m.cbegin(); + b = m.cend(); + QVERIFY(a < b); + QVERIFY(a <= b); + QVERIFY(b > a); + QVERIFY(b >= a); + a = b; + QVERIFY(!(a < b)); + QVERIFY(a <= b); + QVERIFY(!(b > a)); + QVERIFY(b >= a); + + // de-referencing + a = m.cbegin(); + auto ref0 = *a; + QCOMPARE(ref0.first, 1); + QCOMPARE(ref0.second, "foo"); + auto ref1 = a[1]; + QCOMPARE(ref1.first, 2); + QCOMPARE(ref1.second, "bar"); + } + { + Map::iterator it = m.begin(); + Map::const_iterator cit = it; + Q_UNUSED(it); + Q_UNUSED(cit); + } + { + std::list<Map::value_type> revlst; + std::copy(m.begin(), m.end(), std::front_inserter(revlst)); + std::vector<Map::value_type> v0; + std::copy(revlst.begin(), revlst.end(), std::back_inserter(v0)); + std::vector<Map::value_type> v1; + std::copy(m.rbegin(), m.rend(), std::back_inserter(v1)); + const Map cm = m; + std::vector<Map::value_type> v2; + std::copy(cm.rbegin(), cm.rend(), std::back_inserter(v2)); + std::vector<Map::value_type> v3; + std::copy(m.crbegin(), m.crend(), std::back_inserter(v3)); + QCOMPARE(v0, v1); + QCOMPARE(v1, v2); + QCOMPARE(v2, v3); + } +} + +void tst_QFlatMap::removal() +{ + using Map = QFlatMap<int, QByteArray>; + Map m({ { 2, "bar" }, { 3, "baz" }, { 1, "foo" } }); + QCOMPARE(m.value(2).data(), "bar"); + QCOMPARE(m.take(2).data(), "bar"); + QVERIFY(!m.contains(2)); + QCOMPARE(m.size(), Map::size_type(2)); + QVERIFY(m.remove(1)); + QVERIFY(!m.contains(1)); + QVERIFY(!m.remove(1)); + QCOMPARE(m.size(), Map::size_type(1)); + m.clear(); + QVERIFY(m.isEmpty()); + QVERIFY(m.empty()); + + m[1] = "een"; + m[2] = "twee"; + m[3] = "dree"; + auto it = m.lower_bound(1); + QCOMPARE(it.key(), 1); + it = m.erase(it); + QCOMPARE(it.key(), 2); + QVERIFY(!m.contains(1)); +} + +void tst_QFlatMap::statefulComparator() +{ + struct CountingCompare { + mutable int count = 0; + + bool operator()(const QString &lhs, const QString &rhs) const + { + ++count; + return lhs < rhs; + } + }; + + using Map = QFlatMap<QString, QString, CountingCompare>; + auto m1 = Map{ { "en", "een"}, { "to", "twee" }, { "tre", "dree" } }; + QVERIFY(m1.key_comp().count > 0); + auto m2 = Map(m1.key_comp()); + QCOMPARE(m2.key_comp().count, m1.key_comp().count); + m2.insert(m1.begin(), m1.end()); + QVERIFY(m2.key_comp().count > m1.key_comp().count); +} + +void tst_QFlatMap::transparency() +{ + struct StringViewCompare + { + using is_transparent = void; + bool operator()(const QStringView &lhs, const QStringView &rhs) const + { + return lhs < rhs; + } + }; + + using Map = QFlatMap<QString, QString, StringViewCompare>; + auto m = Map{ { "one", "een" }, { "two", "twee" }, { "three", "dree" } }; + + const QString numbers = "one two three"; + const QStringView sv1{numbers.constData(), 3}; + const QStringView sv2{numbers.constData() + 4, 3}; + const QStringView sv3{numbers.constData() + 8, 5}; + QCOMPARE(m.lower_bound(sv1).value(), "een"); + QCOMPARE(m.lower_bound(sv2).value(), "twee"); + QCOMPARE(m.lower_bound(sv3).value(), "dree"); +} + +void tst_QFlatMap::viewIterators() +{ + using Map = QFlatMap<QByteArray, QByteArray>; + Map m({ { "yksi", "een"}, { "kaksi", "twee" }, { "kolme", "dree" } }); + { + std::vector<QByteArray> keys; + std::transform(m.begin(), m.end(), std::back_inserter(keys), + [](const Map::value_type &v) + { + return v.first; + }); + auto it = keys.begin(); + QCOMPARE(*it, "kaksi"); + QCOMPARE(it->length(), 5); + ++it; + QCOMPARE(*it, "kolme"); + it++; + QCOMPARE(*it, "yksi"); + ++it; + QCOMPARE(it, keys.end()); + --it; + QCOMPARE(*it, "yksi"); + it--; + QCOMPARE(*it, "kolme"); + } + { + std::vector<QByteArray> values; + std::transform(m.begin(), m.end(), std::back_inserter(values), + [](const Map::value_type &v) + { + return v.second; + }); + auto it = values.begin(); + QCOMPARE(*it, "twee"); + QCOMPARE(it->length(), 4); + ++it; + QCOMPARE(*it, "dree"); + it++; + QCOMPARE(*it, "een"); + ++it; + QCOMPARE(it, values.end()); + --it; + QCOMPARE(*it, "een"); + it--; + QCOMPARE(*it, "dree"); + } +} + +void tst_QFlatMap::varLengthArray() +{ + using Map = QFlatMap<int, QByteArray, std::less<int>, + QVarLengthArray<int, 1024>, QVarLengthArray<QByteArray, 1024>>; + Map m{ { 2, "twee" } }; + m.insert(1, "een"); + m.remove(1); + QVERIFY(!m.isEmpty()); + m.remove(2); + QVERIFY(m.isEmpty()); +} + +QTEST_APPLESS_MAIN(tst_QFlatMap) +#include "tst_qflatmap.moc" diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index f0eeb70ccb..91cd3eb0bd 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -69,6 +69,7 @@ private slots: void initializerList(); void eraseValidIteratorOnSharedHash(); void equal_range(); + void insert_hash(); }; struct IdentityTracker { @@ -1636,5 +1637,77 @@ void tst_QHash::equal_range() } } +void tst_QHash::insert_hash() +{ + { + QHash<int, int> hash; + hash.insert(1, 1); + hash.insert(2, 2); + hash.insert(0, -1); + + QHash<int, int> hash2; + hash2.insert(0, 0); + hash2.insert(3, 3); + hash2.insert(4, 4); + + hash.insert(hash2); + + QCOMPARE(hash.count(), 5); + for (int i = 0; i < 5; ++i) + QCOMPARE(hash[i], i); + } + { + QHash<int, int> hash; + hash.insert(0, 5); + + QHash<int, int> hash2; + + hash.insert(hash2); + + QCOMPARE(hash.count(), 1); + QCOMPARE(hash[0], 5); + } + { + QHash<int, int> hash; + QHash<int, int> hash2; + hash2.insert(0, 5); + + hash.insert(hash2); + + QCOMPARE(hash.count(), 1); + QCOMPARE(hash[0], 5); + QCOMPARE(hash, hash2); + } + { + QHash<int, int> hash; + hash.insert(0, 7); + hash.insert(2, 5); + hash.insert(7, 55); + + // insert into ourself, nothing should happen + hash.insert(hash); + + QCOMPARE(hash.count(), 3); + QCOMPARE(hash[0], 7); + QCOMPARE(hash[2], 5); + QCOMPARE(hash[7], 55); + } + { + // This will use a QMultiHash and then insert that into QHash, + // the ordering is undefined so we won't test that but make + // sure this isn't adding multiple entries with the same key + // to the QHash. + QHash<int, int> hash; + QMultiHash<int, int> hash2; + hash2.insert(0, 5); + hash2.insert(0, 6); + hash2.insert(0, 7); + + hash.insert(hash2); + + QCOMPARE(hash.count(), 1); + } +} + QTEST_APPLESS_MAIN(tst_QHash) #include "tst_qhash.moc" diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index 85733639b8..1638ebc992 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -70,6 +70,7 @@ private slots: void equal_range(); void insert(); + void insertMap(); void checkMostLeftNode(); void initializerList(); void testInsertWithHint(); @@ -1202,6 +1203,101 @@ void tst_QMap::insert() } } +void tst_QMap::insertMap() +{ + { + QMap<int, int> map; + map.insert(1, 1); + map.insert(2, 2); + map.insert(0, -1); + + QMap<int, int> map2; + map2.insert(0, 0); + map2.insert(3, 3); + map2.insert(4, 4); + + map.insert(map2); + + QCOMPARE(map.count(), 5); + for (int i = 0; i < 5; ++i) + QCOMPARE(map[i], i); + } + { + QMap<int, int> map; + for (int i = 0; i < 10; ++i) + map.insert(i * 3, i); + + QMap<int, int> map2; + for (int i = 0; i < 10; ++i) + map2.insert(i * 4, i); + + map.insert(map2); + + QCOMPARE(map.count(), 17); + for (int i = 0; i < 10; ++i) { + // i * 3 == i except for i = 4, 8 + QCOMPARE(map[i * 3], (i && i % 4 == 0) ? i - (i / 4) : i); + QCOMPARE(map[i * 4], i); + } + + auto it = map.cbegin(); + int prev = it.key(); + ++it; + for (auto end = map.cend(); it != end; ++it) { + QVERIFY(prev < it.key()); + prev = it.key(); + } + } + { + QMap<int, int> map; + map.insert(1, 1); + + QMap<int, int> map2; + + map.insert(map2); + QCOMPARE(map.count(), 1); + QCOMPARE(map[1], 1); + } + { + QMap<int, int> map; + QMap<int, int> map2; + map2.insert(1, 1); + + map.insert(map2); + QCOMPARE(map.count(), 1); + QCOMPARE(map[1], 1); + } + { + QMap<int, int> map; + map.insert(0, 0); + map.insert(1, 1); + map.insert(2, 2); + + // Test inserting into self, nothing should happen + map.insert(map); + + QCOMPARE(map.count(), 3); + for (int i = 0; i < 3; ++i) + QCOMPARE(map[i], i); + } + { + // Here we use a QMultiMap and insert that into QMap, + // since it has multiple values with the same key the + // ordering is undefined so we won't test that, but + // make sure this isn't adding multiple entries with the + // same key to the QMap. + QMap<int, int> map; + QMultiMap<int, int> map2; + map2.insert(0, 0); + map2.insert(0, 1); + map2.insert(0, 2); + + map.insert(map2); + + QCOMPARE(map.count(), 1); + } +} + void tst_QMap::checkMostLeftNode() { QMap<int, int> map; diff --git a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp index f95d48f042..01181ce20e 100644 --- a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp +++ b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp @@ -67,7 +67,7 @@ void tst_QScopedGuard::exceptions() bool caught = false; QT_TRY { - auto cleanup = qScopeGuard([&caught] { s_globalState++; }); + auto cleanup = qScopeGuard([] { s_globalState++; }); QT_THROW(std::bad_alloc()); //if Qt compiled without exceptions this is noop s_globalState = 100; } diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 0af83c7463..7a69e844d4 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -104,7 +104,7 @@ private: static void check(const State state1, const State state2) { - QCOMPARE(state1, state2); + QCOMPARE(int(state1), int(state2)); } }; @@ -174,7 +174,7 @@ private: { // check if c object has been moved QCOMPARE(c, c->that); - QCOMPARE(c->state, Constructed); + QCOMPARE(int(c->state), int(Constructed)); } }; QAtomicInt Custom::counter = 0; @@ -307,8 +307,6 @@ private slots: void reserve(); void reserveZero(); - void reallocAfterCopy_data(); - void reallocAfterCopy(); void initializeListInt(); void initializeListMovable(); void initializeListCustom(); @@ -726,16 +724,12 @@ void tst_QVector::capacity() const myvec.remove(3); myvec.remove(3); myvec.remove(3); - // TODO: is this a safe assumption? presumably it won't release memory until shrink(), but can we asser that is true? - QVERIFY(myvec.capacity() >= 6); myvec.squeeze(); QVERIFY(myvec.capacity() >= 3); myvec.remove(0); myvec.remove(0); myvec.remove(0); - // TODO: as above note - QVERIFY(myvec.capacity() >= 3); myvec.squeeze(); QVERIFY(myvec.capacity() == 0); } @@ -2341,87 +2335,6 @@ void tst_QVector::reserveZero() QVERIFY(vec.capacity() >= 1); } -// This is a regression test for QTBUG-11763, where memory would be reallocated -// soon after copying a QVector. -void tst_QVector::reallocAfterCopy_data() -{ - QTest::addColumn<int>("capacity"); - QTest::addColumn<int>("fill_size"); - QTest::addColumn<int>("func_id"); - QTest::addColumn<int>("result1"); - QTest::addColumn<int>("result2"); - QTest::addColumn<int>("result3"); - QTest::addColumn<int>("result4"); - - int result1, result2, result3, result4; - int fill_size; - for (int i = 70; i <= 100; i += 10) { - const QByteArray prefix = "reallocAfterCopy:" + QByteArray::number(i) + ','; - fill_size = i - 20; - for (int j = 0; j <= 3; j++) { - if (j == 0) { // append - result1 = i; - result2 = i; - result3 = i - 19; - result4 = i - 20; - } else if (j == 1) { // insert(0) - result1 = i; - result2 = i; - result3 = i - 19; - result4 = i - 20; - } else if (j == 2) { // insert(20) - result1 = i; - result2 = i; - result3 = i - 19; - result4 = i - 20; - } else if (j == 3) { // insert(0, 10) - result1 = i; - result2 = i; - result3 = i - 10; - result4 = i - 20; - } - QTest::newRow((prefix + QByteArray::number(j)).constData()) - << i << fill_size << j << result1 << result2 << result3 << result4; - } - } -} - -void tst_QVector::reallocAfterCopy() -{ - QFETCH(int, capacity); - QFETCH(int, fill_size); - QFETCH(int, func_id); - QFETCH(int, result1); - QFETCH(int, result2); - QFETCH(int, result3); - QFETCH(int, result4); - - QVector<qreal> v1; - QVector<qreal> v2; - - v1.reserve(capacity); - v1.resize(0); - v1.fill(qreal(1.0), fill_size); - - v2 = v1; - - // no need to test begin() and end(), there is a detach() in them - if (func_id == 0) { - v1.append(qreal(1.0)); //push_back is same as append - } else if (func_id == 1) { - v1.insert(0, qreal(1.0)); //push_front is same as prepend, insert(0) - } else if (func_id == 2) { - v1.insert(20, qreal(1.0)); - } else if (func_id == 3) { - v1.insert(0, 10, qreal(1.0)); - } - - QCOMPARE(v1.capacity(), result1); - QCOMPARE(v2.capacity(), result2); - QCOMPARE(v1.size(), result3); - QCOMPARE(v2.size(), result4); -} - template<typename T> void tst_QVector::initializeList() { diff --git a/tests/auto/corelib/tools/tools.pro b/tests/auto/corelib/tools/tools.pro index 5a7c8478f1..be195ea037 100644 --- a/tests/auto/corelib/tools/tools.pro +++ b/tests/auto/corelib/tools/tools.pro @@ -11,6 +11,7 @@ SUBDIRS=\ qcryptographichash \ qeasingcurve \ qexplicitlyshareddatapointer \ + qflatmap \ qfreelist \ qhash \ qhashfunctions \ |