Skip to content

Commit

Permalink
Replaced the hash algorithm with classic FNV-1a with product folding.
Browse files Browse the repository at this point in the history
- The seed is treated as a character to be appended to the string.
  • Loading branch information
Sergio Losilla committed Nov 13, 2020
1 parent d7e1fb7 commit 605a0ea
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions include/internal/catch_test_case_registry_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +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;

uint64_t basis;
explicit TestHasher(hash_t seed): m_seed{seed} {
}

uint32_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;
}
hash ^= m_seed;
hash *= prime;
const auto low{ static_cast<uint32_t>( hash ) };
const auto high{ static_cast<uint32_t>( hash >> 32 ) };
return high * low;
return hash_t{ low * high };
}
private:
hash_t m_seed;
};
} // end unnamed namespace

Expand All @@ -60,7 +61,7 @@ namespace Catch {

case RunTests::InRandomOrder: {
seedRng( config );
TestHasher h( rng() );
TestHasher h{ config.rngSeed() };

using hashedTest = std::pair<uint32_t, TestCase const*>;
std::vector<hashedTest> indexed_tests;
Expand Down

0 comments on commit 605a0ea

Please sign in to comment.