tst_bench_QCryptographicHash: use QMetaEnum trick

... conveniently wrapped in a generator, to not have to keep an
algoname() function in sync with QCryptographicHash::Algorithm.

The MaxCryptoAlgorithm constant was already stale following the
addition of BLAKE2b/s algorithms in
5d69aa3ee1214cf689e2357bff8688f2ff138471.

Also make the data-driven tests have an actual Algorithm column
(was: int) to minimize casting.

Pick-to: 6.5 6.2 5.15
Change-Id: I89a6098e512a72f623fd50a6f88fc351c7bb1418
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Marc Mutz 2023-03-22 08:53:05 +01:00
parent f68b9e432b
commit d0149e2404

View File

@ -4,10 +4,13 @@
#include <QByteArray> #include <QByteArray>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QFile> #include <QFile>
#include <QMetaEnum>
#include <QRandomGenerator> #include <QRandomGenerator>
#include <QString> #include <QString>
#include <QTest> #include <QTest>
#include <functional>
#include <time.h> #include <time.h>
class tst_QCryptographicHash : public QObject class tst_QCryptographicHash : public QObject
@ -15,6 +18,8 @@ class tst_QCryptographicHash : public QObject
Q_OBJECT Q_OBJECT
QByteArray blockOfData; QByteArray blockOfData;
using Algorithm = QCryptographicHash::Algorithm;
public: public:
tst_QCryptographicHash(); tst_QCryptographicHash();
@ -27,60 +32,15 @@ private Q_SLOTS:
void addDataChunked(); void addDataChunked();
}; };
const int MaxCryptoAlgorithm = QCryptographicHash::Sha3_512;
const int MaxBlockSize = 65536; const int MaxBlockSize = 65536;
const char *algoname(int i) static void for_each_algorithm(std::function<void(QCryptographicHash::Algorithm, const char*)> f)
{ {
switch (QCryptographicHash::Algorithm(i)) { Q_ASSERT(f);
case QCryptographicHash::Md4: using A = QCryptographicHash::Algorithm;
return "md4"; static const auto metaEnum = QMetaEnum::fromType<A>();
case QCryptographicHash::Md5: for (int i = 0, value = metaEnum.value(i); value != -1; value = metaEnum.value(++i))
return "md5"; f(A(value), metaEnum.key(i));
case QCryptographicHash::Sha1:
return "sha1";
case QCryptographicHash::Sha224:
return "sha2_224";
case QCryptographicHash::Sha256:
return "sha2_256";
case QCryptographicHash::Sha384:
return "sha2_384";
case QCryptographicHash::Sha512:
return "sha2_512";
case QCryptographicHash::Sha3_224:
return "sha3_224";
case QCryptographicHash::Sha3_256:
return "sha3_256";
case QCryptographicHash::Sha3_384:
return "sha3_384";
case QCryptographicHash::Sha3_512:
return "sha3_512";
case QCryptographicHash::Keccak_224:
return "keccak_224";
case QCryptographicHash::Keccak_256:
return "keccak_256";
case QCryptographicHash::Keccak_384:
return "keccak_384";
case QCryptographicHash::Keccak_512:
return "keccak_512";
case QCryptographicHash::Blake2b_160:
return "blake2b_160";
case QCryptographicHash::Blake2b_256:
return "blake2b_256";
case QCryptographicHash::Blake2b_384:
return "blake2b_384";
case QCryptographicHash::Blake2b_512:
return "blake2b_512";
case QCryptographicHash::Blake2s_128:
return "blake2s_128";
case QCryptographicHash::Blake2s_160:
return "blake2s_160";
case QCryptographicHash::Blake2s_224:
return "blake2s_224";
case QCryptographicHash::Blake2s_256:
return "blake2s_256";
}
Q_UNREACHABLE_RETURN(nullptr);
} }
tst_QCryptographicHash::tst_QCryptographicHash() tst_QCryptographicHash::tst_QCryptographicHash()
@ -100,7 +60,7 @@ tst_QCryptographicHash::tst_QCryptographicHash()
void tst_QCryptographicHash::hash_data() void tst_QCryptographicHash::hash_data()
{ {
QTest::addColumn<int>("algorithm"); QTest::addColumn<Algorithm>("algo");
QTest::addColumn<QByteArray>("data"); QTest::addColumn<QByteArray>("data");
static const int datasizes[] = { 0, 1, 64, 65, 512, 4095, 4096, 4097, 65536 }; static const int datasizes[] = { 0, 1, 64, 65, 512, 4095, 4096, 4097, 65536 };
@ -108,17 +68,19 @@ void tst_QCryptographicHash::hash_data()
Q_ASSERT(datasizes[i] < MaxBlockSize); Q_ASSERT(datasizes[i] < MaxBlockSize);
QByteArray data = QByteArray::fromRawData(blockOfData.constData(), datasizes[i]); QByteArray data = QByteArray::fromRawData(blockOfData.constData(), datasizes[i]);
for (int algo = QCryptographicHash::Md4; algo <= MaxCryptoAlgorithm; ++algo) for_each_algorithm([&] (Algorithm algo, const char *name) {
QTest::addRow("%s-%d", algoname(algo), datasizes[i]) << algo << data; if (algo == Algorithm::NumAlgorithms)
return;
QTest::addRow("%s-%d", name, datasizes[i]) << algo << data;
});
} }
} }
void tst_QCryptographicHash::hash() void tst_QCryptographicHash::hash()
{ {
QFETCH(int, algorithm); QFETCH(const Algorithm, algo);
QFETCH(QByteArray, data); QFETCH(QByteArray, data);
QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm);
QBENCHMARK { QBENCHMARK {
QCryptographicHash::hash(data, algo); QCryptographicHash::hash(data, algo);
} }
@ -126,10 +88,9 @@ void tst_QCryptographicHash::hash()
void tst_QCryptographicHash::addData() void tst_QCryptographicHash::addData()
{ {
QFETCH(int, algorithm); QFETCH(const Algorithm, algo);
QFETCH(QByteArray, data); QFETCH(QByteArray, data);
QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm);
QCryptographicHash hash(algo); QCryptographicHash hash(algo);
QBENCHMARK { QBENCHMARK {
hash.reset(); hash.reset();
@ -140,10 +101,9 @@ void tst_QCryptographicHash::addData()
void tst_QCryptographicHash::addDataChunked() void tst_QCryptographicHash::addDataChunked()
{ {
QFETCH(int, algorithm); QFETCH(const Algorithm, algo);
QFETCH(QByteArray, data); QFETCH(QByteArray, data);
QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm);
QCryptographicHash hash(algo); QCryptographicHash hash(algo);
QBENCHMARK { QBENCHMARK {
hash.reset(); hash.reset();