Modified hash to make it more independent of the choice of test names.

This commit is contained in:
Sergio Losilla 2020-11-17 11:16:36 +02:00 committed by Martin Hořeňovský
parent 520b6dace9
commit cdf4748d1c
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
1 changed files with 17 additions and 14 deletions

View File

@ -22,24 +22,27 @@
namespace Catch { namespace Catch {
namespace { namespace {
struct HashTest { struct TestHasher {
explicit HashTest(SimplePcg32& rng_inst) { using hash_t = uint64_t;
basis = rng_inst();
basis <<= 32;
basis |= rng_inst();
}
uint64_t basis; explicit TestHasher( hash_t hashSuffix ):
m_hashSuffix( hashSuffix ) {}
uint64_t operator()(TestCaseInfo const& t) const { uint64_t m_hashSuffix;
// Modified FNV-1a hash
static constexpr uint64_t prime = 1099511628211; uint32_t operator()( TestCaseInfo const& t ) const {
uint64_t hash = basis; // FNV-1a hash with multiplication fold.
const hash_t prime = 1099511628211u;
hash_t hash = 14695981039346656037u;
for (const char c : t.name) { for (const char c : t.name) {
hash ^= c; hash ^= c;
hash *= prime; hash *= prime;
} }
return hash; hash ^= m_hashSuffix;
hash *= prime;
const uint32_t low{ static_cast<uint32_t>(hash) };
const uint32_t high{ static_cast<uint32_t>(hash >> 32) };
return low * high;
} }
}; };
} // end anonymous namespace } // end anonymous namespace
@ -56,8 +59,8 @@ namespace {
} }
case TestRunOrder::Randomized: { case TestRunOrder::Randomized: {
seedRng(config); seedRng(config);
HashTest h(rng()); TestHasher h{ config.rngSeed() };
std::vector<std::pair<uint64_t, TestCaseHandle>> indexed_tests; std::vector<std::pair<TestHasher::hash_t, TestCaseHandle>> indexed_tests;
indexed_tests.reserve(unsortedTestCases.size()); indexed_tests.reserve(unsortedTestCases.size());
for (auto const& handle : unsortedTestCases) { for (auto const& handle : unsortedTestCases) {