diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-04-16 17:49:20 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2021-05-23 12:08:43 -0700 |
commit | 64bfc927b09b46bb7fc0dc6caf1bf1a4d4133ab4 (patch) | |
tree | d1b3bd74bc20ce817ea362fc5280bc0ce4f6c0bd /tests/auto/corelib/tools | |
parent | a5ff71578da5f6441ab8b07ae1fbf736d7f1e3ba (diff) |
QHash: fix qHash(std::pair)
There were two problems here: first, qHash(std::pair) must be declared
before qHashMulti that might call back to qHash(std::pair) (i.e., a pair
with one element that is also a pair). But moving the declaration above
causes the second problem: the noexcept expression can't refer to qHash
functions that aren't declared yet. So we forward-declare a constexpr
function for that result, but implement it far below.
Fixes: QTBUG-92910
Change-Id: Ia8e48103a54446509e3bfffd16767ed2e29b026c
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r-- | tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp index d313fbb0b0..a440d43454 100644 --- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp +++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp @@ -46,6 +46,8 @@ public: }; uint seed; + template <typename T1, typename T2> void stdPair_template(const T1 &t1, const T2 &t2); + public slots: void initTestCase(); void init(); @@ -63,6 +65,18 @@ private Q_SLOTS: void stdHash(); + void stdPair_int_int() { stdPair_template(1, 2); } + void stdPair_ulong_llong() { stdPair_template(1UL, -2LL); } + void stdPair_ullong_long() { stdPair_template(1ULL, -2L); } + void stdPair_string_int() { stdPair_template(QString("Hello"), 2); } + void stdPair_int_string() { stdPair_template(1, QString("Hello")); } + void stdPair_bytearray_string() { stdPair_template(QByteArray("Hello"), QString("World")); } + void stdPair_string_bytearray() { stdPair_template(QString("Hello"), QByteArray("World")); } + void stdPair_int_pairIntInt() { stdPair_template(1, std::make_pair(2, 3)); } + void stdPair_2x_pairIntInt() { stdPair_template(std::make_pair(1, 2), std::make_pair(2, 3)); } + void stdPair_string_pairIntInt() { stdPair_template(QString("Hello"), std::make_pair(42, -47)); } // QTBUG-92910 + void stdPair_int_pairIntPairIntInt() { stdPair_template(1, std::make_pair(2, std::make_pair(3, 4))); } + void setGlobalQHashSeed(); }; @@ -310,6 +324,32 @@ void tst_QHashFunctions::stdHash() } +template <typename T1, typename T2> +void tst_QHashFunctions::stdPair_template(const T1 &t1, const T2 &t2) +{ + std::pair<T1, T2> dpair{}; + std::pair<T1, T2> vpair{t1, t2}; + + size_t seed = QHashSeed::globalSeed(); + + // confirm proper working of the pair and of the underlying types + QVERIFY(t1 == t1); + QVERIFY(t2 == t2); + QCOMPARE(qHash(t1), qHash(t1)); + QCOMPARE(qHash(t2), qHash(t2)); + QCOMPARE(qHash(t1, seed), qHash(t1, seed)); + QCOMPARE(qHash(t2, seed), qHash(t2, seed)); + + QVERIFY(dpair == dpair); + QVERIFY(vpair == vpair); + + // therefore their hashes should be equal + QCOMPARE(qHash(dpair), qHash(dpair)); + QCOMPARE(qHash(dpair, seed), qHash(dpair, seed)); + QCOMPARE(qHash(vpair), qHash(vpair)); + QCOMPARE(qHash(vpair, seed), qHash(vpair, seed)); +} + void tst_QHashFunctions::setGlobalQHashSeed() { // Setter works as advertised |