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/containerapisymmetry/tst_containerapisymmetry.cpp52
-rw-r--r--tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp36
-rw-r--r--tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp66
-rw-r--r--tests/auto/corelib/tools/qvector/tst_qvector.cpp108
4 files changed, 253 insertions, 9 deletions
diff --git a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
index 2c7c8f6514..11f181a955 100644
--- a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
+++ b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
@@ -40,8 +40,10 @@
#include <algorithm>
#include <functional>
#include <vector> // for reference
+#include <iostream>
#include <list>
#include <set>
+#include <sstream>
#include <map>
// MSVC has these containers from the Standard Library, but it lacks
@@ -64,6 +66,28 @@
#include <unordered_map>
#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
+
+namespace {
+template <typename T>
+struct is_qlist : std::false_type {};
+template <typename T>
+struct is_qlist<QList<T>> : std::true_type {};
+}
+
struct Movable
{
explicit Movable(int i = 0) Q_DECL_NOTHROW
@@ -85,6 +109,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;
@@ -124,6 +153,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;
@@ -550,12 +584,30 @@ 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 = [&] {
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) // QTBUG-99036
+ if constexpr (is_qlist<Container>::value) {
+ return c4;
+ } else
+#endif
+ {
+ 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/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
index f76f3aa0c6..4eb8a97051 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 uint qHash(Hashable h, uint 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,10 +291,16 @@ 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);
+ }
}
void tst_QHashFunctions::stdHash()
diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
index 5443cf120b..0174885cf3 100644
--- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
+++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
@@ -29,6 +29,7 @@
#include <QtTest/QTest>
#include <qvarlengtharray.h>
#include <qvariant.h>
+#include <qscopedvaluerollback.h>
#include <memory>
@@ -36,10 +37,13 @@ class tst_QVarLengthArray : public QObject
{
Q_OBJECT
private slots:
+ void defaultConstructor_int() { defaultConstructor<int>(); }
+ void defaultConstructor_QString() { defaultConstructor<QString>(); }
void append();
void removeLast();
void oldTests();
void appendCausingRealloc();
+ void appendIsStronglyExceptionSafe();
void resize();
void realloc();
void reverseIterators();
@@ -61,6 +65,8 @@ private slots:
void implicitDefaultCtor();
private:
+ template <typename T>
+ void defaultConstructor();
template<typename T>
void initializeList();
};
@@ -80,6 +86,23 @@ struct Tracker
int Tracker::count = 0;
+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;
@@ -245,6 +268,49 @@ 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;
+ };
+
+ {
+ // ### TODO: QVLA isn't exception-safe when throwing during reallocation,
+ // ### so check with size() < capacity() for now
+ QVarLengthArray<Thrower, 2> vla(1);
+ {
+ Thrower t;
+ const QScopedValueRollback<bool> rb(throwOnCopyNow, true);
+ QVERIFY_EXCEPTION_THROWN(vla.push_back(t), int);
+ QCOMPARE(vla.size(), 1);
+ }
+ {
+ const QScopedValueRollback<bool> rb(throwOnMoveNow, true);
+ QVERIFY_EXCEPTION_THROWN(vla.push_back({}), int);
+ QCOMPARE(vla.size(), 1);
+ }
+ }
+#endif
+}
+
void tst_QVarLengthArray::resize()
{
//MOVABLE
diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp
index 08d5a8cd50..01409bd188 100644
--- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp
+++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp
@@ -183,6 +183,94 @@ inline uint qHash(const Custom &key, uint seed = 0) { return qHash(key.i, seed);
Q_DECLARE_METATYPE(Custom);
+// Similar to Custom but not default-constructible and has move constructor and assignment
+struct NonDefaultConstructible {
+ NonDefaultConstructible() = delete;
+ NonDefaultConstructible(char input)
+ : i(input)
+ , that(this)
+ , state(Constructed)
+ {
+ counter.fetchAndAddRelaxed(1);
+ }
+ NonDefaultConstructible(const NonDefaultConstructible &other)
+ : i(other.i)
+ , that(this)
+ , state(Constructed)
+ {
+ check(&other);
+ counter.fetchAndAddRelaxed(1);
+ }
+ NonDefaultConstructible(NonDefaultConstructible &&other)
+ : i(other.i)
+ , that(this)
+ , state(Constructed)
+ {
+ check(&other);
+ other.state = MovedFrom;
+ counter.fetchAndAddRelaxed(1);
+ }
+ ~NonDefaultConstructible()
+ {
+ check(this, true);
+ i = 0;
+ counter.fetchAndAddRelaxed(-1);
+ state = Destructed;
+ }
+
+ bool operator==(const NonDefaultConstructible &other) const
+ {
+ check(&other);
+ check(this);
+ return i == other.i;
+ }
+
+ bool operator<(const NonDefaultConstructible &other) const
+ {
+ check(&other);
+ check(this);
+ return i < other.i;
+ }
+
+ NonDefaultConstructible &operator=(const NonDefaultConstructible &other)
+ {
+ check(&other);
+ check(this, true);
+ i = other.i;
+ state = Constructed;
+ return *this;
+ }
+
+ NonDefaultConstructible &operator=(NonDefaultConstructible &&other)
+ {
+ check(&other);
+ check(this, true);
+ i = other.i;
+ state = Constructed;
+ other.state = MovedFrom;
+ return *this;
+ }
+ static QAtomicInt counter;
+
+ char i; // used to identify origin of an instance
+private:
+ NonDefaultConstructible *that; // used to catch copying using mem{cpy,move}()
+
+ enum State { Constructed = 106, Destructed = 110, MovedFrom = 120 };
+ State state;
+
+ static void check(const NonDefaultConstructible *c, bool movedOk = false)
+ {
+ // check if c object has been moved incorrectly
+ QCOMPARE(c, c->that);
+ if (!movedOk || c->state != MovedFrom)
+ QCOMPARE(c->state, Constructed);
+ }
+};
+QAtomicInt NonDefaultConstructible::counter = 0;
+
+inline uint qHash(const NonDefaultConstructible &key, uint seed = 0) { return qHash(key.i, seed); }
+
// tests depends on the fact that:
Q_STATIC_ASSERT(!QTypeInfo<int>::isStatic);
Q_STATIC_ASSERT(!QTypeInfo<int>::isComplex);
@@ -190,6 +278,8 @@ Q_STATIC_ASSERT(!QTypeInfo<Movable>::isStatic);
Q_STATIC_ASSERT(QTypeInfo<Movable>::isComplex);
Q_STATIC_ASSERT(QTypeInfo<Custom>::isStatic);
Q_STATIC_ASSERT(QTypeInfo<Custom>::isComplex);
+Q_STATIC_ASSERT(QTypeInfo<NonDefaultConstructible>::isStatic);
+Q_STATIC_ASSERT(QTypeInfo<NonDefaultConstructible>::isComplex);
class tst_QVector : public QObject
@@ -263,6 +353,7 @@ private slots:
void insertInt() const;
void insertMovable() const;
void insertCustom() const;
+ void insertNonDefaultConstructible() const;
void isEmpty() const;
void last() const;
void lastIndexOf() const;
@@ -273,6 +364,7 @@ private slots:
void prependInt() const;
void prependMovable() const;
void prependCustom() const;
+ void prependNonDefaultConstructible() const;
void qhashInt() const { qhash<int>(); }
void qhashMovable() const { qhash<Movable>(); }
void qhashCustom() const { qhash<Custom>(); }
@@ -390,6 +482,10 @@ template<>
const Movable SimpleValue<Movable>::Values[] = { 110, 105, 101, 114, 111, 98 };
template<>
const Custom SimpleValue<Custom>::Values[] = { 110, 105, 101, 114, 111, 98 };
+template<>
+const NonDefaultConstructible SimpleValue<NonDefaultConstructible>::Values[] =
+ { 110, 105, 101, 114, 111, 98 };
+
// Make some macros for the tests to use in order to be slightly more readable...
#define T_FOO SimpleValue<T>::at(0)
@@ -1592,6 +1688,11 @@ void tst_QVector::insertCustom() const
insert<Custom>();
}
+void tst_QVector::insertNonDefaultConstructible() const
+{
+ insert<NonDefaultConstructible>();
+}
+
void tst_QVector::isEmpty() const
{
QVector<QString> myvec;
@@ -1831,6 +1932,13 @@ void tst_QVector::prependCustom() const
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
+void tst_QVector::prependNonDefaultConstructible() const
+{
+ const int instancesCount = NonDefaultConstructible::counter.loadAcquire();
+ prepend<NonDefaultConstructible>();
+ QCOMPARE(instancesCount, NonDefaultConstructible::counter.loadAcquire());
+}
+
void tst_QVector::removeAllWithAlias() const
{
QVector<QString> strings;