Add unit tests

Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2025-08-08 15:46:41 +01:00
parent 73c83dfcad
commit 96d0fcfbd1
2 changed files with 66 additions and 1 deletions

View File

@@ -172,7 +172,14 @@ public:
explicit
operator result_type() noexcept
{
return retrieveHash();
auto result = retrieveHash();
resetBuffers();
if (state_)
{
XXH3_freeState(state_);
state_ = nullptr;
}
return result;
}
};

View File

@@ -20,6 +20,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <xrpl/beast/hash/xxhasher.h>
#include <xrpl/beast/unit_test.h>
#include "../../../include/xrpl/protocol/digest.h"
namespace beast {
class XXHasher_test : public unit_test::suite
@@ -182,6 +184,60 @@ public:
17285302196561698791ULL);
}
void
testOperatorResultTypeDoesTheSameAsOtherHashers()
{
testcase("Operator result type does the same as other hashers");
xxhasher hasher1;
ripple::sha256_hasher hasher2{};
std::string object{"Hello xxhash"};
hasher1(object.data(), object.size());
hasher2(object.data(), object.size());
auto xxhashResult1 = static_cast<xxhasher::result_type>(hasher1);
auto xxhashResult2 = static_cast<xxhasher::result_type>(hasher1);
auto sha256Result1 = static_cast<ripple::sha256_hasher::result_type>(hasher2);
auto sha256Result2 = static_cast<ripple::sha256_hasher::result_type>(hasher2);
auto xxhashChanged = xxhashResult1 == xxhashResult2;
auto sha256Changed = sha256Result1 == sha256Result2;
BEAST_EXPECT(xxhashChanged == sha256Changed);
}
void
testHasherStateStillValidAfterReset()
{
testcase("Hasher state still valid after reset");
{
xxhasher hasher1;
std::string object{"Hello xxhash"};
hasher1(object.data(), object.size());
auto xxhashResult1 = static_cast<xxhasher::result_type>(hasher1);
hasher1(object.data(), object.size());
auto xxhashResult2 = static_cast<xxhasher::result_type>(hasher1);
BEAST_EXPECT(xxhashResult1 == xxhashResult2);
}
{
xxhasher hasher1;
std::string object;
for (int i = 0; i < 100; i++)
{
object += "Hello, xxHash!";
}
hasher1(object.data(), object.size());
auto xxhashResult1 = static_cast<xxhasher::result_type>(hasher1);
hasher1(object.data(), object.size());
auto xxhashResult2 = static_cast<xxhasher::result_type>(hasher1);
BEAST_EXPECT(xxhashResult1 == xxhashResult2);
}
}
void
run() override
{
@@ -194,6 +250,8 @@ public:
testBigObjectWithSmallAndBigUpdatesWithSeed();
testBigObjectWithOneUpdateWithoutSeed();
testBigObjectWithOneUpdateWithSeed();
testOperatorResultTypeDoesTheSameAsOtherHashers();
testHasherStateStillValidAfterReset();
}
};