summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/collections/tst_collections.cpp90
-rw-r--r--tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp40
-rw-r--r--tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp12
-rw-r--r--tests/auto/corelib/tools/qcache/tst_qcache.cpp83
-rw-r--r--tests/auto/corelib/tools/qcryptographichash/CMakeLists.txt2
-rw-r--r--tests/auto/corelib/tools/qcryptographichash/testdata.qrc6
-rw-r--r--tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp16
-rw-r--r--tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp185
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp150
-rw-r--r--tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp62
-rw-r--r--tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp37
-rw-r--r--tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp10
-rw-r--r--tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp15
-rw-r--r--tests/auto/corelib/tools/qsharedpointer/externaltests.cpp2
-rw-r--r--tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp2
-rw-r--r--tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp126
-rw-r--r--tests/auto/corelib/tools/qversionnumber/tst_qversionnumber.cpp43
17 files changed, 823 insertions, 58 deletions
diff --git a/tests/auto/corelib/tools/collections/tst_collections.cpp b/tests/auto/corelib/tools/collections/tst_collections.cpp
index 80d1d90f2a..7306e036a9 100644
--- a/tests/auto/corelib/tools/collections/tst_collections.cpp
+++ b/tests/auto/corelib/tools/collections/tst_collections.cpp
@@ -63,6 +63,9 @@ void foo()
#include <QTest>
#include <QVector>
+#include <QScopedPointer>
+#include <QThread>
+#include <QSemaphore>
#include <algorithm>
@@ -123,6 +126,15 @@ private slots:
void foreach_2();
void insert_remove_loop();
+
+ void detachAssociativeContainerQMap() { detachAssociativeContainerImpl<QMap>(); }
+ void detachAssociativeContainerQMultiMap() { detachAssociativeContainerImpl<QMultiMap>(); }
+ void detachAssociativeContainerQHash() { detachAssociativeContainerImpl<QHash>(); }
+ void detachAssociativeContainerQMultiHash() { detachAssociativeContainerImpl<QMultiHash>(); }
+
+private:
+ template <template<typename, typename> typename Container>
+ void detachAssociativeContainerImpl();
};
struct LargeStatic {
@@ -253,15 +265,11 @@ Q_DECLARE_METATYPE(NoCmpParamRecursiveMapK);
Q_DECLARE_METATYPE(NoCmpParamRecursiveMultiMapV);
Q_DECLARE_METATYPE(NoCmpParamRecursiveMultiMapK);
Q_DECLARE_METATYPE(NoCmpParamRecursiveHashK);
-// TODO: fix, this requires operator== from key type (QTBUG-96256)
-// Q_DECLARE_METATYPE(NoCmpParamRecursiveHashV);
+Q_DECLARE_METATYPE(NoCmpParamRecursiveHashV);
Q_DECLARE_METATYPE(NoCmpParamRecursiveMultiHashK);
-// TODO: fix, this requires operator== from key type (QTBUG-96256)
-// Q_DECLARE_METATYPE(NoCmpParamRecursiveMultiHashK);
+Q_DECLARE_METATYPE(NoCmpParamRecursiveMultiHashV);
Q_DECLARE_METATYPE(NoCmpRecursiveList);
-// TODO: fix, this requires operator== (QTBUG-96257)
-// Q_DECLARE_METATYPE(NoCmpRecursiveSet);
Q_DECLARE_METATYPE(NoCmpRecursiveMapV);
Q_DECLARE_METATYPE(NoCmpRecursiveMapK);
Q_DECLARE_METATYPE(NoCmpRecursiveMultiMapV);
@@ -287,9 +295,9 @@ static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMapV>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMapK>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMultiMapV>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMultiMapK>);
-static_assert(QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveHashV>);
+static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveHashV>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveHashK>);
-static_assert(QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMultiHashV>);
+static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMultiHashV>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpParamRecursiveMultiHashK>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpRecursiveList>);
@@ -304,6 +312,11 @@ static_assert(!QTypeTraits::has_operator_equal_v<NoCmpRecursiveHashK>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpRecursiveMultiHashV>);
static_assert(!QTypeTraits::has_operator_equal_v<NoCmpRecursiveMultiHashK>);
+template <typename T>
+constexpr inline bool has_prepend_v = true;
+template <typename T, qsizetype N>
+constexpr inline bool has_prepend_v<QVarLengthArray<T,N>> = false; // deprecated in Qt 6.3
+
void tst_Collections::typeinfo()
{
QVERIFY(QTypeInfo<int*>::isPointer);
@@ -3444,7 +3457,10 @@ template<class Container> void insert_remove_loop_impl()
t.append(T(IntOrString(1)));
t << (T(IntOrString(2)));
t += (T(IntOrString(3)));
- t.prepend(T(IntOrString(4)));
+ if constexpr (has_prepend_v<Container>)
+ t.prepend(T(IntOrString(4)));
+ else
+ t.insert(t.cbegin(), T(IntOrString(4)));
t.insert(2, 3 , T(IntOrString(5)));
t.insert(4, T(IntOrString(6)));
t.insert(t.begin() + 2, T(IntOrString(7)));
@@ -3549,7 +3565,63 @@ void tst_Collections::insert_remove_loop()
insert_remove_loop_impl<QVarLengthArray<std::string, 15>>();
}
+template <template<typename, typename> typename Container>
+void tst_Collections::detachAssociativeContainerImpl()
+{
+ constexpr int RUNS = 50;
+
+ for (int run = 0; run < RUNS; ++run) {
+ Container<int, int> container;
+
+ for (int i = 0; i < 1'000; ++i) {
+ container.insert(i, i);
+ container.insert(i, i); // for multi-keyed containers
+ }
+
+ const auto it = container.constBegin();
+ const auto &key = it.key();
+ const auto &value = it.value();
+ const auto keyCopy = key;
+ const auto valueCopy = value;
+ QSemaphore sem1, sem2;
+ auto detachInAnotherThread = [&sem1, &sem2, copy = container]() mutable {
+ sem1.release();
+ sem2.acquire();
+ copy.clear(); // <==
+ };
+
+ QScopedPointer thread(QThread::create(std::move(detachInAnotherThread)));
+ thread->start();
+
+ sem2.release();
+ sem1.acquire();
+
+ // The following call may detach (because the container is
+ // shared), and then use key/value to search+insert.
+ //
+ // This means that key/value, as references, have to be valid
+ // throughout the insertion procedure. Note that they are
+ // references into the container *itself*; and that the
+ // insertion procedure is working on a new (detached) copy of
+ // the container's payload.
+ //
+ // There is now a possible scenario in which the clear() above
+ // finds the copy's refcount at 1, hence not perform a detach,
+ // and destroy its payload. But key/value were references into
+ // *that* payload (it's the payload that `container` itself
+ // used to share). If inside insert() we don't take extra
+ // measures to keep the payload alive, now they're dangling and
+ // the insertion will malfunction.
+
+ container.insert(key, value);
+
+ QVERIFY(container.contains(keyCopy));
+ QCOMPARE(container.value(keyCopy), valueCopy);
+
+ thread->wait();
+ }
+}
QTEST_APPLESS_MAIN(tst_Collections)
#include "tst_collections.moc"
diff --git a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
index 3224a8c877..7fe1dd9b4d 100644
--- a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
+++ b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
@@ -38,8 +38,10 @@
#include <algorithm>
#include <functional>
#include <vector> // for reference
+#include <iostream>
#include <list>
#include <set>
+#include <sstream>
#include <map>
#include <forward_list>
#include <unordered_set>
@@ -49,6 +51,21 @@
# define STDLIB_HAS_UNIFORM_ERASURE
#endif
+QT_BEGIN_NAMESPACE
+std::ostream &operator<<(std::ostream &os, const QChar &c)
+{
+ Q_ASSERT(c == QLatin1Char{c.toLatin1()});
+ return os << c.toLatin1();
+}
+std::istream &operator>>(std::istream &os, QChar &c)
+{
+ char cL1;
+ os >> cL1;
+ c = QLatin1Char{cL1};
+ return os;
+}
+QT_END_NAMESPACE
+
struct Movable
{
explicit Movable(int i = 0) noexcept
@@ -70,6 +87,11 @@ struct Movable
int i;
static int instanceCount;
+
+ friend std::ostream &operator<<(std::ostream &os, const Movable &m)
+ { return os << m.i; }
+ friend std::istream &operator>>(std::istream &os, Movable &m)
+ { return os >> m.i; }
};
int Movable::instanceCount = 0;
@@ -111,6 +133,11 @@ struct Complex
int i;
static int instanceCount;
+
+ friend std::ostream &operator<<(std::ostream &os, const Complex &c)
+ { return os << c.i; }
+ friend std::istream &operator>>(std::istream &os, Complex &c)
+ { return os >> c.i; }
};
int Complex::instanceCount = 0;
@@ -422,12 +449,25 @@ void tst_ContainerApiSymmetry::ranged_ctor_non_associative_impl() const
// from itself
const Container c4(reference.begin(), reference.end());
+ // from stringsteam (= pure input_iterator)
+ const Container c5 = [&] {
+ {
+ std::stringstream ss;
+ for (auto &v : values1)
+ ss << v << ' ';
+ ss.seekg(0);
+ return Container(std::istream_iterator<V>{ss},
+ std::istream_iterator<V>{});
+ }
+ }();
+
QCOMPARE(c1, reference);
QCOMPARE(c2a, reference);
QCOMPARE(c2b, reference);
QCOMPARE(c3a, reference);
QCOMPARE(c3b, reference);
QCOMPARE(c4, reference);
+ QCOMPARE(c5, reference);
}
diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
index 81c75fffbc..6d51039d02 100644
--- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
+++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@@ -1797,7 +1797,7 @@ void tst_QArrayData::literals()
{
{
QArrayDataPointer<char> d = Q_ARRAY_LITERAL(char, "ABCDEFGHIJ");
- QCOMPARE(d.size, 10u + 1u);
+ QCOMPARE(d.size, 10 + 1);
for (int i = 0; i < 10; ++i)
QCOMPARE(d.data()[i], char('A' + i));
}
@@ -1820,7 +1820,7 @@ void tst_QArrayData::literals()
{
// wchar_t is not necessarily 2-bytes
QArrayDataPointer<wchar_t> d = Q_ARRAY_LITERAL(wchar_t, L"ABCDEFGHIJ");
- QCOMPARE(d.size, 10u + 1u);
+ QCOMPARE(d.size, 10 + 1);
for (int i = 0; i < 10; ++i)
QCOMPARE(d.data()[i], wchar_t('A' + i));
}
@@ -1861,7 +1861,7 @@ void tst_QArrayData::variadicLiterals()
{
QArrayDataPointer<int> d =
Q_ARRAY_LITERAL(int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
- QCOMPARE(d.size, 10u);
+ QCOMPARE(d.size, 10);
for (int i = 0; i < 10; ++i)
QCOMPARE(d.data()[i], i);
}
@@ -1869,7 +1869,7 @@ void tst_QArrayData::variadicLiterals()
{
QArrayDataPointer<char> d = Q_ARRAY_LITERAL(char,
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
- QCOMPARE(d.size, 10u);
+ QCOMPARE(d.size, 10);
for (int i = 0; i < 10; ++i)
QCOMPARE(d.data()[i], char('A' + i));
}
@@ -1877,7 +1877,7 @@ void tst_QArrayData::variadicLiterals()
{
QArrayDataPointer<const char *> d = Q_ARRAY_LITERAL(const char *,
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
- QCOMPARE(d.size, 10u);
+ QCOMPARE(d.size, 10);
for (int i = 0; i < 10; ++i) {
QCOMPARE(d.data()[i][0], char('A' + i));
QCOMPARE(d.data()[i][1], '\0');
diff --git a/tests/auto/corelib/tools/qcache/tst_qcache.cpp b/tests/auto/corelib/tools/qcache/tst_qcache.cpp
index eb024e8f9e..2312a66d39 100644
--- a/tests/auto/corelib/tools/qcache/tst_qcache.cpp
+++ b/tests/auto/corelib/tools/qcache/tst_qcache.cpp
@@ -37,6 +37,7 @@ public slots:
void initTestCase();
void cleanupTestCase();
private slots:
+ void empty();
void maxCost();
void setMaxCost();
void totalCost();
@@ -50,6 +51,7 @@ private slots:
void largeCache();
void internalChainOrderAfterEntryUpdate();
void emplaceLowerCost();
+ void trimWithMovingAcrossSpans();
};
@@ -75,6 +77,21 @@ void tst_QCache::cleanupTestCase()
QCOMPARE(Foo::count, 0);
}
+void tst_QCache::empty()
+{
+ QCache<int, int> cache;
+ QCOMPARE(cache.size(), 0);
+ QCOMPARE(cache.count(), 0);
+ QVERIFY(cache.isEmpty());
+ QVERIFY(!cache.contains(1));
+ QCOMPARE(cache.keys().size(), 0);
+ QCOMPARE(cache.take(1), nullptr);
+ QVERIFY(!cache.remove(1));
+ QCOMPARE(cache.object(1), nullptr);
+ QCOMPARE(cache[1], nullptr);
+ QCOMPARE(cache.totalCost(), 0);
+}
+
void tst_QCache::maxCost()
{
QCache<QString, int> cache1, cache2(100), cache3(200), cache4(-50);
@@ -446,5 +463,71 @@ void tst_QCache::emplaceLowerCost()
QVERIFY(cache.isEmpty());
}
+struct TrivialHashType {
+ int i = -1;
+ size_t hash = 0;
+
+ TrivialHashType(int i, size_t hash) : i(i), hash(hash) {}
+ TrivialHashType(const TrivialHashType &o) noexcept = default;
+ TrivialHashType &operator=(const TrivialHashType &o) noexcept = default;
+ TrivialHashType(TrivialHashType &&o) noexcept : i(o.i), hash(o.hash) {
+ o.i = -1;
+ o.hash = 0;
+ }
+ TrivialHashType &operator=(TrivialHashType &&o) noexcept {
+ i = o.i;
+ hash = o.hash;
+ o.i = -1;
+ o.hash = 0;
+ return *this;
+ }
+
+
+ friend bool operator==(const TrivialHashType &lhs, const TrivialHashType &rhs)
+ {
+ return lhs.i == rhs.i;
+ }
+};
+quint64 qHash(TrivialHashType t, size_t seed = 0)
+{
+ Q_UNUSED(seed);
+ return t.hash;
+}
+
+// During trim(), if the Node we have a pointer to in the function is moved
+// to another span in the hash table, our pointer would end up pointing to
+// garbage memory. Test that this no longer happens
+void tst_QCache::trimWithMovingAcrossSpans()
+{
+ qsizetype numBuckets = [](){
+ QHash<int, int> h;
+ h.reserve(1);
+ // Beholden to QHash internals:
+ return h.capacity() << 1;
+ }();
+
+ QCache<TrivialHashType, int> cache;
+ cache.setMaxCost(1000);
+
+ auto lastBucketInSpan = size_t(numBuckets - 1);
+ // If this fails then the test is no longer valid
+ QCOMPARE(QHashPrivate::GrowthPolicy::bucketForHash(numBuckets, lastBucketInSpan),
+ lastBucketInSpan);
+
+ // Pad some space so we have two spans:
+ for (int i = 2; i < numBuckets; ++i)
+ cache.insert({i, 0}, nullptr);
+
+ // These two are vying for the last bucket in the first span,
+ // when '0' is deleted, '1' is moved across the span boundary,
+ // invalidating any pointer to its Node.
+ cache.insert({0, lastBucketInSpan}, nullptr);
+ cache.insert({1, lastBucketInSpan}, nullptr);
+
+ QCOMPARE(cache.size(), numBuckets);
+ cache.setMaxCost(0);
+ QCOMPARE(cache.size(), 0);
+}
+
QTEST_APPLESS_MAIN(tst_QCache)
#include "tst_qcache.moc"
diff --git a/tests/auto/corelib/tools/qcryptographichash/CMakeLists.txt b/tests/auto/corelib/tools/qcryptographichash/CMakeLists.txt
index d8de1798f3..cea74758cc 100644
--- a/tests/auto/corelib/tools/qcryptographichash/CMakeLists.txt
+++ b/tests/auto/corelib/tools/qcryptographichash/CMakeLists.txt
@@ -19,7 +19,7 @@ qt_internal_add_test(tst_qcryptographichash
## Scopes:
#####################################################################
-if(ANDROID)
+if(ANDROID OR INTEGRITY)
# Resources:
set(testdata_resource_files
"data/2c1517dad3678f03917f15849b052fd5.md5"
diff --git a/tests/auto/corelib/tools/qcryptographichash/testdata.qrc b/tests/auto/corelib/tools/qcryptographichash/testdata.qrc
deleted file mode 100644
index 8f7bcea63c..0000000000
--- a/tests/auto/corelib/tools/qcryptographichash/testdata.qrc
+++ /dev/null
@@ -1,6 +0,0 @@
-<RCC>
- <qresource prefix="/">
- <file>data/2c1517dad3678f03917f15849b052fd5.md5</file>
- <file>data/d41d8cd98f00b204e9800998ecf8427e.md5</file>
- </qresource>
-</RCC>
diff --git a/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp b/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp
index 12a4103060..f2cb3b3b44 100644
--- a/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp
+++ b/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp
@@ -38,6 +38,7 @@ class tst_QDuplicateTracker : public QObject
Q_OBJECT
private slots:
void hasSeen();
+ void clear();
void appendTo();
void appendTo_special();
};
@@ -76,6 +77,21 @@ void tst_QDuplicateTracker::hasSeen()
}
}
+void tst_QDuplicateTracker::clear()
+{
+ QDuplicateTracker<int, 2> tracker;
+ QVERIFY(!tracker.hasSeen(0));
+ QVERIFY(tracker.hasSeen(0));
+ QVERIFY(!tracker.hasSeen(1));
+ QVERIFY(tracker.hasSeen(1));
+
+ tracker.clear();
+ QVERIFY(!tracker.hasSeen(0));
+ QVERIFY(tracker.hasSeen(0));
+ QVERIFY(!tracker.hasSeen(1));
+ QVERIFY(tracker.hasSeen(1));
+}
+
void tst_QDuplicateTracker::appendTo()
{
QDuplicateTracker<int, 2> tracker;
diff --git a/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp b/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp
index 674d9fa6c0..1de1c3ad71 100644
--- a/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp
+++ b/tests/auto/corelib/tools/qflatmap/tst_qflatmap.cpp
@@ -26,6 +26,8 @@
**
****************************************************************************/
+#define QT_USE_QSTRINGBUILDER
+
#include <QTest>
#include <private/qflatmap_p.h>
@@ -45,11 +47,13 @@ private slots:
void constructing();
void constAccess();
void insertion();
+ void insertRValuesAndLValues();
void removal();
void extraction();
void iterators();
void statefulComparator();
void transparency();
+ void try_emplace_and_insert_or_assign();
void viewIterators();
void varLengthArray();
};
@@ -156,6 +160,42 @@ void tst_QFlatMap::insertion()
QCOMPARE(m.value("gnampf").data(), "GNAMPF");
}
+void tst_QFlatMap::insertRValuesAndLValues()
+{
+ using Map = QFlatMap<QByteArray, QByteArray>;
+ const QByteArray foo = QByteArrayLiteral("foo");
+ const QByteArray bar = QByteArrayLiteral("bar");
+
+ auto rvalue = [](const QByteArray &ba) { return ba; };
+#define lvalue(x) x
+
+ {
+ Map m;
+ QVERIFY( m.insert(lvalue(foo), lvalue(bar)).second);
+ QVERIFY(!m.insert(lvalue(foo), lvalue(bar)).second);
+ }
+
+ {
+ Map m;
+ QVERIFY( m.insert(lvalue(foo), rvalue(bar)).second);
+ QVERIFY(!m.insert(lvalue(foo), rvalue(bar)).second);
+ }
+
+ {
+ Map m;
+ QVERIFY( m.insert(rvalue(foo), lvalue(bar)).second);
+ QVERIFY(!m.insert(rvalue(foo), lvalue(bar)).second);
+ }
+
+ {
+ Map m;
+ QVERIFY( m.insert(rvalue(foo), rvalue(bar)).second);
+ QVERIFY(!m.insert(rvalue(foo), rvalue(bar)).second);
+ }
+
+#undef lvalue
+}
+
void tst_QFlatMap::extraction()
{
using Map = QFlatMap<int, QByteArray>;
@@ -388,6 +428,148 @@ void tst_QFlatMap::transparency()
QCOMPARE(m.lower_bound(sv3).value(), "dree");
}
+void tst_QFlatMap::try_emplace_and_insert_or_assign()
+{
+ using Map = QFlatMap<QByteArray, QByteArray>;
+
+ const QByteArray foo = QByteArrayLiteral("foo");
+ const qsizetype qqq_1 = 3;
+ const char qqq_2 = 'q';
+ const QByteArray qqq = QByteArray(qqq_1, qqq_2);
+
+ auto sb = [] (const auto &str) { return str % ""; };
+ auto rvalue = [](const auto &x) { return x; };
+#define lvalue(x) x
+#define CHECKS() \
+ do { \
+ QVERIFY(!m.try_emplace(rvalue(foo), lvalue(foo)).second); \
+ QCOMPARE(m.value(foo), qqq); \
+ QVERIFY(!m.try_emplace(lvalue(foo), lvalue(foo)).second); \
+ QCOMPARE(m.value(foo), qqq); \
+ QVERIFY(!m.try_emplace(lvalue(foo), sb(foo)).second); \
+ QCOMPARE(m.value(foo), qqq); \
+ QVERIFY(!m.try_emplace(rvalue(foo), sb(foo)).second); \
+ QCOMPARE(m.value(foo), qqq); \
+ } while (0) \
+ /* end */
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(lvalue(foo), lvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.insert_or_assign(lvalue(foo), lvalue(foo)).second);
+ QCOMPARE(m.value(foo), foo);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.insert_or_assign(lvalue(foo), lvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.try_emplace(lvalue(foo), lvalue(foo)).second);
+ QCOMPARE(m.value(foo), qqq);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(lvalue(foo), rvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.insert_or_assign(lvalue(foo), rvalue(foo)).second);
+ QCOMPARE(m.value(foo), foo);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.insert_or_assign(lvalue(foo), rvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.try_emplace(lvalue(foo), rvalue(foo)).second);
+ QCOMPARE(m.value(foo), qqq);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(lvalue(foo), qqq_1, qqq_2).second);
+ QCOMPARE(m.value(foo), qqq);
+ CHECKS();
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(lvalue(foo), sb(qqq)).second);
+ QCOMPARE(m.value(foo), qqq);
+ CHECKS();
+ QVERIFY(!m.insert_or_assign(lvalue(foo), sb(foo)).second);
+ QCOMPARE(m.value(foo), foo);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.insert_or_assign(lvalue(foo), sb(qqq)).second);
+ QCOMPARE(m.value(foo), qqq);
+ CHECKS();
+ QVERIFY(!m.try_emplace(lvalue(foo), sb(foo)).second);
+ QCOMPARE(m.value(foo), qqq);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(rvalue(foo), lvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.insert_or_assign(rvalue(foo), lvalue(foo)).second);
+ QCOMPARE(m.value(foo), foo);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.insert_or_assign(rvalue(foo), lvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.try_emplace(rvalue(foo), lvalue(foo)).second);
+ QCOMPARE(m.value(foo), qqq);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(rvalue(foo), rvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.insert_or_assign(rvalue(foo), rvalue(foo)).second);
+ QCOMPARE(m.value(foo), foo);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.insert_or_assign(rvalue(foo), rvalue(qqq)).second);
+ CHECKS();
+ QVERIFY(!m.try_emplace(rvalue(foo), rvalue(foo)).second);
+ QCOMPARE(m.value(foo), qqq);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(rvalue(foo), qqq_1, qqq_2).second);
+ QCOMPARE(m.value(foo), qqq);
+ CHECKS();
+ }
+
+ {
+ Map m;
+ QVERIFY(m.try_emplace(rvalue(foo), sb(qqq)).second);
+ QCOMPARE(m.value(foo), qqq);
+ CHECKS();
+ QVERIFY(!m.insert_or_assign(rvalue(foo), sb(foo)).second);
+ QCOMPARE(m.value(foo), foo);
+ }
+
+ {
+ Map m;
+ QVERIFY(m.insert_or_assign(rvalue(foo), sb(qqq)).second);
+ QCOMPARE(m.value(foo), qqq);
+ CHECKS();
+ QVERIFY(!m.try_emplace(rvalue(foo), sb(foo)).second);
+ QCOMPARE(m.value(foo), qqq);
+ }
+#undef CHECKS
+#undef lvalue
+}
+
void tst_QFlatMap::viewIterators()
{
using Map = QFlatMap<QByteArray, QByteArray>;
@@ -438,8 +620,7 @@ void tst_QFlatMap::viewIterators()
void tst_QFlatMap::varLengthArray()
{
- using Map = QFlatMap<int, QByteArray, std::less<int>,
- QVarLengthArray<int, 1024>, QVarLengthArray<QByteArray, 1024>>;
+ using Map = QVarLengthFlatMap<int, QByteArray, 1024>;
Map m{ { 2, "twee" } };
m.insert(1, "een");
m.remove(1);
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index 3c873d093e..bab22c454a 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -36,6 +36,8 @@
#include <unordered_set>
#include <string>
+#include <qsemaphore.h>
+
class tst_QHash : public QObject
{
Q_OBJECT
@@ -93,6 +95,14 @@ private slots:
void removeInEmptyHash();
void valueInEmptyHash();
void fineTuningInEmptyHash();
+
+ void reserveShared();
+
+ void QTBUG98265();
+
+ void detachAndReferences();
+
+ void lookupUsingKeyIterator();
};
struct IdentityTracker {
@@ -542,6 +552,22 @@ void tst_QHash::erase()
auto mit = h2.erase(bit);
mit = h2.erase(h2.begin());
QVERIFY(mit == h2.end());
+
+ h2 = QMultiHash<int, int>();
+ h2.emplace(1, 1);
+ h2.emplace(1, 2);
+ h2.emplace(3, 1);
+ h2.emplace(3, 4);
+ QMultiHash<int, int> h3 = h2;
+ auto it = h3.constFind(3);
+ ++it;
+ QVERIFY(h3.isSharedWith(h2));
+ it = h3.erase(it);
+ QVERIFY(!h3.isSharedWith(h2));
+ if (it != h3.cend()) {
+ auto it2 = h3.constFind(it.key());
+ QCOMPARE(it, it2);
+ }
}
/*
@@ -551,14 +577,40 @@ void tst_QHash::erase()
*/
void tst_QHash::erase_edge_case()
{
+ QHashSeed::setDeterministicGlobalSeed();
+ auto resetSeed = qScopeGuard([&]() {
+ QHashSeed::resetRandomGlobalSeed();
+ });
+
QHash<int, int> h1;
h1.reserve(2);
- h1.d->seed = 10230148258692185509ull;
- h1.insert(3, 4);
- h1.insert(5, 6);
+ qsizetype capacity = h1.capacity();
+ // Beholden to QHash internals:
+ qsizetype numBuckets = capacity << 1;
+
+ // Find some keys which will both be slotted into the last bucket:
+ int keys[2];
+ int index = 0;
+ for (qsizetype i = 0; i < numBuckets * 4 && index < 2; ++i) {
+ const size_t hash = qHash(i, QHashSeed::globalSeed());
+ const size_t bucketForHash = QHashPrivate::GrowthPolicy::bucketForHash(numBuckets, hash);
+ if (bucketForHash == numBuckets - 1)
+ keys[index++] = i;
+ }
+ QCOMPARE(index, 2); // Sanity check. If this fails then the test needs an update!
+
+ // As mentioned earlier these are both calculated to be in the last bucket:
+ h1.insert(keys[0], 4);
+ h1.insert(keys[1], 6);
+ // As a sanity-check, make sure that the key we inserted last is the first one (because its
+ // allocation to the last bucket would make it wrap around):
+ // NOTE: If this fails this then this test may need an update!!!
+ QCOMPARE(h1.constBegin().key(), keys[1]);
+ // Then we delete the last entry:
QHash<int, int>::iterator it1 = h1.begin();
++it1;
it1 = h1.erase(it1);
+ // Now, since we deleted the last entry, the iterator should be at the end():
QVERIFY(it1 == h1.end());
}
@@ -1740,6 +1792,7 @@ void tst_QHash::qmultihash_specific()
map2.insert(48, 3);
QCOMPARE(map1.count(), map2.count());
QVERIFY(map1.remove(42,5));
+ QVERIFY(map1 != map2);
QVERIFY(map2.remove(42,5));
QVERIFY(map1 == map2);
@@ -2598,5 +2651,96 @@ void tst_QHash::fineTuningInEmptyHash()
QVERIFY(hash.capacity() > 0);
}
+void tst_QHash::reserveShared()
+{
+ QHash<char, char> hash;
+ hash.insert('c', 'c');
+ auto hash2 = hash;
+
+ QCOMPARE(hash2.capacity(), hash.capacity());
+ auto oldCap = hash.capacity();
+
+ hash2.reserve(100); // This shouldn't crash
+
+ QVERIFY(hash2.capacity() >= 100);
+ QCOMPARE(hash.capacity(), oldCap);
+}
+
+void tst_QHash::QTBUG98265()
+{
+ QMultiHash<QUuid, QByteArray> a;
+ QMultiHash<QUuid, QByteArray> b;
+ a.insert(QUuid("3e0dfb4d-90eb-43a4-bd54-88f5b69832c1"), QByteArray());
+ b.insert(QUuid("1b710ada-3dd7-432e-b7c8-e852e59f46a0"), QByteArray());
+
+ QVERIFY(a != b);
+}
+
+/*
+ Calling functions which take a const-ref argument for a key with a reference
+ to a key inside the hash itself should keep the key valid as long as it is
+ needed. If not users may get hard-to-debug races where CoW should've
+ shielded them.
+*/
+void tst_QHash::detachAndReferences()
+{
+#if !QT_CONFIG(cxx11_future)
+ QSKIP("This test requires cxx11_future")
+#else
+ // Repeat a few times because it's not a guarantee
+ for (int i = 0; i < 50; ++i) {
+ QHash<char, char> hash;
+ hash.insert('a', 'a');
+ hash.insert('b', 'a');
+ hash.insert('c', 'a');
+ hash.insert('d', 'a');
+ hash.insert('e', 'a');
+ hash.insert('f', 'a');
+ hash.insert('g', 'a');
+
+ QSemaphore sem;
+ QSemaphore sem2;
+ std::thread th([&sem, &sem2, hash]() mutable {
+ sem.release();
+ sem2.acquire();
+ hash.reserve(100); // [2]: ...then this rehashes directly, without detaching
+ });
+
+ // The key is a reference to an entry in the hash. If we were already
+ // detached then no problem occurs! The problem happens because _after_
+ // we detach but before using the key the other thread resizes and
+ // rehashes, leaving our const-ref dangling.
+ auto it = hash.constBegin();
+ const auto &key = it.key(); // [3]: leaving our const-refs dangling
+ auto kCopy = key;
+ const auto &value = it.value();
+ auto vCopy = value;
+ sem2.release();
+ sem.acquire();
+ hash.insert(key, value); // [1]: this detaches first...
+
+ th.join();
+ QCOMPARE(hash.size(), 7);
+ QVERIFY(hash.contains(kCopy));
+ QCOMPARE(hash.value(kCopy), vCopy);
+ }
+#endif
+}
+
+void tst_QHash::lookupUsingKeyIterator()
+{
+ QHash<QString, QString> hash;
+ hash.reserve(1);
+ qsizetype minCapacity = hash.capacity();
+ // Beholden to internal implementation details:
+ qsizetype rehashLimit = minCapacity == 64 ? 63 : 8;
+
+ for (char16_t c = u'a'; c <= u'a' + rehashLimit; ++c)
+ hash.insert(QString(QChar(c)), u"h"_qs);
+
+ for (auto it = hash.keyBegin(), end = hash.keyEnd(); it != end; ++it)
+ QVERIFY(!hash[*it].isEmpty());
+}
+
QTEST_APPLESS_MAIN(tst_QHash)
#include "tst_qhash.moc"
diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
index a440d43454..3281c4cefd 100644
--- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
+++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
@@ -230,8 +230,14 @@ namespace SomeNamespace {
struct Hashable { int i; };
inline size_t qHash(Hashable h, size_t seed = 0)
{ return QT_PREPEND_NAMESPACE(qHash)(h.i, seed); }
-}
+ struct AdlHashable {
+ int i;
+ private:
+ friend size_t qHash(AdlHashable h, size_t seed = 0)
+ { return QT_PREPEND_NAMESPACE(qHash)(h.i, seed); }
+ };
+}
void tst_QHashFunctions::range()
{
static const int ints[] = {0, 1, 2, 3, 4, 5};
@@ -253,10 +259,16 @@ void tst_QHashFunctions::range()
QCOMPARE(qHashRange(ints, ints + numInts, seed), qHashRange(it, end, seed));
}
- SomeNamespace::Hashable hashables[] = {{0}, {1}, {2}, {3}, {4}, {5}};
- static const size_t numHashables = sizeof hashables / sizeof *hashables;
- // compile check: is qHash() found using ADL?
- (void)qHashRange(hashables, hashables + numHashables, seed);
+ {
+ SomeNamespace::Hashable hashables[] = {{0}, {1}, {2}, {3}, {4}, {5}};
+ // compile check: is qHash() found using ADL?
+ [[maybe_unused]] auto r = qHashRange(std::begin(hashables), std::end(hashables), seed);
+ }
+ {
+ SomeNamespace::AdlHashable hashables[] = {{0}, {1}, {2}, {3}, {4}, {5}};
+ // compile check: is qHash() found as a hidden friend?
+ [[maybe_unused]] auto r = qHashRange(std::begin(hashables), std::end(hashables), seed);
+ }
}
void tst_QHashFunctions::rangeCommutative()
@@ -279,15 +291,47 @@ void tst_QHashFunctions::rangeCommutative()
QCOMPARE(qHashRangeCommutative(ints, ints + numInts, seed), qHashRangeCommutative(it, end, seed));
}
- SomeNamespace::Hashable hashables[] = {{0}, {1}, {2}, {3}, {4}, {5}};
- static const size_t numHashables = sizeof hashables / sizeof *hashables;
- // compile check: is qHash() found using ADL?
- (void)qHashRangeCommutative(hashables, hashables + numHashables, seed);
+ {
+ SomeNamespace::Hashable hashables[] = {{0}, {1}, {2}, {3}, {4}, {5}};
+ // compile check: is qHash() found using ADL?
+ [[maybe_unused]] auto r = qHashRangeCommutative(std::begin(hashables), std::end(hashables), seed);
+ }
+ {
+ SomeNamespace::AdlHashable hashables[] = {{0}, {1}, {2}, {3}, {4}, {5}};
+ // compile check: is qHash() found as a hidden friend?
+ [[maybe_unused]] auto r = qHashRangeCommutative(std::begin(hashables), std::end(hashables), seed);
+ }
}
+// QVarLengthArray these days has a qHash() as a hidden friend.
+// This checks that QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH can deal with that:
+
+QT_BEGIN_NAMESPACE
+QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QVarLengthArray<QVector<int>>)
+QT_END_NAMESPACE
+
void tst_QHashFunctions::stdHash()
{
{
+ std::unordered_set<QVarLengthArray<QVector<int>>> s = {
+ {
+ {0, 1, 2},
+ {42, 43, 44},
+ {},
+ }, {
+ {11, 12, 13},
+ {},
+ },
+ };
+ QCOMPARE(s.size(), 2UL);
+ s.insert({
+ {11, 12, 13},
+ {},
+ });
+ QCOMPARE(s.size(), 2UL);
+ }
+
+ {
std::unordered_set<QString> s = {QStringLiteral("Hello"), QStringLiteral("World")};
QCOMPARE(s.size(), 2UL);
s.insert(QStringLiteral("Hello"));
diff --git a/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp b/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
index 60f3fe9e3c..f91ecced76 100644
--- a/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
+++ b/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
@@ -134,8 +134,13 @@ void tst_QHashSeed::reseeding()
void tst_QHashSeed::quality()
{
- constexpr int Iterations = 16;
+ // this "bad seed" is used internally in qhash.cpp and should never leak!
+ constexpr size_t BadSeed = size_t(Q_UINT64_C(0x5555'5555'5555'5555));
+
+ constexpr int Iterations = 24; // nicely divisible by 3
int oneThird = 0;
+ int badSeeds = 0;
+ int seedsToMinus1 = 0;
size_t ored = 0;
for (int i = 0; i < Iterations; ++i) {
@@ -146,21 +151,35 @@ void tst_QHashSeed::quality()
if (bits >= std::numeric_limits<size_t>::digits / 3)
++oneThird;
+ if (seed == BadSeed)
+ ++badSeeds;
+ if (ored != size_t(-1))
+ ++seedsToMinus1;
+
+ QHashSeed::resetRandomGlobalSeed();
}
// report out
+ qInfo() << "Number of seeds until all bits became set:" << seedsToMinus1 << '/' << Iterations;
qInfo() << "Number of seeds with at least one third of the bits set:"
<< oneThird << '/' << Iterations;
- qInfo() << "Number of bits in OR'ed value:" << qPopulationCount(quintptr(ored))
- << '/' << std::numeric_limits<size_t>::digits;
- if (std::numeric_limits<size_t>::digits > 32) {
- quint32 upper = quint64(ored) >> 32;
- qInfo() << "Number of bits in the upper half:" << qPopulationCount(upper) << "/ 32";
- QVERIFY(qPopulationCount(upper) > (32/3));
- }
+
+ // we must have set all bits after all the iterations
+ QCOMPARE(ored, size_t(-1));
// at least one third of the seeds must have one third of all the bits set
- QVERIFY(oneThird > (16/3));
+ QVERIFY(oneThird > (Iterations/3));
+
+ // at most one seed can be the bad seed, if 32-bit, none on 64-bit
+ if (std::numeric_limits<size_t>::digits > 32)
+ QCOMPARE(badSeeds, 0);
+ else
+ QVERIFY2(badSeeds <= 1, "badSeeds = " + QByteArray::number(badSeeds));
+
+ // we must have taken at most two thirds of the iterations to have set each
+ // bit at least once
+ QVERIFY2(seedsToMinus1 < 2*Iterations/3,
+ "seedsToMinus1 = " + QByteArray::number(seedsToMinus1));
}
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
diff --git a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp
index 0130939b4e..aecbe0b616 100644
--- a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp
+++ b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp
@@ -46,8 +46,7 @@ constexpr const auto messages = qOffsetStringArray(
"level - 1",
"level - 2",
"level - 3",
- "level - 4",
- ""
+ "level - 4"
);
constexpr const auto messages257 = qOffsetStringArray(
@@ -90,15 +89,15 @@ constexpr const auto messagesBigOffsets = qOffsetStringArray(
void tst_QOffsetStringArray::init()
{
- static_assert(messages.m_string.size() == 51);
+ static_assert(messages.m_string.size() == 50);
static_assert(messages.m_offsets.size() == 6);
static_assert(std::is_same_v<decltype(messages.m_offsets)::value_type, quint8>);
- static_assert(messages257.m_offsets.size() == 257);
+ static_assert(messages257.m_offsets.size() == 258);
static_assert(messages257.m_string.size() == 260);
static_assert(std::is_same_v<decltype(messages257.m_offsets)::value_type, quint16>);
- static_assert(messagesBigOffsets.m_offsets.size() == 4);
+ static_assert(messagesBigOffsets.m_offsets.size() == 5);
static_assert(messagesBigOffsets.m_string.size() == 364);
static_assert(std::is_same_v<decltype(messagesBigOffsets.m_offsets)::value_type, quint16>);
}
@@ -110,6 +109,7 @@ void tst_QOffsetStringArray::access()
QCOMPARE(messages[2], "level - 2");
QCOMPARE(messages[3], "level - 3");
QCOMPARE(messages[4], "level - 4");
+ // out of bounds returns empty strings:
QCOMPARE(messages[5], "");
QCOMPARE(messages[6], "");
}
diff --git a/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp b/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp
index 3b922de0ca..6efa39f4f5 100644
--- a/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp
+++ b/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp
@@ -38,6 +38,7 @@ class tst_QRingBuffer : public QObject
private slots:
void constructing();
void usingInVector();
+ void usingInVarLengthArray();
void readPointerAtPositionWriteRead();
void readPointerAtPositionEmptyRead();
void readPointerAtPositionWithHead();
@@ -83,10 +84,20 @@ void tst_QRingBuffer::constructing()
void tst_QRingBuffer::usingInVector()
{
QRingBuffer ringBuffer;
- QList<QRingBuffer> buffers;
+ std::vector<QRingBuffer> buffers;
ringBuffer.reserve(5);
- buffers.append(ringBuffer);
+ buffers.push_back(std::move(ringBuffer));
+ QCOMPARE(buffers[0].size(), Q_INT64_C(5));
+}
+
+void tst_QRingBuffer::usingInVarLengthArray()
+{
+ QRingBuffer ringBuffer;
+ QVarLengthArray<QRingBuffer, 42> buffers;
+
+ ringBuffer.reserve(5);
+ buffers.push_back(std::move(ringBuffer));
QCOMPARE(buffers[0].size(), Q_INT64_C(5));
}
diff --git a/tests/auto/corelib/tools/qsharedpointer/externaltests.cpp b/tests/auto/corelib/tools/qsharedpointer/externaltests.cpp
index 37a24b6b9b..08b0f21cc9 100644
--- a/tests/auto/corelib/tools/qsharedpointer/externaltests.cpp
+++ b/tests/auto/corelib/tools/qsharedpointer/externaltests.cpp
@@ -362,7 +362,7 @@ namespace QTest {
"}\n"
"\n"
"#ifdef Q_OS_WIN\n"
- "#include <windows.h>\n"
+ "#include <qt_windows.h>\n"
"#if defined(Q_CC_MSVC)\n"
"#include <crtdbg.h>\n"
"#endif\n"
diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp
index bd84d0bb3c..d526314d41 100644
--- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp
+++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp
@@ -2688,7 +2688,7 @@ void tst_QSharedPointer::constructorThrow()
int childDestructorCounter = ThrowData::childDestructorCounter;
QSharedPointer<ThrowData> ptr;
- QVERIFY_EXCEPTION_THROWN(ptr = QSharedPointer<ThrowData>::create(), QString);
+ QVERIFY_THROWS_EXCEPTION(QString, ptr = QSharedPointer<ThrowData>::create());
QVERIFY(ptr.isNull());
QCOMPARE(ThrowData::childGenerationCounter, childGeneration + 1);
// destructor should never be called, if a constructor throws
diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
index 6e95577655..cc4454fbd4 100644
--- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
+++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
@@ -27,9 +27,10 @@
****************************************************************************/
#include <QtTest/QTest>
-#include <qvarlengtharray.h>
+#include <QVarLengthArray>
#include <qvariant.h>
#include <qscopeguard.h>
+#include <qscopedvaluerollback.h>
#include <memory>
@@ -68,8 +69,12 @@ class tst_QVarLengthArray : public QObject
{
Q_OBJECT
private slots:
+ void defaultConstructor_int() { defaultConstructor<int>(); }
+ void defaultConstructor_QString() { defaultConstructor<QString>(); }
void append();
+#if QT_DEPRECATED_SINCE(6, 3)
void prepend();
+#endif
void emplace();
void move_int_1() { move_int<1>(); }
void move_int_2() { move_int<2>(); }
@@ -83,6 +88,7 @@ private slots:
void removeLast();
void oldTests();
void appendCausingRealloc();
+ void appendIsStronglyExceptionSafe();
void resize();
void realloc();
void iterators();
@@ -112,6 +118,8 @@ private slots:
void erase();
private:
+ template <typename T>
+ void defaultConstructor();
template <qsizetype N, typename T>
void move(T t1, T t2);
template <qsizetype N>
@@ -124,6 +132,23 @@ private:
void initializeList();
};
+template <typename T>
+void tst_QVarLengthArray::defaultConstructor()
+{
+ {
+ QVarLengthArray<T, 123> vla;
+ QCOMPARE(vla.size(), 0);
+ QVERIFY(vla.empty());
+ QVERIFY(vla.isEmpty());
+ QCOMPARE(vla.begin(), vla.end());
+ QCOMPARE(vla.capacity(), 123);
+ }
+ {
+ QVarLengthArray<T> vla;
+ QCOMPARE(vla.capacity(), 256); // notice, should we change the default
+ }
+}
+
void tst_QVarLengthArray::append()
{
QVarLengthArray<QString, 2> v;
@@ -146,6 +171,9 @@ void tst_QVarLengthArray::append()
v2.append(5);
}
+#if QT_DEPRECATED_SINCE(6, 3)
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_DEPRECATED
void tst_QVarLengthArray::prepend()
{
QVarLengthArray<QString, 2> v;
@@ -164,6 +192,8 @@ void tst_QVarLengthArray::prepend()
v.prepend(v.back());
QCOMPARE(v.front(), v.back());
}
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 3)
void tst_QVarLengthArray::emplace()
{
@@ -180,6 +210,33 @@ void tst_QVarLengthArray::emplace()
QCOMPARE(&r, &strings.back());
QCOMPARE(strings.size(), 3);
QCOMPARE(strings.back(), QString(42, u'y'));
+
+ // test growing from empty arrays
+ QVarLengthArray<QString> emptyArrDefaultPrealloc;
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 0);
+ emptyArrDefaultPrealloc.emplace_back();
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 1);
+ emptyArrDefaultPrealloc.resize(1024);
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 1024);
+ emptyArrDefaultPrealloc.resize(0);
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 0);
+ emptyArrDefaultPrealloc.squeeze();
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 0);
+ emptyArrDefaultPrealloc.emplace_back();
+ QCOMPARE(emptyArrDefaultPrealloc.size(), 1);
+
+ QVarLengthArray<QString, 1> emptyArrSmallPrealloc;
+ QCOMPARE(emptyArrSmallPrealloc.size(), 0);
+ emptyArrSmallPrealloc.emplace_back();
+ QCOMPARE(emptyArrSmallPrealloc.size(), 1);
+ emptyArrSmallPrealloc.resize(1024);
+ QCOMPARE(emptyArrSmallPrealloc.size(), 1024);
+ emptyArrSmallPrealloc.resize(0);
+ QCOMPARE(emptyArrSmallPrealloc.size(), 0);
+ emptyArrSmallPrealloc.squeeze();
+ QCOMPARE(emptyArrSmallPrealloc.size(), 0);
+ emptyArrSmallPrealloc.emplace_back();
+ QCOMPARE(emptyArrSmallPrealloc.size(), 1);
}
}
@@ -354,6 +411,69 @@ void tst_QVarLengthArray::appendCausingRealloc()
d.append(i);
}
+void tst_QVarLengthArray::appendIsStronglyExceptionSafe()
+{
+#ifdef QT_NO_EXCEPTIONS
+ QSKIP("This test requires exception support enabled in the compiler.");
+#else
+ static bool throwOnCopyNow = false;
+ static bool throwOnMoveNow = false;
+ struct Thrower {
+ Thrower() = default;
+ Thrower(const Thrower &)
+ {
+ if (throwOnCopyNow)
+ throw 1;
+ }
+ Thrower &operator=(const Thrower &) = default;
+ Thrower(Thrower &&)
+ {
+ if (throwOnMoveNow)
+ throw 1;
+ }
+ Thrower &operator=(Thrower &&) = default;
+ ~Thrower() = default;
+ };
+
+ {
+ QVarLengthArray<Thrower, 2> vla(1);
+ {
+ Thrower t;
+ const QScopedValueRollback rb(throwOnCopyNow, true);
+ QVERIFY_THROWS_EXCEPTION(int, vla.push_back(t));
+ QCOMPARE(vla.size(), 1);
+ }
+ {
+ const QScopedValueRollback rb(throwOnMoveNow, true);
+ QVERIFY_THROWS_EXCEPTION(int, vla.push_back({}));
+ QCOMPARE(vla.size(), 1);
+ }
+ vla.push_back({});
+ QCOMPARE(vla.size(), 2);
+ {
+ Thrower t;
+ {
+ // tests the copy inside append()
+ const QScopedValueRollback rb(throwOnCopyNow, true);
+ QVERIFY_THROWS_EXCEPTION(int, vla.push_back(t));
+ QCOMPARE(vla.size(), 2);
+ }
+ {
+ // tests the move inside reallocate()
+ const QScopedValueRollback rb(throwOnMoveNow, true);
+ QVERIFY_THROWS_EXCEPTION(int, vla.push_back(t));
+ QCOMPARE(vla.size(), 2);
+ }
+ }
+ {
+ const QScopedValueRollback rb(throwOnMoveNow, true);
+ QVERIFY_THROWS_EXCEPTION(int, vla.push_back({}));
+ QCOMPARE(vla.size(), 2);
+ }
+ }
+#endif
+}
+
void tst_QVarLengthArray::resize()
{
// Empty Movable
@@ -1226,7 +1346,7 @@ void tst_QVarLengthArray::insertMove()
QCOMPARE(MyBase::liveCount, 6);
QCOMPARE(MyBase::movedCount, 2);
- vec.prepend(std::move(m1));
+ vec.insert(vec.cbegin(), std::move(m1));
QVERIFY(m1.wasConstructedAt(nullptr));
QVERIFY(vec.at(0).wasConstructedAt(&m1));
QVERIFY(vec.at(1).wasConstructedAt(&m3));
@@ -1298,7 +1418,7 @@ void tst_QVarLengthArray::nonCopyable()
QVERIFY(!val4);
QVERIFY(ptr3 == vec.at(0).get());
QVERIFY(ptr4 == vec.at(1).get());
- vec.prepend(std::move(val1));
+ vec.insert(vec.cbegin(), std::move(val1));
QVERIFY(!val1);
QVERIFY(ptr1 == vec.at(0).get());
QVERIFY(ptr3 == vec.at(1).get());
diff --git a/tests/auto/corelib/tools/qversionnumber/tst_qversionnumber.cpp b/tests/auto/corelib/tools/qversionnumber/tst_qversionnumber.cpp
index b0dbcb042d..42ef770817 100644
--- a/tests/auto/corelib/tools/qversionnumber/tst_qversionnumber.cpp
+++ b/tests/auto/corelib/tools/qversionnumber/tst_qversionnumber.cpp
@@ -73,6 +73,7 @@ private slots:
void assignment();
void fromString_data();
void fromString();
+ void fromString_extra();
void toString_data();
void toString();
void isNull_data();
@@ -511,7 +512,7 @@ void tst_QVersionNumber::fromString()
QFETCH(QVersionNumber, expectedVersion);
QFETCH(int, suffixIndex);
- int index;
+ qsizetype index;
QCOMPARE(QVersionNumber::fromString(constructionString), expectedVersion);
QCOMPARE(QVersionNumber::fromString(constructionString, &index), expectedVersion);
QCOMPARE(index, suffixIndex);
@@ -523,6 +524,46 @@ void tst_QVersionNumber::fromString()
QCOMPARE(QVersionNumber::fromString(QLatin1String(constructionString.toLatin1())), expectedVersion);
QCOMPARE(QVersionNumber::fromString(QLatin1String(constructionString.toLatin1()), &index), expectedVersion);
QCOMPARE(index, suffixIndex);
+
+#if QT_DEPRECATED_SINCE(6, 4)
+ QT_WARNING_PUSH
+ QT_WARNING_DISABLE_DEPRECATED
+ // check deprecated `int *suffixIndex` overload, too
+ {
+ int i;
+ QCOMPARE(QVersionNumber::fromString(constructionString, &i), expectedVersion);
+ QCOMPARE(i, suffixIndex);
+
+ QCOMPARE(QVersionNumber::fromString(QStringView(constructionString), &i), expectedVersion);
+ QCOMPARE(i, suffixIndex);
+
+ QCOMPARE(QVersionNumber::fromString(QLatin1String(constructionString.toLatin1()), &i), expectedVersion);
+ QCOMPARE(i, suffixIndex);
+ }
+ QT_WARNING_POP
+#endif
+}
+
+void tst_QVersionNumber::fromString_extra()
+{
+ // check the overloaded fromString() functions aren't ambiguous
+ // when passing explicit nullptr:
+ {
+ auto v = QVersionNumber::fromString("1.2.3-rc1", nullptr);
+ QCOMPARE(v, QVersionNumber({1, 2, 3}));
+ }
+ {
+ auto v = QVersionNumber::fromString("1.2.3-rc1", 0);
+ QCOMPARE(v, QVersionNumber({1, 2, 3}));
+ }
+
+ // check the UTF16->L1 conversion isn't doing something weird
+ {
+ qsizetype i = -1;
+ auto v = QVersionNumber::fromString(u"1.0ı", &i); // LATIN SMALL LETTER DOTLESS I
+ QCOMPARE(v, QVersionNumber(1, 0));
+ QCOMPARE(i, 3);
+ }
}
void tst_QVersionNumber::toString_data()