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
5d69aa3ee1.

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 <QCryptographicHash>
#include <QFile>
#include <QMetaEnum>
#include <QRandomGenerator>
#include <QString>
#include <QTest>
#include <functional>
#include <time.h>
class tst_QCryptographicHash : public QObject
@ -15,6 +18,8 @@ class tst_QCryptographicHash : public QObject
Q_OBJECT
QByteArray blockOfData;
using Algorithm = QCryptographicHash::Algorithm;
public:
tst_QCryptographicHash();
@ -27,60 +32,15 @@ private Q_SLOTS:
void addDataChunked();
};
const int MaxCryptoAlgorithm = QCryptographicHash::Sha3_512;
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)) {
case QCryptographicHash::Md4:
return "md4";
case QCryptographicHash::Md5:
return "md5";
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);
Q_ASSERT(f);
using A = QCryptographicHash::Algorithm;
static const auto metaEnum = QMetaEnum::fromType<A>();
for (int i = 0, value = metaEnum.value(i); value != -1; value = metaEnum.value(++i))
f(A(value), metaEnum.key(i));
}
tst_QCryptographicHash::tst_QCryptographicHash()
@ -100,7 +60,7 @@ tst_QCryptographicHash::tst_QCryptographicHash()
void tst_QCryptographicHash::hash_data()
{
QTest::addColumn<int>("algorithm");
QTest::addColumn<Algorithm>("algo");
QTest::addColumn<QByteArray>("data");
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);
QByteArray data = QByteArray::fromRawData(blockOfData.constData(), datasizes[i]);
for (int algo = QCryptographicHash::Md4; algo <= MaxCryptoAlgorithm; ++algo)
QTest::addRow("%s-%d", algoname(algo), datasizes[i]) << algo << data;
for_each_algorithm([&] (Algorithm algo, const char *name) {
if (algo == Algorithm::NumAlgorithms)
return;
QTest::addRow("%s-%d", name, datasizes[i]) << algo << data;
});
}
}
void tst_QCryptographicHash::hash()
{
QFETCH(int, algorithm);
QFETCH(const Algorithm, algo);
QFETCH(QByteArray, data);
QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm);
QBENCHMARK {
QCryptographicHash::hash(data, algo);
}
@ -126,10 +88,9 @@ void tst_QCryptographicHash::hash()
void tst_QCryptographicHash::addData()
{
QFETCH(int, algorithm);
QFETCH(const Algorithm, algo);
QFETCH(QByteArray, data);
QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm);
QCryptographicHash hash(algo);
QBENCHMARK {
hash.reset();
@ -140,10 +101,9 @@ void tst_QCryptographicHash::addData()
void tst_QCryptographicHash::addDataChunked()
{
QFETCH(int, algorithm);
QFETCH(const Algorithm, algo);
QFETCH(QByteArray, data);
QCryptographicHash::Algorithm algo = QCryptographicHash::Algorithm(algorithm);
QCryptographicHash hash(algo);
QBENCHMARK {
hash.reset();