From 358248b4950131711ae408da9aa30d41c5143e4f Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Sat, 10 Sep 2022 19:40:51 +0200 Subject: Make QCryptographicHash move constructible This adds a move constructor, a move assignment operator and a swap function to QCryptographicHash. This can (to name one example) be useful when you want to store multiple hashes in a vector. [ChangeLog][QtCore][QCryptographicHash] Added move constructor, move assignment operator and swap() function. Change-Id: Id54594fa69104ec25ad78581f962a021e85531c2 Reviewed-by: Marc Mutz --- .../qcryptographichash/tst_qcryptographichash.cpp | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp') diff --git a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp index 5e2e854067..5445963be8 100644 --- a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp +++ b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp @@ -31,6 +31,8 @@ private slots: void files(); void hashLength_data(); void hashLength(); + void move(); + void swap(); // keep last void moreThan4GiBOfData_data(); void moreThan4GiBOfData(); @@ -401,6 +403,41 @@ void tst_QCryptographicHash::hashLength() QCOMPARE(QCryptographicHash::hashLength(algorithm), output.length()); } +void tst_QCryptographicHash::move() +{ + QCryptographicHash hash1(QCryptographicHash::Sha1); + hash1.addData("a"); + + // move constructor + auto hash2(std::move(hash1)); + hash2.addData("b"); + + // move assign operator + QCryptographicHash hash3(QCryptographicHash::Sha256); + hash3.addData("no effect on the end result"); + hash3 = std::move(hash2); + hash3.addData("c"); + + QCOMPARE(hash3.resultView(), QByteArray::fromHex("A9993E364706816ABA3E25717850C26C9CD0D89D")); +} + +void tst_QCryptographicHash::swap() +{ + QCryptographicHash hash1(QCryptographicHash::Sha1); + QCryptographicHash hash2(QCryptographicHash::Sha256); + + hash1.addData("da"); + hash2.addData("te"); + + hash1.swap(hash2); + + hash2.addData("ta"); + hash1.addData("st"); + + QCOMPARE(hash2.result(), QCryptographicHash::hash("data", QCryptographicHash::Sha1)); + QCOMPARE(hash1.result(), QCryptographicHash::hash("test", QCryptographicHash::Sha256)); +} + void tst_QCryptographicHash::moreThan4GiBOfData_data() { #if QT_POINTER_SIZE > 4 -- cgit v1.2.3