Refactor implementation of hashed random order test sorting

This commit is contained in:
Martin Hořeňovský 2020-04-14 16:39:45 +02:00
parent a2fc7cf8c0
commit bad0fb51f8
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -16,14 +16,13 @@
#include "catch_test_case_info.h"
#include <algorithm>
#include <iterator>
#include <random>
#include <sstream>
namespace Catch {
struct HashTest {
explicit HashTest( Catch::SimplePcg32& rng ) {
namespace {
struct TestHasher {
explicit TestHasher(Catch::SimplePcg32& rng) {
basis = rng();
basis <<= 32;
basis |= rng();
@ -42,37 +41,50 @@ namespace Catch {
return hash;
}
};
} // end unnamed namespace
std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) {
switch( config.runOrder() ) {
case RunTests::InDeclarationOrder:
// already in declaration order
break;
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;
TestHasher h( rng() );
using hashedTest = std::pair<uint64_t, TestCase const*>;
std::vector<hashedTest> 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 );
for (auto const& testCase : unsortedTestCases) {
indexed_tests.emplace_back(h(testCase), &testCase);
}
std::sort(indexed_tests.begin(), indexed_tests.end(),
[](hashedTest const& lhs, hashedTest const& rhs) {
if (lhs.first == rhs.first) {
return lhs.second->name < rhs.second->name;
}
return lhs.first < rhs.first;
});
std::sort( indexed_tests.begin(), indexed_tests.end() );
std::vector<TestCase> sorted;
sorted.reserve( unsortedTestCases.size() );
std::transform( indexed_tests.begin(), indexed_tests.end(),
std::back_inserter( sorted ),
[]( std::tuple<uint64_t, std::string, TestCase const*> const& t ) {
return *std::get<2>( t );
} );
sorted.reserve( indexed_tests.size() );
for (auto const& hashed : indexed_tests) {
sorted.emplace_back(*hashed.second);
}
return sorted;
}
case RunTests::InDeclarationOrder:
// already in declaration order
break;
}
return unsortedTestCases;
}