From 3ba745552bd26cbbb7c271720c0ec9eaaf1dc8dd Mon Sep 17 00:00:00 2001 From: Sergio Losilla Date: Tue, 17 Nov 2020 11:16:36 +0200 Subject: [PATCH] Modified hash to make it more independent of the choice of test names. --- .../catch_test_case_registry_impl.cpp | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/include/internal/catch_test_case_registry_impl.cpp b/include/internal/catch_test_case_registry_impl.cpp index b337ee3e..0fc3e8be 100644 --- a/include/internal/catch_test_case_registry_impl.cpp +++ b/include/internal/catch_test_case_registry_impl.cpp @@ -22,24 +22,27 @@ namespace Catch { namespace { struct TestHasher { - explicit TestHasher(Catch::SimplePcg32& rng_instance) { - basis = rng_instance(); - basis <<= 32; - basis |= rng_instance(); + using hash_t = uint64_t; + + explicit TestHasher(hash_t seed): m_seed{seed} { } - uint64_t basis; - - uint64_t operator()(TestCase const& t) const { - // Modified FNV-1a hash - static constexpr uint64_t prime = 1099511628211; - uint64_t hash = basis; - for (const char c : t.name) { + hash_t operator()(TestCase const& t) const { + // FNV-1a hash with multiplication fold. + const hash_t prime = 1099511628211u; + hash_t hash = 14695981039346656037u; + for ( const char c : t.name ) { hash ^= c; hash *= prime; } - return hash; + hash ^= m_seed; + hash *= prime; + const uint32_t low{ static_cast( hash ) }; + const uint32_t high{ static_cast( hash >> 32 ) }; + return hash_t{ low * high }; } + private: + hash_t m_seed; }; } // end unnamed namespace @@ -58,9 +61,9 @@ namespace Catch { case RunTests::InRandomOrder: { seedRng( config ); - TestHasher h( rng() ); + TestHasher h{ config.rngSeed() }; - using hashedTest = std::pair; + using hashedTest = std::pair; std::vector indexed_tests; indexed_tests.reserve( unsortedTestCases.size() );