Skip to content

Commit

Permalink
refs #12080 - testrunner: do not create test implementation until it …
Browse files Browse the repository at this point in the history
…is used (#6691)

Greatly improves the start-up time because it no longer needs to create
all the implementations before it runs any test.

The following times are using a debug build.

Before: `bin/testrunner TestColor 1,16s user 0,07s system 99% cpu 1,235
total`

After: `bin/testrunner TestColor 0,00s user 0,00s system 88% cpu 0,009
total`
  • Loading branch information
firewave authored Aug 14, 2024
1 parent a25bc07 commit f39f567
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
23 changes: 13 additions & 10 deletions test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
**/
namespace {
struct CompareFixtures {
bool operator()(const TestFixture* lhs, const TestFixture* rhs) const {
bool operator()(const TestInstance* lhs, const TestInstance* rhs) const {
return lhs->classname < rhs->classname;
}
};
}
using TestSet = std::set<TestFixture*, CompareFixtures>;
using TestSet = std::set<TestInstance*, CompareFixtures>;
namespace {
class TestRegistry {
TestSet _tests;
Expand All @@ -57,7 +57,7 @@ namespace {
return testreg;
}

void addTest(TestFixture *t) {
void addTest(TestInstance *t) {
_tests.insert(t);
}

Expand All @@ -67,7 +67,11 @@ namespace {
};
}


TestInstance::TestInstance(const char * _name)
: classname(_name)
{
TestRegistry::theInstance().addTest(this);
}


/**
Expand All @@ -83,9 +87,7 @@ std::size_t TestFixture::succeeded_todos_counter = 0;

TestFixture::TestFixture(const char * const _name)
: classname(_name)
{
TestRegistry::theInstance().addTest(this);
}
{}


bool TestFixture::prepareTest(const char testname[])
Expand Down Expand Up @@ -389,10 +391,11 @@ std::size_t TestFixture::runTests(const options& args)
classname.erase(classname.find("::"));
}

for (TestFixture * test : TestRegistry::theInstance().tests()) {
for (TestInstance * test : TestRegistry::theInstance().tests()) {
if (classname.empty() || test->classname == classname) {
test->processOptions(args);
test->run(testname);
TestFixture* fixture = test->create();
fixture->processOptions(args);
fixture->run(testname);
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ class TestFixture : public ErrorLogger {
static std::size_t runTests(const options& args);
};

class TestInstance {
public:
explicit TestInstance(const char * _name);
virtual ~TestInstance() = default;

virtual TestFixture* create() = 0;

const std::string classname;
protected:
std::unique_ptr<TestFixture> impl;
};

// TODO: most asserts do not actually assert i.e. do not return
#define TEST_CASE( NAME ) do { if (prepareTest(#NAME)) { setVerbose(false); try { NAME(); teardownTest(); } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } } while (false)
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, (CONDITION))) return
Expand All @@ -306,7 +318,7 @@ class TestFixture : public ErrorLogger {
#define TODO_ASSERT( CONDITION ) do { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); } while (false)
#define TODO_ASSERT_EQUALS( WANTED, CURRENT, ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
#define EXPECT_EQ( EXPECTED, ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance_ ## CLASSNAME; }
#define REGISTER_TEST( CLASSNAME ) namespace { class CLASSNAME ## Instance : public TestInstance { public: CLASSNAME ## Instance() : TestInstance(#CLASSNAME) {} TestFixture* create() override { impl.reset(new CLASSNAME); return impl.get(); } }; CLASSNAME ## Instance instance_ ## CLASSNAME; }

#define PLATFORM( P, T ) do { std::string errstr; assertEquals(__FILE__, __LINE__, true, P.set(Platform::toString(T), errstr, {exename}), errstr); } while (false)

Expand Down
2 changes: 1 addition & 1 deletion test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <cstddef>
#include <list>

class TestMemleak : private TestFixture {
class TestMemleak : public TestFixture {
public:
TestMemleak() : TestFixture("TestMemleak") {}

Expand Down

0 comments on commit f39f567

Please sign in to comment.