Repository: catchorg/Catch2
File: include/internal/catch_test_case_registry_impl.cpp (C++)

Code before:
#include "catch_enforce.h"
#include "catch_interfaces_registry_hub.h"
#include "catch_random_number_generator.h"
#include "catch_run_context.h"
#include "catch_string_manip.h"
#include "catch_test_case_info.h"

#include <random>
#include <sstream>

namespace Catch {

    struct CompareTestsUsingRandom {
        explicit CompareTestsUsingRandom( Catch::SimplePcg32& rng ) {
            std::uniform_int_distribution<uint64_t> dist;
            basis = dist( rng );
        }

        uint64_t basis;

        uint64_t fnv_1a_hash( std::string const& s ) const {
            static constexpr uint64_t prime = 1099511628211;
            uint64_t hash = basis;
            for( const char c : s ) {
                hash ^= c;
                hash *= prime;
            }
            return hash;
        }

        bool operator()( TestCase const& l, TestCase const& r ) const {
            return fnv_1a_hash( l.name ) < fnv_1a_hash( r.name );
        }
    };

    std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) {

        std::vector<TestCase> sorted = unsortedTestCases;

        switch( config.runOrder() ) {
            case RunTests::InLexicographicalOrder:
                std::sort( sorted.begin(), sorted.end() );
                break;
            case RunTests::InRandomOrder:
                seedRng( config );
                std::sort( sorted.begin(), sorted.end(), CompareTestsUsingRandom( rng() ) );
                break;
            case RunTests::InDeclarationOrder:
                // already in declaration order
                break;
        }

Review comment (suggestion):
I am not sure why the compiler doesn't warn on this, but AFAIK the constant will be truncated. To generate `uint64_t` constants, you need `ULL` suffix.

Code after:
#include "catch_enforce.h"
#include "catch_interfaces_registry_hub.h"
#include "catch_random_number_generator.h"
#include "catch_run_context.h"
#include "catch_string_manip.h"
#include "catch_test_case_info.h"

#include <algorithm>
#include <iterator>
#include <random>
#include <sstream>

namespace Catch {

    struct HashTest {
        explicit HashTest( Catch::SimplePcg32& rng ) {
            basis = rng();
            basis <<= 32;
            basis |= rng();
        }

        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 ^= c;
                hash *= prime;
            }
            return hash;
        }
    };

    std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) {
        switch( config.runOrder() ) {
            case RunTests::InLexicographicalOrder: {
                std::vector<TestCase> sorted = unsortedTestCases;
                std::sort( sorted.begin(), sorted.end() );
                return sorted;
            }
            case RunTests::InRandomOrder: {
                seedRng( config );
                HashTest h( rng() );
                std::vector<std::tuple<uint64_t, std::string, TestCase const*>> indexed_tests;
                indexed_tests.reserve( unsortedTestCases.size() );
                std::transform( unsortedTestCases.begin(), unsortedTestCases.end(),
                    std::back_inserter( indexed_tests ),
                    [&]( TestCase const& t ) {
                      return std::make_tuple( h(t), t.name, &t );