summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2024-02-02 20:00:33 -0800
committerThiago Macieira <thiago.macieira@intel.com>2024-03-12 17:23:09 -0800
commit970aad541811d002e5004bd3826929247492ba09 (patch)
tree0b2a531dd7ea4a5f80f3b8e6e5475f7983746307 /tests/auto/corelib/tools
parent9a2e21174a08cf282cb933c95380abd900102ae0 (diff)
qHash: implement chunked hashing of QLatin1StringView
So that it hashes to the same value as QString{,View}. In order to test this, you must either run on a CPU other than ARM and x86, or disable the AES hasher. I did that and can confirm siphash and murmurhash do work with on-the-fly conversion from Latin-1. Change-Id: I664b9f014ffc48cbb49bfffd17b03e5e62ec4e89 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp41
-rw-r--r--tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp13
2 files changed, 50 insertions, 4 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index 6b7b5955df..b3dbdfa40c 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -42,6 +42,7 @@ private slots:
void heterogeneousSearchConstKey();
void heterogeneousSearchByteArray();
void heterogeneousSearchString();
+ void heterogeneousSearchLatin1String();
void rehash_isnt_quadratic();
void dont_need_default_constructor();
@@ -54,6 +55,7 @@ private slots:
void qmultihashHeterogeneousSearchConstKey();
void qmultihashHeterogeneousSearchByteArray();
void qmultihashHeterogeneousSearchString();
+ void qmultihashHeterogeneousSearchLatin1String();
void compare();
void compare2();
@@ -1221,15 +1223,17 @@ template <> struct HeterogeneousSearchTestHelper<HeterogeneousHashingType>
using HeterogeneousHashingType = QString;
#endif
-template <template <typename, typename> class Hash, typename String, typename View>
-static void heterogeneousSearchTest(const QList<std::remove_const_t<String>> &keys)
+template <template <typename, typename> class Hash, typename String, typename View, typename Converter>
+static void heterogeneousSearchTest(const QList<std::remove_const_t<String>> &keys, Converter conv)
{
#ifdef __cpp_concepts
using Helper = HeterogeneousSearchTestHelper<View>;
String key = keys.last();
String otherKey = keys.first();
- View keyView(key);
- View otherKeyView(otherKey);
+ auto keyHolder = conv(key);
+ auto otherKeyHolder = conv(otherKey);
+ View keyView(keyHolder);
+ View otherKeyView(otherKeyHolder);
Hash<String, qsizetype> hash;
static constexpr bool IsMultiHash = !std::is_same_v<decltype(hash.remove(String())), bool>;
@@ -1332,10 +1336,29 @@ static void heterogeneousSearchTest(const QList<std::remove_const_t<String>> &ke
Helper::checkCounter();
#else
Q_UNUSED(keys);
+ Q_UNUSED(conv);
QSKIP("This feature requires C++20 (concepts)");
#endif
}
+template <template <typename, typename> class Hash, typename String, typename View>
+static void heterogeneousSearchTest(const QList<std::remove_const_t<String>> &keys)
+{
+ heterogeneousSearchTest<Hash, String, View>(keys, [](const String &s) { return View(s); });
+}
+
+template <template <typename, typename> class Hash, typename T>
+static void heterogeneousSearchLatin1String(T)
+{
+ if constexpr (!T::value) {
+ QSKIP("QLatin1StringView and QString do not have the same hash on this platform");
+ } else {
+ // similar to the above
+ auto toLatin1 = [](const QString &s) { return s.toLatin1(); };
+ heterogeneousSearchTest<Hash, QString, QLatin1StringView>({ "Hello", {}, "World" }, toLatin1);
+ }
+}
+
void tst_QHash::heterogeneousSearch()
{
heterogeneousSearchTest<QHash, QString, HeterogeneousHashingType>({ "Hello", {}, "World" });
@@ -1357,6 +1380,11 @@ void tst_QHash::heterogeneousSearchString()
heterogeneousSearchTest<QHash, QString, QStringView>({ "Hello", {}, "World" });
}
+void tst_QHash::heterogeneousSearchLatin1String()
+{
+ ::heterogeneousSearchLatin1String<QHash>(QHashHeterogeneousSearch<QString, QLatin1StringView>{});
+}
+
void tst_QHash::compare()
{
QHash<int, QString> hash1,hash2;
@@ -2346,6 +2374,11 @@ void tst_QHash::qmultihashHeterogeneousSearchString()
heterogeneousSearchTest<QMultiHash, QString, QStringView>({ "Hello", {}, "World" });
}
+void tst_QHash::qmultihashHeterogeneousSearchLatin1String()
+{
+ ::heterogeneousSearchLatin1String<QMultiHash>(QHashHeterogeneousSearch<QString, QLatin1StringView>{});
+}
+
void tst_QHash::keys_values_uniqueKeys()
{
QMultiHash<QString, int> hash;
diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
index b9a7d88056..fdb2b37346 100644
--- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
+++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp
@@ -321,6 +321,19 @@ void tst_QHashFunctions::stringConsistency()
QCOMPARE(qHash(sv, seed), qHash(value, seed));
QCOMPARE(qHash(u8bav, seed), qHash(u8ba, seed));
+
+ if (seed || QT_VERSION_MAJOR > 6) {
+ QByteArray l1ba = value.toLatin1();
+ QLatin1StringView l1sv(l1ba.data(), l1ba.size());
+#ifdef Q_PROCESSOR_ARM
+ // zero-extending aeshash not implemented on ARM
+#elif defined(Q_PROCESSOR_X86)
+ // zero-extending aeshash not implemented on x86
+#else
+ if (value == l1sv)
+ QCOMPARE(qHash(l1sv, seed), qHash(value, seed));
+#endif
+ }
}
void tst_QHashFunctions::qhash()